-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathpublicSquadRequests.ts
305 lines (277 loc) · 7.2 KB
/
publicSquadRequests.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import {
disposeGraphQLTesting,
GraphQLTestClient,
GraphQLTestingState,
initializeGraphQLTesting,
MockContext,
Mutation,
saveFixtures,
testMutationErrorCode,
testQueryErrorCode,
} from './helpers';
import { Roles, SourceMemberRoles } from '../src/roles';
import {
Source,
SourceMember,
SquadPublicRequest,
SquadPublicRequestStatus,
User,
} from '../src/entity';
import { sourcesFixture } from './fixture/source';
import { usersFixture } from './fixture/user';
import { DataSource } from 'typeorm';
import createOrGetConnection from '../src/db';
let con: DataSource;
let state: GraphQLTestingState;
let client: GraphQLTestClient;
let loggedUser: string | null = null;
let roles: Roles[] = [];
const sourceId = 'squad';
jest.mock('../src/common', () => ({
...(jest.requireActual('../src/common') as Record<string, unknown>),
uploadLogo: jest.fn(),
}));
const testModeratorAuthorization = (mutation: Mutation): Promise<void> => {
roles = [];
return testMutationErrorCode(client, mutation, 'FORBIDDEN');
};
beforeAll(async () => {
con = await createOrGetConnection();
state = await initializeGraphQLTesting(
() => new MockContext(con, loggedUser, roles),
);
client = state.client;
});
beforeEach(async () => {
loggedUser = null;
roles = [];
await saveFixtures(con, Source, [sourcesFixture[5]]);
await saveFixtures(con, User, usersFixture);
await con.getRepository(SourceMember).save([
{
userId: '1',
sourceId,
role: SourceMemberRoles.Admin,
referralToken: 'rt1',
createdAt: new Date(2022, 11, 19),
},
{
userId: '2',
sourceId,
role: SourceMemberRoles.Moderator,
referralToken: 'rt2',
createdAt: new Date(2022, 11, 20),
},
{
userId: '3',
sourceId,
role: SourceMemberRoles.Member,
referralToken: 'rt3',
createdAt: new Date(2022, 11, 19),
},
]);
con.getRepository(SquadPublicRequest).clear();
});
afterAll(() => disposeGraphQLTesting(state));
describe('mutation submitSquadForReview', () => {
const MUTATION = `
mutation SubmitSquadForReview($sourceId: ID!) {
submitSquadForReview(sourceId: $sourceId) {
sourceId
requestorId
status
}
}`;
it('should not authorize when not logged in', () =>
testMutationErrorCode(
client,
{
mutation: MUTATION,
variables: { sourceId },
},
'UNAUTHENTICATED',
));
it('should not authorize when user is normal member', () => {
loggedUser = '3';
return testModeratorAuthorization({
mutation: MUTATION,
variables: { sourceId },
});
});
it('should not authorize when user is not admin of the squad', () => {
loggedUser = '2';
return testMutationErrorCode(
client,
{
mutation: MUTATION,
variables: { sourceId },
},
'FORBIDDEN',
);
});
it('should return bad request when squadID is not valid', async () => {
loggedUser = '1';
return testMutationErrorCode(
client,
{
mutation: MUTATION,
variables: { sourceId: null },
},
'GRAPHQL_VALIDATION_FAILED',
);
});
it('should fail if squadID does not match a squad', async () => {
loggedUser = '1';
return testMutationErrorCode(
client,
{
mutation: MUTATION,
variables: { sourceId: 'invalid' },
},
'NOT_FOUND',
);
});
it('should add new source request', async () => {
loggedUser = '1';
const res = await client.mutate(MUTATION, {
variables: { sourceId },
});
expect(res.data.submitSquadForReview).toMatchObject({
sourceId: sourceId,
requestorId: loggedUser,
status: SquadPublicRequestStatus.Pending,
});
});
it('should fail if there already is a pending request', async () => {
const repo = con.getRepository(SquadPublicRequest);
await repo.save({
sourceId: sourceId,
requestorId: '1',
status: SquadPublicRequestStatus.Pending,
});
loggedUser = '1';
return testMutationErrorCode(
client,
{
mutation: MUTATION,
variables: { sourceId },
},
'CONFLICT',
);
});
it('should fail if there already is a rejected request within last 30 days', async () => {
const repo = con.getRepository(SquadPublicRequest);
await repo.save({
sourceId: sourceId,
requestorId: '1',
status: SquadPublicRequestStatus.Rejected,
});
loggedUser = '1';
return testMutationErrorCode(
client,
{
mutation: MUTATION,
variables: { sourceId },
},
'CONFLICT',
);
});
it('should succeed if the rejected request is older than 30 days', async () => {
const olderDate = new Date();
olderDate.setDate(olderDate.getDate() - 31);
const repo = con.getRepository(SquadPublicRequest);
await repo.save({
sourceId: sourceId,
requestorId: '1',
status: SquadPublicRequestStatus.Rejected,
updatedAt: olderDate,
createdAt: olderDate,
});
loggedUser = '1';
const res = await client.mutate(MUTATION, {
variables: { sourceId },
});
expect(res.data.submitSquadForReview).toMatchObject({
sourceId: sourceId,
requestorId: loggedUser,
status: SquadPublicRequestStatus.Pending,
});
});
});
describe('query pendingSourceRequests', () => {
const query = `query PublicSquadRequests($sourceId: String!, $first: Int) {
publicSquadRequests(sourceId: $sourceId, first: $first) {
pageInfo {
endCursor
hasNextPage
}
edges {
node {
requestorId
status
}
}
}
}`;
it('should not authorize when normal member', async () => {
loggedUser = '3';
return testQueryErrorCode(
client,
{ query, variables: { first: 10, sourceId } },
'FORBIDDEN',
);
});
it('should not authorize when not admin', async () => {
loggedUser = '2';
return testQueryErrorCode(
client,
{ query, variables: { first: 10, sourceId } },
'FORBIDDEN',
);
});
it('should not return anything for non-matching squadId', async () => {
loggedUser = '1';
return testQueryErrorCode(
client,
{ query, variables: { first: 10, sourceId: 'invalid' } },
'NOT_FOUND',
);
});
it('should return pending source requests', async () => {
loggedUser = '1';
await con.getRepository(SquadPublicRequest).save({
sourceId: sourceId,
requestorId: '2',
status: SquadPublicRequestStatus.Rejected,
});
await con.getRepository(SquadPublicRequest).save({
sourceId: sourceId,
requestorId: '1',
status: SquadPublicRequestStatus.Pending,
});
const res = await client.query(query, {
variables: { first: 10, sourceId },
});
expect(res.data).toMatchObject({
publicSquadRequests: {
pageInfo: {
hasNextPage: false,
},
edges: [
{
node: {
requestorId: '1',
status: SquadPublicRequestStatus.Pending,
},
},
{
node: {
requestorId: '2',
status: SquadPublicRequestStatus.Rejected,
},
},
],
},
});
});
});