Skip to content

Commit 2acf819

Browse files
committed
Add tests for getAuthConfig
1 parent d2a54a4 commit 2acf819

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

src/start-proxy/validation.test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import test from "ava";
2+
3+
import * as json from "../json";
4+
import { setupTests } from "../testing-utils";
5+
6+
import * as types from "./types";
7+
import { getAuthConfig } from "./validation";
8+
9+
setupTests(test);
10+
11+
function makeFromSchema(
12+
includeOptional: boolean,
13+
schema: json.Schema,
14+
): json.FromSchema<typeof schema> {
15+
const result = {};
16+
for (const [key, validator] of Object.entries(schema)) {
17+
if (!validator.required && !includeOptional) {
18+
continue;
19+
}
20+
result[key] = `value-for-${key}`;
21+
}
22+
return result;
23+
}
24+
25+
const schemaTests = [
26+
{ schema: types.azureConfigSchema, name: "isAzureConfig" },
27+
{ schema: types.awsConfigSchema, name: "isAWSConfig" },
28+
{ schema: types.jfrogConfigSchema, name: "isJFrogConfig" },
29+
] as Array<{ schema: json.Schema; name: string }>;
30+
31+
for (const schemaTest of schemaTests) {
32+
for (const includeOptional of [true, false]) {
33+
const minimalName = includeOptional ? "full" : "minimal";
34+
35+
test(`getAuthConfig - ${schemaTest.name} - ${minimalName}`, async (t) => {
36+
const config = makeFromSchema(includeOptional, schemaTest.schema);
37+
38+
t.deepEqual(
39+
getAuthConfig({
40+
...config,
41+
unexpected: "unexpected-value",
42+
} as json.UnvalidatedObject<types.AuthConfig>),
43+
config,
44+
);
45+
});
46+
}
47+
}
48+
49+
test("getAuthConfig - token", async (t) => {
50+
const config = makeFromSchema(true, types.tokenSchema);
51+
52+
t.deepEqual(
53+
getAuthConfig({
54+
...config,
55+
unexpected: "unexpected-value",
56+
} as json.UnvalidatedObject<types.AuthConfig>),
57+
config,
58+
);
59+
});
60+
61+
test("getAuthConfig - username and password", async (t) => {
62+
const config = makeFromSchema(true, types.usernamePasswordSchema);
63+
64+
t.deepEqual(
65+
getAuthConfig({
66+
...config,
67+
unexpected: "unexpected-value",
68+
} as json.UnvalidatedObject<types.AuthConfig>),
69+
config,
70+
);
71+
});
72+
73+
test("getAuthConfig - empty", async (t) => {
74+
const config = makeFromSchema(false, types.usernamePasswordSchema);
75+
76+
// Since the purpose of constructing the `AuthConfig` values is for
77+
// serialisation to JSON so that they can be passed to the proxy as configuration,
78+
// we only care that the stringified JSON representations are the same.
79+
t.deepEqual(
80+
JSON.stringify(
81+
getAuthConfig({
82+
...config,
83+
unexpected: "unexpected-value",
84+
} as json.UnvalidatedObject<types.AuthConfig>),
85+
),
86+
JSON.stringify({}),
87+
);
88+
});

0 commit comments

Comments
 (0)