Skip to content

Commit 930dba2

Browse files
committed
Add tests
1 parent 75d9ada commit 930dba2

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

test/authentication.e2e-spec.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import { graphql } from '~/graphql';
77
import {
88
createSession,
99
createTestApp,
10+
CurrentUserDoc,
1011
fragments,
1112
generateRegisterInput,
1213
login,
14+
LoginDoc,
1315
logout,
1416
registerUser,
1517
type TestApp,
@@ -115,11 +117,50 @@ describe('Authentication e2e', () => {
115117
expect(actual.phone.value).toBe(fakeUser.phone);
116118
expect(actual.timezone.value?.name).toBe(fakeUser.timezone);
117119
expect(actual.about.value).toBe(fakeUser.about);
120+
});
121+
122+
it('disabled users are logged out & cannot login', async () => {
123+
const input = await generateRegisterInput();
124+
const user = await registerUser(app, input);
125+
126+
// confirm they're logged in
127+
const before = await app.graphql.query(CurrentUserDoc);
128+
expect(before.session.user).toBeTruthy();
118129

119-
return true;
130+
await app.graphql.query(
131+
graphql(
132+
`
133+
mutation DisableUser($id: ID!) {
134+
updateUser(input: { user: { id: $id, status: Disabled } }) {
135+
__typename
136+
}
137+
}
138+
`,
139+
),
140+
{
141+
id: user.id,
142+
},
143+
);
144+
145+
// Confirm mutation logged them out
146+
const after = await app.graphql.query(CurrentUserDoc);
147+
expect(after.session.user).toBeNull();
148+
149+
// Confirm they can't log back in
150+
await app.graphql
151+
.query(LoginDoc, {
152+
input: {
153+
email: input.email,
154+
password: input.password,
155+
},
156+
})
157+
.expectError({
158+
message: 'User is disabled',
159+
code: ['UserDisabled', 'Authentication', 'Client'],
160+
});
120161
});
121162

122-
it('should return true after password changed', async () => {
163+
it('Password changed', async () => {
123164
const fakeUser = await generateRegisterInput();
124165

125166
const user = await registerUser(app, fakeUser);

test/utility/create-session.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ export async function createSession(app: TestApp) {
1818
}
1919

2020
export async function getUserFromSession(app: TestApp) {
21-
const result = await app.graphql.query(
22-
graphql(`
23-
query SessionUser {
24-
session {
25-
user {
26-
id
27-
}
28-
}
29-
}
30-
`),
31-
);
21+
const result = await app.graphql.query(CurrentUserDoc);
3222
const user = result.session.user;
3323
expect(user).toBeTruthy();
3424
return user!;
3525
}
26+
export const CurrentUserDoc = graphql(`
27+
query SessionUser {
28+
session {
29+
user {
30+
id
31+
}
32+
}
33+
}
34+
`);

test/utility/login.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export async function login(app: TestApp, input: InputOf<typeof LoginDoc>) {
88
app.graphql.email = input.email;
99
return res;
1010
}
11-
const LoginDoc = graphql(`
11+
export const LoginDoc = graphql(`
1212
mutation login($input: LoginInput!) {
1313
login(input: $input) {
1414
user {

0 commit comments

Comments
 (0)