From c1e8f5765f1d4b00fc14575509d496a480b38a43 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 19 Oct 2023 01:05:23 +0200 Subject: [PATCH 1/5] chore: Use cloud enabled version for test db --- packages/backend/.env-example.test | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/backend/.env-example.test b/packages/backend/.env-example.test index 38d96a64..9e435c04 100644 --- a/packages/backend/.env-example.test +++ b/packages/backend/.env-example.test @@ -12,3 +12,4 @@ POSTGRES_PORT=5432 POSTGRES_USERNAME=automatisch_test_user POSTGRES_PASSWORD=automatisch_test_user_password REDIS_HOST=localhost +AUTOMATISCH_CLOUD=true From 6d6b77148ddfcf14327ac259ab6599aa7fd73c65 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 19 Oct 2023 01:06:01 +0200 Subject: [PATCH 2/5] refactor: User fixture to pass additonal params --- packages/backend/test/fixtures/user.ts | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/packages/backend/test/fixtures/user.ts b/packages/backend/test/fixtures/user.ts index f7aaeab6..bcb8d1b3 100644 --- a/packages/backend/test/fixtures/user.ts +++ b/packages/backend/test/fixtures/user.ts @@ -1,25 +1,14 @@ import createRole from './role'; import { faker } from '@faker-js/faker'; +import User from '../../src/models/user'; -type UserParams = { - roleId?: string; - fullName?: string; - email?: string; - password?: string; -}; +const createUser = async (params: Partial = {}) => { + params.roleId = params?.roleId || (await createRole()).id; + params.fullName = params?.fullName || faker.person.fullName(); + params.email = params?.email || faker.internet.email(); + params.password = params?.password || faker.internet.password(); -const createUser = async (params: UserParams = {}) => { - const userData = { - roleId: params?.roleId || (await createRole()).id, - fullName: params?.fullName || faker.person.fullName(), - email: params?.email || faker.internet.email(), - password: params?.password || faker.internet.password(), - }; - - const [user] = await global.knex - .table('users') - .insert(userData) - .returning('*'); + const [user] = await global.knex.table('users').insert(params).returning('*'); return user; }; From 59770c80db13a22a4d0b6cb281ea66be24daa47b Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 19 Oct 2023 01:06:41 +0200 Subject: [PATCH 3/5] test: Implement tests for get trial status graphQL query --- .../queries/get-trial-status.ee.test.ts | 117 ++++++++++++++++++ packages/types/index.d.ts | 1 + 2 files changed, 118 insertions(+) create mode 100644 packages/backend/src/graphql/queries/get-trial-status.ee.test.ts diff --git a/packages/backend/src/graphql/queries/get-trial-status.ee.test.ts b/packages/backend/src/graphql/queries/get-trial-status.ee.test.ts new file mode 100644 index 00000000..285dcebd --- /dev/null +++ b/packages/backend/src/graphql/queries/get-trial-status.ee.test.ts @@ -0,0 +1,117 @@ +import { setMockConfig } from '../../../test/setup/set-mock-config'; +import request from 'supertest'; +import app from '../../app'; +import { IUser } from '@automatisch/types'; +import User from '../../models/user'; +import createUser from '../../../test/fixtures/user'; +import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id'; +import { DateTime } from 'luxon'; + +describe('graphQL getTrialStatus query', () => { + const query = ` + query GetTrialStatus { + getTrialStatus { + expireAt + } + } + `; + + const invalidToken = 'invalid-token'; + + describe('with unauthenticated user', () => { + it('should throw not authorized error', async () => { + const response = await request(app) + .post('/graphql') + .set('Authorization', invalidToken) + .send({ query }) + .expect(200); + + expect(response.body.errors).toBeDefined(); + expect(response.body.errors[0].message).toEqual('Not Authorised!'); + }); + }); + + describe('with authenticated user', () => { + let user: IUser, userToken: string; + + beforeEach(async () => { + const trialExpiryDate = DateTime.now().plus({ days: 30 }).toISODate(); + + user = await createUser({ trialExpiryDate }); + userToken = createAuthTokenByUserId(user.id); + }); + + describe('and with cloud flag disabled', () => { + beforeEach(async () => { + setMockConfig({ isCloud: false }); + }); + + it('should return null', async () => { + const response = await request(app) + .post('/graphql') + .set('Authorization', userToken) + .send({ query }) + .expect(200); + + const expectedResponsePayload = { + data: { getTrialStatus: null as string }, + }; + + expect(response.body).toEqual(expectedResponsePayload); + }); + }); + + describe('and with cloud flag enabled', () => { + beforeEach(async () => { + setMockConfig({ isCloud: true }); + }); + + describe('and not in trial and has active subscription', () => { + beforeEach(async () => { + jest.spyOn(User.prototype, 'inTrial').mockResolvedValue(false); + jest + .spyOn(User.prototype, 'hasActiveSubscription') + .mockResolvedValue(true); + }); + + it('should return null', async () => { + const response = await request(app) + .post('/graphql') + .set('Authorization', userToken) + .send({ query }) + .expect(200); + + const expectedResponsePayload = { + data: { getTrialStatus: null as string }, + }; + + expect(response.body).toEqual(expectedResponsePayload); + }); + }); + + describe('and in trial period', () => { + beforeEach(async () => { + jest.spyOn(User.prototype, 'inTrial').mockResolvedValue(true); + }); + + it('should return null', async () => { + const response = await request(app) + .post('/graphql') + .set('Authorization', userToken) + .send({ query }) + .expect(200); + + const expectedResponsePayload = { + data: { + getTrialStatus: { + expireAt: new Date(user.trialExpiryDate).getTime().toString(), + }, + }, + }; + + expect(response.body).toEqual(expectedResponsePayload); + }); + }); + }); + }); +}); diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index 514eb206..e8a70979 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -101,6 +101,7 @@ export interface IUser { permissions: IPermission[]; createdAt: string | Date; updatedAt: string | Date; + trialExpiryDate: string | Date; } export interface IRole { From aead014bcf99f7f1d07e3acb6d0f0e42f9ed76e5 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Thu, 19 Oct 2023 13:41:12 +0200 Subject: [PATCH 4/5] refactor: remove additional mock implentation in tests --- .../graphql/queries/get-automatisch-info.test.ts | 8 +++++--- .../src/graphql/queries/get-trial-status.ee.test.ts | 6 +++--- packages/backend/test/setup/global-hooks.ts | 11 ----------- packages/backend/test/setup/set-mock-config.ts | 13 ------------- 4 files changed, 8 insertions(+), 30 deletions(-) delete mode 100644 packages/backend/test/setup/set-mock-config.ts diff --git a/packages/backend/src/graphql/queries/get-automatisch-info.test.ts b/packages/backend/src/graphql/queries/get-automatisch-info.test.ts index 0e72f2e8..bd06a2a7 100644 --- a/packages/backend/src/graphql/queries/get-automatisch-info.test.ts +++ b/packages/backend/src/graphql/queries/get-automatisch-info.test.ts @@ -1,7 +1,7 @@ -import { setMockConfig } from '../../../test/setup/set-mock-config'; import request from 'supertest'; import app from '../../app'; import * as license from '../../helpers/license.ee'; +import appConfig from '../../config/app'; describe('graphQL getAutomatischInfo query', () => { const query = ` @@ -21,6 +21,8 @@ describe('graphQL getAutomatischInfo query', () => { describe('and without valid license', () => { beforeEach(async () => { jest.spyOn(license, 'getLicense').mockResolvedValue(false); + + jest.replaceProperty(appConfig, 'isCloud', false) }); it('should return empty license data', async () => { @@ -61,7 +63,7 @@ describe('graphQL getAutomatischInfo query', () => { describe('and with cloud flag enabled', () => { beforeEach(async () => { - setMockConfig({ isCloud: true }); + jest.replaceProperty(appConfig, 'isCloud', true) }); it('should return all license data', async () => { @@ -90,7 +92,7 @@ describe('graphQL getAutomatischInfo query', () => { describe('and with cloud flag disabled', () => { beforeEach(async () => { - setMockConfig({ isCloud: false }); + jest.replaceProperty(appConfig, 'isCloud', false) }); it('should return all license data', async () => { diff --git a/packages/backend/src/graphql/queries/get-trial-status.ee.test.ts b/packages/backend/src/graphql/queries/get-trial-status.ee.test.ts index 285dcebd..703486a1 100644 --- a/packages/backend/src/graphql/queries/get-trial-status.ee.test.ts +++ b/packages/backend/src/graphql/queries/get-trial-status.ee.test.ts @@ -1,4 +1,3 @@ -import { setMockConfig } from '../../../test/setup/set-mock-config'; import request from 'supertest'; import app from '../../app'; import { IUser } from '@automatisch/types'; @@ -6,6 +5,7 @@ import User from '../../models/user'; import createUser from '../../../test/fixtures/user'; import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id'; import { DateTime } from 'luxon'; +import appConfig from '../../config/app'; describe('graphQL getTrialStatus query', () => { const query = ` @@ -43,7 +43,7 @@ describe('graphQL getTrialStatus query', () => { describe('and with cloud flag disabled', () => { beforeEach(async () => { - setMockConfig({ isCloud: false }); + jest.replaceProperty(appConfig, 'isCloud', false); }); it('should return null', async () => { @@ -63,7 +63,7 @@ describe('graphQL getTrialStatus query', () => { describe('and with cloud flag enabled', () => { beforeEach(async () => { - setMockConfig({ isCloud: true }); + jest.replaceProperty(appConfig, 'isCloud', true); }); describe('and not in trial and has active subscription', () => { diff --git a/packages/backend/test/setup/global-hooks.ts b/packages/backend/test/setup/global-hooks.ts index 80d72630..e83f6204 100644 --- a/packages/backend/test/setup/global-hooks.ts +++ b/packages/backend/test/setup/global-hooks.ts @@ -1,16 +1,6 @@ import { Model } from 'objection'; import { client as knex } from '../../src/config/database'; import logger from '../../src/helpers/logger'; -import { mockConfigState, resetMockConfig } from './set-mock-config'; - -jest.mock('../../src/config/app', () => ({ - ...jest.requireActual('../../src/config/app').default, - get isCloud() { - return mockConfigState.isCloud !== undefined - ? mockConfigState.isCloud - : jest.requireActual('../../src/config/app').default.isCloud; - }, -})); global.beforeAll(async () => { global.knex = null; @@ -33,7 +23,6 @@ global.afterEach(async () => { Model.knex(knex); jest.clearAllMocks(); - resetMockConfig(); }); global.afterAll(async () => { diff --git a/packages/backend/test/setup/set-mock-config.ts b/packages/backend/test/setup/set-mock-config.ts deleted file mode 100644 index 954a93e3..00000000 --- a/packages/backend/test/setup/set-mock-config.ts +++ /dev/null @@ -1,13 +0,0 @@ -export const mockConfigState = { - isCloud: false, -}; - -export const resetMockConfig = () => { - for (const key in mockConfigState) { - delete (mockConfigState as any)[key]; - } -}; - -export const setMockConfig = (config: Partial) => { - Object.assign(mockConfigState, config); -}; From 172a8934e3117b2c7126f929ca730daa587e4636 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 19 Oct 2023 14:22:00 +0200 Subject: [PATCH 5/5] test: Add restoreAllMocks to global afterEach for spy and replaceProperty --- packages/backend/test/setup/global-hooks.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/backend/test/setup/global-hooks.ts b/packages/backend/test/setup/global-hooks.ts index e83f6204..efc8305b 100644 --- a/packages/backend/test/setup/global-hooks.ts +++ b/packages/backend/test/setup/global-hooks.ts @@ -22,6 +22,7 @@ global.afterEach(async () => { await global.knex.rollback(); Model.knex(knex); + jest.restoreAllMocks(); jest.clearAllMocks(); });