Merge pull request #1363 from automatisch/test/get-trial-status
test: Implement tests for get trial status graphQL query
This commit is contained in:
@@ -12,3 +12,4 @@ POSTGRES_PORT=5432
|
|||||||
POSTGRES_USERNAME=automatisch_test_user
|
POSTGRES_USERNAME=automatisch_test_user
|
||||||
POSTGRES_PASSWORD=automatisch_test_user_password
|
POSTGRES_PASSWORD=automatisch_test_user_password
|
||||||
REDIS_HOST=localhost
|
REDIS_HOST=localhost
|
||||||
|
AUTOMATISCH_CLOUD=true
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import { setMockConfig } from '../../../test/setup/set-mock-config';
|
|
||||||
import request from 'supertest';
|
import request from 'supertest';
|
||||||
import app from '../../app';
|
import app from '../../app';
|
||||||
import * as license from '../../helpers/license.ee';
|
import * as license from '../../helpers/license.ee';
|
||||||
|
import appConfig from '../../config/app';
|
||||||
|
|
||||||
describe('graphQL getAutomatischInfo query', () => {
|
describe('graphQL getAutomatischInfo query', () => {
|
||||||
const query = `
|
const query = `
|
||||||
@@ -21,6 +21,8 @@ describe('graphQL getAutomatischInfo query', () => {
|
|||||||
describe('and without valid license', () => {
|
describe('and without valid license', () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
jest.spyOn(license, 'getLicense').mockResolvedValue(false);
|
jest.spyOn(license, 'getLicense').mockResolvedValue(false);
|
||||||
|
|
||||||
|
jest.replaceProperty(appConfig, 'isCloud', false)
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return empty license data', async () => {
|
it('should return empty license data', async () => {
|
||||||
@@ -61,7 +63,7 @@ describe('graphQL getAutomatischInfo query', () => {
|
|||||||
|
|
||||||
describe('and with cloud flag enabled', () => {
|
describe('and with cloud flag enabled', () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
setMockConfig({ isCloud: true });
|
jest.replaceProperty(appConfig, 'isCloud', true)
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return all license data', async () => {
|
it('should return all license data', async () => {
|
||||||
@@ -90,7 +92,7 @@ describe('graphQL getAutomatischInfo query', () => {
|
|||||||
|
|
||||||
describe('and with cloud flag disabled', () => {
|
describe('and with cloud flag disabled', () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
setMockConfig({ isCloud: false });
|
jest.replaceProperty(appConfig, 'isCloud', false)
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return all license data', async () => {
|
it('should return all license data', async () => {
|
||||||
|
117
packages/backend/src/graphql/queries/get-trial-status.ee.test.ts
Normal file
117
packages/backend/src/graphql/queries/get-trial-status.ee.test.ts
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
25
packages/backend/test/fixtures/user.ts
vendored
25
packages/backend/test/fixtures/user.ts
vendored
@@ -1,25 +1,14 @@
|
|||||||
import createRole from './role';
|
import createRole from './role';
|
||||||
import { faker } from '@faker-js/faker';
|
import { faker } from '@faker-js/faker';
|
||||||
|
import User from '../../src/models/user';
|
||||||
|
|
||||||
type UserParams = {
|
const createUser = async (params: Partial<User> = {}) => {
|
||||||
roleId?: string;
|
params.roleId = params?.roleId || (await createRole()).id;
|
||||||
fullName?: string;
|
params.fullName = params?.fullName || faker.person.fullName();
|
||||||
email?: string;
|
params.email = params?.email || faker.internet.email();
|
||||||
password?: string;
|
params.password = params?.password || faker.internet.password();
|
||||||
};
|
|
||||||
|
|
||||||
const createUser = async (params: UserParams = {}) => {
|
const [user] = await global.knex.table('users').insert(params).returning('*');
|
||||||
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('*');
|
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
};
|
};
|
||||||
|
@@ -1,16 +1,6 @@
|
|||||||
import { Model } from 'objection';
|
import { Model } from 'objection';
|
||||||
import { client as knex } from '../../src/config/database';
|
import { client as knex } from '../../src/config/database';
|
||||||
import logger from '../../src/helpers/logger';
|
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.beforeAll(async () => {
|
||||||
global.knex = null;
|
global.knex = null;
|
||||||
@@ -32,8 +22,8 @@ global.afterEach(async () => {
|
|||||||
await global.knex.rollback();
|
await global.knex.rollback();
|
||||||
Model.knex(knex);
|
Model.knex(knex);
|
||||||
|
|
||||||
|
jest.restoreAllMocks();
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
resetMockConfig();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
global.afterAll(async () => {
|
global.afterAll(async () => {
|
||||||
|
@@ -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);
|
|
||||||
};
|
|
1
packages/types/index.d.ts
vendored
1
packages/types/index.d.ts
vendored
@@ -101,6 +101,7 @@ export interface IUser {
|
|||||||
permissions: IPermission[];
|
permissions: IPermission[];
|
||||||
createdAt: string | Date;
|
createdAt: string | Date;
|
||||||
updatedAt: string | Date;
|
updatedAt: string | Date;
|
||||||
|
trialExpiryDate: string | Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IRole {
|
export interface IRole {
|
||||||
|
Reference in New Issue
Block a user