Skip to content

Commit

Permalink
style: code format
Browse files Browse the repository at this point in the history
  • Loading branch information
bxb100 committed Dec 19, 2024
1 parent d38d73e commit 41de4f1
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 398 deletions.
148 changes: 79 additions & 69 deletions __tests__/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,89 @@ import { FullItem } from "@1password/connect/dist/model/fullItem";
import { Connect } from "../src/auth/connect";

describe("test connect with different secret refs", () => {
// eslint-disable-next-line no-restricted-syntax
const host = "http://localhost:8080";
const token = "token";
// eslint-disable-next-line no-restricted-syntax
const host = "http://localhost:8080";
const token = "token";

beforeEach(() => {
jest.spyOn(OPConnect.prototype, "getVault").mockResolvedValue({
id: "vault",
});
jest.spyOn(OPConnect.prototype, "getItem").mockResolvedValue({
id: "item",
extractOTP: jest.fn(),
category: FullItem.CategoryEnum.Password,
vault: {
id: "vault_id",
},
fields: [
{
id: "filed_id_0",
label: "text",
value: "filed_id_0_value"
},
{
id: "filed_id_1",
label: "text",
value: "filed_id_1_value",
section: {
id: "section_id_1",
},
},
{
id: "filed_id_2",
label: "text",
value: "filed_id_2_value",
section: {
id: "section_id_2",
},
},
],
sections: [
{
id: "section_id_1",
label: "duplicate",
},
{
id: "section_id_2",
label: "duplicate",
},
],
});
});
beforeEach(() => {
jest.spyOn(OPConnect.prototype, "getVault").mockResolvedValue({
id: "vault",
});
jest.spyOn(OPConnect.prototype, "getItem").mockResolvedValue({
id: "item",
extractOTP: jest.fn(),

Check warning on line 17 in __tests__/connect.test.ts

View workflow job for this annotation

GitHub Actions / lint

Object Literal Property name `extractOTP` must match one of the following formats: strictCamelCase
category: FullItem.CategoryEnum.Password,
vault: {
id: "vault_id",
},
fields: [
{
id: "filed_id_0",
label: "text",
value: "filed_id_0_value",
},
{
id: "filed_id_1",
label: "text",
value: "filed_id_1_value",
section: {
id: "section_id_1",
},
},
{
id: "filed_id_2",
label: "text",
value: "filed_id_2_value",
section: {
id: "section_id_2",
},
},
],
sections: [
{
id: "section_id_1",
label: "duplicate",
},
{
id: "section_id_2",
label: "duplicate",
},
],
});
});

it("filed section not exist", async () => {
const connect = new Connect(host, token);
await expect(connect.resolve("op://vault/item/not_exist/text"),).rejects.toThrow("The item does not have a field 'not_exist.text'");
});
it("filed section not exist", async () => {
const connect = new Connect(host, token);
await expect(
connect.resolve("op://vault/item/not_exist/text"),
).rejects.toThrow("The item does not have a field 'not_exist.text'");
});

it("filed not exist", async () => {
const connect = new Connect(host, token);
await expect(connect.resolve("op://vault/item/duplicate/not_exist"),).rejects.toThrow("The item does not have a field 'duplicate.not_exist'");
});
it("filed not exist", async () => {
const connect = new Connect(host, token);
await expect(
connect.resolve("op://vault/item/duplicate/not_exist"),
).rejects.toThrow("The item does not have a field 'duplicate.not_exist'");
});

it("filed duplicate", async () => {
const connect = new Connect(host, token);
await expect(connect.resolve("op://vault/item/duplicate/text"),).rejects.toThrow("The item has more than one 'duplicate.text' field");
});
it("filed duplicate", async () => {
const connect = new Connect(host, token);
await expect(
connect.resolve("op://vault/item/duplicate/text"),
).rejects.toThrow("The item has more than one 'duplicate.text' field");
});

it("duplicate section and with unique id", async () => {
const connect = new Connect(host, token);
await expect(connect.resolve("op://vault/item/duplicate/filed_id_1")).resolves.toBe("filed_id_1_value");
});
it("duplicate section and with unique id", async () => {
const connect = new Connect(host, token);
await expect(
connect.resolve("op://vault/item/duplicate/filed_id_1"),
).resolves.toBe("filed_id_1_value");
});

it("without section", async () => {
const connect = new Connect(host, token);
await expect(connect.resolve("op://vault/item/filed_id_0")).resolves.toBe("filed_id_0_value");
})
it("without section", async () => {
const connect = new Connect(host, token);
await expect(connect.resolve("op://vault/item/filed_id_0")).resolves.toBe(
"filed_id_0_value",
);
});
});
8 changes: 4 additions & 4 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import * as main from "../src/main";
const runMock = jest.spyOn(main, "run").mockImplementation();

describe("index", () => {
it("calls run when imported", async () => {
await import("../src/index");
it("calls run when imported", async () => {
await import("../src/index");

expect(runMock).toHaveBeenCalled();
});
expect(runMock).toHaveBeenCalled();
});
});
208 changes: 106 additions & 102 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,106 +8,110 @@ const runMock = jest.spyOn(main, "run");
jest.mock("@actions/core");

describe("test main action", () => {
const OLD_ENV = process.env;

beforeEach(() => {
process.env = {
OP_MANAGED_VARIABLES: "OTHER_TEST_KEY",
TEST: "op://dev/test/test-key",
};
});

afterEach(() => {
process.env = {};
});

afterAll(() => {
process.env = OLD_ENV;
});

const commonCheck = (): void => {
expect(runMock).toHaveReturned();
expect(core.error).not.toHaveBeenCalled();

// Verify that all the core library functions were called correctly
expect(core.info).toHaveBeenNthCalledWith(
1,
"Unsetting previous values ...",
);
expect(core.info).toHaveBeenNthCalledWith(2, "Unsetting OTHER_TEST_KEY");
expect(core.exportVariable).toHaveBeenNthCalledWith(1, "OTHER_TEST_KEY", "");

expect(core.setOutput).toHaveBeenNthCalledWith(1, "TEST", "test-secret");
};

it("test service account", async () => {
process.env.OP_SERVICE_ACCOUNT_TOKEN = "test-token";

jest.spyOn(core, "getBooleanInput").mockImplementation((name) => {
switch (name) {
case "unset-previous":
return true;
case "export-env":
return false;
default:
return false;
}
});

const opMock: jest.SpiedFunction<typeof op.createClient> = jest
.spyOn(op, "createClient")
.mockResolvedValue({
secrets: {
resolve: jest.fn().mockResolvedValue("test-secret"),
},
items: {
get: jest.fn().mockImplementation(),
create: jest.fn().mockImplementation(),
put: jest.fn().mockImplementation(),
delete: jest.fn().mockImplementation(),
listAll: jest.fn().mockImplementation(),
},
vaults: {
listAll: jest.fn().mockImplementation(),
},
});

await main.run();
expect(opMock).toHaveReturned();

commonCheck();
});

it("test connect", async () => {
process.env = {
...process.env,
OP_CONNECT_HOST: "test-host",
OP_CONNECT_TOKEN: "test-token",
};

jest.spyOn(core, "getBooleanInput").mockImplementation((name) => {
switch (name) {
case "unset-previous":
return true;
case "export-env":
return false;
default:
return false;
}
});

jest.spyOn(connect, "OnePasswordConnect").mockImplementation(
jest.fn().mockImplementation(() => ({
getVault: jest.fn().mockResolvedValue({ id: "dev" }),
getItem: jest.fn().mockResolvedValue({
id: "test",
fields: [{ id: "test-key", value: "test-secret" }],
}),
})),
);

await main.run();

commonCheck();
});
const OLD_ENV = process.env;

beforeEach(() => {
process.env = {
OP_MANAGED_VARIABLES: "OTHER_TEST_KEY",

Check warning on line 15 in __tests__/main.test.ts

View workflow job for this annotation

GitHub Actions / lint

Object Literal Property name `OP_MANAGED_VARIABLES` must match one of the following formats: strictCamelCase
TEST: "op://dev/test/test-key",

Check warning on line 16 in __tests__/main.test.ts

View workflow job for this annotation

GitHub Actions / lint

Object Literal Property name `TEST` must match one of the following formats: strictCamelCase
};
});

afterEach(() => {
process.env = {};
});

afterAll(() => {
process.env = OLD_ENV;
});

const commonCheck = (): void => {
expect(runMock).toHaveReturned();
expect(core.error).not.toHaveBeenCalled();

// Verify that all the core library functions were called correctly
expect(core.info).toHaveBeenNthCalledWith(
1,
"Unsetting previous values ...",
);
expect(core.info).toHaveBeenNthCalledWith(2, "Unsetting OTHER_TEST_KEY");
expect(core.exportVariable).toHaveBeenNthCalledWith(
1,
"OTHER_TEST_KEY",
"",
);

expect(core.setOutput).toHaveBeenNthCalledWith(1, "TEST", "test-secret");
};

it("test service account", async () => {
process.env.OP_SERVICE_ACCOUNT_TOKEN = "test-token";

jest.spyOn(core, "getBooleanInput").mockImplementation((name) => {
switch (name) {
case "unset-previous":
return true;
case "export-env":
return false;
default:
return false;
}
});

const opMock: jest.SpiedFunction<typeof op.createClient> = jest
.spyOn(op, "createClient")
.mockResolvedValue({
secrets: {
resolve: jest.fn().mockResolvedValue("test-secret"),
},
items: {
get: jest.fn().mockImplementation(),
create: jest.fn().mockImplementation(),
put: jest.fn().mockImplementation(),
delete: jest.fn().mockImplementation(),
listAll: jest.fn().mockImplementation(),
},
vaults: {
listAll: jest.fn().mockImplementation(),
},
});

await main.run();
expect(opMock).toHaveReturned();

commonCheck();
});

it("test connect", async () => {
process.env = {
...process.env,
OP_CONNECT_HOST: "test-host",

Check warning on line 88 in __tests__/main.test.ts

View workflow job for this annotation

GitHub Actions / lint

Object Literal Property name `OP_CONNECT_HOST` must match one of the following formats: strictCamelCase
OP_CONNECT_TOKEN: "test-token",

Check warning on line 89 in __tests__/main.test.ts

View workflow job for this annotation

GitHub Actions / lint

Object Literal Property name `OP_CONNECT_TOKEN` must match one of the following formats: strictCamelCase
};

jest.spyOn(core, "getBooleanInput").mockImplementation((name) => {
switch (name) {
case "unset-previous":
return true;
case "export-env":
return false;
default:
return false;
}
});

jest.spyOn(connect, "OnePasswordConnect").mockImplementation(
jest.fn().mockImplementation(() => ({
getVault: jest.fn().mockResolvedValue({ id: "dev" }),
getItem: jest.fn().mockResolvedValue({
id: "test",
fields: [{ id: "test-key", value: "test-secret" }],
}),
})),
);

await main.run();

commonCheck();
});
});
Loading

0 comments on commit 41de4f1

Please sign in to comment.