test: Implement tests for get trial status graphQL query

This commit is contained in:
Faruk AYDIN
2023-10-19 01:06:41 +02:00
parent 6d6b77148d
commit 59770c80db
2 changed files with 118 additions and 0 deletions

View File

@@ -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);
});
});
});
});
});

View File

@@ -101,6 +101,7 @@ export interface IUser {
permissions: IPermission[];
createdAt: string | Date;
updatedAt: string | Date;
trialExpiryDate: string | Date;
}
export interface IRole {