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 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 new file mode 100644 index 00000000..703486a1 --- /dev/null +++ b/packages/backend/src/graphql/queries/get-trial-status.ee.test.ts @@ -0,0 +1,117 @@ +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'; +import appConfig from '../../config/app'; + +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 () => { + jest.replaceProperty(appConfig, '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 () => { + jest.replaceProperty(appConfig, '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/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; }; diff --git a/packages/backend/test/setup/global-hooks.ts b/packages/backend/test/setup/global-hooks.ts index 80d72630..efc8305b 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; @@ -32,8 +22,8 @@ global.afterEach(async () => { await global.knex.rollback(); Model.knex(knex); + jest.restoreAllMocks(); 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); -}; 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 {