Skip to content

Commit ed41053

Browse files
committed
Add tests for getAuthConfig
1 parent 3e8824a commit ed41053

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

src/start-proxy/validation.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
{ schema: types.tokenSchema, name: "isToken" },
30+
] as Array<{ schema: json.Schema; name: string }>;
31+
32+
for (const schemaTest of schemaTests) {
33+
for (const includeOptional of [true, false]) {
34+
const minimalName = includeOptional ? "full" : "minimal";
35+
36+
test(`getAuthConfig - ${schemaTest.name} - ${minimalName}`, async (t) => {
37+
const config = makeFromSchema(includeOptional, schemaTest.schema);
38+
39+
t.deepEqual(
40+
getAuthConfig({
41+
...config,
42+
unexpected: "unexpected-value",
43+
} as json.UnvalidatedObject<types.AuthConfig>),
44+
config,
45+
);
46+
});
47+
}
48+
}
49+
50+
test("getAuthConfig - username and password", async (t) => {
51+
const config = makeFromSchema(true, types.usernamePasswordSchema);
52+
53+
t.deepEqual(
54+
getAuthConfig({
55+
...config,
56+
unexpected: "unexpected-value",
57+
} as json.UnvalidatedObject<types.AuthConfig>),
58+
config,
59+
);
60+
});
61+
62+
test("getAuthConfig - empty", async (t) => {
63+
const config = makeFromSchema(false, types.usernamePasswordSchema);
64+
65+
// Since the purpose of constructing the `AuthConfig` values is for
66+
// serialisation to JSON so that they can be passed to the proxy as configuration,
67+
// we only care that the stringified JSON representations are the same.
68+
t.deepEqual(
69+
JSON.stringify(
70+
getAuthConfig({
71+
...config,
72+
unexpected: "unexpected-value",
73+
} as json.UnvalidatedObject<types.AuthConfig>),
74+
),
75+
JSON.stringify({}),
76+
);
77+
});

0 commit comments

Comments
 (0)