Skip to content

Commit 9e06681

Browse files
committed
add: prefix credentials for tests
1 parent 61202e5 commit 9e06681

File tree

3 files changed

+80
-104
lines changed

3 files changed

+80
-104
lines changed

test/auth.spec.ts

Lines changed: 33 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
WrongCredentialsError,
1010
UsernameAlreadyExistsError,
1111
} from "../src/errors/authErrors";
12-
import { safelyDeleteUser } from "./helpers/createUser";
12+
import { getUserCredentials, safelyDeleteUser } from "./helpers/createUser";
1313
import { NodeFile } from "../src/helpers/file";
1414
import { FileTooLargeError, UnsupportedFileTypeError } from "../src/errors";
1515

@@ -23,33 +23,33 @@ const client = await createClient({
2323
endpoint: process.env.KODADO_URL || "",
2424
});
2525

26+
const credentials = getUserCredentials({
27+
email: "auth-lib-user@turingpoint.de",
28+
password: "Abcd1234!",
29+
username: "auth-lib-user",
30+
fullName: "Auth Lib User",
31+
companyName: "CompanyXYZ",
32+
});
33+
2634
beforeAll(async () => {
27-
await safelyDeleteUser(client, {
28-
email: "auth-lib-user@turingpoint.de",
29-
password: "Abcd1234!",
30-
username: "auth-lib-user",
31-
});
35+
await safelyDeleteUser(client, credentials);
3236
});
3337

3438
describe("signUp", () => {
3539
it("Should sign up a new user", async () => {
3640
const result = await client.auth.signUp({
37-
email: "auth-lib-user@turingpoint.de",
38-
password: "Abcd1234!",
39-
nickname: "auth-lib-user",
40-
fullName: "Auth Lib User",
41-
companyName: "CompanyXYZ",
41+
...credentials,
42+
nickname: credentials.username,
4243
});
4344

44-
expect(result?.username).toBe("auth-lib-user");
45+
expect(result?.username).toBe(credentials.username);
4546
});
4647

4748
it("Should throw error if email was already taken", async () => {
4849
try {
4950
await client.auth.signUp({
50-
email: "auth-lib-user@turingpoint.de",
51-
password: "Abcd1234!",
52-
nickname: "auth-lib-user",
51+
...credentials,
52+
nickname: credentials.username,
5353
});
5454
} catch (e) {
5555
expect(e).toBeInstanceOf(UsernameAlreadyExistsError);
@@ -59,20 +59,17 @@ describe("signUp", () => {
5959

6060
describe("signIn", () => {
6161
it("Should sign the user in", async () => {
62-
const user = await client.auth.signIn({
63-
email: "auth-lib-user@turingpoint.de",
64-
password: "Abcd1234!",
65-
});
62+
const user = await client.auth.signIn(credentials);
6663

6764
if (!user) {
6865
expect(false).toBe(true);
6966
return;
7067
}
7168

72-
expect(user.email).toBe("auth-lib-user@turingpoint.de");
73-
expect(user.nickname).toBe("auth-lib-user");
74-
expect(user.fullName).toBe("Auth Lib User");
75-
expect(user.companyName).toBe("CompanyXYZ");
69+
expect(user.email).toBe(credentials.email);
70+
expect(user.nickname).toBe(credentials.username);
71+
expect(user.fullName).toBe(credentials.fullName);
72+
expect(user.companyName).toBe(credentials.companyName);
7673
expect(user).toHaveProperty("keys");
7774
expect(user).toHaveProperty("keys.encryptionPublicKey");
7875
expect(user).toHaveProperty("keys.encryptionSecretKey");
@@ -86,8 +83,8 @@ describe("signIn", () => {
8683
it("Should throw error if user is not found", async () => {
8784
try {
8885
await client.auth.signIn({
86+
...credentials,
8987
email: "notexisting@test.de",
90-
password: "Abcd1234!",
9188
});
9289
expect(false).toBe(true);
9390
} catch (e) {
@@ -97,10 +94,7 @@ describe("signIn", () => {
9794

9895
it("Should throw an error if password is wrong", async () => {
9996
try {
100-
await client.auth.signIn({
101-
email: "auth-lib-user@turingpoint.de",
102-
password: "wrongpw",
103-
});
97+
await client.auth.signIn({ ...credentials, password: "wrongpw" });
10498
expect(false).toBe(true);
10599
} catch (e) {
106100
expect(e).toBeInstanceOf(WrongCredentialsError);
@@ -109,14 +103,8 @@ describe("signIn", () => {
109103

110104
it("Should throw error if user is already signed in.", async () => {
111105
try {
112-
await client.auth.signIn({
113-
email: "auth-lib-user@turingpoint.de",
114-
password: "Abcd1234!",
115-
});
116-
await client.auth.signIn({
117-
email: "auth-lib-user@turingpoint.de",
118-
password: "Abcd1234!",
119-
});
106+
await client.auth.signIn(credentials);
107+
await client.auth.signIn(credentials);
120108
expect(false).toBe(true);
121109
} catch (e) {
122110
expect(e).toBeInstanceOf(AlreadySignedInError);
@@ -127,10 +115,7 @@ describe("signIn", () => {
127115

128116
describe("updateProfile", () => {
129117
it("Should update the profile", async () => {
130-
await client.auth.signIn({
131-
email: "auth-lib-user@turingpoint.de",
132-
password: "Abcd1234!",
133-
});
118+
await client.auth.signIn(credentials);
134119

135120
await client.auth.updateProfile({
136121
fullName: "updated fullName",
@@ -139,10 +124,7 @@ describe("updateProfile", () => {
139124
});
140125

141126
client.auth.signOut();
142-
const session = await client.auth.signIn({
143-
email: "auth-lib-user@turingpoint.de",
144-
password: "Abcd1234!",
145-
});
127+
const session = await client.auth.signIn(credentials);
146128

147129
if (!session) {
148130
expect(false).toBe(true);
@@ -159,10 +141,7 @@ describe("updateProfile", () => {
159141

160142
describe("uploadProfileImage", () => {
161143
it("Should fail uploading large images", async () => {
162-
await client.auth.signIn({
163-
email: "auth-lib-user@turingpoint.de",
164-
password: "Abcd1234!",
165-
});
144+
await client.auth.signIn(credentials);
166145

167146
const file: NodeFile = {
168147
buffer: fs.readFileSync(
@@ -179,10 +158,7 @@ describe("uploadProfileImage", () => {
179158
});
180159

181160
it("Should fail uploading non image files", async () => {
182-
await client.auth.signIn({
183-
email: "auth-lib-user@turingpoint.de",
184-
password: "Abcd1234!",
185-
});
161+
await client.auth.signIn(credentials);
186162

187163
const file: NodeFile = {
188164
buffer: fs.readFileSync(
@@ -199,10 +175,7 @@ describe("uploadProfileImage", () => {
199175
});
200176

201177
it("Should upload a profile image", async () => {
202-
await client.auth.signIn({
203-
email: "auth-lib-user@turingpoint.de",
204-
password: "Abcd1234!",
205-
});
178+
await client.auth.signIn(credentials);
206179

207180
const file: NodeFile = {
208181
buffer: fs.readFileSync(
@@ -215,10 +188,7 @@ describe("uploadProfileImage", () => {
215188

216189
client.auth.signOut();
217190

218-
const session = await client.auth.signIn({
219-
email: "auth-lib-user@turingpoint.de",
220-
password: "Abcd1234!",
221-
});
191+
const session = await client.auth.signIn(credentials);
222192

223193
if (session === "MFA_REQUIRED") {
224194
expect(false).toBe(true);
@@ -236,10 +206,7 @@ describe("uploadProfileImage", () => {
236206
});
237207

238208
it("Uploaded image should be the same as accessable via imageUrl", async () => {
239-
const session = await client.auth.signIn({
240-
email: "auth-lib-user@turingpoint.de",
241-
password: "Abcd1234!",
242-
});
209+
const session = await client.auth.signIn(credentials);
243210

244211
if (session === "MFA_REQUIRED") {
245212
expect(false).toBe(true);
@@ -268,18 +235,12 @@ describe("uploadProfileImage", () => {
268235

269236
describe("deleteUser", () => {
270237
it("Should delete the current user", async () => {
271-
await client.auth.signIn({
272-
email: "auth-lib-user@turingpoint.de",
273-
password: "Abcd1234!",
274-
});
238+
await client.auth.signIn(credentials);
275239

276240
await client.auth.deleteUser();
277241

278242
try {
279-
await client.auth.signIn({
280-
email: "auth-lib-user@turingpoint.de",
281-
password: "Abcd1234!",
282-
});
243+
await client.auth.signIn(credentials);
283244
expect(false).toBe(true);
284245
} catch (e) {
285246
if (e instanceof WrongCredentialsError) {

test/helpers/createUser.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,18 @@ export async function recreateUser(
3939
companyName: credentials.companyName,
4040
});
4141
}
42+
43+
export function getUserCredentials(credentials: Credentials) {
44+
const prefix =
45+
process.env.VITE_USER_PREFIX || Math.random().toString(36).substring(7);
46+
47+
const prefixedCredentials: Credentials = {
48+
email: `${prefix}-${credentials.email}`,
49+
username: `${prefix}-${credentials.username}`,
50+
password: credentials.password,
51+
fullName: credentials.fullName,
52+
companyName: credentials.companyName,
53+
};
54+
55+
return prefixedCredentials;
56+
}

test/updatePassword.spec.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import gql from "graphql-tag";
44
import { createClient } from "../src";
55
import typeDefs from "./fixtures/schema";
66

7-
import { recreateUser } from "./helpers/createUser";
7+
import {
8+
getUserCredentials,
9+
recreateUser,
10+
safelyDeleteUser,
11+
} from "./helpers/createUser";
812

913
let id: string;
1014

@@ -18,20 +22,22 @@ const client = await createClient({
1822
endpoint: process.env.KODADO_URL || "",
1923
});
2024

25+
const credentials = getUserCredentials({
26+
email: "updatePwUser@turingpoint.de",
27+
password: "Abcd1234!",
28+
username: "updatePwUser",
29+
});
30+
2131
beforeAll(async () => {
22-
await recreateUser(client, {
23-
email: "updatePwUser@turingpoint.de",
24-
password: "Abcd1234!",
25-
username: "updatePwUser",
26-
});
32+
await safelyDeleteUser(client, { ...credentials, password: "Abcd12345!" });
33+
await safelyDeleteUser(client, { ...credentials, password: "Abcd123456!" });
34+
35+
await recreateUser(client, credentials);
2736
});
2837

2938
describe("updatePassword", () => {
3039
it("Should update the user's pw", async () => {
31-
await client.auth.signIn({
32-
email: "updatePwUser@turingpoint.de",
33-
password: "Abcd1234!",
34-
});
40+
await client.auth.signIn(credentials);
3541
const qry = gql`
3642
mutation createTodo($item: String!) {
3743
createItem(item: $item, type: "Todo") {
@@ -50,22 +56,22 @@ describe("updatePassword", () => {
5056
});
5157
id = insertedTodo.id;
5258

59+
const oldPassword = credentials.password;
60+
credentials.password = "Abcd12345!";
61+
5362
try {
5463
await client.auth.updatePassword({
55-
oldPassword: "Abcd1234!",
56-
newPassword: "Abcd12345!",
64+
oldPassword,
65+
newPassword: credentials.password,
5766
});
5867
} catch (e) {
5968
console.log(e);
6069
}
6170

6271
client.auth.signOut();
6372

64-
const session = await client.auth.signIn({
65-
email: "updatePwUser@turingpoint.de",
66-
password: "Abcd12345!",
67-
});
68-
expect(session?.email).toBe("updatePwUser@turingpoint.de");
73+
const session = await client.auth.signIn(credentials);
74+
expect(session?.email).toBe(credentials.email);
6975

7076
const todoQuery = gql`
7177
query getTodo($id: String!) {
@@ -98,10 +104,7 @@ describe("updatePassword", () => {
98104
});
99105

100106
it("Should work with a large amount of items", async () => {
101-
await client.auth.signIn({
102-
email: "updatePwUser@turingpoint.de",
103-
password: "Abcd12345!",
104-
});
107+
await client.auth.signIn(credentials);
105108

106109
// Create > 5000 Todos
107110
for (let i = 0; i < 201; i++) {
@@ -120,22 +123,22 @@ describe("updatePassword", () => {
120123
});
121124
}
122125

126+
const oldPassword = credentials.password;
127+
credentials.password = "Abcd123456!";
128+
123129
try {
124130
await client.auth.updatePassword({
125-
oldPassword: "Abcd12345!",
126-
newPassword: "Abcd1234!",
131+
oldPassword,
132+
newPassword: credentials.password,
127133
});
128134
} catch (e) {
129135
console.log(e);
130136
}
131137

132138
client.auth.signOut();
133139

134-
const session = await client.auth.signIn({
135-
email: "updatePwUser@turingpoint.de",
136-
password: "Abcd1234!",
137-
});
138-
expect(session?.email).toBe("updatePwUser@turingpoint.de");
140+
const session = await client.auth.signIn(credentials);
141+
expect(session?.email).toBe(credentials.email);
139142

140143
const todoQuery = gql`
141144
query getTodo($id: String!) {
@@ -169,9 +172,6 @@ describe("updatePassword", () => {
169172
});
170173

171174
afterAll(async () => {
172-
await client.auth.signIn({
173-
email: "updatePwUser@turingpoint.de",
174-
password: "Abcd1234!",
175-
});
175+
await client.auth.signIn(credentials);
176176
await client.auth.deleteUser();
177177
});

0 commit comments

Comments
 (0)