Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth tests #498

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
38 changes: 32 additions & 6 deletions app/services/github-app-auth.server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import type { GitHubExtraParams } from "remix-auth-github";
import type {
GitHubExtraParams,
GitHubProfile,
GitHubStrategyOptions} from "remix-auth-github";
import {
GitHubStrategy
} from "remix-auth-github";
import { createAppAuth, createOAuthUserAuth } from "@octokit/auth-app";
// import createDebug from "debug";
import { octokitFromConfig } from "~/octokit.server";
import _ from "lodash";
import type { RequestInterface } from "@octokit/types";
import getCache from "~/services/cache";
import { GitHubStrategy } from "remix-auth-github";
import type { StrategyVerifyCallback } from "remix-auth";
import type { OAuth2StrategyVerifyParams } from "remix-auth-oauth2";

function checkNonNull(name: string): NonNullable<string> {
const value = process.env[name];
Expand All @@ -13,7 +22,9 @@ function checkNonNull(name: string): NonNullable<string> {
return value;
}

export const appAuth = _.memoize(() => createAppAuth(getAuthConfig()));
export const appAuth = _.memoize((requestOverride?: RequestInterface) =>
createAppAuth(getAuthConfig(requestOverride)),
);

export const getConfig = _.memoize(() => {
return {
Expand All @@ -29,7 +40,7 @@ export const getConfig = _.memoize(() => {
};
});

function getAuthConfig() {
function getAuthConfig(requestOverride?: RequestInterface) {
return {
appId: checkNonNull("GITHUB_APP_ID"),
clientId: checkNonNull("GITHUB_APP_CLIENT_ID"),
Expand All @@ -43,6 +54,7 @@ function getAuthConfig() {
info: console.info.bind(console),
},
cache: getCache(),
request: requestOverride,
};
}

Expand All @@ -54,6 +66,20 @@ export async function getAppOctokit() {
}

export class GitHubAppAuthStrategy<User> extends GitHubStrategy<User> {
requestOverride?: RequestInterface;

constructor(
options: GitHubStrategyOptions,
verify: StrategyVerifyCallback<
User,
OAuth2StrategyVerifyParams<GitHubProfile, GitHubExtraParams>
>,
requestOverride?: RequestInterface,
) {
super(options, verify);
this.requestOverride = requestOverride;
}

async fetchAccessToken(
code: string,
params: URLSearchParams,
Expand All @@ -62,7 +88,7 @@ export class GitHubAppAuthStrategy<User> extends GitHubStrategy<User> {
extraParams: GitHubExtraParams;
refreshToken: string;
}> {
const authentication = await appAuth()({
const authentication = await appAuth(this.requestOverride)({
type: "oauth-user",
code: code,
redirectUrl: params.get("redirect_uri")! as string,
Expand All @@ -87,7 +113,7 @@ export class GitHubAppAuthStrategy<User> extends GitHubStrategy<User> {
});

if (!("expiresAt" in token)) {
throw new Error("invalid response from GitHub");
throw new Error("invalid response from GitHub: " + JSON.stringify(token));
}

const now = Date.now();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-react": "^7.32.2",
"husky": ">=6",
"isomorphic-fetch": "^3.0.0",
"jsdom": "^23.2.0",
"jsdom-testing-mocks": "^1.11.0",
"lint-staged": ">=10",
Expand Down
67 changes: 67 additions & 0 deletions test/__snapshots__/auth.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`auth > make login request 1`] = `
[
[
"scope",
"scope",
],
[
"allow_signup",
"true",
],
[
"response_type",
"code",
],
[
"client_id",
"clientId",
],
[
"redirect_uri",
"http://localhost:3000/callback",
],
]
`;

exports[`auth > make login request 2`] = `"https://github.com/login/oauth/authorize?scope=scope&allow_signup=true&response_type=code&client_id=clientId&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback"`;

exports[`auth > should be able to call strategy.authenticate 1`] = `
{
"login": "login",
}
`;

exports[`auth not logged in 1`] = `
{
"accessToken": "fake-token",
"context": undefined,
"profile": {
"_json": {
"id": 690,
"login": "Mause",
"name": "Elli",
},
"displayName": "Mause",
"emails": [
{
"value": undefined,
},
],
"id": "690",
"name": {
"familyName": "Elli",
"givenName": "Elli",
"middleName": "Elli",
},
"photos": [
{
"value": undefined,
},
],
"provider": "github",
},
"refreshToken": "fake-refresh-token",
}
`;
58 changes: 58 additions & 0 deletions test/__snapshots__/mock.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`auth > make login request 1`] = `
[
[
"scope",
"scope",
],
[
"allow_signup",
"true",
],
[
"response_type",
"code",
],
[
"client_id",
"clientId",
],
[
"redirect_uri",
"http://localhost:3000/callback",
],
]
`;

exports[`auth > make login request 2`] = `"https://github.com/login/oauth/authorize?scope=scope&allow_signup=true&response_type=code&client_id=clientId&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback"`;

exports[`auth > should be able to call strategy.authenticate 1`] = `
{
"login": "login",
}
`;

exports[`request 1`] = `
{
"data": {
"id": 690,
"login": "Mause",
"name": "Elli",
},
"headers": {
"date": "Fri, 19 Jan 2024 07:12:21 GMT",
},
"status": 200,
"url": "https://api.github.com/user",
}
`;

exports[`request with octokit 1`] = `
{
"data": "{"id":690,"login":"Mause","name":"Elli"}",
"headers": {},
"status": 200,
"url": "https://api.github.com/user",
}
`;
Loading
Loading