Skip to content

Commit

Permalink
move provider controller to admin-api
Browse files Browse the repository at this point in the history
  • Loading branch information
GunnlaugurG committed May 27, 2024
1 parent 808d9ef commit 3215127
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 68 deletions.
2 changes: 2 additions & 0 deletions apps/services/auth/admin-api/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ClientsModule as ClientsV2Module } from './v2/clients/clients.module'
import { ClientSecretsModule } from './v2/secrets/client-secrets.module'
import { TenantsModule } from './v2/tenants/tenants.module'
import { ScopesModule } from './v2/scopes/scopes.module'
import { ProvidersModule } from './v2/providers/providers.module'

@Module({
imports: [
Expand All @@ -43,6 +44,7 @@ import { ScopesModule } from './v2/scopes/scopes.module'
ClientsV2Module,
ClientSecretsModule,
ProblemModule,
ProvidersModule,
ScopesModule,
ConfigModule.forRoot({
isGlobal: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Controller, Get, UseGuards } from '@nestjs/common'
import {
Auth,
CurrentAuth,
IdsUserGuard,
Scopes,
ScopesGuard,
} from '@island.is/auth-nest-tools'
import { idsAdminScopes } from '@island.is/auth/scopes'
import { ApiSecurity, ApiTags } from '@nestjs/swagger'
import { Documentation } from '@island.is/nest/swagger'
import {
DelegationProviderService,
PaginatedDelegationProviderDto,
} from '@island.is/auth-api-lib'
import { AuditService } from '@island.is/nest/audit'

@UseGuards(IdsUserGuard, ScopesGuard)
@Scopes(...idsAdminScopes)
@ApiSecurity('ias', idsAdminScopes)
@ApiTags('admin')
@Controller({
path: '/providers',
version: ['2'],
})
export class ProvidersController {
constructor(
private readonly auditService: AuditService,
private readonly delegationProviderService: DelegationProviderService,
) {}

@Get()
@Documentation({
description: 'Fetch all delegationProviders and their delegationTypes',
response: { status: 200, type: PaginatedDelegationProviderDto },
})
async getDelegationProviders(
@CurrentAuth() auth: Auth,
): Promise<PaginatedDelegationProviderDto> {
return this.auditService.auditPromise(
{
auth,
action: 'getDelegationProviders',
resources: (delegations) => delegations.data.map((d) => d.id),
},
this.delegationProviderService.findAll(),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common'
import {
ClientsModule,
DelegationProviderService,
} from '@island.is/auth-api-lib'
import { ProvidersController } from './providers.controller'

@Module({
imports: [ClientsModule],
controllers: [ProvidersController],
providers: [DelegationProviderService],
})
export class ProvidersModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { setupApp, TestApp } from '@island.is/testing/nest'
import request from 'supertest'
import { FixtureFactory } from '@island.is/services/auth/testing'
import {
DelegationProviderDto,
SequelizeConfigService,
} from '@island.is/auth-api-lib'
import { AppModule } from '../../../app.module'
import { createCurrentUser } from '@island.is/testing/fixtures'
import { AdminPortalScope } from '@island.is/auth/scopes'

const path = '/v2/providers'

const delegationProviderTypesData = [
{
id: 'custom',
delegationTypes: [
{
id: 'custom:1',
name: 'custom:1',
},
],
},
{
id: 'procuration',
delegationTypes: [
{
id: 'procuration:1',
name: 'procuration:1',
},
{
id: 'procuration:2',
name: 'procuration:2',
},
],
},
]

describe('ProverController', () => {
let app: TestApp
let server: request.SuperTest<request.Test>
let factory: FixtureFactory

describe('authentication and authorization', () => {
it('user with no scopes should not have access to /providers', async () => {
app = await setupApp({
AppModule,
SequelizeConfigService,
user: createCurrentUser(),
dbType: 'postgres',
})
server = request(app.getHttpServer())
factory = new FixtureFactory(app)

const response = await server.get(path)

expect(response.status).toBe(403)
})
})

describe('getDelegationProviders', () => {
beforeAll(async () => {
const user = createCurrentUser({
scope: [AdminPortalScope.idsAdmin],
})

app = await setupApp({
AppModule,
SequelizeConfigService,
user,
dbType: 'postgres',
})
server = request(app.getHttpServer())
factory = new FixtureFactory(app)

for (const { id: dpId, delegationTypes } of delegationProviderTypesData) {
for (const _ of delegationTypes) {
await factory.createDelegationType({ providerId: dpId })
}
}
})

afterAll(async () => {
await app.cleanUp()
})

it('should return all delegation providers and their delegation types', async () => {
// Act
const response = await server.get(path)

// Assert
expect(response.status).toBe(200)
expect(response.body.totalCount).toBe(delegationProviderTypesData.length)
expect(response.body.data.length).toBe(delegationProviderTypesData.length)

response.body.data.forEach((dp: DelegationProviderDto) => {
const expectedDp = delegationProviderTypesData.find(
({ id }) => id === dp.id,
)

expect(expectedDp).toBeDefined()
expect(dp.delegationTypes.length).toBe(
expectedDp?.delegationTypes.length,
)
})
})
})

afterAll(async () => {
await app?.cleanUp()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
DelegationDirection,
DelegationsIndexService,
PaginatedDelegationRecordDTO,
DelegationProviderService,
PaginatedDelegationProviderDto,
} from '@island.is/auth-api-lib'
import { Documentation } from '@island.is/nest/swagger'
import {
Expand Down Expand Up @@ -34,7 +32,6 @@ export class DelegationsController {
constructor(
private readonly delegationIndexService: DelegationsIndexService,
private readonly auditService: AuditService,
private readonly delegationProviderService: DelegationProviderService,
) {}

@Get()
Expand Down Expand Up @@ -91,22 +88,4 @@ export class DelegationsController {
}),
)
}

@Get('/providers')
@Documentation({
description: 'Fetch all delegationProviders and their delegationTypes',
response: { status: 200, type: PaginatedDelegationProviderDto },
})
async getDelegationProviders(
@CurrentAuth() auth: Auth,
): Promise<PaginatedDelegationProviderDto> {
return this.auditService.auditPromise(
{
auth,
action: 'getDelegationProviders',
resources: (delegations) => delegations.data.map((d) => d.id),
},
this.delegationProviderService.findAll(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { Sequelize } from 'sequelize-typescript'

import { TestApp, truncate } from '@island.is/testing/nest'
import { FixtureFactory } from '@island.is/services/auth/testing'
import {
DelegationRecordDTO,
DelegationProviderDto,
} from '@island.is/auth-api-lib'
import { DelegationRecordDTO } from '@island.is/auth-api-lib'

import {
user,
Expand Down Expand Up @@ -214,47 +211,4 @@ describe('DelegationsController', () => {
expect(response.status).toBe(403)
})
})

describe('GET(getDelegationProviders)', () => {
beforeAll(async () => {
app = await setupWithAuth({
user,
})

server = request(app.getHttpServer())
factory = new FixtureFactory(app)
sequelize = await app.resolve(getConnectionToken() as Type<Sequelize>)

for (const { id: dpId, delegationTypes } of delegationProviderTypesData) {
for (const _ of delegationTypes) {
await factory.createDelegationType({ providerId: dpId })
}
}
})

afterAll(async () => {
await app.cleanUp()
})

it('should return all delegation providers and their delegation types', async () => {
// Act
const response = await server.get(`${path}/providers`)

// Assert
expect(response.status).toBe(200)
expect(response.body.totalCount).toBe(delegationProviderTypesData.length)
expect(response.body.data.length).toBe(delegationProviderTypesData.length)

response.body.data.forEach((dp: DelegationProviderDto) => {
const expectedDp = delegationProviderTypesData.find(
({ id }) => id === dp.id,
)

expect(expectedDp).toBeDefined()
expect(dp.delegationTypes.length).toBe(
expectedDp?.delegationTypes.length,
)
})
})
})
})

0 comments on commit 3215127

Please sign in to comment.