Skip to content

Commit

Permalink
Handle legal representative in scope patch.
Browse files Browse the repository at this point in the history
Add tests.
  • Loading branch information
saevarma committed Sep 18, 2024
1 parent 74e9793 commit 09276db
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ApiScopeDelegationType,
AdminPatchScopeDto,
ApiScope,
SUPER_USER_DELEGATION_TYPES,
} from '@island.is/auth-api-lib'
import { FixtureFactory } from '@island.is/services/auth/testing'
import {
Expand Down Expand Up @@ -127,6 +128,10 @@ const createTestData = async ({
AuthDelegationType.LegalGuardian,
AuthDelegationProvider.NationalRegistry,
],
[
AuthDelegationType.LegalRepresentative,
AuthDelegationProvider.DistrictCommissionersRegistry,
],
].map(async ([delegationType, provider]) =>
fixtureFactory.createDelegationType({
id: delegationType,
Expand Down Expand Up @@ -374,6 +379,8 @@ interface PatchTestCase {
allowExplicitDelegationGrant?: boolean
grantToPersonalRepresentatives?: boolean
isAccessControlled?: boolean
addedDelegationTypes?: AuthDelegationType[]
removedDelegationTypes?: AuthDelegationType[]
}
expected: {
status: number
Expand Down Expand Up @@ -513,6 +520,44 @@ const patchTestCases: Record<string, PatchTestCase> = {
},
}

const expected403Response = {
status: 403,
body: {
title: 'Forbidden',
status: 403,
detail: 'User does not have access to update admin controlled fields',
type: 'https://httpstatuses.org/403',
},
}

SUPER_USER_DELEGATION_TYPES.map((delegationType) => {
const delegationTypeName = AuthDelegationType[delegationType]

patchTestCases[
`should return a forbidden exception when adding super user delegation type: ${delegationTypeName}`
] = {
user: currentUser,
tenantId: TENANT_ID,
scopeName: mockedPatchApiScope.name,
input: {
addedDelegationTypes: [delegationType],
},
expected: expected403Response,
}

patchTestCases[
`should return a forbidden exception when removing super user delegation type: ${delegationTypeName}`
] = {
user: currentUser,
tenantId: TENANT_ID,
scopeName: mockedPatchApiScope.name,
input: {
removedDelegationTypes: [delegationType],
},
expected: expected403Response,
}
})

describe('MeScopesController', () => {
describe('with auth', () => {
// GET: /v2/me/tenants/:tenantId/scopes
Expand Down Expand Up @@ -761,7 +806,7 @@ describe('MeScopesController', () => {
})
})

describe('PATCH: /v2/me/tenants/:tenantId/scopes/:scopeName', () => {
describe('PATCH: /v2/me/tenants/:tenantId/scopes/:scopeName as super user', () => {
let app: TestApp
let server: request.SuperTest<request.Test>
let apiScopeDelegationTypeModel: typeof ApiScopeDelegationType
Expand Down Expand Up @@ -831,6 +876,7 @@ describe('MeScopesController', () => {
AuthDelegationType.LegalGuardian,
AuthDelegationType.ProcurationHolder,
AuthDelegationType.PersonalRepresentative,
AuthDelegationType.LegalRepresentative,
],
},
expected: {
Expand All @@ -845,6 +891,7 @@ describe('MeScopesController', () => {
AuthDelegationType.LegalGuardian,
AuthDelegationType.ProcurationHolder,
AuthDelegationType.PersonalRepresentative,
AuthDelegationType.LegalRepresentative,
],
},
})
Expand All @@ -858,6 +905,7 @@ describe('MeScopesController', () => {
AuthDelegationType.LegalGuardian,
AuthDelegationType.ProcurationHolder,
AuthDelegationType.PersonalRepresentative,
AuthDelegationType.LegalRepresentative,
],
},
expected: {
Expand All @@ -872,6 +920,7 @@ describe('MeScopesController', () => {
AuthDelegationType.LegalGuardian,
AuthDelegationType.ProcurationHolder,
AuthDelegationType.PersonalRepresentative,
AuthDelegationType.LegalRepresentative,
],
},
})
Expand All @@ -883,6 +932,7 @@ describe('MeScopesController', () => {
AuthDelegationType.LegalGuardian,
AuthDelegationType.ProcurationHolder,
AuthDelegationType.PersonalRepresentative,
AuthDelegationType.LegalRepresentative,
],
},
expected: {
Expand Down Expand Up @@ -949,7 +999,7 @@ describe('MeScopesController', () => {
})
})

describe('POST: /v2/me/tenants/:tenantId/scopes', () => {
describe('POST: /v2/me/tenants/:tenantId/scopes as super user', () => {
let app: TestApp
let server: request.SuperTest<request.Test>
let apiScopeDelegationTypeModel: typeof ApiScopeDelegationType
Expand Down Expand Up @@ -1035,7 +1085,7 @@ describe('MeScopesController', () => {
})
})

describe('POST: /v2/me/tenants/:tenantId/scopes', () => {
describe('POST: /v2/me/tenants/:tenantId/scopes as normal user', () => {
let app: TestApp
let server: request.SuperTest<request.Test>
let apiScopeDelegationTypeModel: typeof ApiScopeDelegationType
Expand Down
11 changes: 5 additions & 6 deletions libs/auth-api-lib/src/lib/clients/admin/admin-clients.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,12 +668,11 @@ export class AdminClientsService {
...(input.addedDelegationTypes ?? []),
]

if (
!isSuperUser &&
allDelegationTypes.some((delegationType) =>
SUPER_USER_DELEGATION_TYPES.includes(delegationType),
)
) {
const hasSuperUserDelegationType = allDelegationTypes.some(
(delegationType) => SUPER_USER_DELEGATION_TYPES.includes(delegationType),
)

if (!isSuperUser && hasSuperUserDelegationType) {
return false
}

Expand Down
14 changes: 7 additions & 7 deletions libs/auth-api-lib/src/lib/resources/admin/admin-scope.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import { User } from '@island.is/auth-nest-tools'
import { AdminPortalScope } from '@island.is/auth/scopes'
import { AuthDelegationType } from '@island.is/shared/types'
import { ApiScopeDelegationType } from '../models/api-scope-delegation-type.model'
import { delegationTypeSuperUserFilter } from '../utils/filters'
import {
delegationTypeSuperUserFilter,
SUPER_USER_DELEGATION_TYPES,
} from '../utils/filters'

/**
* This is a service that is used to access the admin scopes
Expand Down Expand Up @@ -409,14 +412,11 @@ export class AdminScopeService {
...(input.removedDelegationTypes ?? []),
]

const isPersonalRepresentativeUpdate = allDelegationTypes.some(
(delegationType) =>
delegationType.startsWith(
`${AuthDelegationType.PersonalRepresentative}:`,
),
const hasSuperUserDelegationType = allDelegationTypes.some(
(delegationType) => SUPER_USER_DELEGATION_TYPES.includes(delegationType),
)

if (isPersonalRepresentativeUpdate && !isSuperUser) {
if (!isSuperUser && hasSuperUserDelegationType) {
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IsArray, IsBoolean, IsOptional, ValidateNested } from 'class-validator'
import { Type } from 'class-transformer'

import { TranslatedValueDto } from '../../../translation/dto/translated-value.dto'
import { AuthDelegationType } from '@island.is/shared/types'

export class AdminPatchScopeDto {
@ApiPropertyOptional({
Expand Down Expand Up @@ -95,15 +96,15 @@ export class AdminPatchScopeDto {
type: [String],
example: ['Custom'],
})
addedDelegationTypes?: string[]
addedDelegationTypes?: AuthDelegationType[]

@IsArray()
@IsOptional()
@ApiPropertyOptional({
type: [String],
example: ['Custom'],
})
removedDelegationTypes?: string[]
removedDelegationTypes?: AuthDelegationType[]
}

/**
Expand Down

0 comments on commit 09276db

Please sign in to comment.