-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathdatabase.test.ts
More file actions
137 lines (120 loc) · 4.35 KB
/
database.test.ts
File metadata and controls
137 lines (120 loc) · 4.35 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import nock from "nock";
import { beforeAll, afterAll } from "@jest/globals";
import { Database, AuthApiError } from "../../src/index.js";
const { back: nockBack } = nock;
const EMAIL = "test-email@example.com";
const DUPLICATE_EMAIL = "test-email-duplicate@example.com";
const PASSWORD = "test-password";
const opts = {
domain: "test-domain.auth0.com",
clientId: "test-client-id",
};
describe("Database", () => {
let nockDone: () => void;
beforeAll(async () => {
({ nockDone } = await nockBack("auth/fixtures/database.json"));
});
afterAll(() => {
nockDone();
});
describe("#signUp", () => {
it("should signup a user", async () => {
const database = new Database(opts);
const email = EMAIL;
const { data } = await database.signUp({
email,
password: PASSWORD,
connection: "Username-Password-Authentication",
});
expect(data).toEqual({
_id: "test-id",
id: "test-id",
email_verified: false,
email,
});
});
it("should signup a user when response param for id is 'user_id'", async () => {
const database = new Database(opts);
const email = "test-email-1@example.com";
const { data } = await database.signUp({
email,
password: PASSWORD,
connection: "Username-Password-Authentication",
});
expect(data).toEqual({
user_id: "test-id",
id: "test-id",
email_verified: false,
email,
});
});
it("should signup a user when response param for id is 'id'", async () => {
const database = new Database(opts);
const email = "test-email-2@example.com";
const { data } = await database.signUp({
email,
password: PASSWORD,
connection: "Username-Password-Authentication",
});
expect(data).toEqual({
id: "test-id",
email_verified: false,
email,
});
});
it("should require connection", async () => {
const database = new Database(opts);
await expect(
database.signUp({
email: EMAIL,
password: PASSWORD,
} as any),
).rejects.toThrow("Required parameter requestParameters.connection was null or undefined.");
});
it("should handle duplicate user error", async () => {
const database = new Database(opts);
const email = DUPLICATE_EMAIL;
let error: AuthApiError | null = null;
try {
await database.signUp({
email,
password: PASSWORD,
connection: "Username-Password-Authentication",
});
} catch (e) {
error = e as AuthApiError;
}
expect(error).toBeDefined();
expect(error).toEqual(
expect.objectContaining({
error: "invalid_signup",
error_description: "Invalid sign up",
}),
);
});
});
describe("#changePassword", () => {
it("should send a change password email", async () => {
const database = new Database(opts);
const email = EMAIL;
await database.signUp({
email,
password: PASSWORD,
connection: "Username-Password-Authentication",
});
const { data: txt } = await database.changePassword({
email,
connection: "Username-Password-Authentication",
});
expect(txt).toBe("We've just sent you an email to reset your password.");
});
it("should require email", async () => {
const database = new Database(opts);
await expect(
database.changePassword({
connection: "Username-Password-Authentication",
} as any),
).rejects.toThrow("Required parameter requestParameters.email was null or undefined.");
});
});
});