Skip to content

Commit

Permalink
chore: topics/k1ch/admin-delete-personas-roles/ add DB and API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
k1ch committed Jan 25, 2024
1 parent 498b114 commit 9db8eb7
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
29 changes: 29 additions & 0 deletions database/test/db-admin-personaroles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,33 @@ describe('Admin persona roles view', () => {
await usherDb('personas').where({ key: testPersonaKey }).del()
})
})

describe('Test Delete personas roles', () => {
let testPersonaKey
let validRoleKey
const invalidPersonaKey = 0

before(async () => {
const { key: roleKey } = await usherDb('roles').select('key').first()
validRoleKey = roleKey
const { key: tenantkey } = await usherDb('tenants').select('key').first()
const [persona] = await usherDb('personas').insert({ tenantkey, sub_claim: 'personarole@test' }).returning('key')
testPersonaKey = persona.key
})

it('Should return 0 when there is no personaroles record to delete', async () => {
const numberOfDeletedRecords = await adminPersonaRoles.deletePersonaRoleByKeys(invalidPersonaKey, validRoleKey)
assert.equal(numberOfDeletedRecords, 0)
})

it('Should return 1 when successfully deletes a personaroles record', async () => {
await usherDb('personaroles').insert({ personakey: testPersonaKey, rolekey: validRoleKey })
const numberOfDeletedRecords = await adminPersonaRoles.deletePersonaRoleByKeys(testPersonaKey, validRoleKey)
assert.equal(numberOfDeletedRecords, 1)
})

after(async () => {
await usherDb('personas').where({ key: testPersonaKey }).del()
})
})
})
2 changes: 1 addition & 1 deletion server/src/api_endpoints/personas/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const deletePersonaRole = async (req, res, next) => {
])
await dbAdminPersonaRoles.deletePersonaRoleByKeys(personaKey, roleKey)
res.status(204).send()
} catch ({ httpStatusCode = 500, message }) {
} catch ({ httpStatusCode = 500, message }) {
return next(createError(httpStatusCode, { message }))
}
}
Expand Down
57 changes: 57 additions & 0 deletions server/test/endpoint_admin_personas_roles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,63 @@ describe('Admin Personas Roles', () => {
})
})

describe('DELETE:/personas/{persona_key}/roles/{role_key}', () => {
const deletePersonasRoles = async (roleKey, personaKey = testPersonaKey, header = requestHeaders) => {
return await fetch(`${url}/personas/${personaKey}/roles/${roleKey}`, {
method: 'DELETE',
headers: header,
})
}

it('should return 204, successful attempt to delete a persona role', async () => {
const response = await deletePersonasRoles(validRoleKey)
assert.equal(response.status, 204)
})

it('should return 204, delete a persona role successfully', async () => {
const [newPersonaRole] = await usherDb('personaroles')
.insert({ personakey: testPersonaKey, rolekey: validRoleKey }).returning('*')
assert.equal(newPersonaRole.personakey, testPersonaKey)
const response = await deletePersonasRoles(newPersonaRole.rolekey)
assert.equal(response.status, 204)
const personaRole = await usherDb('personaroles').select('*').where({ personakey: testPersonaKey, rolekey: validRoleKey })
assert.equal(personaRole.length, 0)
})

it('should return 400, two different invalid requests', async () => {
const [invalidRoleKeyResponse, invalidPersonaKeyResponse] = await Promise.all(
[
deletePersonasRoles('a'),
deletePersonasRoles(validRoleKey, 'a'),
]
)
assert.equal([
invalidRoleKeyResponse.status,
invalidPersonaKeyResponse.status].every((status) => status === 400), true)
})

it('should return 401, unauthorized token', async () => {
const userAccessToken = await getTestUser1IdPToken()
const response = await deletePersonasRoles(
validRoleKey,
testPersonaKey,
{
...requestHeaders,
Authorization: `Bearer ${userAccessToken}`
})
assert.equal(response.status, 401)
})

it('should return 404, fail to delete persona roles for an invalid persona', async () => {
const response = await deletePersonasRoles(validRoleKey, invalidPersona)
assert.equal(response.status, 404)
})

afterEach(async () => {
await usherDb('personaroles').where({ personakey: testPersonaKey }).del()
})
})

after(async () => {
await usherDb('personas').where({ key: testPersonaKey }).del()
})
Expand Down

0 comments on commit 9db8eb7

Please sign in to comment.