/
internal.ts
87 lines (75 loc) · 3.18 KB
/
internal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { User } from '@accounts/typeorm';
import { GraphQLModule } from '@graphql-modules/core';
import { Container } from 'typedi';
import { Connection, getRepository, useContainer } from 'typeorm';
import { createInternal } from '../../src/modules/internal';
import { connect } from '../../src/utils/connect';
useContainer(Container);
describe('InternalModule', () => {
let connection: Connection;
let internal: GraphQLModule;
let mutations: any;
let queries: any;
let context: any;
let user: User;
let info: any;
beforeAll(async () => {
connection = await connect(process.env.TEST_DATABASE_URL);
internal = await createInternal(connection);
mutations = internal.resolvers.Mutation;
queries = internal.resolvers.Query;
user = getRepository(User).create({ username: 'test ' });
info = { session: { user } };
});
beforeEach(async () => {
const requestId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
const container = Container.of(requestId);
context = { requestId, container, user, session: { user } };
container.set('context', context);
await connection.dropDatabase();
await connection.synchronize();
});
afterAll(() => connection.close());
describe('Webhook', () => {
it('should be able to create webhooks', async () => {
const input = { name: 'Hello', url: 'https://example.com', method: 'PUT' };
const webhook = await mutations.createWebhook({}, { input }, context, info);
expect(webhook).toBeTruthy();
const result = await queries.Webhook({}, { id: webhook.id }, context, info);
expect(result.id).toEqual(webhook.id);
});
it('should have no webhooks', async () => {
const result = await queries.allWebhooks({}, { order: 'id_ASC' }, context, info);
expect(result.edges).toHaveLength(0);
expect(result.totalCount).toEqual(0);
});
it('should be able to update webhooks', async () => {
const input = { name: 'Hello', url: 'https://example.com', method: 'PUT' };
const webhook = await mutations.createWebhook({}, { input }, context, info);
expect(webhook).toBeTruthy();
const updatedUrl = 'http://noop.com';
const updateVariables = { id: webhook.id, input: { ...webhook, url: updatedUrl } };
const update = await mutations.updateWebhook({}, updateVariables, context, info);
expect(update.url).toBe(updatedUrl);
expect(update.name).toBe(webhook.name);
});
it('should be able to remove webhooks', async () => {
const input = { name: 'Hello', url: 'https://example.com', method: 'PUT' };
const { id } = await mutations.createWebhook({}, { input }, context, info);
const result = await mutations.removeWebhook({}, { id }, context, info);
expect(result).toBeTruthy();
const webhook = await queries.Webhook({}, { id }, context, info);
expect(webhook).toBeFalsy();
});
});
describe('Authentication', () => {
it('should be authorized', async () => {
try {
await queries.Webhook({}, { id: 'nah' }, { ...context, user: null }, { session: {} });
throw new Error();
} catch (e) {
expect(e.message).toContain('Must be authenticated');
}
});
});
});