Skip to content

Commit

Permalink
Add basic checks for the service account (#566)
Browse files Browse the repository at this point in the history
* add basic checks for the service account

* leftover

* update error reason
  • Loading branch information
juandspy authored Oct 27, 2022
1 parent 7ad472e commit 5e37ee8
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions packages/api-mock/src/handlers/service-account-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const commonServiceAccountFields = {
id: nanoid(),
clientId: nanoid(),
secret: nanoid(),
name: "service-account-name",
description: "service-account-description",
createdBy: "service-account-created-by",
createdAt: new Date().getTime(),
}
Expand All @@ -23,9 +21,15 @@ const commonError = {
};

function createServiceAccountHandlers(preSeed) {

return {
createServiceAccount: async (c, req, res) => {
if (!req.body.name) {
return res.status(400).json({
reason: "Service account name is invalid",
...commonError,
});
}

let serviceAccount = {
name: req.body.name,
Expand All @@ -39,26 +43,40 @@ function createServiceAccountHandlers(preSeed) {

getServiceAccount: async (c, req, res) => {
const id = c.request.params.id;
if (!id || !serviceAccountMap.has(id)) {
if (!id) {
return res.status(400).json({
reason: "Missing or invalid id field",
reason: "Service account id is invalid",
...commonError,
});
}

if (!serviceAccountMap.has(id)) {
return res.status(404).json({
reason: "Failed to find service account",
...commonError,
});
}

const serviceAccount = serviceAccountMap.get(id)
res.status(204).json(serviceAccount);
},

deleteServiceAccount: async (c, req, res) => {
const id = c.request.params.id;
if (!id || !serviceAccountMap.has(id)) {
if (!id) {
return res.status(400).json({
reason: "Missing or invalid id field",
reason: "Service account id is invalid",
...commonError,
});
}

if (!serviceAccountMap.has(id)) {
return res.status(404).json({
reason: "Failed to find service account",
...commonError,
});
}

const serviceAccount = serviceAccountMap.get(id)
res.status(204).json(serviceAccount);
serviceAccountMap.delete(id)
Expand Down

0 comments on commit 5e37ee8

Please sign in to comment.