2024-06-30 22:06:37 +02:00
|
|
|
const assert = require('assert');
|
|
|
|
const openpgp = require('openpgp');
|
|
|
|
const dayjs = require('dayjs');
|
|
|
|
|
|
|
|
const ut = { name: 'Pagans' };
|
|
|
|
|
|
|
|
const apx = {};
|
|
|
|
|
|
|
|
apx.generateKey = async (alias, passphrase) => {
|
2024-07-03 12:18:42 +03:00
|
|
|
try {
|
|
|
|
console.log(`\nGenerating keys for alias: ${alias}`);
|
|
|
|
const pgpParams = {
|
|
|
|
type: 'ecc',
|
|
|
|
curve: 'curve25519',
|
|
|
|
userIDs: [{ name: alias }],
|
|
|
|
passphrase: passphrase,
|
|
|
|
format: 'armored'
|
|
|
|
};
|
|
|
|
|
|
|
|
const key = await openpgp.generateKey(pgpParams);
|
|
|
|
console.log(`Keys generated successfully for alias: ${alias}`);
|
|
|
|
|
|
|
|
return {
|
|
|
|
alias,
|
|
|
|
passphrase,
|
|
|
|
privatekey: key.privateKey,
|
|
|
|
publickey: key.publicKey
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Error generating keys for alias: ${alias}`, error);
|
|
|
|
return {};
|
|
|
|
}
|
2024-06-30 22:06:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
apx.createIdentity = async (alias, passphrase) => {
|
2024-07-03 12:18:42 +03:00
|
|
|
try {
|
|
|
|
const { privatekey, publickey } = await apx.generateKey(alias, passphrase);
|
|
|
|
console.log(`Identity created successfully for alias: ${alias}`);
|
|
|
|
return { alias, privatekey, publickey };
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Error creating identity for alias: ${alias}`, error);
|
|
|
|
}
|
2024-06-30 22:06:37 +02:00
|
|
|
};
|
|
|
|
|
2024-07-03 12:18:42 +03:00
|
|
|
apx.joinTribe = async (alias, tribe) => {
|
|
|
|
// Mock implementation of joining a tribe
|
|
|
|
console.log(`Alias ${alias} joined tribe ${tribe}`);
|
|
|
|
return true;
|
|
|
|
};
|
2024-06-30 22:06:37 +02:00
|
|
|
|
2024-07-03 12:18:42 +03:00
|
|
|
apx.deleteAlias = async (alias) => {
|
|
|
|
// Mock implementation of deleting an alias
|
|
|
|
console.log(`Alias ${alias} deleted`);
|
|
|
|
return true;
|
2024-06-30 22:06:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const personData = {};
|
|
|
|
|
|
|
|
const apxFunctions = {
|
2024-07-03 12:18:42 +03:00
|
|
|
modifyPersonData(alias, newFirstName) {
|
|
|
|
if (!personData[alias]) {
|
|
|
|
personData[alias] = {};
|
|
|
|
}
|
|
|
|
personData[alias].firstName = newFirstName;
|
|
|
|
return personData[alias];
|
|
|
|
},
|
2024-06-30 22:06:37 +02:00
|
|
|
|
2024-07-03 12:18:42 +03:00
|
|
|
checkPersonData(alias, expectedFirstName) {
|
|
|
|
return personData[alias] && personData[alias].firstName === expectedFirstName;
|
|
|
|
},
|
2024-06-30 22:06:37 +02:00
|
|
|
|
2024-07-03 12:18:42 +03:00
|
|
|
removePersonModification(alias, originalFirstName) {
|
|
|
|
if (personData[alias]) {
|
|
|
|
personData[alias].firstName = originalFirstName;
|
|
|
|
}
|
|
|
|
return personData[alias];
|
|
|
|
},
|
2024-06-30 22:06:37 +02:00
|
|
|
|
2024-07-03 12:18:42 +03:00
|
|
|
deleteAlias(alias) {
|
|
|
|
delete personData[alias];
|
|
|
|
return !personData[alias];
|
|
|
|
}
|
2024-06-30 22:06:37 +02:00
|
|
|
};
|
|
|
|
|
2024-07-03 12:18:42 +03:00
|
|
|
const pagans = {
|
|
|
|
adminsmatchits: { alias: 'adminsmatchit', passphrase: 'adminsmatchitPass' },
|
|
|
|
recruiters: { alias: 'recruiter', passphrase: 'recruiterPass' },
|
|
|
|
seekers: { alias: 'seeker', passphrase: 'seekerPass' },
|
|
|
|
adminrecruiters: { alias: 'adminrecruiter', passphrase: 'adminrecruiterPass' }
|
2024-06-30 22:06:37 +02:00
|
|
|
};
|
|
|
|
|
2024-07-03 12:18:42 +03:00
|
|
|
Object.keys(pagans).forEach(key => {
|
|
|
|
pagans[key].headers = {
|
|
|
|
xtrkversion: 1,
|
|
|
|
xalias: 'anonymous',
|
|
|
|
xapp: 'smatchapp',
|
|
|
|
xdays: 0,
|
|
|
|
xhash: 'anonymous',
|
|
|
|
xlang: 'fr',
|
|
|
|
xprofils: 'anonymous',
|
|
|
|
xtribe: 'smatchit',
|
|
|
|
xuuid: '0'
|
|
|
|
};
|
|
|
|
});
|
2024-06-30 22:06:37 +02:00
|
|
|
|
|
|
|
const testCases = [
|
2024-07-03 12:18:42 +03:00
|
|
|
{
|
|
|
|
name: 'Create Identity',
|
|
|
|
async run(user) {
|
|
|
|
const identity = await apx.createIdentity(user.alias, user.passphrase);
|
|
|
|
if (identity) {
|
|
|
|
user.privateKey = identity.privatekey;
|
|
|
|
user.publicKey = identity.publickey;
|
|
|
|
}
|
|
|
|
return identity;
|
|
|
|
},
|
|
|
|
verify(identity, alias) {
|
|
|
|
assert(identity, 'Identity should not be undefined');
|
|
|
|
assert(identity.alias === alias, 'Alias should match');
|
|
|
|
assert(identity.privatekey && identity.privatekey.includes('BEGIN PGP PRIVATE KEY BLOCK'), 'Private key is not valid');
|
|
|
|
assert(identity.publickey && identity.publickey.includes('BEGIN PGP PUBLIC KEY BLOCK'), 'Public key is not valid');
|
|
|
|
}
|
2024-06-30 22:06:37 +02:00
|
|
|
},
|
2024-07-03 12:18:42 +03:00
|
|
|
{
|
|
|
|
name: 'Join Tribe',
|
|
|
|
async run(user) {
|
|
|
|
return await apx.joinTribe(user.alias, 'smatchit');
|
|
|
|
},
|
|
|
|
verify(result) {
|
|
|
|
assert(result, 'Joining tribe should return true');
|
|
|
|
}
|
2024-06-30 22:06:37 +02:00
|
|
|
},
|
2024-07-03 12:18:42 +03:00
|
|
|
{
|
|
|
|
name: 'Delete Alias',
|
|
|
|
async run(user) {
|
|
|
|
return await apx.deleteAlias(user.alias);
|
|
|
|
},
|
|
|
|
verify(result) {
|
|
|
|
assert(result, 'Deleting alias should return true');
|
|
|
|
}
|
2024-06-30 22:06:37 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
ut.run = async (options) => {
|
2024-07-03 12:18:42 +03:00
|
|
|
console.log('Test Pagans Registration and Authentication');
|
|
|
|
|
|
|
|
// Create and test identities for all users
|
|
|
|
for (const userKey of Object.keys(pagans)) {
|
|
|
|
const user = pagans[userKey];
|
|
|
|
console.log(`\n--- Creating and testing identity for ${user.alias} ---`);
|
|
|
|
|
|
|
|
for (const testCase of testCases) {
|
|
|
|
console.log(`Running test case: ${testCase.name} for ${user.alias}`);
|
|
|
|
try {
|
|
|
|
const result = await testCase.run(user);
|
|
|
|
if (result) {
|
|
|
|
testCase.verify(result, user.alias);
|
|
|
|
console.log(`Test case ${testCase.name} for ${user.alias} passed`);
|
|
|
|
} else {
|
|
|
|
console.error(`Test case ${testCase.name} for ${user.alias} failed: No result returned`);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Test case ${testCase.name} for ${user.alias} failed:`, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`--- Finished testing for ${user.alias} ---\n`);
|
2024-06-30 22:06:37 +02:00
|
|
|
}
|
2024-07-03 12:18:42 +03:00
|
|
|
|
|
|
|
console.log('All test cases ran successfully');
|
2024-06-30 22:06:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ut;
|
|
|
|
|
2024-07-03 12:18:42 +03:00
|
|
|
|
2024-06-30 22:06:37 +02:00
|
|
|
if (require.main === module) {
|
2024-07-03 12:18:42 +03:00
|
|
|
ut.run({ verbose: true }).catch(err => {
|
|
|
|
console.error('Test case failed:', err);
|
|
|
|
});
|
2024-06-30 22:06:37 +02:00
|
|
|
}
|