Skip to content

Commit

Permalink
test: initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Aug 16, 2019
1 parent 45ea0fc commit 7bf2f31
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { request } from "@octokit/request";
import fetchMock, { MockMatcherFunction } from "fetch-mock";

import { createActionAuth } from "../src/index";

afterEach(() => {
delete process.env.GITHUB_ACTION;
delete process.env.GITHUB_TOKEN;
});

test("README example", async () => {
process.env.GITHUB_ACTION = "my-action";
process.env.GITHUB_TOKEN = "v1.1234567890abcdef1234567890abcdef12345678";

const auth = createActionAuth();
const authentication = await auth();

expect(authentication).toEqual({
type: "token",
token: "v1.1234567890abcdef1234567890abcdef12345678",
tokenType: "installation"
});
});

test("GITHUB_ACTION not set", async () => {
try {
const auth = createActionAuth();
throw new Error("Should not resolve");
} catch (error) {
expect(error.message).toMatch(
/\[@octokit\/auth-action\] `GITHUB_ACTION` environment variable is not set/i
);
}
});

test("GITHUB_TOKEN not set", async () => {
process.env.GITHUB_ACTION = "my-action";

try {
const auth = createActionAuth();
throw new Error("Should not resolve");
} catch (error) {
expect(error.message).toMatch(
/\[@octokit\/auth-action\] `GITHUB_TOKEN` environment variable is not set/i
);
}
});

test('auth.hook(request, "GET /user")', async () => {
process.env.GITHUB_ACTION = "my-action";
process.env.GITHUB_TOKEN = "v1.1234567890abcdef1234567890abcdef12345678";

const expectedRequestHeaders = {
accept: "application/vnd.github.v3+json",
authorization: "token v1.1234567890abcdef1234567890abcdef12345678",
"user-agent": "test"
};

const matchGetUser: MockMatcherFunction = (url, { body, headers }) => {
expect(url).toEqual("https://api.github.com/user");
expect(headers).toStrictEqual(expectedRequestHeaders);
return true;
};

const requestMock = request.defaults({
headers: {
"user-agent": "test"
},
request: {
fetch: fetchMock.sandbox().getOnce(matchGetUser, { id: 123 })
}
});

const { hook } = createActionAuth();
const { data } = await hook(requestMock, "GET /user");

expect(data).toStrictEqual({ id: 123 });
});

0 comments on commit 7bf2f31

Please sign in to comment.