Skip to content

Commit

Permalink
Break up request adjudicator tests to separate file for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
samayer12 committed Nov 12, 2024
1 parent 19759e4 commit f78935b
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 155 deletions.
156 changes: 1 addition & 155 deletions src/lib/filter/adjudicators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { expect, describe, it } from "@jest/globals";
import * as sut from "./adjudicators";
import { GroupVersionKind, KubernetesObject } from "kubernetes-fluent-client";
import { KubernetesObject } from "kubernetes-fluent-client";
import { AdmissionRequest, Binding, DeepPartial } from "../types";
import { Event, Operation } from "../enums";

Expand Down Expand Up @@ -691,37 +691,6 @@ describe("operationMatchesEvent", () => {
});
});

describe("declaredOperation", () => {
const defaultAdmissionRequest = {
uid: "some-uid",
kind: { kind: "a-kind", group: "a-group" },
resource: { group: "some-group", version: "some-version", resource: "some-resource" },
operation: undefined,
name: "some-name",
userInfo: {},
object: {},
};
//[ AdmissionRequest, result ]
it.each([
[{}, ""],
[{ operation: null }, ""],
[{ operation: "" }, ""],
[{ operation: "operation" }, "operation"],
[{ operation: Operation.CONNECT }, Operation.CONNECT],
[{ operation: Operation.CREATE }, Operation.CREATE],
[{ operation: Operation.UPDATE }, Operation.UPDATE],
[{ operation: Operation.DELETE }, Operation.DELETE],
])("given %j, returns '%s'", (given, expected) => {
const request = {
...defaultAdmissionRequest,
operation: ("operation" in given ? given.operation : (undefined as unknown as string)) as Operation,
};
const result = sut.declaredOperation(request);

expect(result).toEqual(expected);
});
});

describe("mismatchedEvent", () => {
//[ Binding, AdmissionRequest, result ]
it.each([
Expand Down Expand Up @@ -798,38 +767,6 @@ describe("definesGroup", () => {
});
});

describe("declaredGroup", () => {
const defaultAdmissionRequest = {
uid: "some-uid",
kind: undefined,
group: "a-group",
resource: { group: "some-group", version: "some-version", resource: "some-resource" },
operation: Operation.CONNECT,
name: "some-name",
userInfo: {},
object: {},
};

//[ AdmissionRequest, result ]
it.each([
[{}, ""],
[{ kind: null }, ""],
[{ kind: {} }, ""],
[{ kind: { group: null } }, ""],
[{ kind: { group: "" } }, ""],
[{ kind: { group: "group" } }, "group"],
])("given %j, returns '%s'", (given, expected) => {
const request = {
...defaultAdmissionRequest,
kind: ("kind" in given ? given.kind : (undefined as unknown as string)) as GroupVersionKind,
};

const result = sut.declaredGroup(request);

expect(result).toEqual(expected);
});
});

describe("mismatchedGroup", () => {
//[ Binding, AdmissionRequest, result ]
it.each([
Expand Down Expand Up @@ -884,37 +821,6 @@ describe("definesVersion", () => {
});
});

describe("declaredVersion", () => {
const defaultAdmissionRequest = {
uid: "some-uid",
kind: undefined,
group: "a-group",
resource: { group: "some-group", version: "some-version", resource: "some-resource" },
operation: Operation.CONNECT,
name: "some-name",
userInfo: {},
object: {},
};
//[ AdmissionRequest, result ]
it.each([
[{}, ""],
[{ kind: null }, ""],
[{ kind: {} }, ""],
[{ kind: { version: null } }, ""],
[{ kind: { version: "" } }, ""],
[{ kind: { version: "version" } }, "version"],
])("given %j, returns '%s'", (given, expected) => {
const request = {
...defaultAdmissionRequest,
kind: ("kind" in given ? given.kind : (undefined as unknown as string)) as GroupVersionKind,
};

const result = sut.declaredVersion(request);

expect(result).toEqual(expected);
});
});

describe("mismatchedVersion", () => {
//[ Binding, AdmissionRequest, result ]
it.each([
Expand Down Expand Up @@ -969,38 +875,6 @@ describe("definesKind", () => {
});
});

describe("declaredKind", () => {
const defaultAdmissionRequest = {
uid: "some-uid",
kind: undefined,
group: "a-group",
resource: { group: "some-group", version: "some-version", resource: "some-resource" },
operation: Operation.CONNECT,
name: "some-name",
userInfo: {},
object: {},
};

//[ AdmissionRequest, result ]
it.each([
[{}, ""],
[{ kind: null }, ""],
[{ kind: {} }, ""],
[{ kind: { kind: null } }, ""],
[{ kind: { kind: "" } }, ""],
[{ kind: { kind: "kind" } }, "kind"],
])("given %j, returns '%s'", (given, expected) => {
const request = {
...defaultAdmissionRequest,
kind: ("kind" in given ? given.kind : (undefined as unknown as string)) as GroupVersionKind,
};

const result = sut.declaredKind(request);

expect(result).toEqual(expected);
});
});

describe("mismatchedKind", () => {
//[ Binding, AdmissionRequest, result ]
it.each([
Expand Down Expand Up @@ -1079,31 +953,3 @@ describe("definedCallbackName", () => {
expect(result).toEqual(expected);
});
});

describe("declaredUid", () => {
const defaultAdmissionRequest = {
uid: undefined,
kind: { kind: "a-kind", group: "a-group" },
group: "a-group",
resource: { group: "some-group", version: "some-version", resource: "some-resource" },
operation: Operation.CONNECT,
name: "some-name",
userInfo: {},
object: {},
};
//[ AdmissionRequest, result ]
it.each([
[{}, ""],
[{ uid: null }, ""],
[{ uid: "uid" }, "uid"],
])("given %j, returns '%s'", (given, expected) => {
const request = {
...defaultAdmissionRequest,
uid: ("uid" in given ? given.uid : (undefined as unknown as string)) as string,
};

const result = sut.declaredUid(request);

expect(result).toEqual(expected);
});
});
160 changes: 160 additions & 0 deletions src/lib/filter/adjudicators/requestAdjudicators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors

import { expect, describe, it } from "@jest/globals";
import * as sut from "../adjudicators";
import { Operation } from "../../enums";
import { GroupVersionKind } from "kubernetes-fluent-client";

describe("declaredUid", () => {
const defaultAdmissionRequest = {
uid: undefined,
kind: { kind: "a-kind", group: "a-group" },
group: "a-group",
resource: { group: "some-group", version: "some-version", resource: "some-resource" },
operation: Operation.CONNECT,
name: "some-name",
userInfo: {},
object: {},
};
//[ AdmissionRequest, result ]
it.each([
[{}, ""],
[{ uid: null }, ""],
[{ uid: "uid" }, "uid"],
])("given %j, returns '%s'", (given, expected) => {
const request = {
...defaultAdmissionRequest,
uid: ("uid" in given ? given.uid : (undefined as unknown as string)) as string,
};

const result = sut.declaredUid(request);

expect(result).toEqual(expected);
});
});

describe("declaredKind", () => {
const defaultAdmissionRequest = {
uid: "some-uid",
kind: undefined,
group: "a-group",
resource: { group: "some-group", version: "some-version", resource: "some-resource" },
operation: Operation.CONNECT,
name: "some-name",
userInfo: {},
object: {},
};
//[ AdmissionRequest, result ]
it.each([
[{}, ""],
[{ kind: null }, ""],
[{ kind: {} }, ""],
[{ kind: { kind: null } }, ""],
[{ kind: { kind: "" } }, ""],
[{ kind: { kind: "kind" } }, "kind"],
])("given %j, returns '%s'", (given, expected) => {
const request = {
...defaultAdmissionRequest,
kind: ("kind" in given ? given.kind : (undefined as unknown as string)) as GroupVersionKind,
};

const result = sut.declaredKind(request);

expect(result).toEqual(expected);
});
});

describe("declaredVersion", () => {
const defaultAdmissionRequest = {
uid: "some-uid",
kind: undefined,
group: "a-group",
resource: { group: "some-group", version: "some-version", resource: "some-resource" },
operation: Operation.CONNECT,
name: "some-name",
userInfo: {},
object: {},
};
//[ AdmissionRequest, result ]
it.each([
[{}, ""],
[{ kind: null }, ""],
[{ kind: {} }, ""],
[{ kind: { version: null } }, ""],
[{ kind: { version: "" } }, ""],
[{ kind: { version: "version" } }, "version"],
])("given %j, returns '%s'", (given, expected) => {
const request = {
...defaultAdmissionRequest,
kind: ("kind" in given ? given.kind : (undefined as unknown as string)) as GroupVersionKind,
};

const result = sut.declaredVersion(request);

expect(result).toEqual(expected);
});
});

describe("declaredGroup", () => {
const defaultAdmissionRequest = {
uid: "some-uid",
kind: undefined,
group: "a-group",
resource: { group: "some-group", version: "some-version", resource: "some-resource" },
operation: Operation.CONNECT,
name: "some-name",
userInfo: {},
object: {},
};
//[ AdmissionRequest, result ]
it.each([
[{}, ""],
[{ kind: null }, ""],
[{ kind: {} }, ""],
[{ kind: { group: null } }, ""],
[{ kind: { group: "" } }, ""],
[{ kind: { group: "group" } }, "group"],
])("given %j, returns '%s'", (given, expected) => {
const request = {
...defaultAdmissionRequest,
kind: ("kind" in given ? given.kind : (undefined as unknown as string)) as GroupVersionKind,
};

const result = sut.declaredGroup(request);

expect(result).toEqual(expected);
});
});

describe("declaredOperation", () => {
const defaultAdmissionRequest = {
uid: "some-uid",
kind: { kind: "a-kind", group: "a-group" },
resource: { group: "some-group", version: "some-version", resource: "some-resource" },
operation: undefined,
name: "some-name",
userInfo: {},
object: {},
};
//[ AdmissionRequest, result ]
it.each([
[{}, ""],
[{ operation: null }, ""],
[{ operation: "" }, ""],
[{ operation: "operation" }, "operation"],
[{ operation: Operation.CONNECT }, Operation.CONNECT],
[{ operation: Operation.CREATE }, Operation.CREATE],
[{ operation: Operation.UPDATE }, Operation.UPDATE],
[{ operation: Operation.DELETE }, Operation.DELETE],
])("given %j, returns '%s'", (given, expected) => {
const request = {
...defaultAdmissionRequest,
operation: ("operation" in given ? given.operation : (undefined as unknown as string)) as Operation,
};

const result = sut.declaredOperation(request);

expect(result).toEqual(expected);
});
});

0 comments on commit f78935b

Please sign in to comment.