2024-07-09 21:11:40 +03:00
|
|
|
const assert = require('assert');
|
|
|
|
const openpgp = require('openpgp');
|
|
|
|
const Pagans = require('../Pagans');
|
2024-06-30 22:06:37 +02:00
|
|
|
|
|
|
|
|
2024-07-09 21:11:40 +03:00
|
|
|
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
|
2024-06-30 22:06:37 +02:00
|
|
|
const apx = {};
|
|
|
|
|
|
|
|
apx.generateKey = async (alias, passphrase) => {
|
2024-07-03 12:13:08 +02: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:13:08 +02:00
|
|
|
try {
|
|
|
|
const { privatekey, publickey } = await apx.generateKey(alias, passphrase);
|
2024-07-09 21:11:40 +03:00
|
|
|
if (!privatekey || !publickey) {
|
|
|
|
throw new Error(`Failed to generate keys for ${alias}`);
|
|
|
|
}
|
2024-07-03 12:13:08 +02:00
|
|
|
console.log(`Identity created successfully for alias: ${alias}`);
|
|
|
|
return { alias, privatekey, publickey };
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Error creating identity for alias: ${alias}`, error);
|
2024-07-09 21:11:40 +03:00
|
|
|
throw error;
|
2024-07-03 12:13:08 +02:00
|
|
|
}
|
2024-06-30 22:06:37 +02:00
|
|
|
};
|
|
|
|
|
2024-07-09 21:11:40 +03:00
|
|
|
// Mock joinTribe
|
2024-07-03 12:18:42 +03:00
|
|
|
apx.joinTribe = async (alias, tribe) => {
|
2024-07-09 21:11:40 +03:00
|
|
|
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;
|
|
|
|
}
|
2024-06-30 22:06:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-07-09 21:11:40 +03:00
|
|
|
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}.`);
|
2024-07-03 12:13:08 +02:00
|
|
|
}
|
2024-07-09 21:11:40 +03:00
|
|
|
|
|
|
|
// 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}`);
|
2024-07-03 12:13:08 +02:00
|
|
|
}
|
2024-06-30 22:06:37 +02:00
|
|
|
|
2024-07-09 21:11:40 +03:00
|
|
|
// Join tribe
|
|
|
|
await apx.joinTribe(t.alias, ut.test.tribe);
|
2024-06-30 22:06:37 +02:00
|
|
|
|
2024-07-09 21:11:40 +03:00
|
|
|
return keys;
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`Error creating identity for ${t.alias}: ${error.message}`);
|
|
|
|
}
|
2024-06-30 22:06:37 +02:00
|
|
|
};
|
|
|
|
|
2024-07-09 21:11:40 +03:00
|
|
|
ut.run = async () => {
|
|
|
|
console.log('Test Pagans Registration and Authentication');
|
2024-06-30 22:06:37 +02:00
|
|
|
|
2024-07-09 21:11:40 +03:00
|
|
|
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}`);
|
2024-06-30 22:06:37 +02:00
|
|
|
}
|
2024-07-03 12:13:08 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 21:11:40 +03:00
|
|
|
console.log('All test cases ran successfully');
|
2024-06-30 22:06:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ut;
|
|
|
|
|
2024-07-09 21:11:40 +03:00
|
|
|
|
2024-06-30 22:06:37 +02:00
|
|
|
if (require.main === module) {
|
2024-07-09 21:11:40 +03:00
|
|
|
ut.run().catch(err => {
|
|
|
|
console.error('Test case failed:', err);
|
2024-07-03 12:13:08 +02:00
|
|
|
});
|
2024-06-30 22:06:37 +02:00
|
|
|
}
|