Skip to content

Commit

Permalink
chore: minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
k1ch committed Dec 21, 2024
1 parent 24e5bd0 commit aec5cdd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 49 deletions.
2 changes: 1 addition & 1 deletion database/layer/admin-permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const getPermissionsByNameClientKey = async (name, clientKey) => {
* @param {Object} filters - The filters to apply
* @param {string} [filters.name] - The name of the permission
* @param {string} [filters.clientId] - The client id
* @param {string} [filters.clientKey] - The client key
* @param {number} [filters.clientKey] - The client key
* @returns {Promise<Array<Object>>} - A promise that resolves to an array of permissions
*/
const getPermissions = async (filters = {}) => {
Expand Down
13 changes: 7 additions & 6 deletions database/test/db-admin-permissions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ describe('Admin permissions view', () => {

describe('Test GetPermissions by optional filter', () => {
it('Should return all permissions when no filters are applied', async () => {
const { count: permissionCount } = await usherDb('permissions').count('*').first()
const permissions = await adminPermissions.getPermissions()
assert.ok(permissions.length > 0)
assert.equal(permissions.length, Number(permissionCount))
assert.ok(permissionTableColumns.every((col) => col in permissions[0]))
})

it('Should return permissions for a specific clientId', async () => {
const { client_id: clientId } = await usherDb('clients').select('client_id').first()
const { clientkey: clientKey } = await usherDb('permissions').select('clientkey').first()
const { client_id: clientId } = await usherDb('clients').select('client_id').where({ key: clientKey }).first()
const permissions = await adminPermissions.getPermissions({ clientId })
assert.ok(permissions.length > 0)
assert.ok(permissions.every(permission => permission.client_id === clientId))
Expand All @@ -121,11 +123,10 @@ describe('Admin permissions view', () => {
})

it('Should return permissions for multiple filters', async () => {
const { client_id: clientId } = await usherDb('clients').select('client_id').first()
const { name } = await usherDb('permissions').select('name').first()
const permissions = await adminPermissions.getPermissions({ clientId, name })
const { name, clientkey: clientKey } = await usherDb('permissions').select('*').first()
const permissions = await adminPermissions.getPermissions({ name, clientKey })
assert.ok(permissions.length > 0)
assert.ok(permissions.every(permission => permission.client_id.includes(clientId) && permission.name.includes(name)))
assert.ok(permissions.every(permission => permission.clientkey === clientKey && permission.name === name))
})

it('Should return an empty array if no permissions match the criteria', async () => {
Expand Down
18 changes: 9 additions & 9 deletions server/test/endpoint_permissions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { usherDb } = require('database/layer/knex')
const { getAdmin1IdPToken, getTestUser1IdPToken } = require('./lib/tokens')
const { getServerUrl } = require('./lib/urls')

describe('Admin Roles API Tests', () => {
describe('Admin Permissions API Tests', () => {
const url = getServerUrl()
let requestHeaders
before(async () => {
Expand All @@ -21,9 +21,9 @@ describe('Admin Roles API Tests', () => {
* GET /permissions
* HTTP request to retrieve a list of permissions
*
* @param {string} query - The query params to be added to the URL (E.g. ?name=value1&client_id=value2&client_key=value3)
* @param {string} query - The query params to be added to the URL (e.g., ?name=value1&client_id=value2&client_key=value3)
* @param {Object} header - The request headers
* @returns {Promise<fetch.response>} - A Promise which resolves to fetch.response
* @returns {Promise<fetch.Response>} - A Promise which resolves to fetch.Response
*/
const getPermissions = async (query = '', header = requestHeaders) => {
return await fetch(`${url}/permissions${query}`, {
Expand All @@ -33,20 +33,20 @@ describe('Admin Roles API Tests', () => {
}

it('should return 200, return all the permissions', async () => {
const { count: totalCount } = await usherDb('permissions').count('*').first()
const { count: permissionCount } = await usherDb('permissions').count('*').first()
const response = await getPermissions()
assert.equal(response.status, 200)
const permissions = await response.json()
assert.equal(permissions.length, Number(totalCount))
assert.equal(permissions.length, Number(permissionCount))
})

it('should return 200, return all the permissions for a client', async () => {
const { client_id: validClientId, key: validClientKey } = await usherDb('clients').select('*').first()
const { count: totalCount } = await usherDb('permissions').where({ clientkey: validClientKey }).count('*').first()
const { count: permissionCount } = await usherDb('permissions').where({ clientkey: validClientKey }).count('*').first()
const response = await getPermissions(`?client_id=${validClientId}`)
assert.equal(response.status, 200)
const permissions = await response.json()
assert.equal(permissions.length, Number(totalCount))
assert.equal(permissions.length, Number(permissionCount))
assert.equal(permissions[0]['client_id'], validClientId)
})

Expand All @@ -60,14 +60,14 @@ describe('Admin Roles API Tests', () => {
assert.ok(permissions.every(permission => permission.name === name))
})

it('should return 200, return empty array for invalid client_id', async () => {
it('should return 200, return an empty array for an invalid client_id', async () => {
const response = await getPermissions('?client_id=invalid')
assert.equal(response.status, 200)
const permissions = await response.json()
assert.equal(permissions.length, 0)
})

it('should return 400, due to invalid query param', async () => {
it('should return 400, due to an invalid query param', async () => {
const response = await getPermissions('?client_key=string,')
assert.equal(response.status, 400)
})
Expand Down
66 changes: 33 additions & 33 deletions server/the-usher-openapi-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,39 @@ paths:
/clients/{client_id}/permissions:
parameters:
- $ref: '#/components/parameters/clientIdPathParam'
get:
'x-swagger-router-controller': 'clients/permissions'
operationId: getClientPermissions
summary: Get a list of permissions for a client
tags:
- Client Admin APIs
security:
- bearerAdminAuth: []
- bearerClientAdminAuth: []
responses:
200:
description: List of permissions for a client
content:
application/json:
schema:
type: array
items:
allOf:
- $ref: '#/components/schemas/PermissionObject'
- type: object
properties:
client_id:
type: string
400:
$ref: '#/components/responses/BadRequest'
401:
$ref: '#/components/responses/Unauthorized'
404:
$ref: '#/components/responses/NotFound'
500:
$ref: '#/components/responses/InternalError'
503:
$ref: '#/components/responses/ServiceUnavailableError'
post:
'x-swagger-router-controller': 'clients/permissions'
summary: Create a new permission for the given client
Expand Down Expand Up @@ -1139,39 +1172,6 @@ paths:
$ref: '#/components/responses/InternalError'
503:
$ref: '#/components/responses/ServiceUnavailableError'
get:
'x-swagger-router-controller': 'clients/permissions'
operationId: getClientPermissions
summary: Get a list of permissions for a client
tags:
- Client Admin APIs
security:
- bearerAdminAuth: []
- bearerClientAdminAuth: []
responses:
200:
description: List of permissions for a client
content:
application/json:
schema:
type: array
items:
allOf:
- $ref: '#/components/schemas/PermissionObject'
- type: object
properties:
client_id:
type: string
400:
$ref: '#/components/responses/BadRequest'
401:
$ref: '#/components/responses/Unauthorized'
404:
$ref: '#/components/responses/NotFound'
500:
$ref: '#/components/responses/InternalError'
503:
$ref: '#/components/responses/ServiceUnavailableError'

/sessions:
delete:
Expand Down

0 comments on commit aec5cdd

Please sign in to comment.