-
-
Notifications
You must be signed in to change notification settings - Fork 260
/
GuestMutations.test.ts
173 lines (147 loc) · 6.8 KB
/
GuestMutations.test.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
import { expect } from 'chai';
import config from 'config';
import gqlV2 from 'fake-tag';
import sinon from 'sinon';
import { verifyJwt } from '../../../../../server/lib/auth';
import emailLib from '../../../../../server/lib/email';
import { randEmail } from '../../../../stores';
import { fakeUser, randStr } from '../../../../test-helpers/fake-data';
import { graphqlQueryV2, resetTestDB, waitForCondition } from '../../../../utils';
const sendConfirmationMutation = gqlV2/* GraphQL */ `
mutation SendGuestConfirmation($email: EmailAddress!) {
sendGuestConfirmationEmail(email: $email)
}
`;
const confirmGuestAccountMutation = gqlV2/* GraphQL */ `
mutation ConfirmGuestAccount($email: EmailAddress!, $emailConfirmationToken: String!) {
confirmGuestAccount(email: $email, emailConfirmationToken: $emailConfirmationToken) {
accessToken
account {
id
legacyId
slug
}
}
}
`;
const callSendConfirmation = (email, remoteUser = null) => {
return graphqlQueryV2(sendConfirmationMutation, { email }, remoteUser);
};
const callConfirmGuestAccount = (email, emailConfirmationToken, remoteUser = null) => {
return graphqlQueryV2(confirmGuestAccountMutation, { email, emailConfirmationToken }, remoteUser);
};
describe('server/graphql/v2/mutation/GuestMutations', () => {
let sandbox, emailSendMessageSpy;
before(async () => {
await resetTestDB();
sandbox = sinon.createSandbox();
emailSendMessageSpy = sandbox.spy(emailLib, 'sendMessage');
sandbox.stub(config, 'limits').value({
sendGuestConfirmPerMinutePerIp: 1000000,
sendGuestConfirmPerMinutePerEmail: 1000000,
confirmGuestAccountPerMinutePerIp: 1000000,
});
});
after(() => {
sandbox.restore();
});
describe('sendGuestConfirmationEmail', () => {
it('rejects if the user is signed in', async () => {
const user = await fakeUser();
const result = await callSendConfirmation(randEmail(), user);
expect(result.errors).to.exist;
expect(result.errors[0].message).to.include("You're signed in");
});
it('rejects if the user is already verified', async () => {
const user = await fakeUser();
const result = await callSendConfirmation(user.email);
expect(result.errors).to.exist;
expect(result.errors[0].message).to.include('This account has already been confirmed');
});
it('rejects if the user does not exist', async () => {
const result = await callSendConfirmation(randEmail());
expect(result.errors).to.exist;
expect(result.errors[0].message).to.include('No user found for this email address');
});
it('sends the confirmation email', async () => {
const user = await fakeUser({ confirmedAt: null, emailConfirmationToken: randStr() });
const result = await callSendConfirmation(user.email);
result.errors && console.error(result.errors);
expect(result.errors).to.not.exist;
expect(result.data.sendGuestConfirmationEmail).to.be.true;
console.log(result);
await waitForCondition(() => emailSendMessageSpy.callCount === 1);
expect(emailSendMessageSpy.callCount).to.equal(1);
const [recipient, subject, body] = emailSendMessageSpy.args[0];
expect(recipient).to.eq(user.email);
expect(subject).to.eq('Open Collective: Verify your email');
expect(body).to.include(`/confirm/guest/${user.emailConfirmationToken}?email=${encodeURIComponent(user.email)}`);
});
});
describe('confirmGuestAccount', () => {
it('fails if account is already confirmed', async () => {
const user = await fakeUser({ confirmedAt: new Date(), emailConfirmationToken: randStr() });
const response = await callConfirmGuestAccount(user.email, user.emailConfirmationToken);
expect(response.errors).to.exist;
expect(response.errors[0].message).to.include('This account has already been verified');
});
it('fails if email is invalid', async () => {
const user = await fakeUser({ confirmedAt: null, emailConfirmationToken: randStr() });
const response = await callConfirmGuestAccount(randEmail(), user.emailConfirmationToken);
expect(response.errors).to.exist;
expect(response.errors[0].message).to.include('No account found for');
});
it('fails if confirmation token is invalid', async () => {
const user = await fakeUser({ confirmedAt: null, emailConfirmationToken: randStr() });
const response = await callConfirmGuestAccount(user.email, 'INVALID TOKEN');
expect(response.errors).to.exist;
expect(response.errors[0].message).to.include('Invalid email confirmation token');
});
it('returns a valid login token', async () => {
const user = await fakeUser({ confirmedAt: null, emailConfirmationToken: randStr() });
const response = await callConfirmGuestAccount(user.email, user.emailConfirmationToken);
response.errors && console.error(response.errors);
expect(response.errors).to.not.exist;
const { account, accessToken } = response.data.confirmGuestAccount;
expect(account.legacyId).to.eq(user.CollectiveId);
const decodedJwt = verifyJwt(accessToken);
expect(decodedJwt.sub).to.eq(user.id.toString());
});
});
describe('rate limiting', () => {
afterEach(() => {
sandbox.restore();
});
it('sendGuestConfirmationEmail is rate limited on IP', async () => {
sandbox.stub(config, 'limits').value({
sendGuestConfirmPerMinutePerIp: 0,
sendGuestConfirmPerMinutePerEmail: 1000000,
});
const user = await fakeUser({ confirmedAt: null });
const result = await callSendConfirmation(user.email);
expect(result.errors).to.exist;
expect(result.errors[0].message).to.include(
'An email has already been sent recently. Please try again in a few minutes.',
);
});
it('sendGuestConfirmationEmailis rate limited on email', async () => {
sandbox.stub(config, 'limits').value({
sendGuestConfirmPerMinutePerIp: 1000000,
sendGuestConfirmPerMinutePerEmail: 0,
});
const user = await fakeUser({ confirmedAt: null });
const result = await callSendConfirmation(user.email);
expect(result.errors).to.exist;
expect(result.errors[0].message).to.include(
'An email has already been sent for this address recently. Please check your SPAM folder, or try again in a few minutes.',
);
});
it('confirmGuestAccount rate limited on IP', async () => {
sandbox.stub(config, 'limits').value({ confirmGuestAccountPerMinutePerIp: 0 });
const user = await fakeUser({ confirmedAt: null, emailConfirmationToken: randStr() });
const response = await callConfirmGuestAccount(user.email, user.emailConfirmationToken);
expect(response.errors).to.exist;
expect(response.errors[0].message).to.include('Rate limit exceeded');
});
});
});