/
get-two-factor-auth-required.test.js
102 lines (76 loc) · 2.74 KB
/
get-two-factor-auth-required.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"use strict";
jest.mock("../lib/get-profile-data");
const loggingOutput = require("@lerna-test/logging-output");
const getProfileData = require("../lib/get-profile-data");
const getTwoFactorAuthRequired = require("../lib/get-two-factor-auth-required");
getProfileData.mockImplementation(() => Promise.resolve({ tfa: {} }));
describe("getTwoFactorAuthRequired", () => {
const origConsoleError = console.error;
beforeEach(() => {
console.error = jest.fn();
});
afterEach(() => {
console.error = origConsoleError;
});
it("resolves true if tfa.mode === 'auth-and-writes'", async () => {
getProfileData.mockResolvedValueOnce({
tfa: {
mode: "auth-and-writes",
},
});
const result = await getTwoFactorAuthRequired();
expect(result).toBe(true);
expect(getProfileData).toHaveBeenLastCalledWith(expect.objectContaining({ fetchRetries: 0 }));
});
it("resolves false if tfa.mode !== 'auth-and-writes'", async () => {
getProfileData.mockResolvedValueOnce({
tfa: {
mode: "auth-only",
},
});
const result = await getTwoFactorAuthRequired();
expect(result).toBe(false);
});
it("resolves false if tfa.pending === true", async () => {
getProfileData.mockResolvedValueOnce({
tfa: {
pending: true,
mode: "ignored",
},
});
const result = await getTwoFactorAuthRequired();
expect(result).toBe(false);
});
it("resolves false after profile 404", async () => {
getProfileData.mockImplementationOnce(() => {
const err = new Error("third-party profile fail");
err.code = "E404";
return Promise.reject(err);
});
const result = await getTwoFactorAuthRequired();
expect(result).toBe(false);
expect(console.error).not.toHaveBeenCalled();
});
it("resolves false after profile 500", async () => {
getProfileData.mockImplementationOnce(() => {
const err = new Error("legacy npm Enterprise profile fail");
err.code = "E500";
return Promise.reject(err);
});
const opts = { registry: "such-registry-wow" };
const result = await getTwoFactorAuthRequired(opts);
expect(result).toBe(false);
expect(loggingOutput("warn")).toContain(
`Registry "${opts.registry}" does not support 'npm profile get', skipping two-factor auth check...`
);
});
it("logs unexpected failure message before throwing validation error", async () => {
getProfileData.mockImplementationOnce(() => {
const err = new Error("zomg explosions");
err.code = "E401";
return Promise.reject(err);
});
await expect(getTwoFactorAuthRequired()).rejects.toThrow("Unable to obtain two-factor auth mode");
expect(console.error).toHaveBeenCalledWith("zomg explosions");
});
});