Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { param } from "./schemas.ts";

const oauthEndpoint = "https://accounts.google.com/o/oauth2/v2/auth";
const scopes = [
export const scopes = [
"https://www.googleapis.com/auth/youtube",
"https://www.googleapis.com/auth/youtube.channel-memberships.creator",
"https://www.googleapis.com/auth/youtube.force-ssl",
Expand Down
86 changes: 86 additions & 0 deletions test/oauth_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {
assertEquals,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { authenticator, authParams, scopes } from "../mod.ts";

Deno.test({
name: "testing when created url successfully",
fn(): void {
for (const s of scopes) {
const auth = new authenticator();
const creds: authParams = {
"client_id": "testId",
"redirect_uri": "testhost",
scope: s,
};
const createdUrl: string = auth.authenticate(creds);
const expectedUrl =
"https://accounts.google.com/o/oauth2/v2/auth?response_type=token&client_id=" +
`${creds.client_id}` + "&redirect_uri=" +
`${creds.redirect_uri}` + "&scope=" +
`${s}`;
assertEquals(createdUrl, expectedUrl);
}
},
});

Deno.test({
name: "testing when created url with mutiple scopes",
fn(): void {
for (const s of scopes) {
const auth = new authenticator();
const creds: authParams = {
"client_id": "testId",
"redirect_uri": "testhost",
scope: s,
"response_type": "code",
"access_type": "online",
state: "CN",
"include_granted_scopes": true,
"login_hint": "testhint",
prompt: "consent",
};
const createdUrl: string = auth.authenticate(creds);
const expectedUrl =
"https://accounts.google.com/o/oauth2/v2/auth?response_type=token&client_id=" +
`${creds.client_id}` + "&redirect_uri=" +
`${creds.redirect_uri}` + "&scope=" +
`${s}` + "&access_type=" +
`${creds.access_type}` + "&state=" +
`${creds.state}` + "&include_granted_scopes=" +
`${creds.include_granted_scopes}` + "&login_hint=" +
`${creds.login_hint}` + "&prompt=" +
`${creds.prompt}`;
assertEquals(createdUrl, expectedUrl);
}
},
});

Deno.test({
name: "testing when scope is invalid",
fn(): void {
const creds: authParams = {
"client_id": "testId",
"redirect_uri": "testhost",
scope: "http://invalidscope",
};
assertThrows((): void => {
throw new TypeError("Invalid scope: " + creds.scope);
});
},
});

Deno.test({
name: "testing when missing the required parameters",
fn(): void {
const creds: authParams = {
"client_id": "testId",
"redirect_uri": "",
scope: "https://www.googleapis.com/auth/youtube",
};
assertThrows((): void => {
throw new TypeError("Missing the required parameters");
});
},
});