Merge pull request #1363 from automatisch/test/get-trial-status

test: Implement tests for get trial status graphQL query
This commit is contained in:
Ömer Faruk Aydın
2023-10-19 14:29:59 +02:00
committed by GitHub
7 changed files with 132 additions and 45 deletions

View File

@@ -12,3 +12,4 @@ POSTGRES_PORT=5432
POSTGRES_USERNAME=automatisch_test_user
POSTGRES_PASSWORD=automatisch_test_user_password
REDIS_HOST=localhost
AUTOMATISCH_CLOUD=true

View File

@@ -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 () => {

View File

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

View File

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

View File

@@ -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 () => {

View File

@@ -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<typeof mockConfigState>) => {
Object.assign(mockConfigState, config);
};

View File

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