|
| 1 | +import { setupServer, SetupServerApi } from "msw/node"; |
| 2 | +import { rest } from "msw"; |
| 3 | +import { ConnectionOptions, Firebolt } from "../../../src"; |
| 4 | + |
| 5 | +const apiEndpoint = "fake.api.com"; |
| 6 | +const accountName = "my_account"; |
| 7 | + |
| 8 | +const engineObject = { |
| 9 | + id: { |
| 10 | + engine_id: "123", |
| 11 | + account_id: "some_account" |
| 12 | + }, |
| 13 | + name: "some_engine", |
| 14 | + endpoint: "https://some_engine.com" |
| 15 | +}; |
| 16 | + |
| 17 | +const selectOneResponse = { |
| 18 | + meta: [ |
| 19 | + { |
| 20 | + name: "one", |
| 21 | + type: "Int32" |
| 22 | + } |
| 23 | + ], |
| 24 | + data: [ |
| 25 | + { |
| 26 | + one: 1 |
| 27 | + } |
| 28 | + ], |
| 29 | + rows: 1 |
| 30 | +}; |
| 31 | + |
| 32 | +const defaultAccountResponse = { |
| 33 | + account: { |
| 34 | + id: "some_other_account", |
| 35 | + name: "some_other_account_name", |
| 36 | + title: "some_other_account_name" |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +function resetServerHandlers(server: SetupServerApi) { |
| 41 | + server.use( |
| 42 | + // Authentication |
| 43 | + rest.post(`https://${apiEndpoint}/auth/v1/login`, (req, res, ctx) => { |
| 44 | + return res( |
| 45 | + ctx.json({ |
| 46 | + access_token: "fake_access_token", |
| 47 | + refresh_token: "fake_refresh_token" |
| 48 | + }) |
| 49 | + ); |
| 50 | + }), |
| 51 | + // Get account id |
| 52 | + rest.get( |
| 53 | + `https://${apiEndpoint}/iam/v2/accounts:getIdByName`, |
| 54 | + (req, res, ctx) => { |
| 55 | + const queryParam = req.url.searchParams.get("account_name"); |
| 56 | + expect(queryParam).toBe(accountName); |
| 57 | + return res( |
| 58 | + ctx.json({ |
| 59 | + account_id: "some_account" |
| 60 | + }) |
| 61 | + ); |
| 62 | + } |
| 63 | + ), |
| 64 | + // Get default account |
| 65 | + rest.get(`https://${apiEndpoint}/iam/v2/account`, (req, res, ctx) => { |
| 66 | + return res(ctx.json(defaultAccountResponse)); |
| 67 | + }), |
| 68 | + // Get engine id by name |
| 69 | + rest.get( |
| 70 | + `https://${apiEndpoint}/core/v1/accounts/some_account/engines:getIdByName`, |
| 71 | + (req, res, ctx) => { |
| 72 | + return res(ctx.json({ engine_id: engineObject.id })); |
| 73 | + } |
| 74 | + ), |
| 75 | + // Default account engine id by name |
| 76 | + rest.get( |
| 77 | + `https://${apiEndpoint}/core/v1/accounts/some_other_account/engines:getIdByName`, |
| 78 | + (req, res, ctx) => { |
| 79 | + return res(ctx.json({ engine_id: engineObject.id })); |
| 80 | + } |
| 81 | + ), |
| 82 | + // Get engine by id |
| 83 | + rest.get( |
| 84 | + `https://${apiEndpoint}/core/v1/accounts/some_account/engines/123`, |
| 85 | + (req, res, ctx) => { |
| 86 | + return res(ctx.json({ engine: engineObject })); |
| 87 | + } |
| 88 | + ), |
| 89 | + // Default get engine by id |
| 90 | + rest.get( |
| 91 | + `https://${apiEndpoint}/core/v1/accounts/some_other_account/engines/123`, |
| 92 | + (req, res, ctx) => { |
| 93 | + return res(ctx.json({ engine: engineObject })); |
| 94 | + } |
| 95 | + ), |
| 96 | + // Respond to select 1 |
| 97 | + rest.post(`https://some_engine.com/`, (req, res, ctx) => { |
| 98 | + return res(ctx.json(selectOneResponse)); |
| 99 | + }) |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +describe("Connection v1", () => { |
| 104 | + const server = setupServer(); |
| 105 | + beforeAll(() => { |
| 106 | + server.listen(); |
| 107 | + }); |
| 108 | + afterAll(() => { |
| 109 | + server.close(); |
| 110 | + }); |
| 111 | + beforeEach(() => { |
| 112 | + resetServerHandlers(server); |
| 113 | + }); |
| 114 | + |
| 115 | + it("Can connect", async () => { |
| 116 | + const firebolt = Firebolt({ apiEndpoint }); |
| 117 | + |
| 118 | + const connectionParams: ConnectionOptions = { |
| 119 | + auth: { |
| 120 | + username: "user", |
| 121 | + password: "pass" |
| 122 | + }, |
| 123 | + database: "dummy", |
| 124 | + engineName: "dummy", |
| 125 | + account: accountName |
| 126 | + }; |
| 127 | + |
| 128 | + const connection = await firebolt.connect(connectionParams); |
| 129 | + await connection.execute("SELECT 1"); |
| 130 | + expect(connection.engineEndpoint).toBe(engineObject.endpoint); |
| 131 | + const account_info = await connection.resolveAccountInfo(); |
| 132 | + expect(account_info.id).toBe("some_account"); |
| 133 | + expect(account_info.infraVersion).toBe(1); |
| 134 | + }); |
| 135 | + it("Can run set statements", async () => { |
| 136 | + const param = "my_var"; |
| 137 | + const value = "1"; |
| 138 | + server.use( |
| 139 | + // Verify that the parameter is set when querying the engine |
| 140 | + rest.post(`https://some_engine.com/`, (req, res, ctx) => { |
| 141 | + const myParam = req.url.searchParams.get(param); |
| 142 | + expect(myParam).toBe(value); |
| 143 | + return res(ctx.json(selectOneResponse)); |
| 144 | + }) |
| 145 | + ); |
| 146 | + |
| 147 | + const firebolt = Firebolt({ apiEndpoint }); |
| 148 | + const connectionParams: ConnectionOptions = { |
| 149 | + auth: { |
| 150 | + username: "user", |
| 151 | + password: "pass" |
| 152 | + }, |
| 153 | + database: "dummy", |
| 154 | + engineName: "dummy", |
| 155 | + account: accountName |
| 156 | + }; |
| 157 | + |
| 158 | + const connection = await firebolt.connect(connectionParams); |
| 159 | + await connection.execute(`SET ${param} = ${value}`); |
| 160 | + const statement = await connection.execute("SELECT 1"); |
| 161 | + const { data } = await statement.fetchResult(); |
| 162 | + const record = data[0] as Record<string, string>; |
| 163 | + expect(record.one).toBe(1); |
| 164 | + }); |
| 165 | + it("Incorrect set statements don't get saved", async () => { |
| 166 | + const param = "my_var"; |
| 167 | + const value = "1"; |
| 168 | + let param_checked = false; |
| 169 | + server.use( |
| 170 | + // Verify that the parameter is not set when querying the engine |
| 171 | + rest.post(`https://some_engine.com/`, (req, res, ctx) => { |
| 172 | + if ( |
| 173 | + req.body == "SELECT 1" && |
| 174 | + req.url.searchParams.get(param)?.toString() == value |
| 175 | + ) { |
| 176 | + return res(ctx.status(404)); |
| 177 | + } |
| 178 | + if (req.body == "SELECT 2") { |
| 179 | + expect(req.url.searchParams.get(param)).toBe(null); |
| 180 | + param_checked = true; |
| 181 | + } |
| 182 | + return res(ctx.json(selectOneResponse)); |
| 183 | + }) |
| 184 | + ); |
| 185 | + |
| 186 | + const firebolt = Firebolt({ apiEndpoint }); |
| 187 | + const connectionParams: ConnectionOptions = { |
| 188 | + auth: { |
| 189 | + username: "user", |
| 190 | + password: "pass" |
| 191 | + }, |
| 192 | + database: "dummy", |
| 193 | + engineName: "dummy", |
| 194 | + account: accountName |
| 195 | + }; |
| 196 | + |
| 197 | + const connection = await firebolt.connect(connectionParams); |
| 198 | + await expect( |
| 199 | + connection.execute(`SET ${param} = ${value}`) |
| 200 | + ).rejects.toThrow(); |
| 201 | + const statement = await connection.execute("SELECT 2"); |
| 202 | + const { data } = await statement.fetchResult(); |
| 203 | + const record = data[0] as Record<string, string>; |
| 204 | + expect(record.one).toBeDefined(); |
| 205 | + expect(param_checked).toBe(true); |
| 206 | + }); |
| 207 | + it("Can connect without account", async () => { |
| 208 | + let other_account_used = false; |
| 209 | + server.use( |
| 210 | + // Verify that some_other_account was used |
| 211 | + rest.get( |
| 212 | + `https://${apiEndpoint}/core/v1/accounts/some_other_account/engines/123`, |
| 213 | + (req, res, ctx) => { |
| 214 | + other_account_used = true; |
| 215 | + return res(ctx.json({ engine: engineObject })); |
| 216 | + } |
| 217 | + ) |
| 218 | + ); |
| 219 | + const firebolt = Firebolt({ |
| 220 | + apiEndpoint |
| 221 | + }); |
| 222 | + |
| 223 | + const connectionParams: ConnectionOptions = { |
| 224 | + auth: { |
| 225 | + username: "user", |
| 226 | + password: "pass" |
| 227 | + }, |
| 228 | + database: "dummy", |
| 229 | + engineName: "dummy" |
| 230 | + }; |
| 231 | + |
| 232 | + const connection = await firebolt.connect(connectionParams); |
| 233 | + const statement = await connection.execute("SELECT 1"); |
| 234 | + const { data } = await statement.fetchResult(); |
| 235 | + const record = data[0] as Record<string, string>; |
| 236 | + expect(record.one).toBe(1); |
| 237 | + expect(other_account_used).toBe(true); |
| 238 | + }); |
| 239 | +}); |
0 commit comments