@tenonhq/dovetail-core 0.0.129 → 0.0.131
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -0
- package/dist/authFailure.js +243 -0
- package/dist/bootstrap.js +15 -1
- package/dist/envArg.js +159 -4
- package/dist/index.js +4 -0
- package/dist/reconcileCommand.js +1 -0
- package/dist/snClient.js +77 -5
- package/dist/tests/authExitCode.process.test.js +81 -0
- package/dist/tests/authFailure.test.js +227 -0
- package/dist/tests/bootstrapEnvWalkUp.test.js +162 -0
- package/dist/tests/envArg.test.js +13 -1
- package/dist/tests/envWalkUp.test.js +209 -0
- package/dist/tests/retryOnHttpErr.test.js +37 -2
- package/dist/tests/snClient.authInterceptor.test.js +250 -0
- package/package.json +2 -2
- package/release-manifest.json +540 -720
- package/skills/dove-setup-project.md +1 -1
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Tests for the parent-directory lookup of a bare `--env <name>` (envArg.ts):
|
|
3
|
+
// resolveEnvArgPath walks up from the cwd for `<prefix><name>` when the cwd
|
|
4
|
+
// has none, bounded by the home directory, the filesystem root and
|
|
5
|
+
// ENV_WALK_UP_MAX_LEVELS. Paths and dotted filenames never walk up.
|
|
6
|
+
// Every file here is a fake, created in a temp directory at runtime.
|
|
7
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
|
+
};
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
const fs_1 = __importDefault(require("fs"));
|
|
12
|
+
const os_1 = __importDefault(require("os"));
|
|
13
|
+
const path_1 = __importDefault(require("path"));
|
|
14
|
+
const envArg_1 = require("../envArg");
|
|
15
|
+
// The conventional per-instance env-file prefix (`loft` → `<prefix>loft`).
|
|
16
|
+
var ENV_PREFIX = ".env.";
|
|
17
|
+
var NAME = "loft";
|
|
18
|
+
var FILE = ENV_PREFIX + NAME;
|
|
19
|
+
describe("resolveEnvArgPath — bare name walks up to a parent directory", function () {
|
|
20
|
+
var root;
|
|
21
|
+
var home;
|
|
22
|
+
var cwd;
|
|
23
|
+
beforeEach(function () {
|
|
24
|
+
root = fs_1.default.realpathSync(fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "dove-walkup-")));
|
|
25
|
+
// root/home/a/b/c/d — cwd is d, the fake home directory is root/home.
|
|
26
|
+
home = path_1.default.join(root, "home");
|
|
27
|
+
cwd = path_1.default.join(home, "a", "b", "c", "d");
|
|
28
|
+
fs_1.default.mkdirSync(cwd, { recursive: true });
|
|
29
|
+
});
|
|
30
|
+
afterEach(function () {
|
|
31
|
+
fs_1.default.rmSync(root, { recursive: true, force: true });
|
|
32
|
+
});
|
|
33
|
+
function touch(dir, name) {
|
|
34
|
+
var p = path_1.default.join(dir, name || FILE);
|
|
35
|
+
fs_1.default.writeFileSync(p, "SN_INSTANCE=fake\n");
|
|
36
|
+
return p;
|
|
37
|
+
}
|
|
38
|
+
it("uses the file in cwd when it is there (no walk-up)", function () {
|
|
39
|
+
var inCwd = touch(cwd);
|
|
40
|
+
touch(path_1.default.join(home, "a"));
|
|
41
|
+
var calls = [];
|
|
42
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, {
|
|
43
|
+
homeDir: home,
|
|
44
|
+
onParentMatch: function (p) {
|
|
45
|
+
calls.push(p);
|
|
46
|
+
},
|
|
47
|
+
})).toBe(inCwd);
|
|
48
|
+
expect(calls).toEqual([]);
|
|
49
|
+
});
|
|
50
|
+
it("finds the file one level up and reports it", function () {
|
|
51
|
+
var up1 = touch(path_1.default.join(home, "a", "b", "c"));
|
|
52
|
+
var calls = [];
|
|
53
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, {
|
|
54
|
+
homeDir: home,
|
|
55
|
+
onParentMatch: function (p) {
|
|
56
|
+
calls.push(p);
|
|
57
|
+
},
|
|
58
|
+
})).toBe(up1);
|
|
59
|
+
expect(calls).toEqual([up1]);
|
|
60
|
+
});
|
|
61
|
+
it("finds the file three levels up", function () {
|
|
62
|
+
var up3 = touch(path_1.default.join(home, "a"));
|
|
63
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, { homeDir: home })).toBe(up3);
|
|
64
|
+
});
|
|
65
|
+
it("takes the nearest match when several parents have one", function () {
|
|
66
|
+
var near = touch(path_1.default.join(home, "a", "b"));
|
|
67
|
+
touch(path_1.default.join(home, "a"));
|
|
68
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, { homeDir: home })).toBe(near);
|
|
69
|
+
});
|
|
70
|
+
it("returns the cwd path (historical behavior) when no parent has the file", function () {
|
|
71
|
+
var calls = [];
|
|
72
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, {
|
|
73
|
+
homeDir: home,
|
|
74
|
+
onParentMatch: function (p) {
|
|
75
|
+
calls.push(p);
|
|
76
|
+
},
|
|
77
|
+
})).toBe(path_1.default.join(cwd, FILE));
|
|
78
|
+
expect(calls).toEqual([]);
|
|
79
|
+
});
|
|
80
|
+
it("never uses the home directory itself", function () {
|
|
81
|
+
touch(home);
|
|
82
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, { homeDir: home })).toBe(path_1.default.join(cwd, FILE));
|
|
83
|
+
});
|
|
84
|
+
it("never uses a directory above the home directory", function () {
|
|
85
|
+
touch(root);
|
|
86
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, { homeDir: home })).toBe(path_1.default.join(cwd, FILE));
|
|
87
|
+
});
|
|
88
|
+
it("skips a parent entry with that name that is a directory, not a file", function () {
|
|
89
|
+
fs_1.default.mkdirSync(path_1.default.join(home, "a", "b", FILE));
|
|
90
|
+
var up3 = touch(path_1.default.join(home, "a"));
|
|
91
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, { homeDir: home })).toBe(up3);
|
|
92
|
+
});
|
|
93
|
+
it("does not walk up for a dotted filename", function () {
|
|
94
|
+
touch(path_1.default.join(home, "a"), "loft.env");
|
|
95
|
+
touch(path_1.default.join(home, "a"), FILE);
|
|
96
|
+
expect((0, envArg_1.resolveEnvArgPath)("loft.env", cwd, { homeDir: home })).toBe(path_1.default.join(cwd, "loft.env"));
|
|
97
|
+
expect((0, envArg_1.resolveEnvArgPath)(FILE, cwd, { homeDir: home })).toBe(path_1.default.join(cwd, FILE));
|
|
98
|
+
});
|
|
99
|
+
it("does not walk up for a path value", function () {
|
|
100
|
+
touch(path_1.default.join(home, "a"));
|
|
101
|
+
expect((0, envArg_1.resolveEnvArgPath)(path_1.default.join("envs", NAME), cwd, { homeDir: home })).toBe(path_1.default.join(cwd, "envs", NAME));
|
|
102
|
+
});
|
|
103
|
+
it("checks existence with stat only — never opens or reads a file", function () {
|
|
104
|
+
var up3 = touch(path_1.default.join(home, "a"));
|
|
105
|
+
var readSpy = jest.spyOn(fs_1.default, "readFileSync");
|
|
106
|
+
var openSpy = jest.spyOn(fs_1.default, "openSync");
|
|
107
|
+
try {
|
|
108
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, { homeDir: home })).toBe(up3);
|
|
109
|
+
expect((0, envArg_1.resolveEnvArgPath)("missing", cwd, { homeDir: home })).toBe(path_1.default.join(cwd, ENV_PREFIX + "missing"));
|
|
110
|
+
expect(readSpy).not.toHaveBeenCalled();
|
|
111
|
+
expect(openSpy).not.toHaveBeenCalled();
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
readSpy.mockRestore();
|
|
115
|
+
openSpy.mockRestore();
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
it("skips a parent file owned by another user (e.g. planted in /tmp)", function () {
|
|
119
|
+
touch(path_1.default.join(home, "a"));
|
|
120
|
+
var otherUid = (typeof process.getuid === "function" ? process.getuid() : 0) + 4242;
|
|
121
|
+
var calls = [];
|
|
122
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, {
|
|
123
|
+
homeDir: home,
|
|
124
|
+
ownerUid: otherUid,
|
|
125
|
+
onParentMatch: function (p) {
|
|
126
|
+
calls.push(p);
|
|
127
|
+
},
|
|
128
|
+
})).toBe(path_1.default.join(cwd, FILE));
|
|
129
|
+
expect(calls).toEqual([]);
|
|
130
|
+
});
|
|
131
|
+
it("accepts a parent file owned by the current user, and any owner when ownerUid < 0", function () {
|
|
132
|
+
var up3 = touch(path_1.default.join(home, "a"));
|
|
133
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, { homeDir: home })).toBe(up3);
|
|
134
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, { homeDir: home, ownerUid: -1 })).toBe(up3);
|
|
135
|
+
});
|
|
136
|
+
it("keeps the selection when the callback throws", function () {
|
|
137
|
+
var up1 = touch(path_1.default.join(home, "a", "b", "c"));
|
|
138
|
+
expect((0, envArg_1.resolveEnvArgPath)(NAME, cwd, {
|
|
139
|
+
homeDir: home,
|
|
140
|
+
onParentMatch: function () {
|
|
141
|
+
throw new Error("logger down");
|
|
142
|
+
},
|
|
143
|
+
})).toBe(up1);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
describe("findEnvFileUpward", function () {
|
|
147
|
+
var root;
|
|
148
|
+
beforeEach(function () {
|
|
149
|
+
root = fs_1.default.realpathSync(fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "dove-walkup-depth-")));
|
|
150
|
+
});
|
|
151
|
+
afterEach(function () {
|
|
152
|
+
fs_1.default.rmSync(root, { recursive: true, force: true });
|
|
153
|
+
});
|
|
154
|
+
// root/l0/l1/.../l<depth-1>; returns the deepest directory and every level.
|
|
155
|
+
function chain(depth) {
|
|
156
|
+
var levels = [];
|
|
157
|
+
var dir = root;
|
|
158
|
+
for (var i = 0; i < depth; i++) {
|
|
159
|
+
dir = path_1.default.join(dir, "l" + i);
|
|
160
|
+
levels.push(dir);
|
|
161
|
+
}
|
|
162
|
+
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
163
|
+
return { deepest: dir, levels: levels };
|
|
164
|
+
}
|
|
165
|
+
it("defaults to " + envArg_1.ENV_WALK_UP_MAX_LEVELS + " parent levels", function () {
|
|
166
|
+
expect(envArg_1.ENV_WALK_UP_MAX_LEVELS).toBe(8);
|
|
167
|
+
});
|
|
168
|
+
it("finds a file exactly ENV_WALK_UP_MAX_LEVELS levels up", function () {
|
|
169
|
+
var c = chain(envArg_1.ENV_WALK_UP_MAX_LEVELS + 2);
|
|
170
|
+
// levels[last] is the start; ENV_WALK_UP_MAX_LEVELS up from it:
|
|
171
|
+
var target = c.levels[c.levels.length - 1 - envArg_1.ENV_WALK_UP_MAX_LEVELS];
|
|
172
|
+
fs_1.default.writeFileSync(path_1.default.join(target, FILE), "x");
|
|
173
|
+
expect((0, envArg_1.findEnvFileUpward)({ fileName: FILE, startDir: c.deepest, homeDir: "" })).toBe(path_1.default.join(target, FILE));
|
|
174
|
+
});
|
|
175
|
+
it("stops after ENV_WALK_UP_MAX_LEVELS levels", function () {
|
|
176
|
+
var c = chain(envArg_1.ENV_WALK_UP_MAX_LEVELS + 3);
|
|
177
|
+
var tooFar = c.levels[c.levels.length - 2 - envArg_1.ENV_WALK_UP_MAX_LEVELS];
|
|
178
|
+
fs_1.default.writeFileSync(path_1.default.join(tooFar, FILE), "x");
|
|
179
|
+
expect((0, envArg_1.findEnvFileUpward)({ fileName: FILE, startDir: c.deepest, homeDir: "" })).toBeNull();
|
|
180
|
+
});
|
|
181
|
+
it("honors a smaller maxLevels", function () {
|
|
182
|
+
var c = chain(5);
|
|
183
|
+
fs_1.default.writeFileSync(path_1.default.join(c.levels[1], FILE), "x"); // 3 levels up from l4
|
|
184
|
+
expect((0, envArg_1.findEnvFileUpward)({
|
|
185
|
+
fileName: FILE,
|
|
186
|
+
startDir: c.deepest,
|
|
187
|
+
homeDir: "",
|
|
188
|
+
maxLevels: 2,
|
|
189
|
+
})).toBeNull();
|
|
190
|
+
expect((0, envArg_1.findEnvFileUpward)({
|
|
191
|
+
fileName: FILE,
|
|
192
|
+
startDir: c.deepest,
|
|
193
|
+
homeDir: "",
|
|
194
|
+
maxLevels: 3,
|
|
195
|
+
})).toBe(path_1.default.join(c.levels[1], FILE));
|
|
196
|
+
});
|
|
197
|
+
it("never looks in startDir itself", function () {
|
|
198
|
+
var c = chain(2);
|
|
199
|
+
fs_1.default.writeFileSync(path_1.default.join(c.deepest, FILE), "x");
|
|
200
|
+
expect((0, envArg_1.findEnvFileUpward)({ fileName: FILE, startDir: c.deepest, homeDir: "" })).toBeNull();
|
|
201
|
+
});
|
|
202
|
+
it("returns null for bad input instead of throwing", function () {
|
|
203
|
+
expect((0, envArg_1.findEnvFileUpward)({ fileName: "", startDir: root })).toBeNull();
|
|
204
|
+
expect((0, envArg_1.findEnvFileUpward)({ fileName: path_1.default.join("a", FILE), startDir: root })).toBeNull();
|
|
205
|
+
expect((0, envArg_1.findEnvFileUpward)({ fileName: "..", startDir: root })).toBeNull();
|
|
206
|
+
expect((0, envArg_1.findEnvFileUpward)({ fileName: FILE, startDir: "" })).toBeNull();
|
|
207
|
+
expect((0, envArg_1.findEnvFileUpward)(null)).toBeNull();
|
|
208
|
+
});
|
|
209
|
+
});
|
|
@@ -21,6 +21,7 @@ jest.mock("../genericUtils", function () {
|
|
|
21
21
|
});
|
|
22
22
|
const snClient_1 = require("../snClient");
|
|
23
23
|
const genericUtils_1 = require("../genericUtils");
|
|
24
|
+
const authFailure_1 = require("../authFailure");
|
|
24
25
|
function makeAxiosError(status, headers) {
|
|
25
26
|
var error = new Error("Request failed with status " + status);
|
|
26
27
|
error.isAxiosError = true;
|
|
@@ -49,12 +50,46 @@ describe("retryOnHttpErr", function () {
|
|
|
49
50
|
expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining("Unauthorized (401)"));
|
|
50
51
|
expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining("credentials"));
|
|
51
52
|
});
|
|
52
|
-
it("fails immediately on 403 with
|
|
53
|
+
it("fails immediately on 403 with a refused-write message", async function () {
|
|
53
54
|
var fn = jest.fn().mockRejectedValue(makeAxiosError(403));
|
|
54
55
|
await expect((0, snClient_1.retryOnHttpErr)(fn, "test > rec1")).rejects.toThrow();
|
|
55
56
|
expect(fn).toHaveBeenCalledTimes(1);
|
|
56
57
|
expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining("Forbidden (403)"));
|
|
57
|
-
expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining("
|
|
58
|
+
expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining("refused the write"));
|
|
59
|
+
});
|
|
60
|
+
it("401/403: the per-record line names the record and prints no credential; recording is the client interceptor's job (#185)", async function () {
|
|
61
|
+
var saved = { password: process.env.SN_PASSWORD };
|
|
62
|
+
process.env.SN_PASSWORD = "hunter2-do-not-print";
|
|
63
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
64
|
+
try {
|
|
65
|
+
var statuses = [401, 403];
|
|
66
|
+
for (var i = 0; i < statuses.length; i += 1) {
|
|
67
|
+
mockLogger.error.mockClear();
|
|
68
|
+
var err = makeAxiosError(statuses[i]);
|
|
69
|
+
var fn = jest.fn().mockRejectedValue(err);
|
|
70
|
+
// The original error is still what is thrown — callers keep its status.
|
|
71
|
+
await expect((0, snClient_1.retryOnHttpErr)(fn, "test > rec1")).rejects.toBe(err);
|
|
72
|
+
var logged = String(mockLogger.error.mock.calls[0][0]);
|
|
73
|
+
expect(logged).toContain("pushing test > rec1");
|
|
74
|
+
expect(logged).toContain("See the message above");
|
|
75
|
+
expect(logged).not.toContain("hunter2-do-not-print");
|
|
76
|
+
}
|
|
77
|
+
// A function that never went through an snClient records nothing here;
|
|
78
|
+
// snClient.authInterceptor.test.ts covers the recording.
|
|
79
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(0);
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
83
|
+
if (saved.password === undefined)
|
|
84
|
+
delete process.env.SN_PASSWORD;
|
|
85
|
+
else
|
|
86
|
+
process.env.SN_PASSWORD = saved.password;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
it("a 404 does not record an auth failure", async function () {
|
|
90
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
91
|
+
await expect((0, snClient_1.retryOnHttpErr)(jest.fn().mockRejectedValue(makeAxiosError(404)), "test > rec1")).rejects.toThrow();
|
|
92
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(1);
|
|
58
93
|
});
|
|
59
94
|
// 404: Fail immediately
|
|
60
95
|
it("fails immediately on 404 with record not found message", async function () {
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Every request an snClient makes passes its response interceptor, which is the
|
|
3
|
+
// ONE place an HTTP 401/403 is recorded (#185). These tests drive real client
|
|
4
|
+
// methods through a stub axios adapter, so the interceptor itself is exercised:
|
|
5
|
+
// a rejected write that is NOT `push` (reconcile CREATE/DELETE, create-record,
|
|
6
|
+
// delete-record, createUpdateSet) must exit 77 like a rejected push does.
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
var mockLogger = {
|
|
9
|
+
info: jest.fn(),
|
|
10
|
+
debug: jest.fn(),
|
|
11
|
+
warn: jest.fn(),
|
|
12
|
+
error: jest.fn(),
|
|
13
|
+
success: jest.fn(),
|
|
14
|
+
getLogLevel: function () {
|
|
15
|
+
return "debug";
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
jest.mock("../Logger", function () {
|
|
19
|
+
return { logger: mockLogger };
|
|
20
|
+
});
|
|
21
|
+
jest.mock("../FileLogger", function () {
|
|
22
|
+
return {
|
|
23
|
+
fileLogger: {
|
|
24
|
+
debug: jest.fn(),
|
|
25
|
+
info: jest.fn(),
|
|
26
|
+
warn: jest.fn(),
|
|
27
|
+
error: jest.fn(),
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
jest.mock("../genericUtils", function () {
|
|
32
|
+
return { wait: jest.fn().mockResolvedValue(undefined) };
|
|
33
|
+
});
|
|
34
|
+
jest.mock("../config", function () {
|
|
35
|
+
return {
|
|
36
|
+
getSourcePathForScope: function () {
|
|
37
|
+
return "/nonexistent-reconcile-source";
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
var mockHolder = { client: undefined };
|
|
42
|
+
jest.mock("../snClient", function () {
|
|
43
|
+
var actual = jest.requireActual("../snClient");
|
|
44
|
+
return Object.assign({}, actual, {
|
|
45
|
+
defaultClient: function () {
|
|
46
|
+
return mockHolder.client;
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
const axios_1 = require("axios");
|
|
51
|
+
const snClient_1 = require("../snClient");
|
|
52
|
+
const reconcileCommand_1 = require("../reconcileCommand");
|
|
53
|
+
const authFailure_1 = require("../authFailure");
|
|
54
|
+
var SECRET = "hunter2-do-not-print";
|
|
55
|
+
// A client whose transport answers every request with `status`. Only the adapter
|
|
56
|
+
// is replaced, so the request still runs the client's real interceptors.
|
|
57
|
+
function clientAnswering(status) {
|
|
58
|
+
var seen = [];
|
|
59
|
+
var c = (0, snClient_1.snClient)("https://admin:" + SECRET + "@example.invalid/", "svc-user", SECRET);
|
|
60
|
+
c.client.defaults.adapter = function (config) {
|
|
61
|
+
seen.push({
|
|
62
|
+
method: String(config.method).toUpperCase(),
|
|
63
|
+
url: String(config.url),
|
|
64
|
+
});
|
|
65
|
+
var response = {
|
|
66
|
+
status: status,
|
|
67
|
+
statusText: "",
|
|
68
|
+
headers: {},
|
|
69
|
+
config: config,
|
|
70
|
+
data: { error: { message: "denied" } },
|
|
71
|
+
};
|
|
72
|
+
return Promise.reject(new axios_1.AxiosError("Request failed with status code " + status, "ERR_BAD_REQUEST", config, null, response));
|
|
73
|
+
};
|
|
74
|
+
return { client: c, seen: seen };
|
|
75
|
+
}
|
|
76
|
+
function loggedErrors() {
|
|
77
|
+
return mockLogger.error.mock.calls
|
|
78
|
+
.map(function (c) {
|
|
79
|
+
return String(c[0]);
|
|
80
|
+
})
|
|
81
|
+
.join("\n");
|
|
82
|
+
}
|
|
83
|
+
function change(kind) {
|
|
84
|
+
return {
|
|
85
|
+
kind: kind,
|
|
86
|
+
table: "sys_script_include",
|
|
87
|
+
scope: "x_s",
|
|
88
|
+
sys_id: "abc123",
|
|
89
|
+
name: "Foo",
|
|
90
|
+
fieldDeltas: [],
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
describe("snClient auth interceptor (#185)", function () {
|
|
94
|
+
beforeEach(function () {
|
|
95
|
+
jest.clearAllMocks();
|
|
96
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
97
|
+
(0, snClient_1._resetDovetailApiFallback)();
|
|
98
|
+
(0, authFailure_1.setAuthContext)({ envPath: "/work/.env.loft", envArg: "loft" });
|
|
99
|
+
});
|
|
100
|
+
afterAll(function () {
|
|
101
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
102
|
+
});
|
|
103
|
+
it("a 403 on createRecord is a rejected write: exit 77 even over a clean exit", async function () {
|
|
104
|
+
var t = clientAnswering(403);
|
|
105
|
+
await expect(t.client.createRecord({
|
|
106
|
+
table: "sys_script_include",
|
|
107
|
+
fields: { name: "Foo" },
|
|
108
|
+
})).rejects.toMatchObject({ response: { status: 403 } });
|
|
109
|
+
expect(t.seen).toEqual([
|
|
110
|
+
{ method: "POST", url: "api/cadso/dovetail_core/createRecord" },
|
|
111
|
+
]);
|
|
112
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
113
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
114
|
+
var lines = loggedErrors();
|
|
115
|
+
expect(lines).toContain("Forbidden (403) calling POST api/cadso/dovetail_core/createRecord");
|
|
116
|
+
expect(lines).toContain("example.invalid");
|
|
117
|
+
expect(lines).toContain("/work/.env.loft");
|
|
118
|
+
// The table comes from the request body. A 403 does not send the user to dove login.
|
|
119
|
+
expect(lines).toContain("Check the role or ACL for table sys_script_include");
|
|
120
|
+
expect(lines).not.toContain("dove login");
|
|
121
|
+
expect(lines).not.toContain(SECRET);
|
|
122
|
+
});
|
|
123
|
+
it("a 403 on a Table API write names the table from the path", async function () {
|
|
124
|
+
var t = clientAnswering(403);
|
|
125
|
+
await expect(t.client.updateRecord("sys_script", "abc123", { script: "x" })).rejects.toMatchObject({ response: { status: 403 } });
|
|
126
|
+
expect(loggedErrors()).toContain("Check the role or ACL for table sys_script for the user");
|
|
127
|
+
});
|
|
128
|
+
it("a 403 on deleteRecord is a rejected write", async function () {
|
|
129
|
+
var t = clientAnswering(403);
|
|
130
|
+
await expect(t.client.deleteRecord({ table: "sys_script_include", sys_id: "abc123" })).rejects.toMatchObject({ response: { status: 403 } });
|
|
131
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
132
|
+
});
|
|
133
|
+
it("a 403 on createUpdateSet through unwrapSNResponse is a rejected write", async function () {
|
|
134
|
+
var t = clientAnswering(403);
|
|
135
|
+
await expect((0, snClient_1.unwrapSNResponse)(t.client.createUpdateSet("My set", "scope1"))).rejects.toMatchObject({
|
|
136
|
+
response: { status: 403 },
|
|
137
|
+
});
|
|
138
|
+
expect(t.seen[0].method).toBe("POST");
|
|
139
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
140
|
+
});
|
|
141
|
+
it("reconcile --apply: a 403 on CREATE and on DELETE fails the record and exits 77", async function () {
|
|
142
|
+
var t = clientAnswering(403);
|
|
143
|
+
mockHolder.client = t.client;
|
|
144
|
+
var deleted = await (0, reconcileCommand_1.applyDeletes)("x_s", [change("delete")]);
|
|
145
|
+
expect(deleted).toHaveLength(1);
|
|
146
|
+
expect(deleted[0].ok).toBe(false);
|
|
147
|
+
expect(t.seen[t.seen.length - 1].url).toBe("api/cadso/dovetail_core/deleteRecord");
|
|
148
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
149
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
150
|
+
var created = await (0, reconcileCommand_1.applyCreates)("x_s", [change("create")], "us1");
|
|
151
|
+
expect(created).toHaveLength(1);
|
|
152
|
+
expect(created[0].ok).toBe(false);
|
|
153
|
+
expect(t.seen[t.seen.length - 1].url).toBe("api/cadso/dovetail_core/createRecord");
|
|
154
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
155
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
156
|
+
});
|
|
157
|
+
it("push: a 401 or 403 on updateRecord through retryOnHttpErr is still a rejected write", async function () {
|
|
158
|
+
var statuses = [401, 403];
|
|
159
|
+
for (var i = 0; i < statuses.length; i += 1) {
|
|
160
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
161
|
+
var t = clientAnswering(statuses[i]);
|
|
162
|
+
await expect((0, snClient_1.retryOnHttpErr)(function () {
|
|
163
|
+
return t.client.updateRecord("sys_script_include", "abc123", {
|
|
164
|
+
script: "x",
|
|
165
|
+
});
|
|
166
|
+
}, "sys_script_include > Foo")).rejects.toMatchObject({ response: { status: statuses[i] } });
|
|
167
|
+
expect(t.seen).toHaveLength(1);
|
|
168
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
it("a 403 on a GET changes nothing and prints no auth message", async function () {
|
|
172
|
+
var t = clientAnswering(403);
|
|
173
|
+
await expect((0, snClient_1.unwrapSNResponse)(t.client.getAppList())).rejects.toMatchObject({ response: { status: 403 } });
|
|
174
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(0);
|
|
175
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(1);
|
|
176
|
+
expect(loggedErrors()).not.toContain("Forbidden (403) calling");
|
|
177
|
+
});
|
|
178
|
+
it("a 403 on a read-only POST (getManifest, bulkDownload) is a read, not a rejected write", async function () {
|
|
179
|
+
var t = clientAnswering(403);
|
|
180
|
+
await expect(t.client.getManifest("x_s", {
|
|
181
|
+
sourceDirectory: "src",
|
|
182
|
+
buildDirectory: "build",
|
|
183
|
+
tableOptions: {},
|
|
184
|
+
refreshInterval: 30,
|
|
185
|
+
})).rejects.toMatchObject({
|
|
186
|
+
response: { status: 403 },
|
|
187
|
+
});
|
|
188
|
+
await expect(t.client.getMissingFiles({}, {})).rejects.toMatchObject({
|
|
189
|
+
response: { status: 403 },
|
|
190
|
+
});
|
|
191
|
+
expect(t.seen.map(function (r) {
|
|
192
|
+
return r.method;
|
|
193
|
+
})).toEqual(["POST", "POST"]);
|
|
194
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(0);
|
|
195
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(1);
|
|
196
|
+
});
|
|
197
|
+
it("a 401 on a GET re-maps only a failing exit and names the re-auth command", async function () {
|
|
198
|
+
var t = clientAnswering(401);
|
|
199
|
+
await expect((0, snClient_1.unwrapSNResponse)(t.client.getAppList())).rejects.toMatchObject({ response: { status: 401 } });
|
|
200
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(0);
|
|
201
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
202
|
+
var lines = loggedErrors();
|
|
203
|
+
expect(lines).toContain("Unauthorized (401) calling GET api/cadso/dovetail_sync/getAppList");
|
|
204
|
+
expect(lines).toContain("dove login --env loft");
|
|
205
|
+
expect(lines).not.toContain(SECRET);
|
|
206
|
+
});
|
|
207
|
+
it("a 401 on a write is a rejected write", async function () {
|
|
208
|
+
var t = clientAnswering(401);
|
|
209
|
+
await expect(t.client.createRecord({
|
|
210
|
+
table: "sys_script_include",
|
|
211
|
+
fields: { name: "Foo" },
|
|
212
|
+
})).rejects.toMatchObject({ response: { status: 401 } });
|
|
213
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
214
|
+
});
|
|
215
|
+
it("prints the full message once per kind, however many records are rejected", async function () {
|
|
216
|
+
var t = clientAnswering(403);
|
|
217
|
+
for (var i = 0; i < 3; i += 1) {
|
|
218
|
+
await expect(t.client.deleteRecord({
|
|
219
|
+
table: "sys_script_include",
|
|
220
|
+
sys_id: "abc12" + i,
|
|
221
|
+
})).rejects.toBeDefined();
|
|
222
|
+
}
|
|
223
|
+
var full = mockLogger.error.mock.calls.filter(function (c) {
|
|
224
|
+
return String(c[0]).indexOf("Forbidden (403) calling") !== -1;
|
|
225
|
+
});
|
|
226
|
+
expect(full).toHaveLength(1);
|
|
227
|
+
});
|
|
228
|
+
it("a 404 or 500 records nothing", async function () {
|
|
229
|
+
var statuses = [404, 500];
|
|
230
|
+
for (var i = 0; i < statuses.length; i += 1) {
|
|
231
|
+
var t = clientAnswering(statuses[i]);
|
|
232
|
+
await expect(t.client.updateRecord("sys_script_include", "abc123", { script: "x" })).rejects.toBeDefined();
|
|
233
|
+
}
|
|
234
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(1);
|
|
235
|
+
});
|
|
236
|
+
it("_noteAuthRejection never throws on a malformed error", function () {
|
|
237
|
+
expect(function () {
|
|
238
|
+
(0, snClient_1._noteAuthRejection)(undefined);
|
|
239
|
+
(0, snClient_1._noteAuthRejection)(new Error("boom"));
|
|
240
|
+
(0, snClient_1._noteAuthRejection)({ isAxiosError: true, response: { status: 403 } });
|
|
241
|
+
(0, snClient_1._noteAuthRejection)({
|
|
242
|
+
isAxiosError: true,
|
|
243
|
+
response: { status: 401 },
|
|
244
|
+
config: { method: 7, url: {} },
|
|
245
|
+
});
|
|
246
|
+
}).not.toThrow();
|
|
247
|
+
// No method on the 403 means it cannot be called a write.
|
|
248
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(0);
|
|
249
|
+
});
|
|
250
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tenonhq/dovetail-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.131",
|
|
4
4
|
"description": "Next-gen file syncer",
|
|
5
5
|
"license": "GPL-3.0",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@tenonhq/dovetail-clickup": "~0.0.15",
|
|
42
|
-
"@tenonhq/dovetail-schema": "~0.0.
|
|
42
|
+
"@tenonhq/dovetail-schema": "~0.0.16",
|
|
43
43
|
"axios": "^1.20.0",
|
|
44
44
|
"axios-cookiejar-support": "^4.0.7",
|
|
45
45
|
"axios-rate-limit": "^1.3.0",
|