Skip to content

Commit

Permalink
IS-1488: Test using environment variables for url and token
Browse files Browse the repository at this point in the history
  • Loading branch information
vetlesolgaard committed Aug 28, 2023
1 parent 8bc4e34 commit cfc03e7
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 60 deletions.
4 changes: 2 additions & 2 deletions mock/mockEndepunkter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { mockIspengestopp } from "./ispengestopp/mockIspengestopp";
import { mockIsdialogmote } from "./isdialogmote/mockIsdialogmote";
import { mockIsdialogmotekandidat } from "./isdialogmotekandidat/mockIsdialogmotekandidat";
import { mockSyfoveileder } from "./syfoveileder/mockSyfoveileder";
import { mockUnleash } from "./unleash/mockUnleash";
import { mockUnleashEndpoint } from "./unleashMocks";
import { mockIsdialogmelding } from "./isdialogmelding/mockIsdialogmelding";
import { mockIsaktivitetskrav } from "./isaktivitetskrav/mockIsaktivitetskrav";
import { mockIsbehandlerdialog } from "./isbehandlerdialog/mockIsbehandlerdialog";
Expand Down Expand Up @@ -49,7 +49,7 @@ const mockEndepunkter = (server: any) => {
mockIsdialogmotekandidat,
mockIsdialogmelding,
mockSyfoveileder,
mockUnleash,
mockUnleashEndpoint,
].forEach((func) => {
func(server);
});
Expand Down
14 changes: 0 additions & 14 deletions mock/unleash/mockUnleash.ts

This file was deleted.

8 changes: 0 additions & 8 deletions mock/unleash/unleashMock.ts

This file was deleted.

20 changes: 20 additions & 0 deletions mock/unleashMocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { UNLEASH_ROOT } from "@/apiConstants";
import { ToggleNames } from "@/data/unleash/unleash_types";
import express from "express";

export const mockUnleashEndpoint = (server: any) => {
server.get(
`${UNLEASH_ROOT}/toggles`,
(req: express.Request, res: express.Response) => {
res.setHeader("Content-Type", "application/json");
res.send(JSON.stringify(mockUnleashResponse));
}
);
};

export const mockUnleashResponse = Object.values(ToggleNames).reduce(
(accumulator, toggleName) => {
return { ...accumulator, [toggleName]: true };
},
{}
);
9 changes: 2 additions & 7 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import prometheus = require("prom-client");
import { getOpenIdClient, getOpenIdIssuer } from "./server/authUtils";
import { setupProxy } from "./server/proxy";
import { setupSession } from "./server/session";

import Config = require("./server/config");
import unleash = require("./server/unleash");

// Prometheus metrics
Expand Down Expand Up @@ -68,15 +68,10 @@ const setupServer = async () => {
"/unleash/toggles",
redirectIfUnauthorized,
(req: express.Request, res: express.Response<unleash.Toggles>) => {
console.log(`Query:\n ${JSON.stringify(req.query)}`);

const togglesResponse = unleash.toggles(
const togglesResponse = unleash.getToggles(
req.query.veilederId,
req.query.enhetId
);

console.log(`Response:\n ${JSON.stringify(togglesResponse)}`);

res.status(200).send(togglesResponse);
}
);
Expand Down
10 changes: 4 additions & 6 deletions server/unleash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unleashClient = require("unleash-client");
import { Strategy } from "unleash-client";
import Config = require("./config");

const { initialize } = unleashClient;

Expand Down Expand Up @@ -35,16 +36,13 @@ class EnhetIds extends Strategy {
}

export const unleash = initialize({
url: "https://teamsykefravr-unleash-api.nav.cloud.nais.io/api",
url: Config.unleash.serverApiUrl + "/api",
appName: "syfomodiaperson",
customHeaders: {
Authorization:
"*:development.fe45f8b56cf48ef952592c9f4796a3f6ee61504d15d98c697277f510",
},
customHeaders: { Authorization: Config.unleash.serverApiToken },
strategies: [new VeilederIds(), new EnhetIds()],
});

export const toggles = (veilederId, enhetId) => {
export const getToggles = (veilederId, enhetId) => {
const context = {
veilederId: veilederId,
enhetId: enhetId,
Expand Down
4 changes: 2 additions & 2 deletions test/behandlerdialog/MeldingTilBehandlerTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
BEHANDLENDE_ENHET_DEFAULT,
VEILEDER_IDENT_DEFAULT,
} from "../../mock/common/mockConstants";
import { unleashMock } from "../../mock/unleash/unleashMock";
import { mockUnleashResponse } from "../../mock/unleashMocks";

let queryClient: QueryClient;

Expand Down Expand Up @@ -52,7 +52,7 @@ describe("MeldingTilBehandler", () => {
VEILEDER_IDENT_DEFAULT
),
() => {
return { ...unleashMock, isReturLegeerklaringEnabled: false };
return { ...mockUnleashResponse, isReturLegeerklaringEnabled: false };
}
);

Expand Down
41 changes: 24 additions & 17 deletions test/data/unleash/unleashQueryHooksTest.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import nock from "nock";
import { apiMock } from "../../stubs/stubApi";
import { testQueryClient } from "../../testQueryClient";
import { stubAktivVeilederinfoApi } from "../../stubs/stubSyfoveileder";
import { stubFeatureTogglesApi } from "../../stubs/stubUnleash";
import { queryHookWrapper } from "../queryHookTestUtils";
import { renderHook, waitFor } from "@testing-library/react";
import { useFeatureToggles } from "@/data/unleash/unleashQueryHooks";
import { expect } from "chai";
import { mockUnleashResponse } from "../../../mock/unleashMocks";

let queryClient: any;
let apiMockScope: any;
Expand All @@ -14,21 +21,21 @@ describe("unleashQuery tests", () => {
nock.cleanAll();
});

// it("loads unleash toggles", async () => {
// stubAktivVeilederinfoApi(apiMockScope);
// stubFeatureTogglesApi(apiMockScope);
//
// const wrapper = queryHookWrapper(queryClient);
//
// const { result } = renderHook(() => useFeatureToggles(), {
// wrapper,
// });
//
// await waitFor(() => {
// console.log(`RESULT ${JSON.stringify(result.current)}`);
// expect(result.current.isSuccess).to.be.true;
// });
// console.log(`UNLEASH MOCK: ${unleashMock}`);
// expect(result.current.toggles).to.deep.equal(unleashMock);
// });
it("loads unleash toggles", async () => {
stubAktivVeilederinfoApi(apiMockScope);
stubFeatureTogglesApi(apiMockScope);

const wrapper = queryHookWrapper(queryClient);

const { result } = renderHook(() => useFeatureToggles(), {
wrapper,
});

await waitFor(() => {
console.log(`RESULT ${JSON.stringify(result.current)}`);
expect(result.current.isSuccess).to.be.true;
});
console.log(`UNLEASH MOCK: ${mockUnleashResponse}`);
expect(result.current.toggles).to.deep.equal(mockUnleashResponse);
});
});
4 changes: 2 additions & 2 deletions test/stubs/stubUnleash.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import nock from "nock";
import { UNLEASH_ROOT } from "@/apiConstants";
import { unleashMock } from "../../mock/unleash/unleashMock";
import { mockUnleashResponse } from "../../mock/unleashMocks";

export const stubFeatureTogglesApi = (scope: nock.Scope) =>
scope
.post((uri) => uri.includes(`${UNLEASH_ROOT}/toggles`))
.reply(200, () => unleashMock);
.reply(200, () => mockUnleashResponse);
4 changes: 2 additions & 2 deletions test/testQueryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { modiacontextQueryKeys } from "@/data/modiacontext/modiacontextQueryHook
import { oppfolgingstilfellePersonQueryKeys } from "@/data/oppfolgingstilfelle/person/oppfolgingstilfellePersonQueryHooks";
import { oppfolgingstilfellePersonMock } from "../mock/isoppfolgingstilfelle/oppfolgingstilfellePersonMock";
import { unleashQueryKeys } from "@/data/unleash/unleashQueryHooks";
import { unleashMock } from "../mock/unleash/unleashMock";
import { mockUnleashResponse } from "../mock/unleashMocks";
import { brukerinfoQueryKeys } from "@/data/navbruker/navbrukerQueryHooks";
import { behandlereQueryKeys } from "@/data/behandler/behandlereQueryHooks";
import {
Expand Down Expand Up @@ -122,7 +122,7 @@ export const queryClientWithMockData = (): QueryClient => {
BEHANDLENDE_ENHET_DEFAULT.enhetId,
VEILEDER_IDENT_DEFAULT
),
() => unleashMock
() => mockUnleashResponse
);

queryClient.setQueryData(
Expand Down

0 comments on commit cfc03e7

Please sign in to comment.