Skip to content

Commit 05a290a

Browse files
committed
test: oauth access tokens
1 parent accc70e commit 05a290a

File tree

1 file changed

+200
-15
lines changed

1 file changed

+200
-15
lines changed

test/index.test.ts

+200-15
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ test("README example for app auth", async () => {
5858
});
5959

6060
test("README example for installation auth", async () => {
61-
const matchCreateAccessToken: MockMatcherFunction = (
61+
const matchCreateInstallationAccessToken: MockMatcherFunction = (
6262
url,
6363
{ body, headers }
6464
) => {
@@ -75,7 +75,7 @@ test("README example for installation auth", async () => {
7575
return true;
7676
};
7777

78-
const createAccessTokenResponseData = {
78+
const createInstallationAccessTokenResponseData = {
7979
token: "secret123",
8080
expires_at: "1970-01-01T01:00:00.000Z",
8181
permissions: {
@@ -94,7 +94,10 @@ test("README example for installation auth", async () => {
9494
request: {
9595
fetch: fetchMock
9696
.sandbox()
97-
.postOnce(matchCreateAccessToken, createAccessTokenResponseData)
97+
.postOnce(
98+
matchCreateInstallationAccessToken,
99+
createInstallationAccessTokenResponseData
100+
)
98101
}
99102
})
100103
});
@@ -117,6 +120,64 @@ test("README example for installation auth", async () => {
117120
});
118121
});
119122

123+
test("README example for oauth", async () => {
124+
const matchCreateOAuthAccessToken: MockMatcherFunction = (
125+
url,
126+
{ body, headers }
127+
) => {
128+
expect(url).toEqual("https://github.com/login/oauth/access_token");
129+
expect(headers).toStrictEqual({
130+
accept: "application/json",
131+
"user-agent": "test",
132+
"content-type": "application/json; charset=utf-8"
133+
});
134+
expect(JSON.parse(String(body))).toStrictEqual({
135+
client_id: "12345678901234567890",
136+
client_secret: "1234567890123456789012345678901234567890",
137+
code: "123456"
138+
});
139+
return true;
140+
};
141+
142+
const createOAuthAccessTokenResponseData = {
143+
access_token: "secret123",
144+
scope: "",
145+
token_type: "bearer"
146+
};
147+
148+
const auth = createAppAuth({
149+
id: APP_ID,
150+
privateKey: PRIVATE_KEY,
151+
clientId: "12345678901234567890",
152+
clientSecret: "1234567890123456789012345678901234567890",
153+
request: request.defaults({
154+
headers: {
155+
"user-agent": "test"
156+
},
157+
request: {
158+
fetch: fetchMock
159+
.sandbox()
160+
.postOnce(
161+
matchCreateOAuthAccessToken,
162+
createOAuthAccessTokenResponseData
163+
)
164+
}
165+
})
166+
});
167+
168+
const authentication = await auth({
169+
type: "oauth",
170+
code: "123456"
171+
});
172+
173+
expect(authentication).toEqual({
174+
type: "token",
175+
token: "secret123",
176+
tokenType: "oauth",
177+
scopes: []
178+
});
179+
});
180+
120181
test("installationId strategy option", async () => {
121182
const requestMock = request.defaults({
122183
headers: {
@@ -164,14 +225,17 @@ test("installationId strategy option", async () => {
164225
});
165226

166227
test("repositoryIds auth option", async () => {
167-
const matchCreateAccessToken: MockMatcherFunction = (url, { body }) => {
228+
const matchCreateInstallationAccessToken: MockMatcherFunction = (
229+
url,
230+
{ body }
231+
) => {
168232
expect(JSON.parse(String(body))).toStrictEqual({
169233
repository_ids: [1, 2, 3]
170234
});
171235
return true;
172236
};
173237

174-
const createAccessTokenResponseData = {
238+
const createInstallationAccessTokenResponseData = {
175239
token: "secret123",
176240
expires_at: "1970-01-01T01:00:00.000Z",
177241
permissions: {
@@ -191,7 +255,10 @@ test("repositoryIds auth option", async () => {
191255
request: {
192256
fetch: fetchMock
193257
.sandbox()
194-
.postOnce(matchCreateAccessToken, createAccessTokenResponseData)
258+
.postOnce(
259+
matchCreateInstallationAccessToken,
260+
createInstallationAccessTokenResponseData
261+
)
195262
}
196263
})
197264
});
@@ -217,7 +284,10 @@ test("repositoryIds auth option", async () => {
217284
});
218285

219286
test("permissions auth option", async () => {
220-
const matchCreateAccessToken: MockMatcherFunction = (url, { body }) => {
287+
const matchCreateInstallationAccessToken: MockMatcherFunction = (
288+
url,
289+
{ body }
290+
) => {
221291
expect(JSON.parse(String(body))).toStrictEqual({
222292
permissions: {
223293
single_file: "write"
@@ -226,7 +296,7 @@ test("permissions auth option", async () => {
226296
return true;
227297
};
228298

229-
const createAccessTokenResponseData = {
299+
const createInstallationAccessTokenResponseData = {
230300
token: "secret123",
231301
expires_at: "1970-01-01T01:00:00.000Z",
232302
permissions: {
@@ -246,7 +316,10 @@ test("permissions auth option", async () => {
246316
request: {
247317
fetch: fetchMock
248318
.sandbox()
249-
.postOnce(matchCreateAccessToken, createAccessTokenResponseData)
319+
.postOnce(
320+
matchCreateInstallationAccessToken,
321+
createInstallationAccessTokenResponseData
322+
)
250323
}
251324
})
252325
});
@@ -449,7 +522,7 @@ test("installation cache with different options", async () => {
449522
return true;
450523
};
451524

452-
const createAccessTokenResponseData = {
525+
const createInstallationAccessTokenResponseData = {
453526
token: "secret123",
454527
expires_at: "1970-01-01T01:00:00.000Z",
455528
permissions: {
@@ -460,8 +533,14 @@ test("installation cache with different options", async () => {
460533

461534
const mock = fetchMock
462535
.sandbox()
463-
.postOnce(matchCreateAccessToken1, createAccessTokenResponseData)
464-
.postOnce(matchCreateAccessToken2, createAccessTokenResponseData);
536+
.postOnce(
537+
matchCreateAccessToken1,
538+
createInstallationAccessTokenResponseData
539+
)
540+
.postOnce(
541+
matchCreateAccessToken2,
542+
createInstallationAccessTokenResponseData
543+
);
465544

466545
const requestMock = request.defaults({
467546
headers: {
@@ -563,8 +642,114 @@ test("refresh option", async () => {
563642
expect(authentication2).toEqual(EXPECTED);
564643
});
565644

645+
test("oauth with `code`, `redirectUrl` and `state`", async () => {
646+
const matchCreateOAuthAccessToken: MockMatcherFunction = (
647+
url,
648+
{ body, headers }
649+
) => {
650+
expect(url).toEqual("https://github.com/login/oauth/access_token");
651+
expect(headers).toStrictEqual({
652+
accept: "application/json",
653+
"user-agent": "test",
654+
"content-type": "application/json; charset=utf-8"
655+
});
656+
expect(JSON.parse(String(body))).toStrictEqual({
657+
client_id: "12345678901234567890",
658+
client_secret: "1234567890123456789012345678901234567890",
659+
code: "123456",
660+
redirect_uri: "https://example.com/login",
661+
state: "mystate123"
662+
});
663+
664+
return true;
665+
};
666+
667+
const createOAuthAccessTokenResponseData = {
668+
access_token: "secret123",
669+
scope: "",
670+
token_type: "bearer"
671+
};
672+
673+
const auth = createAppAuth({
674+
id: APP_ID,
675+
privateKey: PRIVATE_KEY,
676+
clientId: "12345678901234567890",
677+
clientSecret: "1234567890123456789012345678901234567890",
678+
request: request.defaults({
679+
headers: {
680+
"user-agent": "test"
681+
},
682+
request: {
683+
fetch: fetchMock
684+
.sandbox()
685+
.postOnce(
686+
matchCreateOAuthAccessToken,
687+
createOAuthAccessTokenResponseData
688+
)
689+
}
690+
})
691+
});
692+
693+
const authentication = await auth({
694+
type: "oauth",
695+
code: "123456",
696+
state: "mystate123",
697+
redirectUrl: "https://example.com/login"
698+
});
699+
700+
expect(authentication).toEqual({
701+
type: "token",
702+
token: "secret123",
703+
tokenType: "oauth",
704+
scopes: []
705+
});
706+
});
707+
708+
test("oauth with custom baseUrl (GHE)", async () => {
709+
const createOAuthAccessTokenResponseData = {
710+
access_token: "secret123",
711+
scope: "",
712+
token_type: "bearer"
713+
};
714+
715+
const auth = createAppAuth({
716+
id: APP_ID,
717+
privateKey: PRIVATE_KEY,
718+
clientId: "12345678901234567890",
719+
clientSecret: "1234567890123456789012345678901234567890",
720+
request: request.defaults({
721+
baseUrl: "https://github.acme-inc.com/api/v3",
722+
headers: {
723+
"user-agent": "test"
724+
},
725+
request: {
726+
fetch: fetchMock
727+
.sandbox()
728+
.postOnce(
729+
"https://github.acme-inc.com/login/oauth/access_token",
730+
createOAuthAccessTokenResponseData
731+
)
732+
}
733+
})
734+
});
735+
736+
const authentication = await auth({
737+
type: "oauth",
738+
code: "123456",
739+
state: "mystate123",
740+
redirectUrl: "https://example.com/login"
741+
});
742+
743+
expect(authentication).toEqual({
744+
type: "token",
745+
token: "secret123",
746+
tokenType: "oauth",
747+
scopes: []
748+
});
749+
});
750+
566751
test("caches based on installation id", async () => {
567-
const createAccessTokenResponseData = {
752+
const createInstallationAccessTokenResponseData = {
568753
token: "secret123",
569754
expires_at: "1970-01-01T01:00:00.000Z",
570755
permissions: {
@@ -582,11 +767,11 @@ test("caches based on installation id", async () => {
582767
.sandbox()
583768
.postOnce(
584769
"path:/app/installations/123/access_tokens",
585-
createAccessTokenResponseData
770+
createInstallationAccessTokenResponseData
586771
)
587772
.postOnce(
588773
"path:/app/installations/456/access_tokens",
589-
Object.assign({}, createAccessTokenResponseData, {
774+
Object.assign({}, createInstallationAccessTokenResponseData, {
590775
token: "secret456"
591776
})
592777
)

0 commit comments

Comments
 (0)