-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathunit.test.ts
110 lines (95 loc) · 2.96 KB
/
unit.test.ts
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
import {testServer} from "../test/server";
import {
AuthClientCredentials,
AuthUserPasswordCredentials,
AuthAccessTokenCredentials
} from "./auth";
import Connection from "./index";
describe("mock server auth tests", () => {
const server = testServer();
it("should login with client_credentials grant", async () => {
const conn = new Connection({
scheme: "http",
host: "localhost:" + server.port,
authClientSecret: new AuthClientCredentials({
clientSecret: "supersecret",
scopes: ["some_scope"]
})
});
await conn.login()
.then(token => {
expect(token).toEqual("access_token_000");
expect(conn.auth.refreshToken).toEqual("refresh_token_000");
expect(conn.auth.expiresAt).toBeGreaterThan(Date.now());
})
.catch(e => {
throw new Error("it should not have failed: " + e)
})
const request = server.lastRequest();
expect(request.body).toEqual({
client_id: "client123",
client_secret: "supersecret",
grant_type: "client_credentials",
scope: "some_scope"
});
})
it("should login with password grant", async () => {
const conn = new Connection({
scheme: "http",
host: "localhost:" + server.port,
authClientSecret: new AuthUserPasswordCredentials({
username: "user123",
password: "secure_password",
scopes: ["custom_scope"]
})
});
await conn.login()
.then(token => {
expect(token).toEqual("access_token_000");
expect(conn.auth.refreshToken).toEqual("refresh_token_000");
expect(conn.auth.expiresAt).toBeGreaterThan(Date.now());
})
.catch(e => {
throw new Error("it should not have failed: " + e)
})
const request = server.lastRequest();
expect(request.body).toEqual({
username: "user123",
password: "secure_password",
grant_type: "password",
client_id: "client123",
scope: "custom_scope offline_access"
});
})
it("should login with refresh_token grant", async () => {
const conn = new Connection({
scheme: "http",
host: "localhost:" + server.port,
authClientSecret: new AuthAccessTokenCredentials({
accessToken: "old-access-token",
expiresIn: 1,
refreshToken: "old-refresh-token"
})
});
// force the use of refreshToken
conn.auth.expiresAt = 0
await conn.login()
.then(token => {
expect(token).toEqual("access_token_000");
expect(conn.auth.refreshToken).toEqual("refresh_token_000");
expect(conn.auth.expiresAt).toBeGreaterThan(Date.now());
})
.catch(e => {
throw new Error("it should not have failed: " + e)
})
const request = server.lastRequest();
expect(request.body).toEqual({
client_id: "client123",
grant_type: "refresh_token",
refresh_token: "old-refresh-token"
});
})
it("shuts down the server", async () => {
return server.close();
})
})