-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): formating + add basic tenant query resolvers test
- Loading branch information
Showing
9 changed files
with
197 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ process.env.USE_TIGERBEETLE = false | |
process.env.ENABLE_TELEMETRY = false | ||
process.env.KRATOS_ADMIN_URL = 'http://127.0.0.1:4434/admin' | ||
process.env.KRATOS_ADMIN_EMAIL = '[email protected]' | ||
process.env.AUTH_ADMIN_URL = 'http://example.com/graphql' | ||
process.env.AUTH_ADMIN_API_SECRET = 'verysecuresecret' | ||
|
||
module.exports = { | ||
...baseConfig, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { Knex } from 'knex' | ||
import { IocContract } from '@adonisjs/fold' | ||
import { createTestApp, TestContainer } from '../../tests/app' | ||
import { AppServices } from '../../app' | ||
import { initIocContainer } from '../..' | ||
import { Config, IAppConfig } from '../../config/app' | ||
import { truncateTables } from '../../tests/tableManager' | ||
import { getPageTests } from './page.test' | ||
import { | ||
createTenant, | ||
mockAdminAuthApiTenantCreation | ||
} from '../../tests/tenant' | ||
import { ApolloError, gql } from '@apollo/client' | ||
import { Scope } from 'nock' | ||
import { v4 as uuidv4 } from 'uuid' | ||
import { errorToCode, errorToMessage, TenantError } from '../../tenant/errors' | ||
|
||
describe('Tenant Resolver', (): void => { | ||
let deps: IocContract<AppServices> | ||
let appContainer: TestContainer | ||
let knex: Knex | ||
let config: IAppConfig | ||
let scope: Scope | ||
|
||
beforeAll(async (): Promise<void> => { | ||
deps = initIocContainer(Config) | ||
appContainer = await createTestApp(deps) | ||
knex = appContainer.knex | ||
config = await deps.use('config') | ||
scope = mockAdminAuthApiTenantCreation(config.authAdminApiUrl).persist() | ||
}) | ||
|
||
afterEach(async (): Promise<void> => { | ||
await truncateTables(knex) | ||
}) | ||
|
||
afterAll(async (): Promise<void> => { | ||
scope.done() | ||
appContainer.apolloClient.stop() | ||
await appContainer.shutdown() | ||
}) | ||
|
||
describe('Tenant Queries', (): void => { | ||
getPageTests({ | ||
getClient: () => appContainer.apolloClient, | ||
createModel: async () => createTenant(deps), | ||
pagedQuery: 'tenants' | ||
}) | ||
|
||
test('should return error if tenant does not exists', async (): Promise<void> => { | ||
try { | ||
await appContainer.apolloClient | ||
.query({ | ||
query: gql` | ||
query GetTenant($id: ID!) { | ||
tenant(id: $id) { | ||
id | ||
name | ||
kratosIdentityId | ||
} | ||
} | ||
`, | ||
variables: { | ||
id: uuidv4() | ||
} | ||
}) | ||
.then((query) => { | ||
if (query.data) return query.data.tenant | ||
throw new Error('Data was empty') | ||
}) | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(ApolloError) | ||
expect((error as ApolloError).graphQLErrors).toContainEqual( | ||
expect.objectContaining({ | ||
message: errorToMessage[TenantError.UnknownTenant], | ||
extensions: expect.objectContaining({ | ||
code: errorToCode[TenantError.UnknownTenant] | ||
}) | ||
}) | ||
) | ||
} | ||
}) | ||
|
||
test('should get correct tenant', async (): Promise<void> => { | ||
const tenant = await createTenant(deps) | ||
|
||
const response = await appContainer.apolloClient | ||
.query({ | ||
query: gql` | ||
query GetTenant($id: ID!) { | ||
tenant(id: $id) { | ||
id | ||
name | ||
kratosIdentityId | ||
} | ||
} | ||
`, | ||
variables: { | ||
id: tenant.id | ||
} | ||
}) | ||
.then((query) => { | ||
if (query.data) return query.data.tenant | ||
throw new Error('Data was empty') | ||
}) | ||
|
||
expect(response).toEqual({ | ||
__typename: 'Tenant', | ||
id: tenant.id, | ||
name: tenant.name, | ||
kratosIdentityId: tenant.kratosIdentityId | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 nock from 'nock' | ||
|
||
export function mockAdminAuthApiTenantCreation(mockedUrl: string) { | ||
const url = new URL(mockedUrl) | ||
return nock(`${url.protocol}//${url.host}`) | ||
.post(/.*/) | ||
.reply(200, { | ||
data: { | ||
createTenant: { | ||
tenant: { | ||
id: uuidv4() | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
|
||
export async function createTenant( | ||
deps: IocContract<AppServices> | ||
): Promise<Tenant> { | ||
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<Tenant> | ||
} |