Skip to content

Commit

Permalink
test: use @octokit/auth for authentication strategies other than to…
Browse files Browse the repository at this point in the history
…ken authentication, deprecate previews auth options
  • Loading branch information
gr2m committed Jan 22, 2020
1 parent c0c6fbc commit db35ac8
Show file tree
Hide file tree
Showing 6 changed files with 670 additions and 613 deletions.
48 changes: 16 additions & 32 deletions test/integration/apps-test.js
@@ -1,6 +1,8 @@
const nock = require("nock");

const Octokit = require("../../");
const BEARER_TOKEN =
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NTM4MTkzMTIsImV4cCI6MTU1MzgxOTM3MiwiaXNzIjoxfQ.etiSZ4LFQZ8tiMGJVqKDoGn8hxMCgwL4iLvU5xBUqbAPr4pbk_jJZmMQjuxTlOnRxq4e7NouTizGCdfohRMb3R1mpLzGPzOH9_jqSA_BWYxolsRP_WDSjuNcw6nSxrPRueMVRBKFHrqcTOZJej0djRB5pI61hDZJ_-DGtiOIFexlK3iuVKaqBkvJS5-TbTekGuipJ652g06gXuz-l8i0nHiFJldcuIruwn28hTUrjgtPbjHdSBVn_QQLKc2Fhij8OrhcGqp_D_fvb_KovVmf1X6yWiwXV5VXqWARS-JGD9JTAr2495ZlLV_E4WPxdDpz1jl6XS9HUhMuwBpaCOuipw";

require("../mocha-node-setup");

Expand All @@ -10,56 +12,38 @@ describe("apps", () => {
beforeEach(() => {
octokit = new Octokit({
baseUrl: "https://apps-test-host.com",
auth: "Bearer 123"
auth: `Bearer ${BEARER_TOKEN}`,
log: console.log
});
});

it('adds "machine-man" preview header', () => {
nock("https://apps-test-host.com", {
reqheaders: {
authorization: "Bearer 123",
accept:
"application/vnd.github.v3+json,application/vnd.github.machine-man-preview+json"
}
})
.get("/orgs/myorg")
.reply(200, {});

return octokit.orgs.get({ org: "myorg" });
});

it('adds "machine-man" preview header to custom accept header', () => {
nock("https://apps-test-host.com", {
reqheaders: {
authorization: "Bearer 123",
accept: "foo-bar,application/vnd.github.machine-man-preview+json"
authorization: `bearer ${BEARER_TOKEN}`,
accept: "application/vnd.github.machine-man-preview+json"
}
})
.get("/orgs/myorg")
.get("/app")
.reply(200, {});

return octokit.orgs.get({
org: "myorg",
headers: {
accept: "foo-bar"
}
});
return octokit.apps.getAuthenticated();
});

it('does not add "machine-man" preview header if it is already set', () => {
it('adds "machine-man" preview header to custom media type', () => {
nock("https://apps-test-host.com", {
reqheaders: {
authorization: "Bearer 123",
accept: "application/vnd.github.machine-man-preview+json"
authorization: `bearer ${BEARER_TOKEN}`,
accept:
"application/vnd.github.machine-man-preview+json,application/vnd.github.foo-bar-preview+json"
}
})
.get("/orgs/myorg")
.get("/app")
.reply(200, {});

return octokit.orgs.get({
org: "myorg",
headers: {
accept: "application/vnd.github.machine-man-preview+json"
return octokit.apps.getAuthenticated({
mediaType: {
previews: ["foo-bar"]
}
});
});
Expand Down
152 changes: 152 additions & 0 deletions test/integration/authentication-test.js
@@ -0,0 +1,152 @@
const nock = require("nock");
const { createActionAuth } = require("@octokit/auth");

const Octokit = require("../..");

require("../mocha-node-setup");

describe("authentication", () => {
it("unauthenticated", () => {
nock("https://authentication-test-host.com")
.get("/")
.reply(200, {});

const octokit = new Octokit({
baseUrl: "https://authentication-test-host.com"
});

return octokit.auth().then(authentication => {
expect(authentication).to.deep.equal({
type: "unauthenticated"
});
});
});

it("OAuth token string", () => {
nock("https://authentication-test-host.com", {
reqheaders: {
authorization: "token abc4567"
}
})
.get("/")
.reply(200, {});

const octokit = new Octokit({
baseUrl: "https://authentication-test-host.com",
auth: "token abc4567"
});

return octokit.request("/");
});

const BEARER_TOKEN =
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NTM4MTkzMTIsImV4cCI6MTU1MzgxOTM3MiwiaXNzIjoxfQ.etiSZ4LFQZ8tiMGJVqKDoGn8hxMCgwL4iLvU5xBUqbAPr4pbk_jJZmMQjuxTlOnRxq4e7NouTizGCdfohRMb3R1mpLzGPzOH9_jqSA_BWYxolsRP_WDSjuNcw6nSxrPRueMVRBKFHrqcTOZJej0djRB5pI61hDZJ_-DGtiOIFexlK3iuVKaqBkvJS5-TbTekGuipJ652g06gXuz-l8i0nHiFJldcuIruwn28hTUrjgtPbjHdSBVn_QQLKc2Fhij8OrhcGqp_D_fvb_KovVmf1X6yWiwXV5VXqWARS-JGD9JTAr2495ZlLV_E4WPxdDpz1jl6XS9HUhMuwBpaCOuipw";
it("JSON Web Token string (app authentication)", () => {
nock("https://authentication-test-host.com", {
reqheaders: {
authorization: `bearer ${BEARER_TOKEN}`
}
})
.get("/")
.reply(200, {});

const octokit = new Octokit({
baseUrl: "https://authentication-test-host.com",
auth: `bearer ${BEARER_TOKEN}`
});

return octokit.request("/");
});

it("error to authenticated request", () => {
nock("https://authentication-test-host.com", {
reqheaders: {
authorization: "token abc4567"
}
})
.get("/")
.reply(404, {});

const octokit = new Octokit({
baseUrl: "https://authentication-test-host.com",
auth: "token abc4567",
log: {
warn() {}
}
});

return octokit
.request("/")

.catch(() => {});
});

it("options.auth=token without prefix", () => {
nock("https://authentication-test-host-token-without-prefix.com", {
reqheaders: {
authorization: "token abc4567"
}
})
.get("/")
.reply(200, {});

const octokit = new Octokit({
baseUrl: "https://authentication-test-host-token-without-prefix.com",
auth: "abc4567"
});

return octokit.request("/").catch(error => {
console.log(`error.request`);
console.log(error.request);
});
});

it("options.auth=bearer without prefix", () => {
nock("https://authentication-test-host.com", {
reqheaders: {
authorization:
"bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NTM4MTkzMTIsImV4cCI6MTU1MzgxOTM3MiwiaXNzIjoxfQ.etiSZ4LFQZ8tiMGJVqKDoGn8hxMCgwL4iLvU5xBUqbAPr4pbk_jJZmMQjuxTlOnRxq4e7NouTizGCdfohRMb3R1mpLzGPzOH9_jqSA_BWYxolsRP_WDSjuNcw6nSxrPRueMVRBKFHrqcTOZJej0djRB5pI61hDZJ_-DGtiOIFexlK3iuVKaqBkvJS5-TbTekGuipJ652g06gXuz-l8i0nHiFJldcuIruwn28hTUrjgtPbjHdSBVn_QQLKc2Fhij8OrhcGqp_D_fvb_KovVmf1X6yWiwXV5VXqWARS-JGD9JTAr2495ZlLV_E4WPxdDpz1jl6XS9HUhMuwBpaCOuipw"
}
})
.get("/app")
.reply(200, {});

const octokit = new Octokit({
baseUrl: "https://authentication-test-host.com",
auth:
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NTM4MTkzMTIsImV4cCI6MTU1MzgxOTM3MiwiaXNzIjoxfQ.etiSZ4LFQZ8tiMGJVqKDoGn8hxMCgwL4iLvU5xBUqbAPr4pbk_jJZmMQjuxTlOnRxq4e7NouTizGCdfohRMb3R1mpLzGPzOH9_jqSA_BWYxolsRP_WDSjuNcw6nSxrPRueMVRBKFHrqcTOZJej0djRB5pI61hDZJ_-DGtiOIFexlK3iuVKaqBkvJS5-TbTekGuipJ652g06gXuz-l8i0nHiFJldcuIruwn28hTUrjgtPbjHdSBVn_QQLKc2Fhij8OrhcGqp_D_fvb_KovVmf1X6yWiwXV5VXqWARS-JGD9JTAr2495ZlLV_E4WPxdDpz1jl6XS9HUhMuwBpaCOuipw"
});

return octokit.request("/app");
});

it("invalid auth errors", () => {
expect(() => {
Octokit({ auth: {}, log: { warn() {} } });
}).to.throw(Error);
});

it("auth = createActionAuth()", async () => {
nock("https://api.github.com", {
reqheaders: {
authorization: `token githubtoken123`
}
})
.get("/")
.reply(200, {});

const currentEnv = process.env;
process.env = {
GITHUB_ACTION: "1",
GITHUB_TOKEN: "githubtoken123"
};

const octokit = new Octokit({
authStrategy: createActionAuth
});

return octokit.request("/").then(() => {
process.env = currentEnv;
});
});
});

0 comments on commit db35ac8

Please sign in to comment.