1
0
forked from apxtri/apxtri
apxtri/models/unittest/Pagansunittest.js

145 lines
3.8 KiB
JavaScript

const assert = require('assert');
const openpgp = require('openpgp');
const Pagans = require('../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) => {
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 {};
}
};
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) => {
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.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}.`);
}
// 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');
};
module.exports = ut;
if (require.main === module) {
ut.run().catch(err => {
console.error('Test case failed:', err);
});
}