Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

UserAPI merge #268

Merged
merged 13 commits into from
Apr 18, 2024
Merged
5 changes: 4 additions & 1 deletion auth_service/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const YAML = require('yaml')

// My own libs
const auth = require('./auth/authEndpoints');
const user = require('./user/userEndpoints')

const port = 8001;
const app = express();
Expand All @@ -28,7 +29,9 @@ app.post("/api/auth/register", auth.register);
app.post("/api/auth/login", auth.login);
app.post("/api/auth/verify", auth.verify);
app.post("/api/auth/getName", auth.getUsername);
app.post("/api/auth/getUsers", auth.getUsers);
app.get("/api/user/getUsers", user.getUsers);
app.get("/api/user/getUser", user.getUser);
app.post("/api/user/deleteUser", user.deleteUser);
app.get('/health', (req, res) => {
res.json({ status: 'OK' });
});
Expand Down
65 changes: 65 additions & 0 deletions auth_service/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,69 @@ describe('Authentication Endpoints', () => {
expect(response.body.name).toBe(mockUser.name);
});

it("Should return 200 and the users", async() =>{

await User.create({
id: 2,
name: 'prueba2',
password: 'contraseña2'
});

const response = await request(app)
.get('/api/user/getUsers');
expect(response.statusCode).toBe(200);
expect(response.body).toEqual([{name: 'testuser', id:1},{name: 'prueba2',id:2}]);
});

it("Should return 200 and the mocked user",async()=>{
const response = await request(app)
.get('/api/user/getUser')
.query({user_id:1});
expect(response.statusCode).toBe(200);
expect(response.body.user.name).toEqual(mockUser.name)

});

it("Should return 401 because the user doesn't exist",async()=>{
const response = await request(app)
.get('/api/user/getUser')
.query({user_id:2});
expect(response.statusCode).toBe(401);

});

it("Should return 401 because it needs a parameter",async()=>{
const response = await request(app)
.get('/api/user/getUser')
expect(response.statusCode).toBe(401);

});

it("Shoud return 200 and delete the user",async()=>{
const response = await request(app)
.post('/api/user/deleteUser')
.query({user_id:1});
expect(response.statusCode).toBe(200);

//try to find the deleted user
const response2 = await request(app)
.get('/api/user/getUser')
.query({user_id:1});
expect(response2.statusCode).toBe(401);
});

it("Shoud return 401 because it needs a parameter in delete",async()=>{
const response = await request(app)
.post('/api/user/deleteUser')
expect(response.statusCode).toBe(401);
});

it("Shoud return 401 because the user doesn't exist in delete",async()=>{
console.log('este')
const response = await request(app)
.post('/api/user/deleteUser')
.query({user_id:8});
expect(response.statusCode).toBe(401);
});

});
10 changes: 1 addition & 9 deletions auth_service/auth/authEndpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,6 @@ const getUsername = async (req,res) => {
});
}

const getUsers = async (req,res) => {

let userf = await User.findAll();

res.status(200).json(userf.map(user => {return {
name: user.name,
id: user.id
}}));
}

module.exports = {login, register, verify, getUsername, getUsers}
module.exports = {login, register, verify, getUsername}
69 changes: 69 additions & 0 deletions auth_service/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,72 @@ paths:
example: "OK"
'500':
description: Internal server error

/api/user/getUsers:
get:
summary: Obtener todos los usuarios
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
type: object
properties:
name:
type: string
example: "User1"
id:
type: integer
example: "1234"

/api/user/getUser:
get:
summary: Obtener un usuario por ID
parameters:
- in: query
name: user_id
required: true
schema:
type: integer
example: "1234"
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
user:
type: object
properties:
name:
type: string
example: "User1"
id:
type: integer
example: "1234"
'401':
description: Usuario no encontrado
requestBody:
required: false
content:
application/json: {}

/api/user/deleteUser:
post:
summary: Eliminar un usuario por ID
requestBody:
required: true
content:
application/json: {}
responses:
'200':
description: Usuario borrado
'401':
description: Usuario no encontrado
'500':
description: Error del servidor
92 changes: 92 additions & 0 deletions auth_service/user/userEndpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

// Model
const { User } = require('../models');



function validateRequiredFields(req, requiredFields) {
for (const field of requiredFields) {
if (!(field in req.query)) {
return false;
}
}
return true;
}



const getUsers = async (req,res) => {
let users = await User.findAll();
res.status(200).json(users.map(user => ({
name: user.name,
id: user.id
})));
}

const getUser = async (req, res) => {

try{

if(!validateRequiredFields(req,['user_id'])){
res
.status(401)
.json({error:"Falta el campo userid"})
.send();
return;
}

let userid = req.query.user_id;
let user = await User.findOne({
where: {
id: userid
}
})

if(user == undefined){
res
.status(401)
.json({error:"Usuario no encontrado"})
.send();
return;
}

res.status(200).json({
user: user
});

}catch (error){
res.status(500).send();
}
}

const deleteUser = async (req, res) => {

try{

if(!validateRequiredFields(req,['user_id'])){
res
.status(401)
.json({error:"Falta el campo userid"})
.send();
return;
}


let userid = req.query.user_id;

let result = await User.destroy({ where: { id: userid } });

if (result === 0) {
return res.status(401).json({ error: "Usuario no encontrado" });
}

return res.status(200).json({ message: "Usuario borrado" });


}catch (error){
res.status(500).send();
}
}


module.exports = {getUsers, getUser, deleteUser}
112 changes: 112 additions & 0 deletions friends_service/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
openapi: 3.0.0
info:
title: Friends API
description: API for friend requests and operations
version: 1.0.0
servers:
- url: http://localhost:3000
paths:
/api/friends/request/send:
post:
summary: Send friend request
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
token:
type: string
example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxMjM0NTY3ODkwLCJpYXQiOjE1MTYyMzkwMjJ9.UzI1NiIsInR5cCI6IkpXVCJ9"
to:
type: string
example: "user_id_to_send_request"
required:
- token
- to
responses:
'200':
description: OK
'400':
description: Invalid request or there is already a pending request or they are already friends
/api/friends/request/accept:
post:
summary: Accept friend request
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
token:
type: string
example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxMjM0NTY3ODkwLCJpYXQiOjE1MTYyMzkwMjJ9.UzI1NiIsInR5cCI6IkpXVCJ9"
from:
type: string
example: "user_id_of_request_sender"
required:
- token
- from
responses:
'200':
description: Friend request accepted
'400':
description: Invalid request or request not found
/api/friends/request/:
post:
summary: Get pending friend requests
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
token:
type: string
example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxMjM0NTY3ODkwLCJpYXQiOjE1MTYyMzkwMjJ9.UzI1NiIsInR5cCI6IkpXVCJ9"
required:
- token
responses:
'200':
description: OK
/api/friends/:
post:
summary: Get friends list
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
token:
type: string
example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxMjM0NTY3ODkwLCJpYXQiOjE1MTYyMzkwMjJ9.UzI1NiIsInR5cCI6IkpXVCJ9"
required:
- token
responses:
'200':
description: OK
components:
schemas:
FriendRequest:
type: object
properties:
from:
type: string
example: "user_id_of_request_sender"
to:
type: string
example: "user_id_to_receive_request"
Friendship:
type: object
properties:
friend1:
type: string
example: "user_id1"
friend2:
type: string
example: "user_id2"
2 changes: 1 addition & 1 deletion webapp/src/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const getUsers = async () =>
{
try {

const response = await axios.post(`${apiEndpoint}:8001/api/auth/getUsers`);
const response = await axios.post(`${apiEndpoint}:8001/api/user/getUsers`);

return response.data;

Expand Down