Pagans unittest update (with auth)
This commit is contained in:
		| @@ -1,7 +1,7 @@ | |||||||
| const assert = require('assert'); | const assert = require('assert'); | ||||||
| const openpgp = require('openpgp'); | const openpgp = require('openpgp'); | ||||||
| const Pagans = require('../Pagans'); | const Pagans = require('../Pagans'); | ||||||
|  | const dayjs = require('dayjs'); | ||||||
|  |  | ||||||
| const ut = { name: 'Pagans' }; | const ut = { name: 'Pagans' }; | ||||||
|  |  | ||||||
| @@ -38,6 +38,8 @@ ut.test = { | |||||||
|  |  | ||||||
| // Apx | // Apx | ||||||
| const apx = {}; | const apx = {}; | ||||||
|  | apx.data = {}; | ||||||
|  | apx.data.headers = {}; | ||||||
|  |  | ||||||
| apx.generateKey = async (alias, passphrase) => { | apx.generateKey = async (alias, passphrase) => { | ||||||
|   try { |   try { | ||||||
| @@ -79,15 +81,46 @@ apx.createIdentity = async (alias, passphrase) => { | |||||||
|   } |   } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| // Mock joinTribe | apx.clearmsgSignature = async (privateKeyArmored, passphrase, message) => { | ||||||
| apx.joinTribe = async (alias, tribe) => { |  | ||||||
|   try { |   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}`); |     if (!privateKey.isDecrypted()) { | ||||||
|     return { status: 200, message: 'Success' }; // Mock success response |       decryptedPrivateKey = await openpgp.decryptKey({ | ||||||
|  |         privateKey, | ||||||
|  |         passphrase | ||||||
|  |       }); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     const signedMessage = await openpgp.sign({ | ||||||
|  |       message: await openpgp.createMessage({ text: message }), | ||||||
|  |       signingKeys: decryptedPrivateKey | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     return signedMessage; | ||||||
|   } catch (error) { |   } 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; |     throw error; | ||||||
|   } |   } | ||||||
| }; | }; | ||||||
| @@ -109,15 +142,22 @@ ut.createIdentity = async (t) => { | |||||||
|       throw new Error(`Failed to generate keys for ${t.alias}`); |       throw new Error(`Failed to generate keys for ${t.alias}`); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     // Join tribe |  | ||||||
|     await apx.joinTribe(t.alias, ut.test.tribe); |  | ||||||
|  |  | ||||||
|     return keys; |     return keys; | ||||||
|   } catch (error) { |   } catch (error) { | ||||||
|     throw new Error(`Error creating identity for ${t.alias}: ${error.message}`); |     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 () => { | ut.run = async () => { | ||||||
|   console.log('Test Pagans Registration and Authentication'); |   console.log('Test Pagans Registration and Authentication'); | ||||||
|  |  | ||||||
| @@ -125,6 +165,11 @@ ut.run = async () => { | |||||||
|     try { |     try { | ||||||
|       console.log(`Creating identity for ${t.alias}`); |       console.log(`Creating identity for ${t.alias}`); | ||||||
|       const identity = await ut.createIdentity(t); |       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.`); |       console.log(`All operations for ${t.alias} completed successfully.`); | ||||||
|     } catch (error) { |     } catch (error) { | ||||||
|       console.error(`Error processing ${t.alias}: ${error.message}`); |       console.error(`Error processing ${t.alias}: ${error.message}`); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user