Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test #27

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open

Test #27

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/app.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
const request = require('supertest');
const jwt = require('jsonwebtoken');
const app = require('./app');

const app = require('./app'); */
/*
test('Modulo app definito', () => {
expect(app).toBeDefined();
});
Expand All @@ -10,4 +11,4 @@ test('GET / ritorna 200', () => {
return request(app)
.get('/')
.expect(200);
});
}); */
52 changes: 23 additions & 29 deletions app/authentication.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const request = require('supertest');
/*const request = require('supertest');
const app = require('./app');
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');

describe('POST /api/v1/authentications', () => {
const mongoose = require('mongoose'); */
/*
describe('POST /api/v1/authentications', () => { //POST per l'autenticazione (login)

let connection;

Expand All @@ -24,64 +24,58 @@ describe('POST /api/v1/authentications', () => {
.post('/api/v1/authentications')
.set('Accept', 'application/json')
.send({nickname: "", password: ""})
.expect(200, { success: false, message: "Nessun utente trovato!" });
.expect(401, { success: false, message: "Nessun utente trovato!" });
});

test('POST /api/v1/authentications con password vuota', () => {
return request(app)
.post('/api/v1/authentications')
.set('Accept', 'application/json')
.send({nickname: "RedRocco"})
.expect(200, { success: false, message: "Password errata!" });
.expect(401, { success: false, message: "Password errata!" });
});

test('POST /api/v1/authentications con nickname non esistente', () => {
return request(app)
.post('/api/v1/authentications')
.set('Accept', 'application/json')
.send({nickname: "GreenRocco"})
.expect(200, { success: false, message: "Nessun utente trovato!" });
.expect(401, { success: false, message: "Nessun utente trovato!" });
});

test('POST /api/v1/authentications con password errata', () => {
return request(app)
.post('/api/v1/authentications')
.set('Accept', 'application/json')
.send({nickname: "RedRocco", password: "Password2x"})
.expect(200, { success: false, message: "Password errata!" });
.expect(401, { success: false, message: "Password errata!" });
});

/*
// crea un token valido
var payload = {
email: "nikmen01@gmail.com",
email: "redroccoalpha@gmail.com",
id: "RedRocco"
}
}
var options = {
expiresIn: 86400 // Scade in 24 ore
}
var token = jwt.sign(payload, process.env.SUPER_SECRET, options);

/* test('POST /api/v1/authentications con nickname e password validi', () => {
test('POST /api/v1/authentications con nickname e password validi', () => {
return request(app)
.post('/api/v1/authentications')
.set('Accept', 'application/json')
.send({nickname: "RedRocco", password: "Password2"})
.expect(200, {
success: true,
message: "Login avvenuto con successo!",
token: expect.,
email: "nikmen01@gmail.com",
id: "64674993a06fea90b9243cf1",
nickname: "RedRocco"
});
}); */
.expect(200, {
success: true,
message: 'Login avvenuto con successo!',
token: token, //Come far combaciare il token?
email: 'redroccoalpha@gmail.com',
id: '6482f1f9631e9982b691f781',
nickname: 'RedRocco'
});
});

// Da implementare ban account
/* test('POST /api/v1/authentications con account bannato', () => {
return request(app)
.post('/api/v1/authentications')
.set('Accept', 'application/json')
.send({nickname: "BannedRocco", password: "Password2"})
.expect(200, { success: false });
}); */
});
}); //NOTOK
*/
148 changes: 148 additions & 0 deletions app/tornei.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
const request = require('supertest'); // utile per richiamare le api con metodi GET SET PUT ecc...
const app = require('./app'); // Questo fa partire l'app principale altrimenti non funziona nulla
const jwt = require('jsonwebtoken'); // Per creare i token
const mongoose = require('mongoose'); // libreria per collegarsi al db
const torneo = require('./models/torneo'); */

/*
describe('POST /api/v1/tornei', () => { //POST per la creazione e il salvataggio di un torneo

beforeAll( async () => {
jest.unmock('mongoose');
connection = await mongoose.connect(process.env.DB_URL, {useNewUrlParser: true, useUnifiedTopology: true});
console.log('Database connected!');
});

afterAll( () => {
mongoose.connection.close(true);
console.log("Database connection closed");
});

test('POST /api/v1/tornei Creazione torneo incompleto e salvataggio', () => { //L'id del torneo cambia sempre, come fare? -> mock function? Inoltre, questo test si può fare solo una volta e poi va modificato il nome o eliminato dal db..
return request(app)
.post('/api/v1/tornei')
.set('Accept', 'application/json')
.send({nomeTorneo: 'Torneo dei test'})
.expect(200, {success: true, message: 'Torneo salvato correttamente!', _id: '6485951c8e42dc216932a92a'});
})

}); //NOTOK

describe('PUT /api/v1/tornei', () => { //PUT per la modifica dei dati di un torneo - mock function da sistemare!

// Moking del metodo torneo.findOne
let torneoSpy;

beforeAll( async () => {
const Torneo = require('./models/torneo');
torneoSpy = jest.spyOn(Torneo,'find').mockImplementation((criterias) => { //Mi da un errore che non capisco come risolvere!
return {_id:'647de69fca346186e39935e0'}; //Torneo "Patronato school" di RedRocco
});
});

afterAll( () => {
torneoSpy.mockRestore();
});

test('PUT /api/v1/tornei Modifica a torneo salvato', () => { //Questo test scritto così è valido solo una volta e poi va modificato!
return request(app)
.put('/api/v1/tornei')
.set('Accept', 'application/json')
.send({nomeTorneo: "TorneoX"})
.expect(200, {success: true, message: 'Torneo salvato correttamente!', _id: '647de69fca346186e39935e0' });
})

}); //NOTOK

describe('GET /api/v1/tornei/list', () => { //GET per ottenere l'elenco dei tornei

beforeAll( async () => {
jest.unmock('mongoose');
connection = await mongoose.connect(process.env.DB_URL, {useNewUrlParser: true, useUnifiedTopology: true});
console.log('Database connected!');
});

afterAll( () => {
mongoose.connection.close(true);
console.log("Database connection closed");
});

test('GET /api/v1/tornei/list Restituisce un array di tornei', async () => {
return request(app)
.get('/api/v1/tornei/list')
.expect('Content-Type', /json/)
.expect(res.body[0]).toEqual({
success: true,
idTorneo: '647cbe409722939a7a15cee6'
});
});

}); //NOTOK

describe('GET /api/v1/tornei/:id', () => { //GET restituisce json

beforeAll( async () => {
jest.unmock('mongoose');
connection = await mongoose.connect(process.env.DB_URL, {useNewUrlParser: true, useUnifiedTopology: true});
console.log('Database connected!');
});

afterAll( () => {
mongoose.connection.close(true);
console.log("Database connection closed");
});

test('GET /api/v1/tornei/:id Restituisce json', async () => {
return request(app)
.get('/api/v1/tornei/647cbe409722939a7a15cee6')
.expect('Content-Type', /json/)
.expect(200, {
success: true,
organizzatore:'Tmao',
nomeTorneo: 'Winners here',
argomento: 'Magic',
id_img: 104,
zona: 'Pove del Grappa',
bio: 'Ciao a tutti, sto organizzando un torneo di Magic a Pove del Grappa, vi aspetto tutti!!',
regolamento: 'Regolamento legacy standard ',
numeroSquadre: 8,
numeroGiocatori: 1,
formatoT: 'eliminazione',
numeroGironi: null,
formatoP: 'bo1',
pubblicato: true,
tags: [307],
piattaforma: '0',
dataInizio: '10/07/2023',
terminato: false,
fasi: 1,
faseAttuale: 0,
});
});

}); //NOTOK

describe('GET verso /api/v1/tornei/:idTorneo', () => { //GET per ottenere i dati di un torneo

beforeAll( async () => {
jest.unmock('mongoose');
connection = await mongoose.connect(process.env.DB_URL, {useNewUrlParser: true, useUnifiedTopology: true});
console.log('Database connected!');
});

afterAll( () => {
mongoose.connection.close(true);
console.log("Database connection closed");
});

test('GET /api/v1/tornei/:idTorneo Restituisce i dati di un torneo', () => {
return request(app)
.get('/api/v1/:idTorneo')
.set('Accept', 'application/json')
.send({_id: '647de69fca346186e39935e0'})
.expect(200, { });
})

}); //NOTOK
*/
Loading