forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusersService.ts
More file actions
120 lines (109 loc) · 3.75 KB
/
Copy pathusersService.ts
File metadata and controls
120 lines (109 loc) · 3.75 KB
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
import type { ClerkClient, Organization, User } from '@clerk/backend';
import { faker } from '@faker-js/faker';
import { hash } from '../models/helpers';
type FakeUserOptions = {
/**
* A fictional email is an email that contains +clerk_test and can always be verified using 424242 as the OTP. No email will be sent.
* https://clerk.com/docs/testing/test-emails-and-phones#email-addresses
*
* @default false
**/
fictionalEmail?: boolean;
/**
* If true, the user will have a password otherwise will be set to undefined.
*
* @default true
**/
withPassword?: boolean;
/**
* If true, the user will have a phone number otherwise will be set to undefined.
*
* @default false
**/
withPhoneNumber?: boolean;
/**
* If true, the user will have a username otherwise will be set to undefined.
*
* @default false
**/
withUsername?: boolean;
};
export type FakeUser = {
firstName: string;
lastName: string;
email: string;
password?: string;
username?: string;
phoneNumber?: string;
deleteIfExists: () => Promise<void>;
};
export type FakeOrganization = {
name: string;
organization: { id: string };
delete: () => Promise<Organization>;
};
export type UserService = {
createFakeUser: (options?: FakeUserOptions) => FakeUser;
createBapiUser: (fakeUser: FakeUser) => Promise<User>;
deleteIfExists: (opts: { id?: string; email?: string }) => Promise<void>;
createFakeOrganization: (userId: string) => Promise<FakeOrganization>;
};
export const createUserService = (clerkClient: ClerkClient) => {
const self: UserService = {
createFakeUser: (options?: FakeUserOptions) => {
const {
fictionalEmail = false,
withPassword = true,
withPhoneNumber = false,
withUsername = false,
} = options || {};
const randomHash = hash();
const email = fictionalEmail
? `${randomHash}+clerk_test@clerkcookie.com`
: `clerkcookie+${randomHash}@mailsac.com`;
return {
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
email,
username: withUsername ? `${randomHash}_clerk_cookie` : undefined,
password: withPassword ? `${email}${randomHash}` : undefined,
// this generates a random fictional number that can be verified
// using the 424242 code. Allowing 10^5 combinations should be enough
// entropy for e2e purposes
// https://clerk.com/docs/testing/e2e-testing#phone-numbers
phoneNumber: withPhoneNumber ? faker.phone.number('+1###55501##') : undefined,
deleteIfExists: () => self.deleteIfExists({ email }),
};
},
createBapiUser: async fakeUser => {
return await clerkClient.users.createUser({
emailAddress: [fakeUser.email],
password: fakeUser.password,
firstName: fakeUser.firstName,
lastName: fakeUser.lastName,
phoneNumber: fakeUser.phoneNumber !== undefined ? [fakeUser.phoneNumber] : undefined,
username: fakeUser.username,
skipPasswordRequirement: fakeUser.password === undefined,
});
},
deleteIfExists: async (opts: { id?: string; email?: string }) => {
const id = opts.id || (await clerkClient.users.getUserList({ emailAddress: [opts.email] }))[0]?.id;
if (id) {
await clerkClient.users.deleteUser(id);
}
},
createFakeOrganization: async userId => {
const name = faker.animal.dog();
const organization = await clerkClient.organizations.createOrganization({
name: faker.animal.dog(),
createdBy: userId,
});
return {
name,
organization,
delete: () => clerkClient.organizations.deleteOrganization(organization.id),
} satisfies FakeOrganization;
},
};
return self;
};