Skip to content

Commit e179b11

Browse files
committed
test: authStrategy option, .auth() method
1 parent 9d50cff commit e179b11

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

test/auth.test.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,12 @@ describe("Authentication", () => {
202202
.getOnce(matchGetUserWithOtp, responseGetUser);
203203

204204
const octokit = new Octokit({
205-
auth: createBasicAuth({
205+
authStrategy: createBasicAuth,
206+
auth: {
206207
username: "octocat",
207208
password: "secret",
208209
on2Fa: () => `123456`
209-
}),
210+
},
210211
request: {
211212
fetch: mock
212213
}
@@ -282,11 +283,12 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
282283
);
283284

284285
const octokit = new Octokit({
285-
auth: createAppAuth({
286+
authStrategy: createAppAuth,
287+
auth: {
286288
id: APP_ID,
287289
privateKey: PRIVATE_KEY,
288290
installationId: 123
289-
}),
291+
},
290292
request: {
291293
fetch: mock
292294
}
@@ -322,7 +324,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
322324
};
323325

324326
const octokit = new Octokit({
325-
auth: createActionAuth(),
327+
authStrategy: createActionAuth,
326328
request: {
327329
fetch: mock
328330
}
@@ -331,4 +333,22 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
331333
await octokit.request("/app");
332334
process.env = currentEnv;
333335
});
336+
337+
it("octokit.auth() is noop by default", async () => {
338+
const octokit = new Octokit();
339+
const result = await octokit.auth();
340+
expect(result).toStrictEqual({ type: "unauthenticated" });
341+
});
342+
343+
it("octokit.auth() with options.auth = secret", async () => {
344+
const octokit = new Octokit({
345+
auth: "secret"
346+
});
347+
const result = await octokit.auth();
348+
expect(result).toStrictEqual({
349+
type: "token",
350+
tokenType: "oauth",
351+
token: "secret"
352+
});
353+
});
334354
});

test/defaults.test.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fetchMock from "fetch-mock";
22
import { getUserAgent } from "universal-user-agent";
33
import { createActionAuth } from "@octokit/auth";
44

5+
import { OctokitOptions } from "../src/types";
56
import { Octokit } from "../src";
67

78
const userAgent = `octokit-core.js/0.0.0-development ${getUserAgent()}`;
@@ -90,7 +91,7 @@ describe("Octokit.defaults", () => {
9091
});
9192
});
9293

93-
it("Octokit.defaults({auth})", async () => {
94+
it("Octokit.defaults({ auth })", async () => {
9495
const mock = fetchMock.sandbox().getOnce(
9596
"https://api.github.com/app",
9697
{ id: 123 },
@@ -109,7 +110,7 @@ describe("Octokit.defaults", () => {
109110
};
110111

111112
const OctokitWithDefaults = Octokit.defaults({
112-
auth: createActionAuth(),
113+
authStrategy: createActionAuth,
113114
request: {
114115
fetch: mock
115116
}
@@ -149,7 +150,7 @@ describe("Octokit.defaults", () => {
149150
});
150151
});
151152

152-
it("Octokit.plugins().defaults()", () => {
153+
it("Octokit.plugin().defaults()", () => {
153154
const mock = fetchMock.sandbox().getOnce(
154155
"https://github.acme-inc.test/api/v3/",
155156
{ ok: true },
@@ -161,16 +162,31 @@ describe("Octokit.defaults", () => {
161162
}
162163
);
163164

164-
const OctokitWithDefaults = Octokit.plugin(octokit => {
165-
octokit.foo = "bar";
166-
}).defaults({
165+
const OctokitWithPlugin = Octokit.plugin(() => {
166+
return {
167+
foo: "bar"
168+
};
169+
});
170+
171+
// See "A note on TypeScript" in README
172+
class OctokitWithPluginWorkaround extends OctokitWithPlugin {
173+
static defaults(defaults: OctokitOptions) {
174+
return class OctokitWithDefaults extends this {
175+
constructor(options: OctokitOptions = {}) {
176+
super(Object.assign({}, defaults, options));
177+
}
178+
};
179+
}
180+
}
181+
182+
const OctokitWithPluginAndDefaults = OctokitWithPluginWorkaround.defaults({
167183
baseUrl: "https://github.acme-inc.test/api/v3",
168184
request: {
169185
fetch: mock
170186
}
171187
});
172188

173-
const octokit = new OctokitWithDefaults();
189+
const octokit = new OctokitWithPluginAndDefaults();
174190

175191
expect(octokit.foo).toEqual("bar");
176192

0 commit comments

Comments
 (0)