diff --git a/packages/backend/src/graphql/resolvers/tenant.test.ts b/packages/backend/src/graphql/resolvers/tenant.test.ts index 671053df90..641de1f8e2 100644 --- a/packages/backend/src/graphql/resolvers/tenant.test.ts +++ b/packages/backend/src/graphql/resolvers/tenant.test.ts @@ -14,6 +14,7 @@ import { ApolloError, gql } from '@apollo/client' import { Scope } from 'nock' import { v4 as uuidv4 } from 'uuid' import { errorToCode, errorToMessage, TenantError } from '../../tenant/errors' +import { TenantEndpointType } from '../generated/graphql' describe('Tenant Resolver', (): void => { let deps: IocContract @@ -112,4 +113,52 @@ describe('Tenant Resolver', (): void => { }) }) }) + + describe('Create Tenant', (): void => { + it('should create new tenant', async (): Promise => { + const mutation = gql` + mutation CreateTenant($input: CreateTenantInput!) { + createTenant(input: $input) { + tenant { + id + name + } + } + } + ` + + const variables = { + input: { + name: 'My Tenant', + idpConsentEndpoint: 'https://example.com/consent', + idpSecret: 'myVerySecureSecret', + endpoints: [ + { + type: TenantEndpointType.RatesUrl, + value: 'https://example.com/rates' + }, + { + type: TenantEndpointType.WebhookBaseUrl, + value: 'https://example.com/webhook' + } + ] + } + } + + const response = await appContainer.apolloClient + .mutate({ + mutation, variables + }) + .then((query) => { + if (query.data) return query.data.createTenant + throw new Error('Data was empty') + }) + + expect(response.tenant).toEqual({ + __typename: "Tenant", + id: response.tenant.id, + name: variables.input.name + }) + }) + }) }) diff --git a/packages/backend/src/tenant/service.test.ts b/packages/backend/src/tenant/service.test.ts index e69de29bb2..8ecb32d96a 100644 --- a/packages/backend/src/tenant/service.test.ts +++ b/packages/backend/src/tenant/service.test.ts @@ -0,0 +1,61 @@ +import { IocContract } from "@adonisjs/fold" +import { AppServices } from "../app" +import { createTestApp, TestContainer } from "../tests/app" +import { TenantService } from "./service" +import { initIocContainer } from ".." +import { Config, IAppConfig } from '../config/app' +import { truncateTables } from "../tests/tableManager" +import { createTenant, mockAdminAuthApiTenantCreation, randomTenant } from "../tests/tenant" +import assert from 'assert' +import { isTenantError } from "./errors" +import { Scope } from "nock" +import { v4 as uuidv4 } from 'uuid' +import { Pagination, SortOrder } from "../shared/baseModel" +import { getPageTests } from '../shared/baseModel.test' + + + +describe('Tenant Service', (): void => { + let deps: IocContract + let appContainer: TestContainer + let tenantService: TenantService + let scope: Scope + let config: IAppConfig + + beforeAll(async (): Promise => { + deps = initIocContainer(Config) + appContainer = await createTestApp(deps) + tenantService = await deps.use('tenantService') + config = await deps.use('config') + scope = mockAdminAuthApiTenantCreation(config.authAdminApiUrl).persist() + }) + + afterEach(async (): Promise => { + await truncateTables(appContainer.knex) + }) + + afterAll(async (): Promise => { + scope.done() + await appContainer.shutdown() + }) + + describe('get', (): void => { + it('can get tenant by id', async (): Promise => { + const tenant = await tenantService.create(randomTenant()) + assert.ok(!isTenantError(tenant)) + await expect(tenantService.get(tenant.id)).resolves.toEqual(tenant) + }) + + it('cannot get unknown tenant', async (): Promise => { + await expect(tenantService.get(uuidv4())).resolves.toBeUndefined() + }) + }) + + describe('getPage', (): void => { + getPageTests({ + createModel: () => createTenant(deps), + getPage: (pagination?: Pagination, sortOrder?: SortOrder) => + tenantService.getPage(pagination, sortOrder) + }) + }) +}) \ No newline at end of file diff --git a/packages/backend/src/tests/tenant.ts b/packages/backend/src/tests/tenant.ts index bb90953f09..2c862f0723 100644 --- a/packages/backend/src/tests/tenant.ts +++ b/packages/backend/src/tests/tenant.ts @@ -1,11 +1,30 @@ import { IocContract } from '@adonisjs/fold' import { AppServices } from '../app' import { Tenant } from '../tenant/model' -import { CreateTenantOptions } from '../tenant/service' import { v4 as uuidv4 } from 'uuid' import { EndpointType } from '../tenant/endpoints/model' +import { faker } from '@faker-js/faker' import nock from 'nock' + +export function randomTenant() { + return { + name: faker.company.name(), + idpConsentEndpoint: faker.internet.url(), + idpSecret: faker.string.sample(10), + endpoints: [ + { + type: EndpointType.RatesUrl, + value: faker.internet.url() + }, + { + type: EndpointType.WebhookBaseUrl, + value: faker.internet.url() + } + ] + } +} + export function mockAdminAuthApiTenantCreation(mockedUrl: string) { const url = new URL(mockedUrl) return nock(`${url.protocol}//${url.host}`) @@ -25,22 +44,5 @@ export async function createTenant( deps: IocContract ): Promise { const tenantService = await deps.use('tenantService') - - const options: CreateTenantOptions = { - name: uuidv4(), - idpConsentEndpoint: `https://example.com/${uuidv4()}`, - idpSecret: `secret-${uuidv4()}`, - endpoints: [ - { - type: EndpointType.RatesUrl, - value: `https://example.com/rates/${uuidv4()}` - }, - { - type: EndpointType.WebhookBaseUrl, - value: `https://example.com/webhook/${uuidv4()}` - } - ] - } - - return tenantService.create(options) as Promise + return tenantService.create(randomTenant()) as Promise }