forked from b00tc4mp/isdi-parttime-202309
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add registerAdmin, deleteUsers and retrieveAllUsers final logics | al…
…l with specs and HTML test b00tc4mp#361
- Loading branch information
Showing
15 changed files
with
244 additions
and
15 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
41 changes: 41 additions & 0 deletions
41
staff/abel-prieto/PROYECT/API/handlers/deleteUsersHandler.js
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,41 @@ | ||
import jwt from 'jsonwebtoken' | ||
import deleteUsers from '../logic/deleteUsers.js' | ||
import { errors } from 'com' | ||
|
||
const { JsonWebTokenError } = jwt | ||
const { NotFoundError, AuthorizationError, TokenError, ContentError } = errors | ||
|
||
|
||
export default async (req, res) => { | ||
const token = req.headers.authorization.substring(7) | ||
const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET) | ||
|
||
const { userToDelete } = req.params | ||
|
||
try { | ||
await deleteUsers(userId, userToDelete) | ||
res.status(201).send() | ||
|
||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof NotFoundError) { | ||
status = 404 | ||
} | ||
|
||
if (error instanceof ContentError || error instanceof TypeError) { | ||
status = 406 | ||
} | ||
|
||
if (error instanceof AuthorizationError) { | ||
status = 409 | ||
} | ||
|
||
if (error instanceof JsonWebTokenError) { | ||
status = 401 | ||
error = new TokenError(error.message) | ||
} | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
staff/abel-prieto/PROYECT/API/handlers/registerAdminHandler.js
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,40 @@ | ||
import jwt from 'jsonwebtoken' | ||
import registerAdmin from '../logic/registerAdmin.js' | ||
import { errors } from 'com' | ||
|
||
const { JsonWebTokenError } = jwt | ||
const { TokenError, ContentError, NotFoundError, AuthorizationError } = errors | ||
|
||
export default async (req, res) => { | ||
const token = req.headers.authorization.substring(7) | ||
const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET) | ||
|
||
const { username, email, password } = req.body | ||
|
||
try { | ||
await registerAdmin(userId, username, email, password) | ||
res.status(200).send() | ||
|
||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof AuthorizationError) { | ||
error = 401 | ||
} | ||
|
||
if (error instanceof NotFoundError) { | ||
status = 404 | ||
} | ||
|
||
if (error instanceof ContentError || error instanceof TypeError) { | ||
status = 406 | ||
} | ||
|
||
if (error instanceof JsonWebTokenError) { | ||
status = 409 | ||
error = new TokenError(error.message) | ||
} | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
staff/abel-prieto/PROYECT/API/handlers/retrieveAllUsersHandler.js
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,39 @@ | ||
import jwt from 'jsonwebtoken' | ||
import retrieveAllUsers from '../logic/retrieveAllUsers.js' | ||
import { errors } from 'com' | ||
|
||
const { JsonWebTokenError } = jwt | ||
const { NotFoundError, AuthorizationError, TokenError, ContentError } = errors | ||
|
||
|
||
export default async (req, res) => { | ||
const token = req.headers.authorization.substring(7) | ||
const { sub: userId } = jwt.verify(token, process.env.JWT_SECRET) | ||
|
||
try { | ||
const allUsers = await retrieveAllUsers(userId) | ||
res.json(allUsers) | ||
|
||
} catch (error) { | ||
let status = 500 | ||
|
||
if (error instanceof NotFoundError) { | ||
status = 404 | ||
} | ||
|
||
if (error instanceof ContentError || error instanceof TypeError) { | ||
status = 406 | ||
} | ||
|
||
if (error instanceof AuthorizationError) { | ||
status = 409 | ||
} | ||
|
||
if (error instanceof JsonWebTokenError) { | ||
status = 401 | ||
error = new TokenError(error.message) | ||
} | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
staff/abel-prieto/PROYECT/API/test/html_test/retrieveAllUsers.test.html
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,20 @@ | ||
<script> | ||
const req = { | ||
method: 'GET', | ||
headers: { | ||
Authorization: 'Bearer 65e8c15ef35504a2bcecf912' | ||
} | ||
} | ||
|
||
fetch('http://localhost:9001/users/all', req) | ||
.catch(error => console.error(error)) | ||
.then(res => { | ||
if (res.ok) { | ||
res.json() | ||
.catch(error => console.error(error)) | ||
.then(body => console.log(res.status, body)) | ||
} else { | ||
console.error(res.status) | ||
} | ||
}) | ||
</script> |
62 changes: 62 additions & 0 deletions
62
staff/abel-prieto/PROYECT/API/test/retrieveAllUsers.spec.js
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,62 @@ | ||
import mongoose from 'mongoose' | ||
import random from './helpers/random.js' | ||
import dotenv from 'dotenv' | ||
import retrieveAllUsers from '../logic/retrieveAllUsers.js' | ||
import { User } from '../data/models.js' | ||
import { expect } from 'chai' | ||
import { errors } from 'com' | ||
const { NotFoundError, AuthorizationError } = errors | ||
|
||
dotenv.config() | ||
|
||
describe('retrieveAllUsers', () => { | ||
before(() => mongoose.connect(process.env.URL_MONGODB_TEST)) | ||
|
||
beforeEach(() => User.deleteMany()) | ||
|
||
// POSITIVE CASE | ||
it('success with retrieve ALL users', async () => { | ||
const admin = await User.create({ username: random.username(), email: random.email(), password: random.password(), group: 'root', role: 'admin' }) | ||
|
||
const user1 = await User.create({ username: random.username(), email: random.email(), password: random.password(), group: 'localhost', role: 'user' }) | ||
const user2 = await User.create({ username: random.username(), email: random.email(), password: random.password(), group: 'localhost', role: 'user' }) | ||
const user3 = await User.create({ username: random.username(), email: random.email(), password: random.password(), group: 'localhost', role: 'user' }) | ||
const user4 = await User.create({ username: random.username(), email: random.email(), password: random.password(), group: 'localhost', role: 'user' }) | ||
|
||
const allUsers = await retrieveAllUsers(admin.id) | ||
|
||
expect(allUsers).to.be.an('Array').that.has.lengthOf(4) | ||
expect(allUsers).to.includes(user1.username) | ||
expect(allUsers).to.includes(user2.username) | ||
expect(allUsers).to.includes(user3.username) | ||
expect(allUsers).to.includes(user4.username) | ||
}) | ||
|
||
// NEGATIVE CASE - Admin not found | ||
it('fails on admin not found', async () => { | ||
const userRequest = random.id() | ||
|
||
try { | ||
await retrieveAllUsers(userRequest) | ||
throw new Error('should not reach this point!') | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(NotFoundError) | ||
expect(error.message).to.be.equal('Admin not found. Try again') | ||
} | ||
}) | ||
|
||
// NEGATIVE CASE - Authorization denied | ||
it('fails on admin not found', async () => { | ||
const userRequest = await User.create({ username: random.username(), email: random.email(), password: random.password(), group: 'localhost', role: 'user' }) | ||
|
||
try { | ||
await retrieveAllUsers(userRequest.id) | ||
throw new Error('should not reach this point!') | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(AuthorizationError) | ||
expect(error.message).to.be.equal('Authorization denied. Only ADMIN user') | ||
} | ||
}) | ||
|
||
after(() => mongoose.disconnect()) | ||
}) |
Binary file removed
BIN
-21.9 KB
staff/abel-prieto/PROYECT/API/uploads/733345512377c5dea65af31cfe6a7bb3
Binary file not shown.
Binary file removed
BIN
-21.9 KB
staff/abel-prieto/PROYECT/API/uploads/e7976a8202fd4cc46609e66f5ac71bbb
Binary file not shown.