// Copyright 2020 Cognite AS import { HttpError } from "@cognite/sdk-core"; import { RefreshMethod, WellsAuthFlowType } from "src/client/clientAuthUtils"; import { ConfigureAPI } from "../client/baseWellsClient"; import { createWellsClient } from "../client/clientCreateUtils"; import CogniteWellsClient from "../client/cogniteWellsClient"; import HttpClientWithIntercept from "../client/httpClientWithIntercept"; import { BLUEFIELD_BASE_URL, BP_NORTHEUROPE_DEV_BASE_URL, EW1_BASE_URL, } from "../constants"; import { authTokens } from "./testUtils"; // test api-key login test("api-key login", async () => { const client = createWellsClient("WELLS TEST CLIENT", BLUEFIELD_BASE_URL); expect(client.isLoggedIn).toBe(false); await client.loginWithApiKey({ project: "subsurface-test", apiKey: "test", }); expect(client.isLoggedIn).toBe(true); // get 401 due to not being able to refresh bad token expect(async () => await client.wells.list({})).rejects.toEqual( new HttpError(401, "Unauthorized", {}) ); }); test("bearer-token login", async () => { const client = createWellsClient("WELLS TEST CLIENT", BLUEFIELD_BASE_URL); expect(client.isLoggedIn).toBe(false); await client.loginWithToken({ project: "subsurface-test", accessToken: authTokens.accessToken, }); expect(client.isLoggedIn).toBe(true); // get 401 due to not being able to refresh bad token expect(async () => await client.wells.list({})).rejects.toEqual( new HttpError(401, "Unauthorized", {}) ); }); test("bearer-token with Azure AD auth flow login", async () => { const client = createWellsClient("WELLS TEST CLIENT", BLUEFIELD_BASE_URL); expect(client.isLoggedIn).toBe(false); const authFlow: WellsAuthFlowType = { type: "AAD_OAUTH", options: { cluster: "cdf-cluster-name", clientId: "azure-application-client-id", tenantId: "azure-tenant-id", debug: true, }, }; // get 401 due to not being able to regenerate token expect( async () => await client.loginWithToken({ project: "subsurface-test", authenticationFlow: authFlow, }) ).rejects.toEqual(new HttpError(401, "Unauthorized", {})); }); // CUSTOM option is broken? // https://cognitedata.slack.com/archives/CG10VQPFX/p1631629297040000 // test('custom auth flow login', async () => { // const client = createWellsClient('WELLS TEST CLIENT', BLUEFIELD_BASE_URL); // expect(client.isLoggedIn).toBe(false); // const authFlow: WellsAuthFlowType = { // type: 'CUSTOM', options: {} // }; // // get 401 due to not being able to regenerate token // expect(async () => await client.loginWithToken({ // project: "subsurface-test", // authenticationFlow: authFlow // })).rejects.toEqual( // new HttpError(401, 'Unauthorized', {}) // ) // }); // test api-key login test("token refresh callback login", async () => { const client = createWellsClient("WELLS TEST CLIENT", BLUEFIELD_BASE_URL); expect(client.isLoggedIn).toBe(false); const functionThatReturnsANewToken: RefreshMethod = async () => "new fresh token"; await client.loginWithToken({ project: "subsurface-test", tokenRefreshCallback: functionThatReturnsANewToken, }); expect(client.isLoggedIn).toBe(true); // get 401 due to invalid token expect(async () => await client.wells.list({})).rejects.toEqual( new HttpError(401, "Unauthorized", {}) ); }); // test api-key login test("configure base url", async () => { let client = createWellsClient("WELLS TEST CLIENT", EW1_BASE_URL); expect(client.isLoggedIn).toBe(false); client.loginWithApiKey({ project: "subsurface-test", apiKey: "test", }); expect(client.isLoggedIn).toBe(true); expect(client.getBaseUrl).toBe(EW1_BASE_URL); client = createWellsClient("WELLS TEST CLIENT", EW1_BASE_URL); expect(client.isLoggedIn).toBe(false); expect(client.getBaseUrl).toBe(EW1_BASE_URL); client = createWellsClient("WELLS TEST CLIENT", BLUEFIELD_BASE_URL); expect(client.isLoggedIn).toBe(false); expect(client.getBaseUrl).toBe(BLUEFIELD_BASE_URL); client = createWellsClient("WELLS TEST CLIENT", BP_NORTHEUROPE_DEV_BASE_URL); expect(client.isLoggedIn).toBe(false); expect(client.getBaseUrl).toBe(BP_NORTHEUROPE_DEV_BASE_URL); client = new CogniteWellsClient({ appId: "WELLS TEST CLIENT", baseUrl: BP_NORTHEUROPE_DEV_BASE_URL, }); expect(client.isLoggedIn).toBe(false); expect(client.getBaseUrl).toBe(BP_NORTHEUROPE_DEV_BASE_URL); }); // test api-key login test("test get path function", async () => { class TestAPI extends ConfigureAPI { constructor() { super(); this.setHttpClient = new HttpClientWithIntercept(EW1_BASE_URL); this.project = "owa-test"; expect(this.client.getBaseUrl()).toBe(EW1_BASE_URL); let path = this.getPath("/test/route", "testCursor"); expect(path).not.toBeNull; expect(path).toContain( "/api/v1/projects/owa-test/wdl/test/route?cursor=testCursor" ); this.setHttpClient = new HttpClientWithIntercept( BP_NORTHEUROPE_DEV_BASE_URL ); this.project = "bp-northeurope"; expect(this.client.getBaseUrl()).toBe(BP_NORTHEUROPE_DEV_BASE_URL); path = this.getPath("/test/route", undefined); expect(path).not.toBeNull; expect(path).toBe("/api/v1/projects/bp-northeurope/wdl/test/route"); this.setHttpClient = new HttpClientWithIntercept(EW1_BASE_URL); this.project = "subsurface-test"; expect(this.client.getBaseUrl()).toBe(EW1_BASE_URL); path = this.getPath("/test/route", undefined); expect(path).not.toBeNull; expect(path).toBe("/api/v1/projects/subsurface-test/wdl/test/route"); this.setHttpClient = new HttpClientWithIntercept(EW1_BASE_URL); this.project = "subsurface-test"; path = this.getPath("/test/route", "testCursor"); expect(path).not.toBeNull; expect(path).toBe( "/api/v1/projects/subsurface-test/wdl/test/route?cursor=testCursor" ); } } // run test new TestAPI(); });