forked from apxtri/apxtri
263 lines
7.0 KiB
JavaScript
263 lines
7.0 KiB
JavaScript
const assert = require('assert');
|
|
const openpgp = require('openpgp');
|
|
const dayjs = require('dayjs');
|
|
|
|
const ut = { name: 'Pagans' };
|
|
|
|
|
|
const apx = {};
|
|
|
|
apx.generateKey = async (alias, passphrase) => {
|
|
try {
|
|
const pgpParams = {
|
|
type: 'ecc',
|
|
curve: 'curve25519',
|
|
userIDs: [{ name: alias }],
|
|
passphrase: passphrase,
|
|
format: 'armored'
|
|
};
|
|
|
|
const key = await openpgp.generateKey(pgpParams);
|
|
return {
|
|
alias,
|
|
passphrase,
|
|
privatekey: key.privateKeyArmored,
|
|
publickey: key.publicKeyArmored
|
|
};
|
|
} catch (error) {
|
|
console.error('Error generating key:', error);
|
|
return {}
|
|
}
|
|
};
|
|
|
|
apx.createIdentity = async (alias, passphrase) => {
|
|
try {
|
|
const { privatekey, publickey } = await apx.generateKey(alias, passphrase);
|
|
return { alias, privatekey, publickey };
|
|
} catch (error) {
|
|
console.error('Error creating identity:', error);
|
|
}
|
|
};
|
|
|
|
apx.clearmsgSignature = async (privateKeyArmored, passphrase, message) => {
|
|
try {
|
|
const { keys: [privateKey] } = await openpgp.decryptKey({
|
|
privateKey: await openpgp.readKey({ armoredKey: privateKeyArmored }),
|
|
passphrase
|
|
});
|
|
|
|
const signedMessage = await openpgp.sign({
|
|
message: await openpgp.createMessage({ text: message }),
|
|
signingKeys: privateKey
|
|
});
|
|
|
|
return signedMessage;
|
|
} catch (error) {
|
|
console.error('Error signing message:', error);
|
|
}
|
|
};
|
|
|
|
const personData = {};
|
|
|
|
const apxFunctions = {
|
|
modifyPersonData(alias, newFirstName) {
|
|
if (!personData[alias]) {
|
|
personData[alias] = {};
|
|
}
|
|
personData[alias].firstName = newFirstName;
|
|
return personData[alias];
|
|
},
|
|
|
|
checkPersonData(alias, expectedFirstName) {
|
|
return personData[alias] && personData[alias].firstName === expectedFirstName;
|
|
},
|
|
|
|
removePersonModification(alias, originalFirstName) {
|
|
if (personData[alias]) {
|
|
personData[alias].firstName = originalFirstName;
|
|
}
|
|
return personData[alias];
|
|
},
|
|
|
|
deleteAlias(alias) {
|
|
delete personData[alias];
|
|
return !personData[alias];
|
|
}
|
|
};
|
|
const pagans={"adminsmatchits":{passphrase:""},"recruiters":{passphrase:""},"seekers":{},"adminrecruiters":{}}
|
|
|
|
pagans.A.headers = {
|
|
xtrkversion: 1,
|
|
xalias: 'anonymous',
|
|
xapp: 'smatchapp',
|
|
xdays: 0,
|
|
xhash: 'anonymous',
|
|
xlang: 'fr',
|
|
xprofils: 'anonymous',
|
|
xtribe: 'smatchit',
|
|
xuuid: '0'
|
|
};
|
|
paganA.alias = 'testalias';
|
|
paganA.passphrase = 'testpassphrase';
|
|
let privateKey, publicKey;
|
|
let authHeaders;
|
|
const testroutes={}
|
|
testroutes.createidentity=(alias,passphrase)=>{
|
|
const mykey=apx.generateKey(alias,passphrase)
|
|
if (!mykey.alias){return testerror}
|
|
Pagans.gertalias(alias) pass test or not
|
|
}
|
|
|
|
|
|
const testCases = [
|
|
{
|
|
name: 'Generate Key',
|
|
async run() {
|
|
const keys = await apx.generateKey(alias, passphrase);
|
|
privateKey = keys.privatekey;
|
|
publicKey = keys.publickey;
|
|
|
|
return keys;
|
|
},
|
|
verify(keys) {
|
|
assert(keys, 'Keys should not be undefined');
|
|
assert(keys.privatekey, 'Private key should not be undefined');
|
|
assert(keys.publickey, 'Public key should not be undefined');
|
|
assert(keys.privatekey.includes('BEGIN PGP PRIVATE KEY BLOCK'), 'Private key is not valid');
|
|
assert(keys.publickey.includes('BEGIN PGP PUBLIC KEY BLOCK'), 'Public key is not valid');
|
|
}
|
|
},
|
|
{
|
|
name: 'Create Identity',
|
|
async run(alias) {
|
|
const identity = await apx.createIdentity(alias, passphrase);
|
|
privateKey = identity.privatekey;
|
|
publicKey = identity.publickey;
|
|
Pagans.getalias
|
|
return identity;
|
|
},
|
|
verify(identity) {
|
|
assert(identity, 'Identity should not be undefined');
|
|
assert(identity.alias === alias, 'Alias should match');
|
|
assert(identity.privatekey.includes('BEGIN PGP PRIVATE KEY BLOCK'), 'Private key is not valid');
|
|
assert(identity.publickey.includes('BEGIN PGP PUBLIC KEY BLOCK'), 'Public key is not valid');
|
|
}
|
|
},
|
|
{
|
|
name: 'Authenticate',
|
|
async run() {
|
|
headers.xalias = alias;
|
|
headers.xdays = dayjs().valueOf();
|
|
const msg = `${alias}_${headers.xdays}`;
|
|
headers.xhash = await apx.clearmsgSignature(privateKey, passphrase, msg);
|
|
|
|
authHeaders = {
|
|
...headers,
|
|
xhash: headers.xhash,
|
|
xdays: headers.xdays
|
|
};
|
|
|
|
return authHeaders;
|
|
},
|
|
verify(authHeaders) {
|
|
assert(authHeaders, 'Auth headers should not be undefined');
|
|
assert(authHeaders.xhash.includes('BEGIN PGP SIGNED MESSAGE'), 'xhash is not valid');
|
|
}
|
|
},
|
|
{
|
|
name: 'Modify Person Data',
|
|
async run() {
|
|
const newFirstName = 'NewFirstName';
|
|
return apxFunctions.modifyPersonData(alias, newFirstName);
|
|
},
|
|
verify(modifiedData) {
|
|
assert(modifiedData, 'Modified data should not be undefined');
|
|
assert.strictEqual(modifiedData.firstName, 'NewFirstName', 'First name should be updated');
|
|
}
|
|
},
|
|
{
|
|
name: 'Check Person Data',
|
|
async run() {
|
|
const expectedFirstName = 'NewFirstName';
|
|
return apxFunctions.checkPersonData(alias, expectedFirstName);
|
|
},
|
|
verify(isCorrect) {
|
|
assert(isCorrect, 'Person data should be correctly updated');
|
|
}
|
|
},
|
|
{
|
|
name: 'Remove Person Modification',
|
|
async run() {
|
|
const originalFirstName = 'OriginalFirstName';
|
|
return apxFunctions.removePersonModification(alias, originalFirstName);
|
|
},
|
|
verify(modifiedData) {
|
|
assert(modifiedData, 'Modified data should not be undefined');
|
|
assert.strictEqual(modifiedData.firstName, 'OriginalFirstName', 'First name should be reverted');
|
|
}
|
|
},
|
|
{
|
|
name: 'Check Reverted Person Data',
|
|
async run() {
|
|
const expectedFirstName = 'OriginalFirstName';
|
|
return apxFunctions.checkPersonData(alias, expectedFirstName);
|
|
},
|
|
verify(isCorrect) {
|
|
assert(isCorrect, 'Person data should be correctly reverted');
|
|
}
|
|
},
|
|
{
|
|
name: 'Delete Alias',
|
|
async run() {
|
|
return apxFunctions.deleteAlias(alias);
|
|
},
|
|
verify(isDeleted) {
|
|
assert(isDeleted, 'Alias should be deleted');
|
|
}
|
|
}
|
|
];
|
|
|
|
ut.run = async (options) => {
|
|
console.log('Test Pagans Registration and Authentication');
|
|
for (const testCase of testCases) {
|
|
console.log(`Running test case: ${testCase.name}`);
|
|
try {
|
|
/*
|
|
Have a look in routes/pagans.jss
|
|
create personae={4 alias utadminsmatchit ut....}
|
|
forEach()alias=>{
|
|
ut.create(alias)
|
|
}
|
|
//test perons
|
|
forEach()alias=>{
|
|
join tribe smatchit === create persons for a tribe
|
|
|
|
}
|
|
|
|
forEach()alias=>W{
|
|
ut.delete(alias)}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
const result = await testCase.run(alias);
|
|
testCase.verify(result);
|
|
console.log(`Test case ${testCase.name} passed`);
|
|
} catch (error) {
|
|
console.error(`Test case ${testCase.name} failed:`, error);
|
|
}
|
|
}
|
|
console.log('All test cases ran successfully');
|
|
};
|
|
|
|
module.exports = ut;
|
|
|
|
// Run the tests
|
|
if (require.main === module) {
|
|
ut.run({ verbose: true }).catch(err => {
|
|
console.error('Test case failed:', err);
|
|
});
|
|
}
|