test: Improve encrypt data tests for AppAuthClient model

This commit is contained in:
Faruk AYDIN
2024-10-07 14:21:14 +02:00
parent 737391c721
commit 26be72f76d

View File

@@ -15,23 +15,52 @@ describe('AppAuthClient model', () => {
expect(AppAuthClient.jsonSchema).toMatchSnapshot(); expect(AppAuthClient.jsonSchema).toMatchSnapshot();
}); });
it('encryptData should encrypt formattedAuthDefaults and set it to authDefaults', async () => { describe('encryptData', () => {
const formattedAuthDefaults = { it('should return undefined if eligibleForEncryption is not true', async () => {
key: 'value', vi.spyOn(
}; AppAuthClient.prototype,
'eligibleForEncryption'
).mockReturnValue(false);
const appAuthClient = await createAppAuthClient({ const appAuthClient = new AppAuthClient();
formattedAuthDefaults,
expect(appAuthClient.encryptData()).toBeUndefined();
}); });
expect( it('should encrypt formattedAuthDefaults and set it to authDefaults', async () => {
JSON.parse( vi.spyOn(
AES.decrypt( AppAuthClient.prototype,
appAuthClient.authDefaults, 'eligibleForEncryption'
appConfig.encryptionKey ).mockReturnValue(true);
).toString(enc)
) const formattedAuthDefaults = {
).toStrictEqual(formattedAuthDefaults); key: 'value',
};
const appAuthClient = new AppAuthClient();
appAuthClient.formattedAuthDefaults = formattedAuthDefaults;
appAuthClient.encryptData();
expect(appAuthClient.authDefaults).toBeDefined();
expect(appAuthClient.authDefaults).not.toEqual(formattedAuthDefaults);
});
it('should encrypt formattedAuthDefaults and remove formattedAuthDefaults', async () => {
vi.spyOn(
AppAuthClient.prototype,
'eligibleForEncryption'
).mockReturnValue(true);
const formattedAuthDefaults = {
key: 'value',
};
const appAuthClient = new AppAuthClient();
appAuthClient.formattedAuthDefaults = formattedAuthDefaults;
appAuthClient.encryptData();
expect(appAuthClient.formattedAuthDefaults).not.toBeDefined();
});
}); });
it('decryptData should decrypt authDefaults and set it to formattedAuthDefaults', async () => { it('decryptData should decrypt authDefaults and set it to formattedAuthDefaults', async () => {