From e2ef4d20633c9960fe55bd397fee10de8a2dccd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20G=C3=B3mez=20Gasch?= <75808025+UO271548@users.noreply.github.com> Date: Thu, 14 Apr 2022 17:14:43 +0200 Subject: [PATCH 1/3] =?UTF-8?q?Correcci=C3=B3n=20b=C3=BAsqueda=20por=20cri?= =?UTF-8?q?terio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- restapi/api.ts | 6 ++---- restapi/controllers/RockController.ts | 12 ++---------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/restapi/api.ts b/restapi/api.ts index 6641ad2..61a3a90 100644 --- a/restapi/api.ts +++ b/restapi/api.ts @@ -1,7 +1,7 @@ import express, { Request, Response, Router } from 'express'; import {check} from 'express-validator'; import { addOrder, findOrdersByUserDni, getDeliveryCosts } from './controllers/OrderController'; -import {findRocks, addRock, deleteRock, findRocksSedimentary, findRocksMetamorphic, findRocksFiery, findByCriteryText, findByCriteryNumber} from './controllers/RockController'; +import {findRocks, addRock, deleteRock, findRocksSedimentary, findRocksMetamorphic, findRocksFiery, findByCritery} from './controllers/RockController'; import {findUsers, addUser, deleteUser, loginUser, logout} from './controllers/UserController'; const User = require("./models/User"); const Rock = require("./models/Rock"); @@ -30,9 +30,7 @@ api.get("/rocks/list/metamorphic", findRocksMetamorphic); api.get("/rocks/list/fiery", findRocksFiery); -api.get("/rocks/list/criteryText", findByCriteryText); - -api.get("/rocks/list/criteryNumber", findByCriteryNumber); +api.get("/rocks/list/critery", findByCritery); api.post("/rocks/add", addRock); diff --git a/restapi/controllers/RockController.ts b/restapi/controllers/RockController.ts index 89cdd80..0737a65 100644 --- a/restapi/controllers/RockController.ts +++ b/restapi/controllers/RockController.ts @@ -32,17 +32,9 @@ export const findRocksMetamorphic = async (req:Request, res:Response) => { res.send(rocks); }; -export const findByCriteryText = async (req:Request, res:Response) => { +export const findByCritery = async (req:Request, res:Response) => { let critery = req.body.critery; - const rocks = await Rock.find({$or:[{name : critery}, {type : critery}, {texture : critery} ]}) - res.setHeader('Content-Type', 'application/json'); - res.status(200); - res.send(rocks); -} - -export const findByCriteryNumber = async (req:Request, res:Response) => { - let critery = req.body.critery; - const rocks = await Rock.find({$or:[{price : critery}, {mohsHardness : critery}, {density : critery}]}) + const rocks = await Rock.find(critery) res.setHeader('Content-Type', 'application/json'); res.status(200); res.send(rocks); From ef11fd63b5ec6f2b72870c5f7f40fa4787cb78d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20G=C3=B3mez=20Gasch?= <75808025+UO271548@users.noreply.github.com> Date: Thu, 14 Apr 2022 17:28:34 +0200 Subject: [PATCH 2/3] =?UTF-8?q?Cambios=20obtenci=C3=B3n=20Orders=20usuario?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- restapi/api.ts | 4 ++-- restapi/controllers/OrderController.ts | 14 ++++++++------ restapi/models/Order.ts | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/restapi/api.ts b/restapi/api.ts index 61a3a90..b39e558 100644 --- a/restapi/api.ts +++ b/restapi/api.ts @@ -1,6 +1,6 @@ import express, { Request, Response, Router } from 'express'; import {check} from 'express-validator'; -import { addOrder, findOrdersByUserDni, getDeliveryCosts } from './controllers/OrderController'; +import { addOrder, findOrdersByUserEmail, getDeliveryCosts } from './controllers/OrderController'; import {findRocks, addRock, deleteRock, findRocksSedimentary, findRocksMetamorphic, findRocksFiery, findByCritery} from './controllers/RockController'; import {findUsers, addUser, deleteUser, loginUser, logout} from './controllers/UserController'; const User = require("./models/User"); @@ -37,7 +37,7 @@ api.post("/rocks/add", addRock); api.post("/rocks/delete", deleteRock); //Methods for product of the app -api.get("/orders/userList", findOrdersByUserDni); +api.get("/orders/userList", findOrdersByUserEmail); api.post("/orders/add", addOrder); diff --git a/restapi/controllers/OrderController.ts b/restapi/controllers/OrderController.ts index 98fbf02..d669fb7 100644 --- a/restapi/controllers/OrderController.ts +++ b/restapi/controllers/OrderController.ts @@ -9,24 +9,26 @@ const geocoder = NodeGeocoder(options); const Order = require("../models/Order"); -export const findOrdersByUserDni = async (req:Request, res:Response) => { - let dni = req.body.dni; - let query = {userDni : dni.toString()}; +export const findOrdersByUserEmail = async (req:Request, res:Response) => { + let email = req.body.email; + let query = {userEmail : email.toString()}; const users = await Order.find(query) - res.send(users).json(); + res.setHeader('Content-Type', 'application/json'); + res.send(users); }; + export const addOrder = async (req:Request, res:Response): Promise => { const crypto = require('crypto'); const orderId = crypto.randomBytes(60); - let userDni = req.body.dni; + let email = req.body.email; let price = req.body.price; let productId = req.body.productId; let order = new Order({ orderId: orderId, - dni: userDni, + userEmail: email, price: price, productId: productId }); diff --git a/restapi/models/Order.ts b/restapi/models/Order.ts index 62f6b21..1464d1c 100644 --- a/restapi/models/Order.ts +++ b/restapi/models/Order.ts @@ -5,7 +5,7 @@ const mongooseOrders = require("mongoose") const orderSchema = mongooseOrders.Schema({ orderId : String, price : Number, - userDni : String, + userEmail : String, productId : String },{ versionKey: false From 1a04d33891be9199c70aef8e585e50e32683dac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20G=C3=B3mez=20Gasch?= <75808025+UO271548@users.noreply.github.com> Date: Thu, 14 Apr 2022 20:03:57 +0200 Subject: [PATCH 3/3] Pruebas restApi usuario --- restapi/.env | 3 +- restapi/api.ts | 2 + restapi/controllers/UserController.ts | 2 + restapi/tests/api.test.ts | 166 +++++++++++++++++++------- 4 files changed, 128 insertions(+), 45 deletions(-) diff --git a/restapi/.env b/restapi/.env index 15687e9..e61fa92 100644 --- a/restapi/.env +++ b/restapi/.env @@ -1,3 +1,4 @@ DB_CONN_STRING="mongodb+srv://dede_es1a:1234@dede-es1a.shdhg.mongodb.net/myFirstDatabase?retryWrites=true&w=majority" DB_NAME="dede-es1a" -DB_COLLECTION_NAME="myFirstDatabase" \ No newline at end of file +DB_COLLECTION_NAME="myFirstDatabase" +DB_CONN_TEST_STRING="mongodb+srv://dede-es1a-Tests:1234@dede-es1a-tests.z8kxq.mongodb.net/myFirstDatabase?retryWrites=true&w=majority" \ No newline at end of file diff --git a/restapi/api.ts b/restapi/api.ts index 8461934..0739ecc 100644 --- a/restapi/api.ts +++ b/restapi/api.ts @@ -18,6 +18,8 @@ api.post("/users/login", loginUser); api.get("/users/logout", logout); +api.post("/users/delete", deleteUser); + //Methods for product of the app api.get("/rocks/list", findRocks); diff --git a/restapi/controllers/UserController.ts b/restapi/controllers/UserController.ts index 5009454..efa1ab3 100644 --- a/restapi/controllers/UserController.ts +++ b/restapi/controllers/UserController.ts @@ -58,6 +58,8 @@ export const deleteUser = async (req: Request, res: Response): Promise => { let user = await User.deleteOne( { dni: dni } ); + res.setHeader('Content-Type', 'application/json'); + res.status(200); res.send(user); }; diff --git a/restapi/tests/api.test.ts b/restapi/tests/api.test.ts index 3b80bb1..bea457f 100644 --- a/restapi/tests/api.test.ts +++ b/restapi/tests/api.test.ts @@ -1,38 +1,56 @@ -import request, {Response} from 'supertest'; +import request, { Response } from 'supertest'; import express, { Application } from 'express'; import * as http from 'http'; import bp from 'body-parser'; import cors from 'cors'; import api from '../api'; -import {findUsers, addUser, deleteUser, loginUser, logout} from '../controllers/UserController'; +import { findUsers, addUser, deleteUser, loginUser, logout } from '../controllers/UserController'; +import path from 'path'; -let app:Application; -let server:http.Server; +let app: Application; +let server: http.Server; const mongoose = require('mongoose'); +let envPath = path.resolve("../.env"); +require('dotenv').config({ path: envPath }); + +let expressSession = require('express-session'); +declare global { + namespace Express { + interface Request { + session: typeof expressSession; + } + } +} beforeAll(async () => { - + app = express(); - const port: number = 5000; + const options: cors.CorsOptions = { origin: ['http://localhost:3000'] }; app.use(cors(options)); app.use(bp.json()); - app.use("/api", api) - api.get("/users/list", findUsers); + app.use(expressSession({ + secret: 'abcdefg', + resave: true, + saveUninitialized: true, + })); + + app.use("/api", api) - server = app.listen(port, ():void => { - console.log('Restapi server for testing listening on '+ port); - }).on("error",(error:Error)=>{ + const port: number = 5000; + server = app.listen(port, (): void => { + console.log('Restapi server for testing listening on ' + port); + }).on("error", (error: Error) => { console.error('Error occured: ' + error.message); }); - mongoose.connect("mongodb+srv://dede_es1a:1234@dede-es1a.shdhg.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", { + mongoose.connect(process.env.DB_CONN_TEST_STRING, { useNewUrlParser: true, useUnifiedTopology: true - }); + }); }); afterAll(async () => { @@ -41,51 +59,111 @@ afterAll(async () => { }) describe('user ', () => { + + jest.setTimeout(100000); + const crypto = require('crypto'); + const pass = crypto.randomBytes(4).toString('utf8'); + /** - * Test that we can list users without any error. + * Tests that a user can be created through the user without throwing any errors. */ - it('can be listed',async () => { - const response:Response = await request(app).get("/users/list"); + it('can be created correctly', async () => { + + let username: string = 'Pablo' + let email: string = 'gonzalezgpablo@uniovi.es' + const response: Response = await request(app).post('/api/users/add') + .send({ + dni: "1", + name: username, + email: email, + rol: 1, + password: pass, + repeatPassword: pass + }) + .set('Accept', 'application/json') expect(response.statusCode).toBe(200); }); - /** - * Tests that a user can be created through the productService without throwing any errors. - */ - it('can be created correctly', async () => { - let username:string = 'Pablo' - let email:string = 'gonzalezgpablo@uniovi.es' - const crypto = require('crypto'); - const pass = crypto.randomBytes(4).toString('utf8'); - const response:Response = await request(app).post('/users/add') - .send({dni: "1", + it('cant be created correctly', async () => { + + let username: string = 'Pablo' + let email: string = 'gonzalezgpablo@uniovi.es' + const response: Response = await request(app).post('/api/users/add') + .send({ + dni: "1", name: username, email: email, rol: 1, - password: pass}) - .set('Accept', 'application/json') - //expect(response.statusCode).toBe(200); + password: pass, + repeatPassword: pass + }) + .set('Accept', 'application/json') + expect(response.statusCode).toBe(401); + }); + + /** + * Test that we can list users without any error. + */ + it('can be listed', async () => { + const response: Response = await request(app).get("/api/users/list"); + expect(response.body[0].email).toBe("gonzalezgpablo@uniovi.es"); + expect(response.statusCode).toBe(200); + }); + + /** + * Test that we can login users without any error. + */ + it('can be login', async () => { + const response: Response = await request(app).post("/api/users/login") + .send({ + email: "gonzalezgpablo@uniovi.es", + password: pass + }) + .set('Accept', 'application/json'); + expect(response.statusCode).toBe(200); + }); + + /** +* Test that we can logout users without any error. +*/ + it('can be logout', async () => { + const response: Response = await request(app).get("/api/users/logout"); + expect(response.statusCode).toBe(200); + }); + + /** + * Tests that a user can be deleted through the user without throwing any errors. + */ + it('can be deleted', async () => { + const response: Response = await request(app).post("/api/users/delete") + .send({ dni: "1" }) + .set('Accept', 'application/json'); + expect(response.statusCode).toBe(200); }); + }); describe('product ', () => { - it('can be listed',async () => { - const response:Response = await request(app).get("/rocks/list"); - // expect(response.statusCode).toBe(200); + jest.setTimeout(10000); + it('can be listed', async () => { + const response: Response = await request(app).get("/api/rocks/list"); + expect(response.statusCode).toBe(200); }); it('can be created correctly', async () => { - const response:Response = await request(app).post('/rocks/add'). - send({rockId:"prueba", - name: "prueba", - type: "prueba", - description: "prueba", - price: 1, - mohsHardness:1, - density:1, - texture:"prueba", - img: "prueba"}) - .set('Accept', 'application/json') - // expect(response.statusCode).toBe(200); + const response: Response = await request(app).post('/api/rocks/add'). + send({ + rockId: "prueba", + name: "prueba", + type: "prueba", + description: "prueba", + price: 1, + mohsHardness: 1, + density: 1, + texture: "prueba", + img: "prueba" + }) + .set('Accept', 'application/json') + expect(response.statusCode).toBe(200); }); }); \ No newline at end of file