forked from apxtri/apxtri
Pagans unittest update (with auth)
This commit is contained in:
parent
6e71911978
commit
0f640116ec
@ -1,7 +1,7 @@
|
||||
const assert = require('assert');
|
||||
const openpgp = require('openpgp');
|
||||
const Pagans = require('../Pagans');
|
||||
|
||||
const dayjs = require('dayjs');
|
||||
|
||||
const ut = { name: 'Pagans' };
|
||||
|
||||
@ -38,6 +38,8 @@ ut.test = {
|
||||
|
||||
// Apx
|
||||
const apx = {};
|
||||
apx.data = {};
|
||||
apx.data.headers = {};
|
||||
|
||||
apx.generateKey = async (alias, passphrase) => {
|
||||
try {
|
||||
@ -79,15 +81,46 @@ apx.createIdentity = async (alias, passphrase) => {
|
||||
}
|
||||
};
|
||||
|
||||
// Mock joinTribe
|
||||
apx.joinTribe = async (alias, tribe) => {
|
||||
apx.clearmsgSignature = async (privateKeyArmored, passphrase, message) => {
|
||||
try {
|
||||
console.log(`Joining alias ${alias} to tribe ${tribe}`);
|
||||
const privateKey = await openpgp.readPrivateKey({ armoredKey: privateKeyArmored });
|
||||
let decryptedPrivateKey = privateKey;
|
||||
|
||||
console.log(`Alias ${alias} successfully joined tribe ${tribe}`);
|
||||
return { status: 200, message: 'Success' }; // Mock success response
|
||||
if (!privateKey.isDecrypted()) {
|
||||
decryptedPrivateKey = await openpgp.decryptKey({
|
||||
privateKey,
|
||||
passphrase
|
||||
});
|
||||
}
|
||||
|
||||
const signedMessage = await openpgp.sign({
|
||||
message: await openpgp.createMessage({ text: message }),
|
||||
signingKeys: decryptedPrivateKey
|
||||
});
|
||||
|
||||
return signedMessage;
|
||||
} catch (error) {
|
||||
console.error(`Error joining tribe for alias ${alias}: ${error.message}`);
|
||||
console.error('Error signing message:', error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
apx.authenticate = async (alias, passphrase, privatekey) => {
|
||||
try {
|
||||
apx.data.headers.xalias = alias;
|
||||
apx.data.headers.xdays = dayjs().valueOf();
|
||||
const msg = `${alias}_${apx.data.headers.xdays}`;
|
||||
|
||||
console.log("pvk", privatekey);
|
||||
apx.data.headers.xhash = await apx.clearmsgSignature(privatekey, passphrase, msg);
|
||||
if (!apx.data.headers.xhash) {
|
||||
throw new Error('Failed to generate xhash for authentication');
|
||||
}
|
||||
|
||||
console.log(`Authentication successful for alias: ${alias}`);
|
||||
return apx.data.headers;
|
||||
} catch (error) {
|
||||
console.error(`Error authenticating alias: ${alias}`, error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
@ -109,15 +142,22 @@ ut.createIdentity = async (t) => {
|
||||
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}`);
|
||||
}
|
||||
};
|
||||
|
||||
// Authentication process
|
||||
ut.authenticate = async (t, privatekey, passphrase) => {
|
||||
try {
|
||||
const headers = await apx.authenticate(t.alias, passphrase, privatekey);
|
||||
return headers;
|
||||
} catch (error) {
|
||||
throw new Error(`Error authenticating ${t.alias}: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
ut.run = async () => {
|
||||
console.log('Test Pagans Registration and Authentication');
|
||||
|
||||
@ -125,6 +165,11 @@ ut.run = async () => {
|
||||
try {
|
||||
console.log(`Creating identity for ${t.alias}`);
|
||||
const identity = await ut.createIdentity(t);
|
||||
|
||||
console.log(`Authenticating ${t.alias}`);
|
||||
const headers = await ut.authenticate(t, identity.privatekey, t.passphrase);
|
||||
console.log(`Headers for ${t.alias}:`, headers);
|
||||
|
||||
console.log(`All operations for ${t.alias} completed successfully.`);
|
||||
} catch (error) {
|
||||
console.error(`Error processing ${t.alias}: ${error.message}`);
|
||||
|
Loading…
x
Reference in New Issue
Block a user