Skip to content

Commit

Permalink
Pruebas restApi usuario
Browse files Browse the repository at this point in the history
  • Loading branch information
UO271548 committed Apr 14, 2022
1 parent d1a3dde commit 1a04d33
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 45 deletions.
3 changes: 2 additions & 1 deletion restapi/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DB_CONN_STRING="mongodb+srv://dede_es1a:[email protected]/myFirstDatabase?retryWrites=true&w=majority"
DB_NAME="dede-es1a"
DB_COLLECTION_NAME="myFirstDatabase"
DB_COLLECTION_NAME="myFirstDatabase"
DB_CONN_TEST_STRING="mongodb+srv://dede-es1a-Tests:[email protected]/myFirstDatabase?retryWrites=true&w=majority"
2 changes: 2 additions & 0 deletions restapi/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions restapi/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export const deleteUser = async (req: Request, res: Response): Promise<any> => {
let user = await User.deleteOne(
{ dni: dni }
);
res.setHeader('Content-Type', 'application/json');
res.status(200);
res.send(user);
};

Expand Down
166 changes: 122 additions & 44 deletions restapi/tests/api.test.ts
Original file line number Diff line number Diff line change
@@ -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:[email protected]/myFirstDatabase?retryWrites=true&w=majority", {
mongoose.connect(process.env.DB_CONN_TEST_STRING, {
useNewUrlParser: true,
useUnifiedTopology: true
});
});
});

afterAll(async () => {
Expand All @@ -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 = '[email protected]'
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 = '[email protected]'
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 = '[email protected]'
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("[email protected]");
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: "[email protected]",
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);
});
});

0 comments on commit 1a04d33

Please sign in to comment.