Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ids-admin): Delegation-Delegation-Type #16068

Merged
merged 18 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import request from 'supertest'
import {
ApiScope,
ApiScopeDelegationType,
Client,
Delegation,
DelegationDelegationType,
DelegationProviderModel,
DelegationScope,
DelegationsIndexService,
DelegationTypeModel,
Domain,
Expand Down Expand Up @@ -51,24 +55,30 @@ import {
getScopePermission,
personalRepresentativeType,
} from '../../../test/stubs/personalRepresentativeStubs'
import { uuid } from 'uuidv4'
import addDays from 'date-fns/addDays'
GunnlaugurG marked this conversation as resolved.
Show resolved Hide resolved

describe('DelegationsController', () => {
describe('Given a user is authenticated', () => {
let app: TestApp
let factory: FixtureFactory
let server: request.SuperTest<request.Test>
let apiScopeModel: typeof ApiScope
let clientModel: typeof Client
let prScopePermission: typeof PersonalRepresentativeScopePermission
let apiScopeDelegationTypeModel: typeof ApiScopeDelegationType
let prModel: typeof PersonalRepresentative
let prRightsModel: typeof PersonalRepresentativeRight
let prRightTypeModel: typeof PersonalRepresentativeRightType
let prTypeModel: typeof PersonalRepresentativeType
let prDelegationTypeModel: typeof PersonalRepresentativeDelegationTypeModel
let delegationDelegationTypeModelModel: typeof DelegationDelegationType
let delegationModel: typeof Delegation
GunnlaugurG marked this conversation as resolved.
Show resolved Hide resolved
let delegationTypeModel: typeof DelegationTypeModel
let nationalRegistryApi: NationalRegistryClientService
let delegationProviderModel: typeof DelegationProviderModel
let delegationIndexService: DelegationsIndexService
let delegationScopesModel: typeof DelegationScope

const client = createClient({
clientId: '@island.is/webapp',
Expand Down Expand Up @@ -143,6 +153,14 @@ describe('DelegationsController', () => {
delegationProviderModel = app.get<typeof DelegationProviderModel>(
getModelToken(DelegationProviderModel),
)
delegationScopesModel = app.get<typeof DelegationScope>(
getModelToken(DelegationScope),
)
clientModel = app.get<typeof Client>(getModelToken(Client))
delegationModel = app.get<typeof Delegation>(getModelToken(Delegation))
delegationDelegationTypeModelModel = app.get<
typeof DelegationDelegationType
>(getModelToken(DelegationDelegationType))
GunnlaugurG marked this conversation as resolved.
Show resolved Hide resolved
nationalRegistryApi = app.get(NationalRegistryClientService)
delegationIndexService = app.get(DelegationsIndexService)
factory = new FixtureFactory(app)
Expand Down Expand Up @@ -172,6 +190,204 @@ describe('DelegationsController', () => {
await app.cleanUp()
})

describe('GET with general mandate delegation type', () => {
const representeeNationalId = getFakeNationalId()
let nationalRegistryApiSpy: jest.SpyInstance
const scopeNames = [
'api-scope/generalMandate1',
'api-scope/generalMandate2',
'api-scope/generalMandate3',
]

beforeAll(async () => {
client.supportedDelegationTypes = delegationTypes
await factory.createClient(client)

const delegations = await delegationModel.create({
id: uuid(),
fromDisplayName: 'Test',
fromNationalId: representeeNationalId,
toNationalId: userNationalId,
toName: 'Test',
})

await delegationProviderModel.create({
id: AuthDelegationProvider.Custom,
name: 'Custom',
description: 'Custom',
})

await delegationDelegationTypeModelModel.create({
delegationId: delegations.id,
delegationTypeId: AuthDelegationType.GeneralMandate,
})

await apiScopeModel.bulkCreate(
scopeNames.map((name) => ({
name,
domainName: domain.name,
enabled: true,
description: `${name}: description`,
displayName: `${name}: display name`,
})),
)

// set 2 of 3 scopes to have general mandate delegation type
await apiScopeDelegationTypeModel.bulkCreate([
{
apiScopeName: scopeNames[0],
delegationType: AuthDelegationType.GeneralMandate,
},
{
apiScopeName: scopeNames[1],
delegationType: AuthDelegationType.GeneralMandate,
},
])

nationalRegistryApiSpy = jest
.spyOn(nationalRegistryApi, 'getIndividual')
.mockImplementation(async (id) => {
const user = createNationalRegistryUser({
nationalId: representeeNationalId,
})

return user ?? null
})
})

afterAll(async () => {
await clientModel.destroy({
where: {},
cascade: true,
truncate: true,
force: true,
})
await delegationDelegationTypeModelModel.destroy({
where: {},
cascade: true,
truncate: true,
force: true,
})
await delegationModel.destroy({
where: {},
cascade: true,
truncate: true,
force: true,
})
await delegationTypeModel.destroy({
where: {},
cascade: true,
truncate: true,
force: true,
})
await delegationProviderModel.destroy({
where: {},
cascade: true,
truncate: true,
force: true,
})
await apiScopeDelegationTypeModel.destroy({
where: {},
cascade: true,
truncate: true,
force: true,
})
await apiScopeModel.destroy({
where: {},
cascade: true,
truncate: true,
force: true,
})
nationalRegistryApiSpy.mockClear()
})
GunnlaugurG marked this conversation as resolved.
Show resolved Hide resolved

it('should return mergedDelegationDTO with the generalMandate', async () => {
const response = await server.get('/v2/delegations')

expect(response.status).toEqual(200)
expect(response.body).toHaveLength(1)
})

it('should get all general mandate scopes', async () => {
const response = await server.get('/delegations/scopes').query({
fromNationalId: representeeNationalId,
delegationType: [AuthDelegationType.GeneralMandate],
})

expect(response.status).toEqual(200)
expect(response.body).toEqual([scopeNames[0], scopeNames[1]])
})

it('should only return valid general mandates', async () => {
const newNationalId = getFakeNationalId()
const newDelegation = await delegationModel.create({
id: uuid(),
fromDisplayName: 'Test',
fromNationalId: newNationalId,
toNationalId: userNationalId,
toName: 'Test',
})

await delegationDelegationTypeModelModel.create({
delegationId: newDelegation.id,
delegationTypeId: AuthDelegationType.GeneralMandate,
validTo: addDays(new Date(), -1),
})

const response = await server.get('/delegations/scopes').query({
fromNationalId: newNationalId,
delegationType: [AuthDelegationType.GeneralMandate],
})

expect(response.status).toEqual(200)
expect(response.body).toEqual([])
})

it('should return all general mandate scopes and other preset scopes', async () => {
const newDelegation = await delegationModel.create({
id: uuid(),
fromDisplayName: 'Test',
fromNationalId: representeeNationalId,
domainName: domain.name,
toNationalId: userNationalId,
toName: 'Test',
})

await delegationTypeModel.create({
id: AuthDelegationType.Custom,
name: 'custom',
description: 'custom',
providerId: AuthDelegationProvider.Custom,
})

await delegationScopesModel.create({
id: uuid(),
delegationId: newDelegation.id,
scopeName: scopeNames[2],
// set valid from as yesterday and valid to as tomorrow
validFrom: addDays(new Date(), -1),
validTo: addDays(new Date(), 1),
})

await apiScopeDelegationTypeModel.create({
apiScopeName: scopeNames[2],
delegationType: AuthDelegationType.Custom,
})

const response = await server.get('/delegations/scopes').query({
fromNationalId: representeeNationalId,
delegationType: [
AuthDelegationType.GeneralMandate,
AuthDelegationType.Custom,
],
})

expect(response.status).toEqual(200)
expect(response.body).toEqual(expect.arrayContaining(scopeNames))
expect(response.body).toHaveLength(scopeNames.length)
})
})

describe('and given we have 3 valid, 1 not yet active and 1 outdate right types', () => {
type rightsTypeStatus = 'valid' | 'unactivated' | 'outdated'
type rightsType = [code: string, status: rightsTypeStatus]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,5 @@ export const delegationTypes = [
AuthDelegationType.PersonalRepresentative + ':valid2',
AuthDelegationType.PersonalRepresentative + ':unactivated',
AuthDelegationType.PersonalRepresentative + ':outdated',
AuthDelegationType.GeneralMandate,
]
Loading
Loading