1
0
forked from apxtri/apxtri

modification Pagansunittest for Peter

This commit is contained in:
philc 2024-07-03 12:13:08 +02:00
parent d6bc1fef8c
commit 9497079b17

View File

@ -1,8 +1,8 @@
const assert = require('assert');
const openpgp = require('openpgp');
const dayjs = require('dayjs');
const assert = require("assert");
const openpgp = require("openpgp");
const dayjs = require("dayjs");
const ut = { name: 'Pagans' };
const ut = { name: "Pagans" };
const apx = {};
@ -10,11 +10,11 @@ apx.generateKey = async (alias, passphrase) => {
try {
console.log(`\nGenerating keys for alias: ${alias}`);
const pgpParams = {
type: 'ecc',
curve: 'curve25519',
type: "ecc",
curve: "curve25519",
userIDs: [{ name: alias }],
passphrase: passphrase,
format: 'armored'
format: "armored",
};
const key = await openpgp.generateKey(pgpParams);
@ -24,7 +24,7 @@ apx.generateKey = async (alias, passphrase) => {
alias,
passphrase,
privatekey: key.privateKey,
publickey: key.publicKey
publickey: key.publicKey,
};
} catch (error) {
console.error(`Error generating keys for alias: ${alias}`, error);
@ -66,7 +66,9 @@ const apxFunctions = {
},
checkPersonData(alias, expectedFirstName) {
return personData[alias] && personData[alias].firstName === expectedFirstName;
return (
personData[alias] && personData[alias].firstName === expectedFirstName
);
},
removePersonModification(alias, originalFirstName) {
@ -79,33 +81,76 @@ const apxFunctions = {
deleteAlias(alias) {
delete personData[alias];
return !personData[alias];
},
};
ut.test = {
tribe: "smatchit",
pagans: [
{
alias: "unittestadminsmatchit",
passphrase: "adminsmatchitPass",
persons: { firstname: "toto", lastname: "titi", profils: ["anonymous"] },
testprofil: "adminrecruiter",
},
{
alias: "unittestseeker",
passphrase: "",
persons: { firstname: "toto", lastname: "titi", profils: ["anonymous"] },
testprofil: "seeker",
},
],
};
ut.createpersons=()=>{
let msg = "";
ut.test.pagans.forEach((t) => {
//test if alias does not already exist
const pagan={}
const getalias= Pagans.getalias(t.alias);
if (getalias.status!=404) then alias already exist
else {delete}
const keys = apx.generatekey(t.alias, t.passphrase)
pagans.public key
schema.properties = t.properties;
const res = Checkjson.schema.data(schema, t.data);
if (res.status != t.status) {
msg = msg == "" ? "Unconsistent testproperties() name list: " : `${msg},`;
if (options.verbose) {
console.log(t);
console.log(res);
}
};
msg += res.err.map((e) => ` ${t.name} ${e.info}`);
}
});
return assert.deepEqual(msg, "", msg);
}
const pagans = {
adminsmatchits: { alias: 'adminsmatchit', passphrase: 'adminsmatchitPass' },
recruiters: { alias: 'recruiter', passphrase: 'recruiterPass' },
seekers: { alias: 'seeker', passphrase: 'seekerPass' },
adminrecruiters: { alias: 'adminrecruiter', passphrase: 'adminrecruiterPass' }
};
Object.keys(pagans).forEach(key => {
Object.keys(pagans).forEach((key) => {
pagans[key].headers = {
xtrkversion: 1,
xalias: 'anonymous',
xapp: 'smatchapp',
xalias: "anonymous",
xapp: "smatchapp",
xdays: 0,
xhash: 'anonymous',
xlang: 'fr',
xprofils: 'anonymous',
xtribe: 'smatchit',
xuuid: '0'
xhash: "anonymous",
xlang: "fr",
xprofils: "anonymous",
xtribe: "smatchit",
xuuid: "0",
};
});
const testCases = [
{
name: 'Create Identity',
name: "Create Identity",
async run(user) {
const identity = await apx.createIdentity(user.alias, user.passphrase);
if (identity) {
@ -115,34 +160,42 @@ const testCases = [
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');
}
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"
);
},
},
{
name: 'Join Tribe',
name: "Join Tribe",
async run(user) {
return await apx.joinTribe(user.alias, 'smatchit');
return await apx.joinTribe(user.alias, "smatchit");
},
verify(result) {
assert(result, 'Joining tribe should return true');
}
assert(result, "Joining tribe should return true");
},
},
{
name: 'Delete Alias',
name: "Delete Alias",
async run(user) {
return await apx.deleteAlias(user.alias);
},
verify(result) {
assert(result, 'Deleting alias should return true');
}
}
assert(result, "Deleting alias should return true");
},
},
];
ut.run = async (options) => {
console.log('Test Pagans Registration and Authentication');
console.log("Test Pagans Registration and Authentication");
// Create and test identities for all users
for (const userKey of Object.keys(pagans)) {
@ -157,24 +210,28 @@ ut.run = async (options) => {
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`);
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.error(
`Test case ${testCase.name} for ${user.alias} failed:`,
error
);
}
}
console.log(`--- Finished testing for ${user.alias} ---\n`);
}
console.log('All test cases ran successfully');
console.log("All test cases ran successfully");
};
module.exports = ut;
if (require.main === module) {
ut.run({ verbose: true }).catch(err => {
console.error('Test case failed:', err);
ut.run({ verbose: true }).catch((err) => {
console.error("Test case failed:", err);
});
}