|
| 1 | +const { setupAuth, executeStackql } = require("../utils"); |
| 2 | +const childProcess = require("child_process"); |
| 3 | + |
| 4 | +jest.mock("child_process", () => ({ |
| 5 | + exec: jest.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +describe("util", () => { |
| 9 | + let core; |
| 10 | + let exec; |
| 11 | + const expectedAuth = |
| 12 | + '{ "google": { "type": "service_account", "credentialsfilepath": "sa-key.json" }}'; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + core = { |
| 16 | + setFailed: jest.fn().mockImplementation((message) => { |
| 17 | + console.error(message); |
| 18 | + }), |
| 19 | + info: jest.fn().mockImplementation((message) => { |
| 20 | + console.log(message); |
| 21 | + }), |
| 22 | + exportVariable: jest.fn(), |
| 23 | + error: jest.fn().mockImplementation((message) => { |
| 24 | + console.error(message); |
| 25 | + }), |
| 26 | + }; |
| 27 | + }); |
| 28 | + |
| 29 | + describe("setupAuth", () => { |
| 30 | + let AUTH_ENV = { |
| 31 | + AUTH_STR: expectedAuth, |
| 32 | + AUTH_FILE_PATH: "./lib/tests/test-auth.json", |
| 33 | + }; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + jest.resetModules(); |
| 37 | + process.env = { ...AUTH_ENV }; |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + process.env = AUTH_ENV; |
| 42 | + }); |
| 43 | + |
| 44 | + it("should throw error when neither AUTH_STR or AUTH_FILE_PATH is set", () => { |
| 45 | + process.env.AUTH_STR = undefined; |
| 46 | + process.env.AUTH_FILE_PATH = undefined; |
| 47 | + |
| 48 | + setupAuth(core); |
| 49 | + expect(core.setFailed).toBeCalledWith( |
| 50 | + "Either AUTH_FILE_PATH or AUTH_STR must be set." |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should set AUTH environment variable when AUTH_STR is set", () => { |
| 55 | + process.env.AUTH_FILE_PATH = undefined; |
| 56 | + |
| 57 | + setupAuth(core); |
| 58 | + |
| 59 | + expect(core.exportVariable).toBeCalledWith("AUTH", expectedAuth); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should set AUTH environment variable when AUTH_FILE_PATH is set", () => { |
| 63 | + process.env.AUTH_STR = undefined; |
| 64 | + |
| 65 | + setupAuth(core); |
| 66 | + |
| 67 | + expect(core.exportVariable).toBeCalledWith("AUTH", expectedAuth); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should throw error when AUTH_FILE_PATH is set but file does not exist", () => { |
| 71 | + process.env.AUTH_STR = undefined; |
| 72 | + process.env.AUTH_FILE_PATH = "./failed-test-auth.json"; |
| 73 | + |
| 74 | + setupAuth(core); |
| 75 | + expect(core.setFailed).toBeCalledWith( |
| 76 | + `Cannot find auth file ${process.env.AUTH_FILE_PATH}` |
| 77 | + ); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("executeStackql", () => { |
| 82 | + const EXECUTE_ENV = { |
| 83 | + QUERY: "test", |
| 84 | + QUERY_FILE_PATH: "test-query.json", |
| 85 | + AUTH: "test-auth", |
| 86 | + }; |
| 87 | + |
| 88 | + beforeEach(() => { |
| 89 | + jest.resetModules(); |
| 90 | + process.env = { ...EXECUTE_ENV }; |
| 91 | + }); |
| 92 | + |
| 93 | + afterEach(() => { |
| 94 | + process.env = EXECUTE_ENV; |
| 95 | + jest.clearAllMocks(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should return error when there is neither query or query file path", async () => { |
| 99 | + process.env = { ...EXECUTE_ENV }; |
| 100 | + process.env.QUERY = undefined; |
| 101 | + process.env.QUERY_FILE_PATH = undefined; |
| 102 | + |
| 103 | + await executeStackql(core); |
| 104 | + |
| 105 | + expect(core.setFailed).toBeCalledWith( |
| 106 | + "Either query or query_file_path need to be set" |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should return error when there is no AUTH", async () => { |
| 111 | + process.env = { ...EXECUTE_ENV }; |
| 112 | + process.env.AUTH = undefined; |
| 113 | + |
| 114 | + await executeStackql(core); |
| 115 | + |
| 116 | + expect(core.setFailed).toHaveBeenCalledWith( |
| 117 | + "Cannot find AUTH environment variable when executing stackql" |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should execute stackql with query file path", async () => { |
| 122 | + process.env = { ...EXECUTE_ENV }; |
| 123 | + process.env.QUERY = undefined; |
| 124 | + |
| 125 | + await executeStackql(core); |
| 126 | + |
| 127 | + expect(childProcess.exec).toHaveBeenCalledWith( |
| 128 | + "stackql exec -i test-query.json --auth=test-auth --output=json" |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it("should execute stackql with query", async () => { |
| 133 | + process.env = { ...EXECUTE_ENV }; |
| 134 | + process.env.QUERY_FILE_PATH = undefined; |
| 135 | + |
| 136 | + await executeStackql(core); |
| 137 | + |
| 138 | + expect(childProcess.exec).toHaveBeenCalledWith( |
| 139 | + "stackql exec test --auth=test-auth --output=json" |
| 140 | + ); |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments