From 807faa3c93dab52b7463a9b26289ef1f01976d0c Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Wed, 20 Nov 2024 11:02:55 +0000 Subject: [PATCH] test(user): write tests for $beforeInsert and $beforeUpdate --- packages/backend/src/models/user.js | 13 ++- packages/backend/src/models/user.test.js | 108 +++++++++++++++++++++++ 2 files changed, 117 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/models/user.js b/packages/backend/src/models/user.js index e75c0c40..f1e88572 100644 --- a/packages/backend/src/models/user.js +++ b/packages/backend/src/models/user.js @@ -602,10 +602,17 @@ class User extends Base { return conditionMap; } + lowercaseEmail() { + if (this.email) { + this.email = this.email.toLowerCase(); + } + } + g; + async $beforeInsert(queryContext) { await super.$beforeInsert(queryContext); - this.email = this.email.toLowerCase(); + this.lowercaseEmail(); await this.generateHash(); if (appConfig.isCloud) { @@ -616,9 +623,7 @@ class User extends Base { async $beforeUpdate(opt, queryContext) { await super.$beforeUpdate(opt, queryContext); - if (this.email) { - this.email = this.email.toLowerCase(); - } + this.lowercaseEmail(); await this.generateHash(); } diff --git a/packages/backend/src/models/user.test.js b/packages/backend/src/models/user.test.js index 51fd8438..50ed156f 100644 --- a/packages/backend/src/models/user.test.js +++ b/packages/backend/src/models/user.test.js @@ -1244,4 +1244,112 @@ describe('User model', () => { ); }); }); + + it('lowercaseEmail should lowercase the user email', () => { + const user = new User(); + user.email = 'USER@AUTOMATISCH.IO'; + + user.lowercaseEmail(); + + expect(user.email).toBe('user@automatisch.io'); + }); + + describe('$beforeInsert', () => { + it('should call super.$beforeInsert', async () => { + const superBeforeInsertSpy = vi + .spyOn(User.prototype, '$beforeInsert') + .mockResolvedValue(); + + await createUser(); + + expect(superBeforeInsertSpy).toHaveBeenCalledOnce(); + }); + + it('should lowercase the user email', async () => { + const user = await createUser({ + fullName: 'Sample user', + email: 'USER@AUTOMATISCH.IO', + }); + + expect(user.email).toBe('user@automatisch.io'); + }); + + it('should generate password hash', async () => { + const user = await createUser({ + fullName: 'Sample user', + email: 'user@automatisch.io', + password: 'sample-password', + }); + + expect(user.password).not.toBe('sample-password'); + expect(await user.login('sample-password')).toBe(true); + }); + + it('should start trial period if Automatisch is a cloud installation', async () => { + vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(true); + + const startTrialPeriodSpy = vi.spyOn(User.prototype, 'startTrialPeriod'); + + await createUser({ + fullName: 'Sample user', + email: 'user@automatisch.io', + }); + + expect(startTrialPeriodSpy).toHaveBeenCalledOnce(); + }); + + it('should not start trial period if Automatisch is not a cloud installation', async () => { + vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(false); + + const startTrialPeriodSpy = vi.spyOn(User.prototype, 'startTrialPeriod'); + + await createUser({ + fullName: 'Sample user', + email: 'user@automatisch.io', + }); + + expect(startTrialPeriodSpy).not.toHaveBeenCalled(); + }); + }); + + describe('$beforeUpdate', () => { + it('should call super.$beforeUpdate', async () => { + const superBeforeUpdateSpy = vi + .spyOn(User.prototype, '$beforeUpdate') + .mockResolvedValue(); + + const user = await createUser({ + fullName: 'Sample user', + email: 'user@automatisch.io', + }); + + await user.$query().patch({ fullName: 'Updated user name' }); + + expect(superBeforeUpdateSpy).toHaveBeenCalledOnce(); + }); + + it('should lowercase the user email if given', async () => { + const user = await createUser({ + fullName: 'Sample user', + email: 'user@automatisch.io', + }); + + await user.$query().patchAndFetch({ email: 'NEW_EMAIL@AUTOMATISCH.IO' }); + + expect(user.email).toBe('new_email@automatisch.io'); + }); + + it('should generate password hash', async () => { + const user = await createUser({ + fullName: 'Sample user', + email: 'user@automatisch.io', + password: 'sample-password', + }); + + await user.$query().patchAndFetch({ password: 'new-password' }); + + expect(user.password).not.toBe('new-password'); + expect(await user.login('new-password')).toBe(true); + }); + }); });