-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Topic / k1ch / Introduce
GET:/permissions
and `GET:/clients/{client…
…_id}/permissions` APIs (#135) * feat: topic/k1ch/introduce API - GET/permissions * feat: topic/k1ch/ introduce API / GET/clients/{id}/permissions * chore: db-admin-permissions / add tests for getPermissions * chore: k1ch / introduce test for GET:/permissions * chore: k1ch / add test for GET:/clients/{client_id}/permissions * chore: minor changes * chore: k1ch / fix test GH workflow
- Loading branch information
Showing
8 changed files
with
330 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const createError = require('http-errors') | ||
const dbAdminPermission = require('database/layer/admin-permission') | ||
|
||
/** | ||
* HTTP Request handler | ||
* Returns a list of permissions | ||
* | ||
* @param {Object} req - The request object | ||
* @param {Object} res - The response object to send 200 statusCode and a list of permissions | ||
* @param {Function} next - The next middleware function | ||
* @returns {Promise<void>} - A promise that resolves to void when permissions are retrieved | ||
*/ | ||
const getPermissions = async (req, res, next) => { | ||
try { | ||
const { name, client_id: clientId, client_key: clientKey } = req.query | ||
const permissions = await dbAdminPermission.getPermissions({ name, clientId, clientKey }) | ||
res.status(200).send(permissions) | ||
} catch ({ httpStatusCode = 500, message }) { | ||
return next(createError(httpStatusCode, { message })) | ||
} | ||
} | ||
|
||
module.exports = { getPermissions } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const { describe, it } = require('mocha') | ||
const fetch = require('node-fetch') | ||
const assert = require('node:assert') | ||
const { usherDb } = require('database/layer/knex') | ||
const { getAdmin1IdPToken, getTestUser1IdPToken } = require('./lib/tokens') | ||
const { getServerUrl } = require('./lib/urls') | ||
|
||
describe('Admin Permissions API Tests', () => { | ||
const url = getServerUrl() | ||
let requestHeaders | ||
before(async () => { | ||
const adminAccessToken = await getAdmin1IdPToken() | ||
requestHeaders = { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${adminAccessToken}`, | ||
} | ||
}) | ||
|
||
describe('GET:/permissions', () => { | ||
/** | ||
* 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 {Object} header - The request headers | ||
* @returns {Promise<fetch.Response>} - A Promise which resolves to fetch.Response | ||
*/ | ||
const getPermissions = async (query = '', header = requestHeaders) => { | ||
return await fetch(`${url}/permissions${query}`, { | ||
method: 'GET', | ||
headers: header, | ||
}) | ||
} | ||
|
||
it('should return 200, return all the permissions', async () => { | ||
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(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: 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(permissionCount)) | ||
assert.equal(permissions[0]['client_id'], validClientId) | ||
}) | ||
|
||
it('should return 200, return a permission with two filter parameters', async () => { | ||
const validPermission = await usherDb('permissions').select('*').first() | ||
const { clientkey, name } = validPermission | ||
const response = await getPermissions(`?client_key=${clientkey}&name=${name}`) | ||
assert.equal(response.status, 200) | ||
const permissions = await response.json() | ||
assert.ok(permissions.every(permission => permission.clientkey === clientkey)) | ||
assert.ok(permissions.every(permission => permission.name === name)) | ||
}) | ||
|
||
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 an invalid query param', async () => { | ||
const response = await getPermissions('?client_key=string,') | ||
assert.equal(response.status, 400) | ||
}) | ||
|
||
it('should return 401, unauthorized token', async () => { | ||
const userAccessToken = await getTestUser1IdPToken() | ||
const response = await getPermissions('', | ||
{ | ||
...requestHeaders, | ||
Authorization: `Bearer ${userAccessToken}` | ||
}) | ||
assert.equal(response.status, 401) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters