Skip to content

Commit

Permalink
Merge pull request #483 from huwshimi/add-api-tests
Browse files Browse the repository at this point in the history
WD-17045 - chore(tests): add API tests
  • Loading branch information
huwshimi authored Dec 1, 2024
2 parents 4579aeb + 5c9acd9 commit f1e7fd0
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 0 deletions.
59 changes: 59 additions & 0 deletions ui/src/api/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import MockAdapter from "axios-mock-adapter";

import { mockClient } from "test/mocks/clients";

import { axiosInstance } from "./axios";
import {
fetchClient,
fetchClients,
createClient,
updateClient,
deleteClient,
} from "./client";

const mock = new MockAdapter(axiosInstance);

beforeEach(() => {
mock.reset();
});

test("fetchClients", async () => {
const url = "/clients?page_token=token1&size=50";
const clients = [mockClient(), mockClient()];
mock.onGet(url).reply(200, clients);
await expect(fetchClients("token1")).resolves.toStrictEqual(clients);
expect(mock.history.get[0].url).toBe(url);
});

test("fetchClient", async () => {
const url = "/clients/client1";
const client = mockClient();
mock.onGet(url).reply(200, client);
await expect(fetchClient("client1")).resolves.toStrictEqual(client);
expect(mock.history.get[0].url).toBe(url);
});

test("createClient", async () => {
const url = "/clients";
const client = JSON.stringify(mockClient());
mock.onPost(url).reply(200);
await createClient(client);
expect(mock.history.post[0].url).toBe(url);
expect(mock.history.post[0].data).toBe(client);
});

test("updateClient", async () => {
const url = "/clients/client1";
const client = JSON.stringify(mockClient());
mock.onPut(url).reply(200);
await updateClient("client1", client);
expect(mock.history.put[0].url).toBe(url);
expect(mock.history.put[0].data).toBe(client);
});

test("deleteClient", async () => {
const url = "/clients/client1";
mock.onDelete(url).reply(200);
await deleteClient("client1");
expect(mock.history.delete[0].url).toBe(url);
});
File renamed without changes.
61 changes: 61 additions & 0 deletions ui/src/api/identities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import MockAdapter from "axios-mock-adapter";

import { mockIdentity } from "test/mocks/identities";

import { axiosInstance } from "./axios";
import {
fetchIdentity,
fetchIdentities,
createIdentity,
updateIdentity,
deleteIdentity,
} from "./identities";

const mock = new MockAdapter(axiosInstance);

beforeEach(() => {
mock.reset();
});

test("fetchIdentities", async () => {
const url = "/identities?page_token=token1&size=50";
const identities = [mockIdentity(), mockIdentity()];
mock.onGet(url).reply(200, identities);
const response = await fetchIdentities("token1");
expect(mock.history.get[0].url).toBe(url);
expect(response).toStrictEqual(identities);
});

test("fetchIdentity", async () => {
const url = "/identities/identity1";
const identity = mockIdentity();
mock.onGet(url).reply(200, identity);
const response = await fetchIdentity("identity1");
expect(mock.history.get[0].url).toBe(url);
expect(response).toStrictEqual(identity);
});

test("createIdentity", async () => {
const url = "/identities";
const identity = JSON.stringify(mockIdentity());
mock.onPost(url).reply(200);
await createIdentity(identity);
expect(mock.history.post[0].url).toBe(url);
expect(mock.history.post[0].data).toBe(identity);
});

test("updateIdentity", async () => {
const url = "/identities/identity1";
const identity = JSON.stringify(mockIdentity());
mock.onPut(url).reply(200);
await updateIdentity("identity1", identity);
expect(mock.history.put[0].url).toBe(url);
expect(mock.history.put[0].data).toBe(identity);
});

test("deleteIdentity", async () => {
const url = "/identities/identity1";
mock.onDelete(url).reply(200);
await deleteIdentity("identity1");
expect(mock.history.delete[0].url).toBe(url);
});
File renamed without changes.
73 changes: 73 additions & 0 deletions ui/src/api/provider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import MockAdapter from "axios-mock-adapter";
import { faker } from "@faker-js/faker";

import { mockIdentityProvider } from "test/mocks/providers";

import { axiosInstance } from "./axios";
import {
fetchProvider,
fetchProviders,
createProvider,
updateProvider,
deleteProvider,
} from "./provider";

const mock = new MockAdapter(axiosInstance);

beforeEach(() => {
mock.reset();
});

test("fetchProviders", async () => {
const url = "/idps";
const providers = [
mockIdentityProvider({ id: faker.word.sample() }),
mockIdentityProvider({ id: faker.word.sample() }),
];
mock.onGet(url).reply(200, providers);
await expect(fetchProviders()).resolves.toStrictEqual(providers);
expect(mock.history.get[0].url).toBe(url);
});

test("fetchProvider", async () => {
const url = "/idps/provider1";
const providers = [mockIdentityProvider({ id: faker.word.sample() })];
mock.onGet(url).reply(200, { data: providers });
await expect(fetchProvider("provider1")).resolves.toStrictEqual(providers[0]);
expect(mock.history.get[0].url).toBe(url);
});

test("fetchProvider handles errors", async () => {
const error = "Uh oh!";
mock.onGet("/idps/provider1").reply(500, { error });
await expect(fetchProvider("provider1")).rejects.toBe(error);
});

test("createProvider", async () => {
const url = "/idps";
const provider = JSON.stringify(
mockIdentityProvider({ id: faker.word.sample() }),
);
mock.onPost(url).reply(200);
await createProvider(provider);
expect(mock.history.post[0].url).toBe(url);
expect(mock.history.post[0].data).toBe(provider);
});

test("updateProvider", async () => {
const url = "/idps/provider1";
const provider = JSON.stringify(
mockIdentityProvider({ id: faker.word.sample() }),
);
mock.onPatch(url).reply(200);
await updateProvider("provider1", provider);
expect(mock.history.patch[0].url).toBe(url);
expect(mock.history.patch[0].data).toBe(provider);
});

test("deleteProvider", async () => {
const url = "/idps/provider1";
mock.onDelete(url).reply(200);
await deleteProvider("provider1");
expect(mock.history.delete[0].url).toBe(url);
});
File renamed without changes.
65 changes: 65 additions & 0 deletions ui/src/api/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import MockAdapter from "axios-mock-adapter";

import { mockSchema } from "test/mocks/schemas";

import { axiosInstance } from "./axios";
import {
fetchSchema,
fetchSchemas,
createSchema,
updateSchema,
deleteSchema,
} from "./schema";

const mock = new MockAdapter(axiosInstance);

beforeEach(() => {
mock.reset();
});

test("fetchSchemas", async () => {
const url = "/schemas?page_token=token1&page_size=50";
const schemas = [mockSchema(), mockSchema()];
mock.onGet(url).reply(200, schemas);
await expect(fetchSchemas("token1")).resolves.toStrictEqual(schemas);
expect(mock.history.get[0].url).toBe(url);
});

test("fetchSchema", async () => {
const url = "/schemas/schema1";
const schemas = [mockSchema()];
mock.onGet(url).reply(200, { data: schemas });
await expect(fetchSchema("schema1")).resolves.toStrictEqual(schemas[0]);
expect(mock.history.get[0].url).toBe(url);
});

test("fetchSchema handles errors", async () => {
const error = "Uh oh!";
mock.onGet("/schemas/schema1").reply(500, { error });
await expect(fetchSchema("schema1")).rejects.toBe(error);
});

test("createSchema", async () => {
const url = "/schemas";
const schema = JSON.stringify(mockSchema());
mock.onPost(url).reply(200);
await createSchema(schema);
expect(mock.history.post[0].url).toBe(url);
expect(mock.history.post[0].data).toBe(schema);
});

test("updateSchema", async () => {
const url = "/schemas/schema1";
const schema = JSON.stringify(mockSchema());
mock.onPatch(url).reply(200);
await updateSchema("schema1", schema);
expect(mock.history.patch[0].url).toBe(url);
expect(mock.history.patch[0].data).toBe(schema);
});

test("deleteSchema", async () => {
const url = "/schemas/schema1";
mock.onDelete(url).reply(200);
await deleteSchema("schema1");
expect(mock.history.delete[0].url).toBe(url);
});
File renamed without changes.
29 changes: 29 additions & 0 deletions ui/src/test/mocks/clients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { faker } from "@faker-js/faker";

import { Client } from "types/client";

export const mockClient = (overrides?: Partial<Client>): Client => ({
allowed_cors_origins: [],
audience: [],
client_id: faker.word.sample(),
client_name: faker.word.sample(),
client_secret: faker.word.sample(),
client_secret_expires_at: faker.number.int(),
client_uri: faker.word.sample(),
contacts: [],
created_at: faker.word.sample(),
grant_types: [],
logo_uri: faker.word.sample(),
owner: faker.word.sample(),
policy_uri: faker.word.sample(),
redirect_uris: [],
request_object_signing_alg: faker.word.sample(),
response_types: [],
scope: faker.word.sample(),
subject_type: faker.word.sample(),
token_endpoint_auth_method: faker.word.sample(),
tos_uri: faker.word.sample(),
updated_at: faker.word.sample(),
userinfo_signed_response_alg: faker.word.sample(),
...overrides,
});
12 changes: 12 additions & 0 deletions ui/src/test/mocks/identities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { faker } from "@faker-js/faker";

import { Identity } from "types/identity";

export const mockIdentity = (overrides?: Partial<Identity>): Identity => ({
id: faker.word.sample(),
schema_id: faker.word.sample(),
schema_url: faker.word.sample(),
state: faker.word.sample(),
state_changed_at: faker.word.sample(),
...overrides,
});
7 changes: 7 additions & 0 deletions ui/src/test/mocks/providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IdentityProvider } from "types/provider";

export const mockIdentityProvider = (
overrides?: Partial<IdentityProvider>,
): IdentityProvider => ({
...overrides,
});
9 changes: 9 additions & 0 deletions ui/src/test/mocks/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { faker } from "@faker-js/faker";

import { Schema } from "types/schema";

export const mockSchema = (overrides?: Partial<Schema>): Schema => ({
id: faker.word.sample(),
schema: {},
...overrides,
});
33 changes: 33 additions & 0 deletions ui/src/util/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { axiosInstance } from "api/axios";
import MockAdapter from "axios-mock-adapter";
import { ApiResponse } from "types/api";

import { handleRequest } from "./api";

const mock = new MockAdapter(axiosInstance);

type Response = { test: string };

const handler = (): Promise<ApiResponse<Response>> =>
handleRequest(() => axiosInstance.get<ApiResponse<Response>>("/test"));

beforeEach(() => {
mock.reset();
});

test("resolves data", async () => {
mock.onGet("/test").reply(200, { test: "Test" });
await expect(handler()).resolves.toMatchObject({ test: "Test" });
});

test("handles errors", async () => {
const error = "Uh oh!";
mock.onGet("/test").reply(500, { error });
await expect(handler()).rejects.toBe(error);
});

test("handles error messages", async () => {
const message = "Uh oh!";
mock.onGet("/test").reply(500, { message });
await expect(handler()).rejects.toBe(message);
});
File renamed without changes.

0 comments on commit f1e7fd0

Please sign in to comment.