-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_oauth.test.ts
More file actions
143 lines (132 loc) · 4.31 KB
/
github_oauth.test.ts
File metadata and controls
143 lines (132 loc) · 4.31 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
138
139
140
141
142
143
import { assertEquals, assertInstanceOf, fail } from "@std/assert";
import { beforeEach, describe, it } from "@std/testing/bdd";
import { stub } from "@std/testing/mock";
import { FetchHttpClient, type HttpClient, HttpClientError, type OAuth, OAuthError } from "../core/mod.ts";
import { GithubOAuth } from "./github_oauth.ts";
const CLIENT_ID = Deno.env.get("GITHUB_CLIENT_ID") ?? "1234567890";
const CLIENT_SECRET = Deno.env.get("GITHUB_CLIENT_SECRET") ?? "1234567890abcdefghijklmnopqrstuvwxyz";
const REDIRECT_URI = "https://openauth.denostack.com/callback/github";
const ACCESS_TOKEN = Deno.env.get("GITHUB_ACCESS_TOKEN") ?? "GITHUB_ACCESS_TOKEN_1234";
describe("GithubOAuth", () => {
let httpClient: HttpClient;
let oauth: OAuth;
beforeEach(() => {
httpClient = new FetchHttpClient();
oauth = new GithubOAuth({
httpClient,
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
redirectUri: REDIRECT_URI,
});
});
it("getAuthRequestUri", async () => {
const uri = await oauth.getAuthRequestUri();
assertEquals(
uri,
`https://github.com/login/oauth/authorize?${new URLSearchParams({
response_type: "code",
client_id: CLIENT_ID,
redirect_uri: REDIRECT_URI,
scope: "user:email",
})}`,
);
});
it("getAccessTokenResponse success", async () => {
stub(httpClient, "request", () => {
return Promise.resolve({
status: 200,
headers: {},
data: {
access_token: ACCESS_TOKEN,
token_type: "bearer",
scope: "read:user,user:email,user:follow",
},
});
});
const code = "CODE";
const result = await oauth.getAccessTokenResponse(code);
assertEquals(result, {
accessToken: ACCESS_TOKEN,
tokenType: "bearer",
scope: "read:user,user:email,user:follow",
});
});
it("getAccessTokenResponse fail", async () => {
stub(httpClient, "request", () => {
return Promise.reject(
new HttpClientError("OK", 200, {
error: "bad_verification_code",
error_description: "The code passed is incorrect or expired.",
error_uri:
"https://docs.github.com/apps/managing-oauth-apps/troubleshooting-oauth-app-access-token-request-errors/#bad-verification-code",
}),
);
});
try {
const code = "CODE";
await oauth.getAccessTokenResponse(code);
fail();
} catch (e) {
assertInstanceOf(e, OAuthError);
assertEquals(e.type, "bad_verification_code");
assertEquals(e.message, "The code passed is incorrect or expired.");
assertEquals(e.extra, {
error_uri:
"https://docs.github.com/apps/managing-oauth-apps/troubleshooting-oauth-app-access-token-request-errors/#bad-verification-code",
});
}
});
it("getUserProfile success", async () => {
stub(httpClient, "request", () => {
return Promise.resolve({
status: 200,
headers: {},
data: {
id: 12345,
login: "wan2land",
avatar_url: "https://avatars.githubusercontent.com/u/12345",
name: "Changwan Jun",
email: "wan2land@gmail.com",
},
});
});
const userProfile = await oauth.getUserProfile(ACCESS_TOKEN);
assertEquals(userProfile, {
id: "12345",
username: "wan2land",
email: "wan2land@gmail.com",
name: "Changwan Jun",
picture: "https://avatars.githubusercontent.com/u/12345",
raw: {
id: 12345,
login: "wan2land",
avatar_url: "https://avatars.githubusercontent.com/u/12345",
name: "Changwan Jun",
email: "wan2land@gmail.com",
},
});
});
it("getUserProfile fail", async () => {
stub(httpClient, "request", () => {
return Promise.reject(
new HttpClientError("Unauthorized", 401, {
message: "Bad credentials",
documentation_url: "https://docs.github.com/rest",
status: "401",
}),
);
});
try {
await oauth.getUserProfile("INVALID_ACCESS_TOKEN");
fail();
} catch (e) {
assertInstanceOf(e, OAuthError);
assertEquals(e.type, "Unauthorized");
assertEquals(e.message, "Bad credentials");
assertEquals(e.extra, {
documentation_url: "https://docs.github.com/rest",
status: "401",
});
}
});
});