modification Pagansunittest for Peter
This commit is contained in:
		@@ -1,180 +1,237 @@
 | 
				
			|||||||
const assert = require('assert');
 | 
					const assert = require("assert");
 | 
				
			||||||
const openpgp = require('openpgp');
 | 
					const openpgp = require("openpgp");
 | 
				
			||||||
const dayjs = require('dayjs');
 | 
					const dayjs = require("dayjs");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const ut = { name: 'Pagans' };
 | 
					const ut = { name: "Pagans" };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const apx = {};
 | 
					const apx = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
apx.generateKey = async (alias, passphrase) => {
 | 
					apx.generateKey = async (alias, passphrase) => {
 | 
				
			||||||
    try {
 | 
					  try {
 | 
				
			||||||
        console.log(`\nGenerating keys for alias: ${alias}`);
 | 
					    console.log(`\nGenerating keys for alias: ${alias}`);
 | 
				
			||||||
        const pgpParams = {
 | 
					    const pgpParams = {
 | 
				
			||||||
            type: 'ecc',
 | 
					      type: "ecc",
 | 
				
			||||||
            curve: 'curve25519',
 | 
					      curve: "curve25519",
 | 
				
			||||||
            userIDs: [{ name: alias }],
 | 
					      userIDs: [{ name: alias }],
 | 
				
			||||||
            passphrase: passphrase,
 | 
					      passphrase: passphrase,
 | 
				
			||||||
            format: 'armored'
 | 
					      format: "armored",
 | 
				
			||||||
        };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const key = await openpgp.generateKey(pgpParams);
 | 
					    const key = await openpgp.generateKey(pgpParams);
 | 
				
			||||||
        console.log(`Keys generated successfully for alias: ${alias}`);
 | 
					    console.log(`Keys generated successfully for alias: ${alias}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return {
 | 
					    return {
 | 
				
			||||||
            alias,
 | 
					      alias,
 | 
				
			||||||
            passphrase,
 | 
					      passphrase,
 | 
				
			||||||
            privatekey: key.privateKey,
 | 
					      privatekey: key.privateKey,
 | 
				
			||||||
            publickey: key.publicKey
 | 
					      publickey: key.publicKey,
 | 
				
			||||||
        };
 | 
					    };
 | 
				
			||||||
    } catch (error) {
 | 
					  } catch (error) {
 | 
				
			||||||
        console.error(`Error generating keys for alias: ${alias}`, error);
 | 
					    console.error(`Error generating keys for alias: ${alias}`, error);
 | 
				
			||||||
        return {};
 | 
					    return {};
 | 
				
			||||||
    }
 | 
					  }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
apx.createIdentity = async (alias, passphrase) => {
 | 
					apx.createIdentity = async (alias, passphrase) => {
 | 
				
			||||||
    try {
 | 
					  try {
 | 
				
			||||||
        const { privatekey, publickey } = await apx.generateKey(alias, passphrase);
 | 
					    const { privatekey, publickey } = await apx.generateKey(alias, passphrase);
 | 
				
			||||||
        console.log(`Identity created successfully for alias: ${alias}`);
 | 
					    console.log(`Identity created successfully for alias: ${alias}`);
 | 
				
			||||||
        return { alias, privatekey, publickey };
 | 
					    return { alias, privatekey, publickey };
 | 
				
			||||||
    } catch (error) {
 | 
					  } catch (error) {
 | 
				
			||||||
        console.error(`Error creating identity for alias: ${alias}`, error);
 | 
					    console.error(`Error creating identity for alias: ${alias}`, error);
 | 
				
			||||||
    }
 | 
					  }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
apx.joinTribe = async (alias, tribe) => {
 | 
					apx.joinTribe = async (alias, tribe) => {
 | 
				
			||||||
    // Mock implementation of joining a tribe
 | 
					  // Mock implementation of joining a tribe
 | 
				
			||||||
    console.log(`Alias ${alias} joined tribe ${tribe}`);
 | 
					  console.log(`Alias ${alias} joined tribe ${tribe}`);
 | 
				
			||||||
    return true;
 | 
					  return true;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
apx.deleteAlias = async (alias) => {
 | 
					apx.deleteAlias = async (alias) => {
 | 
				
			||||||
    // Mock implementation of deleting an alias
 | 
					  // Mock implementation of deleting an alias
 | 
				
			||||||
    console.log(`Alias ${alias} deleted`);
 | 
					  console.log(`Alias ${alias} deleted`);
 | 
				
			||||||
    return true;
 | 
					  return true;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const personData = {};
 | 
					const personData = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const apxFunctions = {
 | 
					const apxFunctions = {
 | 
				
			||||||
    modifyPersonData(alias, newFirstName) {
 | 
					  modifyPersonData(alias, newFirstName) {
 | 
				
			||||||
        if (!personData[alias]) {
 | 
					    if (!personData[alias]) {
 | 
				
			||||||
            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];
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    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];
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const pagans = {
 | 
					ut.test = {
 | 
				
			||||||
    adminsmatchits: { alias: 'adminsmatchit', passphrase: 'adminsmatchitPass' },
 | 
					  tribe: "smatchit",
 | 
				
			||||||
    recruiters: { alias: 'recruiter', passphrase: 'recruiterPass' },
 | 
					  pagans: [
 | 
				
			||||||
    seekers: { alias: 'seeker', passphrase: 'seekerPass' },
 | 
					    {
 | 
				
			||||||
    adminrecruiters: { alias: 'adminrecruiter', passphrase: 'adminrecruiterPass' }
 | 
					      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",
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					  ],
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Object.keys(pagans).forEach(key => {
 | 
					
 | 
				
			||||||
    pagans[key].headers = {
 | 
					ut.createpersons=()=>{
 | 
				
			||||||
        xtrkversion: 1,
 | 
					    let msg = "";
 | 
				
			||||||
        xalias: 'anonymous',
 | 
					    ut.test.pagans.forEach((t) => {
 | 
				
			||||||
        xapp: 'smatchapp',
 | 
					        //test if alias does not already exist
 | 
				
			||||||
        xdays: 0,
 | 
					        const pagan={}
 | 
				
			||||||
        xhash: 'anonymous',
 | 
					        const getalias= Pagans.getalias(t.alias);
 | 
				
			||||||
        xlang: 'fr',
 | 
					        if (getalias.status!=404) then alias already exist 
 | 
				
			||||||
        xprofils: 'anonymous',
 | 
					        else {delete}
 | 
				
			||||||
        xtribe: 'smatchit',
 | 
					
 | 
				
			||||||
        xuuid: '0'
 | 
					        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 = [
 | 
					const testCases = [
 | 
				
			||||||
    {
 | 
					  {
 | 
				
			||||||
        name: 'Create Identity',
 | 
					    name: "Create Identity",
 | 
				
			||||||
        async run(user) {
 | 
					    async run(user) {
 | 
				
			||||||
            const identity = await apx.createIdentity(user.alias, user.passphrase);
 | 
					      const identity = await apx.createIdentity(user.alias, user.passphrase);
 | 
				
			||||||
            if (identity) {
 | 
					      if (identity) {
 | 
				
			||||||
                user.privateKey = identity.privatekey;
 | 
					        user.privateKey = identity.privatekey;
 | 
				
			||||||
                user.publicKey = identity.publickey;
 | 
					        user.publicKey = identity.publickey;
 | 
				
			||||||
            }
 | 
					      }
 | 
				
			||||||
            return identity;
 | 
					      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');
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    verify(identity, alias) {
 | 
				
			||||||
        name: 'Join Tribe',
 | 
					      assert(identity, "Identity should not be undefined");
 | 
				
			||||||
        async run(user) {
 | 
					      assert(identity.alias === alias, "Alias should match");
 | 
				
			||||||
            return await apx.joinTribe(user.alias, 'smatchit');
 | 
					      assert(
 | 
				
			||||||
        },
 | 
					        identity.privatekey &&
 | 
				
			||||||
        verify(result) {
 | 
					          identity.privatekey.includes("BEGIN PGP PRIVATE KEY BLOCK"),
 | 
				
			||||||
            assert(result, 'Joining tribe should return true');
 | 
					        "Private key is not valid"
 | 
				
			||||||
        }
 | 
					      );
 | 
				
			||||||
 | 
					      assert(
 | 
				
			||||||
 | 
					        identity.publickey &&
 | 
				
			||||||
 | 
					          identity.publickey.includes("BEGIN PGP PUBLIC KEY BLOCK"),
 | 
				
			||||||
 | 
					        "Public key is not valid"
 | 
				
			||||||
 | 
					      );
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					  },
 | 
				
			||||||
        name: 'Delete Alias',
 | 
					  {
 | 
				
			||||||
        async run(user) {
 | 
					    name: "Join Tribe",
 | 
				
			||||||
            return await apx.deleteAlias(user.alias);
 | 
					    async run(user) {
 | 
				
			||||||
        },
 | 
					      return await apx.joinTribe(user.alias, "smatchit");
 | 
				
			||||||
        verify(result) {
 | 
					    },
 | 
				
			||||||
            assert(result, 'Deleting alias should return true');
 | 
					    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) => {
 | 
					ut.run = async (options) => {
 | 
				
			||||||
    console.log('Test Pagans Registration and Authentication');
 | 
					  console.log("Test Pagans Registration and Authentication");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Create and test identities for all users
 | 
					  // Create and test identities for all users
 | 
				
			||||||
    for (const userKey of Object.keys(pagans)) {
 | 
					  for (const userKey of Object.keys(pagans)) {
 | 
				
			||||||
        const user = pagans[userKey];
 | 
					    const user = pagans[userKey];
 | 
				
			||||||
        console.log(`\n--- Creating and testing identity for ${user.alias} ---`);
 | 
					    console.log(`\n--- Creating and testing identity for ${user.alias} ---`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for (const testCase of testCases) {
 | 
					    for (const testCase of testCases) {
 | 
				
			||||||
            console.log(`Running test case: ${testCase.name} for ${user.alias}`);
 | 
					      console.log(`Running test case: ${testCase.name} for ${user.alias}`);
 | 
				
			||||||
            try {
 | 
					      try {
 | 
				
			||||||
                const result = await testCase.run(user);
 | 
					        const result = await testCase.run(user);
 | 
				
			||||||
                if (result) {
 | 
					        if (result) {
 | 
				
			||||||
                    testCase.verify(result, user.alias);
 | 
					          testCase.verify(result, user.alias);
 | 
				
			||||||
                    console.log(`Test case ${testCase.name} for ${user.alias} passed`);
 | 
					          console.log(`Test case ${testCase.name} for ${user.alias} passed`);
 | 
				
			||||||
                } else {
 | 
					        } else {
 | 
				
			||||||
                    console.error(`Test case ${testCase.name} for ${user.alias} failed: No result returned`);
 | 
					          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);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					      } catch (error) {
 | 
				
			||||||
        console.log(`--- Finished testing for ${user.alias} ---\n`);
 | 
					        console.error(
 | 
				
			||||||
 | 
					          `Test case ${testCase.name} for ${user.alias} failed:`,
 | 
				
			||||||
 | 
					          error
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    console.log('All test cases ran successfully');
 | 
					    console.log(`--- Finished testing for ${user.alias} ---\n`);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  console.log("All test cases ran successfully");
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = ut;
 | 
					module.exports = ut;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
if (require.main === module) {
 | 
					if (require.main === module) {
 | 
				
			||||||
    ut.run({ verbose: true }).catch(err => {
 | 
					  ut.run({ verbose: true }).catch((err) => {
 | 
				
			||||||
        console.error('Test case failed:', err);
 | 
					    console.error("Test case failed:", err);
 | 
				
			||||||
    });
 | 
					  });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user