Skip to content

Commit

Permalink
chore: topics/k1ch/admin-get-persona-permissions/add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
k1ch committed Dec 27, 2023
1 parent 179fdaa commit eb1f814
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 14 deletions.
56 changes: 42 additions & 14 deletions database/test/db-admin-persona.test.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,83 @@
const { describe, it } = require('mocha')
const assert = require('assert')
const postPersonas = require('../layer/admin-persona.js')
const adminPersonas = require('../layer/admin-persona.js')
const { usherDb } = require('../layer/knex')

describe('Admin persona view', () => {
describe('Test INSERT personas', () => {
it('Should insert persona without an exception', async () => {
const insertResult = await postPersonas.insertPersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
const insertResult = await adminPersonas.insertPersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
assert.strictEqual(insertResult, 'Insert successful')
await postPersonas.deletePersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
await adminPersonas.deletePersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
})
it('Should fail to insert for a nonexistent tenant', async () => {
const insertResult = await postPersonas.insertPersona('test-tenant1 Non-existent', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
const insertResult = await adminPersonas.insertPersona('test-tenant1 Non-existent', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
assert.strictEqual(insertResult, 'Insert failed: Tenant does not exist matching tenantname test-tenant1 Non-existent iss_claim http://idp.dmgt.com.mock.localhost:3002/')
})
it('Should fail to insert duplicate tenant/persona combination - check tenantname', async () => {
await postPersonas.insertPersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
const result = await postPersonas.insertPersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
await adminPersonas.insertPersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
const result = await adminPersonas.insertPersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
assert.strictEqual(result, 'Insert failed: A persona (sub_claim = [email protected]; user_context = ) already exists on tenantname test-tenant1 iss_claim http://idp.dmgt.com.mock.localhost:3002/')
await postPersonas.deletePersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
await adminPersonas.deletePersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
})
it('Should insert persona by tenant key without an exception', async () => {
const subClaim = '[email protected]'
const [tenant] = await usherDb('tenants').select('*').limit(1)
const persona = await postPersonas.insertPersonaByTenantKey(tenant.key, subClaim)
const persona = await adminPersonas.insertPersonaByTenantKey(tenant.key, subClaim)
assert.strictEqual(persona.sub_claim, subClaim)
await usherDb('personas').where({ key: persona.key }).del()
})
})

describe('Test UPDATE personas', () => {
it('Should update persona without an exception by tenantname', async () => {
await postPersonas.insertPersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
const resultTenantname = await postPersonas.updatePersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '[email protected]', '', '')
await adminPersonas.insertPersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
const resultTenantname = await adminPersonas.updatePersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '[email protected]', '', '')
assert.strictEqual(resultTenantname, 'Update successful')
await postPersonas.deletePersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
await adminPersonas.deletePersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
})
it('Should fail to update for a nonexistent tenant', async () => {
const resultTenantname = await postPersonas.updatePersona('test-tenant1 Non-existent', 'http://idp.dmgt.com.mock.localhost:3002/', 'auth0|test-persona2-REPLACE', 'should_not_replace_sub_claim', '', '')
const resultTenantname = await adminPersonas.updatePersona('test-tenant1 Non-existent', 'http://idp.dmgt.com.mock.localhost:3002/', 'auth0|test-persona2-REPLACE', 'should_not_replace_sub_claim', '', '')
assert.strictEqual(resultTenantname, 'Update failed: A persona (sub_claim = auth0|test-persona2-REPLACE; user_context = ) does not exist on tenantname test-tenant1 Non-existent iss_claim http://idp.dmgt.com.mock.localhost:3002/')
})
it('Should fail to update for a nonexistent persona', async () => {
const resultTenantname = await postPersonas.updatePersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', 'should_not_replace_sub_claim', '', '')
const resultTenantname = await adminPersonas.updatePersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', 'should_not_replace_sub_claim', '', '')
assert.strictEqual(resultTenantname, 'Update failed: A persona (sub_claim = [email protected]; user_context = ) does not exist on tenantname test-tenant1 iss_claim http://idp.dmgt.com.mock.localhost:3002/')
})
})

describe('Test DELETE personas', () => {
it('Should fail to delete a persona not linked to a tenant', async () => {
const resultDelete = await postPersonas.deletePersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
const resultDelete = await adminPersonas.deletePersona('test-tenant1', 'http://idp.dmgt.com.mock.localhost:3002/', '[email protected]', '')
assert.strictEqual(resultDelete, 'Delete failed: A persona (sub_claim = [email protected]; user_context = ) does not exist on tenantname test-tenant1 iss_claim http://idp.dmgt.com.mock.localhost:3002/')
})
})

describe('Test GET personas', () => {
const invalidPersonaKey = 0;
it('Should return a valid persona', async () => {
const persona = await adminPersonas.getPersona(1)
assert.strictEqual(persona.key, 1)
})
it('Should return undefined for invalid persona key', async () => {
const persona = await adminPersonas.getPersona(invalidPersonaKey)
assert.strictEqual(persona, undefined)
})
})

describe('Test GET personas permissions', () => {
const invalidPersonaKey = 0;
it('Should return an array of permissions for the persona', async function () {
const { personakey } = await usherDb('personapermissions').select('*').first() || {}
if (!personakey) {
this.skip()
}
const personaPermissions = await adminPersonas.getPersonaPermissions(personakey)
assert.equal(!!personaPermissions.length, true)
})
it('Should return an empty array', async function () {
const personaPermissions = await adminPersonas.getPersonaPermissions(invalidPersonaKey)
assert.equal(personaPermissions.length, 0)
})
})
})
58 changes: 58 additions & 0 deletions server/test/endpoint_admin_personas_permissions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { describe, it, before } = require('mocha')
const fetch = require('node-fetch')
const assert = require('assert')

const { getAdmin1IdPToken } = require('./lib/tokens')
const { getServerUrl } = require('./lib/urls')
const { usherDb } = require('../../database/layer/knex')


describe('Admin Personas Permissions', () => {
let requestHeaders
const url = `${getServerUrl()}`

before(async () => {
const userAccessToken = await getAdmin1IdPToken()
requestHeaders = {
'Content-Type': 'application/json',
Authorization: `Bearer ${userAccessToken}`,
}
})

describe('GET:/personas/{persona_key}/permissions', () => {
const invalidPersona = 0;
const validPersonaWithNoPermissions = 1

it('should return 200 and a list of permissions for the persona', async function () {
const { personakey } = await usherDb('personapermissions').select('*').first() || {}
if (!personakey) {
this.skip()
}
const response = await fetch(`${url}/personas/${personakey}/permissions`, {
method: 'GET',
headers: requestHeaders,
})
assert.equal(response.status, 200)
const personaPermissions = await response.json()
assert.equal(personaPermissions.length > 0, true)
})

it('should return 200 and an empty array', async () => {
const response = await fetch(`${url}/personas/${validPersonaWithNoPermissions}/permissions`, {
method: 'GET',
headers: requestHeaders,
})
assert.equal(response.status, 200)
const personaPermissions = await response.json()
assert.equal(personaPermissions.length, 0)
})

it('should return 404 and fail to get permissions for an invalid persona', async () => {
const response = await fetch(`${url}/personas/${invalidPersona}/permissions`, {
method: 'GET',
headers: requestHeaders,
})
assert.equal(response.status, 404)
})
})
})

0 comments on commit eb1f814

Please sign in to comment.