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 {