From 06cbd8f8e8f94948c1100aac86c4a93c2995568c Mon Sep 17 00:00:00 2001 From: phondropoulos Date: Tue, 9 Jul 2024 21:11:40 +0300 Subject: [PATCH] Updated Pagansunittest --- models/unittest/Pagansunittest.js | 271 ++++++++++-------------------- 1 file changed, 89 insertions(+), 182 deletions(-) diff --git a/models/unittest/Pagansunittest.js b/models/unittest/Pagansunittest.js index ccd7ed3..8313da9 100644 --- a/models/unittest/Pagansunittest.js +++ b/models/unittest/Pagansunittest.js @@ -1,9 +1,42 @@ -const assert = require("assert"); -const openpgp = require("openpgp"); -const dayjs = require("dayjs"); +const assert = require('assert'); +const openpgp = require('openpgp'); +const Pagans = require('../Pagans'); -const ut = { name: "Pagans" }; +const ut = { name: 'Pagans' }; + +// Data +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", + }, + { + alias: "unittestrecruiter", + passphrase: "recruiterPass", + persons: { firstname: "toto", lastname: "titi", profils: ["anonymous"] }, + testprofil: "recruiter", + }, + { + alias: "unittestadminrecruiter", + passphrase: "adminrecruiterPass", + persons: { firstname: "toto", lastname: "titi", profils: ["anonymous"] }, + testprofil: "adminrecruiter", + }, + ], +}; + +// Apx const apx = {}; apx.generateKey = async (alias, passphrase) => { @@ -35,203 +68,77 @@ apx.generateKey = async (alias, passphrase) => { apx.createIdentity = async (alias, passphrase) => { try { const { privatekey, publickey } = await apx.generateKey(alias, passphrase); + if (!privatekey || !publickey) { + throw new Error(`Failed to generate keys for ${alias}`); + } console.log(`Identity created successfully for alias: ${alias}`); return { alias, privatekey, publickey }; } catch (error) { console.error(`Error creating identity for alias: ${alias}`, error); + throw error; } }; +// Mock joinTribe apx.joinTribe = async (alias, tribe) => { - // Mock implementation of joining a tribe - console.log(`Alias ${alias} joined tribe ${tribe}`); - return true; -}; - -apx.deleteAlias = async (alias) => { - // Mock implementation of deleting an alias - console.log(`Alias ${alias} deleted`); - return true; -}; - -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]; - }, -}; - -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", - }, - ], + try { + console.log(`Joining alias ${alias} to tribe ${tribe}`); + + console.log(`Alias ${alias} successfully joined tribe ${tribe}`); + return { status: 200, message: 'Success' }; // Mock success response + } catch (error) { + console.error(`Error joining tribe for alias ${alias}: ${error.message}`); + throw error; + } }; -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); -} - - -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", - }; -}); - -const testCases = [ - { - 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" - ); - }, - }, - { - name: "Join Tribe", - async run(user) { - return await apx.joinTribe(user.alias, "smatchit"); - }, - verify(result) { - assert(result, "Joining tribe should return true"); - }, - }, - { - name: "Delete Alias", - async run(user) { - return await apx.deleteAlias(user.alias); - }, - verify(result) { - assert(result, "Deleting alias should return true"); - }, - }, -]; - -ut.run = async (options) => { - 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 - ); - } +ut.createIdentity = async (t) => { + try { + // Test if alias already exists + const getalias = await Pagans.getalias(t.alias); + if (getalias.status !== 404) { + console.log(`Alias ${t.alias} already exists. Deleting...`); + await Pagans.deletealias(t.alias); + console.log(`Deleted ${t.alias}.`); } - console.log(`--- Finished testing for ${user.alias} ---\n`); + // Generate keys + const keys = await apx.createIdentity(t.alias, t.passphrase); + if (!keys.privatekey || !keys.publickey) { + throw new Error(`Failed to generate keys for ${t.alias}`); + } + + // Join tribe + await apx.joinTribe(t.alias, ut.test.tribe); + + return keys; + } catch (error) { + throw new Error(`Error creating identity for ${t.alias}: ${error.message}`); + } +}; + +ut.run = async () => { + console.log('Test Pagans Registration and Authentication'); + + for (const t of ut.test.pagans) { + try { + console.log(`Creating identity for ${t.alias}`); + const identity = await ut.createIdentity(t); + console.log(`All operations for ${t.alias} completed successfully.`); + } catch (error) { + console.error(`Error processing ${t.alias}: ${error.message}`); + } } - 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().catch(err => { + console.error('Test case failed:', err); }); }