forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor:
getGitHubUser()
test and logic improvements (denoland#577)
TSIA
- Loading branch information
Showing
2 changed files
with
36 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,38 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import { assertRejects } from "std/assert/assert_rejects.ts"; | ||
import { getGitHubUser } from "./kv_oauth.ts"; | ||
import { | ||
assertSpyCall, | ||
assertSpyCalls, | ||
returnsNext, | ||
stub, | ||
} from "std/testing/mock.ts"; | ||
import { Status } from "$fresh/server.ts"; | ||
import { errors } from "std/http/http_errors.ts"; | ||
import { assertEquals } from "std/assert/assert_equals.ts"; | ||
|
||
Deno.test("[plugins] getGitHubUser()", async () => { | ||
const message = crypto.randomUUID(); | ||
const resp1Body = { message }; | ||
const resp1 = Promise.resolve( | ||
Response.json(resp1Body, { status: Status.BadRequest }), | ||
); | ||
const resp2Body = { login: crypto.randomUUID(), email: crypto.randomUUID() }; | ||
const resp2 = Promise.resolve(Response.json(resp2Body)); | ||
const fetchStub = stub(globalThis, "fetch", returnsNext([resp1, resp2])); | ||
|
||
const accessToken1 = crypto.randomUUID(); | ||
const accessToken2 = crypto.randomUUID(); | ||
await assertRejects( | ||
async () => await getGitHubUser("access token"), | ||
Error, | ||
"Bad credentials", | ||
async () => await getGitHubUser(accessToken1), | ||
errors.BadRequest, | ||
message, | ||
); | ||
assertEquals(await getGitHubUser(accessToken2), resp2Body); | ||
|
||
fetchStub.restore(); | ||
|
||
assertSpyCall(fetchStub, 0, { returned: resp1 }); | ||
assertSpyCall(fetchStub, 1, { returned: resp2 }); | ||
assertSpyCalls(fetchStub, 2); | ||
}); |