Skip to content

Commit b22560a

Browse files
committed
Add more tests
1 parent dc08220 commit b22560a

File tree

3 files changed

+138
-3
lines changed

3 files changed

+138
-3
lines changed

packages/graphql-authentication/src/__tests__/mutations.ts

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { client, startServer } from './setup';
1+
import { client, clientWithAuth, startServer, FakeAdapter } from './setup';
22

33
test('signup - a new user', async () => {
44
const req = client(await startServer());
@@ -25,6 +25,7 @@ test('signup - a new user', async () => {
2525

2626
test('signup - with existent user', async () => {
2727
const req = client(await startServer());
28+
expect.assertions(1);
2829

2930
try {
3031
await req.request(`mutation {
@@ -39,6 +40,7 @@ test('signup - with existent user', async () => {
3940

4041
test('signup - with weak password', async () => {
4142
const req = client(await startServer());
43+
expect.assertions(1);
4244

4345
try {
4446
await req.request(`mutation {
@@ -76,6 +78,7 @@ test('login - correct', async () => {
7678

7779
test('login - non-existent user', async () => {
7880
const req = client(await startServer());
81+
expect.assertions(1);
7982

8083
try {
8184
await req.request(`mutation {
@@ -90,6 +93,7 @@ test('login - non-existent user', async () => {
9093

9194
test('login - wrong password', async () => {
9295
const req = client(await startServer());
96+
expect.assertions(1);
9397

9498
try {
9599
await req.request(`mutation {
@@ -101,3 +105,118 @@ test('login - wrong password', async () => {
101105
expect(String(e)).toMatch(/No user found/);
102106
}
103107
});
108+
109+
test('update current user data - correct', async () => {
110+
const req = clientWithAuth(await startServer());
111+
112+
const result = await req.request(`mutation {
113+
updateCurrentUser(data: {name: "Voldemort"}) {
114+
id
115+
name
116+
}
117+
}`);
118+
119+
expect((result as any).updateCurrentUser).toEqual({
120+
id: '2',
121+
name: 'Voldemort'
122+
});
123+
});
124+
125+
test('update current user data - wrong old passwd', async () => {
126+
const req = clientWithAuth(await startServer());
127+
expect.assertions(1);
128+
129+
try {
130+
await req.request(`mutation {
131+
changePassword(oldPassword: "testtest3", newPassword: "testtest4") {
132+
id
133+
}
134+
}`);
135+
} catch (e) {
136+
expect(String(e)).toMatch(/Invalid old password/);
137+
}
138+
});
139+
140+
test('update user password', async () => {
141+
const req = clientWithAuth(await startServer());
142+
143+
const result = await req.request(`mutation {
144+
changePassword(oldPassword: "testtest2", newPassword: "testtest3") {
145+
id
146+
}
147+
}`);
148+
149+
expect((result as any).changePassword).toEqual({
150+
id: '2'
151+
});
152+
153+
// Now verify the password has actually been changed correctly.
154+
const result2 = await req.request(`mutation {
155+
login(email: "[email protected]", password: "testtest3") {
156+
user {
157+
id
158+
}
159+
}
160+
}`);
161+
162+
expect((result2 as any).login.user).toEqual({
163+
id: '2'
164+
});
165+
});
166+
167+
test('trigger password reset - correct', async () => {
168+
const req = clientWithAuth(await startServer());
169+
expect.assertions(6);
170+
const spy = jest.spyOn(FakeAdapter.prototype, 'updateUserResetToken');
171+
172+
const result = await req.request(`mutation {
173+
triggerPasswordReset(email: "[email protected]") {
174+
ok
175+
}
176+
}`);
177+
178+
expect(spy).toHaveBeenCalled();
179+
180+
expect((result as any).triggerPasswordReset).toEqual({
181+
ok: true
182+
});
183+
184+
const { resetToken } = await spy.mock.results[0].value;
185+
// Verify the resetToken is a UUID
186+
expect(resetToken.length).toBe(36);
187+
188+
const result2 = await req.request(`mutation {
189+
passwordReset(email: "[email protected]", password: "testtest4", resetToken: "${resetToken}") {
190+
id
191+
}
192+
}`);
193+
194+
expect((result2 as any).passwordReset).toEqual({
195+
id: '2'
196+
});
197+
198+
const result3 = await req.request(`mutation {
199+
login(email: "[email protected]", password: "testtest4") {
200+
user {
201+
id
202+
}
203+
}
204+
}`);
205+
206+
expect((result3 as any).login.user).toEqual({
207+
id: '2'
208+
});
209+
210+
// Now verify that the resetToken is now invalid
211+
try {
212+
await req.request(`mutation {
213+
passwordReset(email: "[email protected]", password: "badbadbad", resetToken: "${resetToken}") {
214+
id
215+
}
216+
}`);
217+
} catch (e) {
218+
expect(String(e)).toMatch(/No user found/);
219+
}
220+
221+
spy.mockRestore();
222+
});

packages/graphql-authentication/src/__tests__/setup.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ export class FakeAdapter implements GraphqlAuthenticationAdapter {
4545
Object.assign(user, data); // iel
4646
return Promise.resolve(user);
4747
}
48+
async updateUserInfo(ctx: any, userId: string, data: any) {
49+
const user = await this.findUserById(ctx, userId);
50+
Object.assign(user, data); // iel
51+
return Promise.resolve(user);
52+
}
53+
async updateUserPassword(ctx: any, userId: string, data: any) {
54+
const user = await this.findUserById(ctx, userId);
55+
user!.password = data.password;
56+
return Promise.resolve(user);
57+
}
58+
async updateUserResetToken(ctx: any, userId: string, data: any) {
59+
const user = await this.findUserById(ctx, userId);
60+
Object.assign(user, data); // iel
61+
return Promise.resolve(user);
62+
}
4863
}
4964

5065
// In nodejs run `require('jsonwebtoken').sign({ userId: '2' }, 'wherearemyshoes')`
@@ -56,6 +71,7 @@ export async function startServer() {
5671
if (http) {
5772
await http.close();
5873
}
74+
const adapter = new FakeAdapter() as any;
5975
const server = new GraphQLServer({
6076
typeDefs: './schema.graphql',
6177
resolvers: {
@@ -70,7 +86,7 @@ export async function startServer() {
7086
...req,
7187
graphqlAuthentication: graphqlAuthenticationConfig({
7288
secret: 'wherearemyshoes',
73-
adapter: new FakeAdapter() as any
89+
adapter
7490
})
7591
})
7692
});

packages/graphql-authentication/src/mutations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export const mutations = {
215215
) {
216216
const user = await getUser(ctx);
217217

218-
const valid = await bcrypt.compare(user.password, oldPassword);
218+
const valid = await bcrypt.compare(oldPassword, user.password);
219219
if (!valid) {
220220
throw new InvalidOldPasswordError();
221221
}

0 commit comments

Comments
 (0)