Skip to content

Commit

Permalink
rm old methods and their tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed Jan 22, 2025
1 parent 108241c commit 1cfc93b
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 461 deletions.
2 changes: 1 addition & 1 deletion coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 0 additions & 43 deletions src/documentation-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
ResponseObject,
SchemaObject,
SchemaObjectType,
SecurityRequirementObject,
SecuritySchemeObject,
TagObject,
isReferenceObject,
Expand All @@ -23,7 +22,6 @@ import {
has,
isNil,
map,
mergeAll,
mergeDeepRight,
mergeDeepWith,
objOf,
Expand Down Expand Up @@ -57,11 +55,6 @@ import { DateOutSchema, ezDateOutBrand } from "./date-out-schema";
import { DocumentationError } from "./errors";
import { FileSchema, ezFileBrand } from "./file-schema";
import { IOSchema } from "./io-schema";
import {
LogicalContainer,
andToOr,
mapLogicalContainer,
} from "./logical-container";
import { metaSymbol } from "./metadata";
import { Method } from "./method";
import { ProprietaryBrand } from "./proprietary-schemas";
Expand Down Expand Up @@ -896,42 +889,6 @@ export const depictOAuth2Security: SecurityHelper<"oauth2"> = ({
),
});

export const depictSecurity = (
container: LogicalContainer<Security>,
inputSources?: InputSource[],
): LogicalContainer<SecuritySchemeObject> => {
const methods: { [K in Security["type"]]: SecurityHelper<K> } = {
basic: depictBasicSecurity,
bearer: depictBearerSecurity,
input: depictInputSecurity,
header: depictHeaderSecurity,
cookie: depictCookieSecurity,
openid: depictOpenIdSecurity,
oauth2: depictOAuth2Security,
};
return mapLogicalContainer(container, (security) =>
(methods[security.type] as SecurityHelper<typeof security.type>)(
security,
inputSources,
),
);
};

export const depictSecurityRefs = (
container: LogicalContainer<{ name: string; scopes: string[] }>,
): SecurityRequirementObject[] => {
if ("or" in container) {
return container.or.map(
(entry): SecurityRequirementObject =>
"and" in entry
? mergeAll(map(({ name, scopes }) => objOf(name, scopes), entry.and))
: { [entry.name]: entry.scopes },
);
}
if ("and" in container) return depictSecurityRefs(andToOr(container));
return depictSecurityRefs({ or: [container] });
};

export const depictBody = ({
method,
path,
Expand Down
68 changes: 1 addition & 67 deletions src/logical-container.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { chain } from "ramda";
import { combinations, isObject } from "./common-helpers";
import { isObject } from "./common-helpers";

type LogicalOr<T> = { or: T[] };
type LogicalAnd<T> = { and: T[] };
Expand All @@ -15,68 +14,3 @@ export const isLogicalOr = (subject: unknown): subject is LogicalOr<unknown> =>
export const isLogicalAnd = (
subject: unknown,
): subject is LogicalAnd<unknown> => isObject(subject) && "and" in subject;

/** @desc combines several LogicalAnds into a one */
const flattenAnds = <T>(subject: (T | LogicalAnd<T>)[]): LogicalAnd<T> => ({
and: chain((item) => (isLogicalAnd(item) ? item.and : [item]), subject),
});

/** @desc creates a LogicalContainer out of another one */
export const mapLogicalContainer = <T, S>(
container: LogicalContainer<T>,
fn: (subject: T) => S,
): LogicalContainer<S> => {
if (isLogicalAnd(container)) {
return {
and: container.and.map((entry) =>
isLogicalOr(entry) ? { or: entry.or.map(fn) } : fn(entry),
),
};
}
if (isLogicalOr(container)) {
return {
or: container.or.map((entry) =>
isLogicalAnd(entry) ? { and: entry.and.map(fn) } : fn(entry),
),
};
}
return fn(container);
};

/** @desc converts LogicalAnd into LogicalOr */
export const andToOr = <T>(
subject: LogicalAnd<T | LogicalOr<T>>,
): LogicalOr<T | LogicalAnd<T>> =>
subject.and.reduce<LogicalOr<T | LogicalAnd<T>>>(
(acc, item) => ({
or: combinations(
acc.or,
isLogicalOr(item) ? item.or : [item],
flattenAnds,
),
}),
{ or: [] },
);

/** @desc reducer, combines two LogicalContainers */
export const combineContainers = <T>(
left: LogicalContainer<T>,
right: LogicalContainer<T>,
): LogicalContainer<T> => {
if (isLogicalAnd(left)) {
if (isLogicalOr(right)) return combineContainers(andToOr(left), right);
return flattenAnds([left, right]);
}

if (isLogicalOr(left)) {
if (isLogicalAnd(right)) return combineContainers(right, left);
if (isLogicalOr(right))
return { or: combinations(left.or, right.or, flattenAnds) };
return combineContainers(left, { and: [right] });
}

if (isLogicalAnd(right) || isLogicalOr(right))
return combineContainers(right, left);

return { and: [left, right] };
};
183 changes: 0 additions & 183 deletions tests/unit/documentation-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import {
depictReadonly,
depictRecord,
depictRequestParams,
depictSecurity,
depictSecurityRefs,
depictString,
depictTags,
depictTuple,
Expand Down Expand Up @@ -784,187 +782,6 @@ describe("Documentation helpers", () => {
);
});

describe("depictSecurity()", () => {
test("should handle Basic, Bearer and CustomHeader Securities", () => {
expect(
depictSecurity({
or: [
{ and: [{ type: "basic" }, { type: "bearer" }] },
{ type: "header", name: "X-Key" },
],
}),
).toMatchSnapshot();
});
test("should handle Input and Cookie Securities", () => {
expect(
depictSecurity({
and: [
{
or: [
{ type: "input", name: "apiKey" },
{ type: "cookie", name: "hash" },
],
},
],
}),
).toMatchSnapshot();
});
test.each([
{ variant: "alternative", inputSources: ["query", "body"] as const },
{ variant: "actual", inputSources: ["body", "files"] as const },
])(
`should inform on $variant placement of the input security parameter`,
({ inputSources }) => {
expect(
depictSecurity(
{ type: "input", name: "key" },
Array.from(inputSources),
),
).toMatchSnapshot();
},
);
test("should handle OpenID and OAuth2 Securities", () => {
expect(
depictSecurity({
or: [{ type: "openid", url: "https://test.url" }, { type: "oauth2" }],
}),
).toMatchSnapshot();
});
test("should depict OAuth2 Security with flows", () => {
expect(
depictSecurity({
type: "oauth2",
flows: {
implicit: {
authorizationUrl: "https://test.url",
refreshUrl: "https://test2.url",
scopes: {
read: "read something",
write: "write something",
},
},
authorizationCode: {
authorizationUrl: "https://test.url",
refreshUrl: "https://test2.url",
tokenUrl: "https://test3.url",
scopes: {
read: "read something",
write: "write something",
},
},
clientCredentials: {
refreshUrl: "https://test2.url",
tokenUrl: "https://test3.url",
scopes: {
read: "read something",
write: "write something",
},
},
password: {
refreshUrl: "https://test2.url",
tokenUrl: "https://test3.url",
scopes: {
read: "read something",
write: "write something",
},
},
},
}),
).toMatchSnapshot();
});
test("should handle undefined flows", () => {
expect(
depictSecurity({
type: "oauth2",
flows: {
implicit: undefined,
password: undefined,
},
}),
).toMatchSnapshot();
});
test("should add scopes when missing", () => {
expect(
depictSecurity({
type: "oauth2",
flows: {
password: {
tokenUrl: "https://test.url",
},
},
}),
).toMatchSnapshot();
});
});

describe("depictSecurityRefs()", () => {
test("should handle LogicalAnd", () => {
expect(
depictSecurityRefs({
and: [
{ name: "A", scopes: [] },
{ name: "B", scopes: [] },
{ name: "C", scopes: [] },
],
}),
).toMatchSnapshot();
expect(
depictSecurityRefs({
and: [
{ name: "A", scopes: [] },
{
or: [
{ name: "B", scopes: [] },
{ name: "C", scopes: [] },
],
},
],
}),
).toMatchSnapshot();
});

test("should handle LogicalOr", () => {
expect(
depictSecurityRefs({
or: [
{ name: "A", scopes: [] },
{ name: "B", scopes: [] },
{ name: "C", scopes: [] },
],
}),
).toMatchSnapshot();
expect(
depictSecurityRefs({
or: [
{ name: "A", scopes: [] },
{
and: [
{ name: "B", scopes: [] },
{ name: "C", scopes: [] },
],
},
],
}),
).toMatchSnapshot();
});

test("should handle the plain value", () => {
expect(depictSecurityRefs({ name: "A", scopes: [] })).toMatchSnapshot();
});

test("should populate the scopes", () => {
expect(
depictSecurityRefs({
or: [
{ name: "A", scopes: ["write"] },
{ name: "B", scopes: ["read"] },
{ name: "C", scopes: ["read", "write"] },
],
}),
).toMatchSnapshot();
});
});

describe("depictTags()", () => {
test("should accept plain descriptions", () => {
expect(
Expand Down
Loading

0 comments on commit 1cfc93b

Please sign in to comment.