generated from Arquisoft/wiq_0
-
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.
Merge pull request #154 from Arquisoft/develop_samuel
Added in-memory database for Users testing
- Loading branch information
Showing
13 changed files
with
265 additions
and
192 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
teamname="wiq_es04a" | ||
NODE_ENV='' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const { Statistics, sequelize } = require('../../models/user-model.js'); | ||
const request = require('supertest'); | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const statisticsRoutes = require('../../routes/statistics-routes.js'); | ||
const { User } = require('../../models/user-model.js'); | ||
|
||
const app = express(); | ||
app.use(bodyParser.json()); | ||
app.use('/statistics', statisticsRoutes); | ||
|
||
describe('Statistics Routes', () => { | ||
beforeAll(async () => { | ||
await sequelize.authenticate(); | ||
await sequelize.sync({ force: true }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await sequelize.close(); | ||
}); | ||
|
||
it('should update user statistics', async () => { | ||
const newUser = { | ||
username: 'testuser', | ||
password: 'Test1234', | ||
name: 'Test', | ||
surname: 'User' | ||
}; | ||
|
||
// Create user for the statistics | ||
await User.create(newUser); | ||
|
||
const initialStatistics = { | ||
username: 'testuser', | ||
earned_money: 100, | ||
classic_correctly_answered_questions: 5, | ||
classic_incorrectly_answered_questions: 2, | ||
classic_total_time_played: 3600, | ||
classic_games_played: 3 | ||
}; | ||
|
||
await Statistics.create(initialStatistics); | ||
|
||
const updatedStatistics = { | ||
username: 'testuser', | ||
earned_money: 50, | ||
classic_correctly_answered_questions: 3, | ||
classic_incorrectly_answered_questions: 1, | ||
classic_total_time_played: 1800, | ||
classic_games_played: 2 | ||
}; | ||
|
||
const response = await request(app) | ||
.post('/statistics/edit') | ||
.send(updatedStatistics); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body.message).toBe('User statics updated successfully'); | ||
|
||
// Check if the statistics are updated in the database | ||
const userStatistics = await Statistics.findOne({ where: { username: updatedStatistics.username } }); | ||
|
||
expect(userStatistics.earned_money).toBe(initialStatistics.earned_money + updatedStatistics.earned_money); | ||
expect(userStatistics.classic_correctly_answered_questions).toBe(initialStatistics.classic_correctly_answered_questions + updatedStatistics.classic_correctly_answered_questions); | ||
expect(userStatistics.classic_incorrectly_answered_questions).toBe(initialStatistics.classic_incorrectly_answered_questions + updatedStatistics.classic_incorrectly_answered_questions); | ||
expect(userStatistics.classic_total_time_played).toBe(initialStatistics.classic_total_time_played + updatedStatistics.classic_total_time_played); | ||
expect(userStatistics.classic_games_played).toBe(initialStatistics.classic_games_played + updatedStatistics.classic_games_played); | ||
}); | ||
|
||
it('should return error if user not found', async () => { | ||
const nonExistingUserStatistics = { | ||
username: 'nonexistinguser', | ||
earned_money: 50, | ||
classic_correctly_answered_questions: 3, | ||
classic_incorrectly_answered_questions: 1, | ||
classic_total_time_played: 1800, | ||
classic_games_played: 2 | ||
}; | ||
|
||
const response = await request(app) | ||
.post('/statistics/edit') | ||
.send(nonExistingUserStatistics); | ||
|
||
expect(response.status).toBe(404); | ||
expect(response.body.error).toBe('User not found'); | ||
}); | ||
}); |
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,76 @@ | ||
const { User, Statistics, sequelize } = require('../../models/user-model.js'); | ||
const bcrypt = require('bcrypt'); | ||
const request = require('supertest'); | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const userRoutes = require('../../routes/user-routes.js'); | ||
|
||
const app = express(); | ||
app.use(bodyParser.json()); | ||
app.use('/user', userRoutes); | ||
|
||
describe('User Routes', () => { | ||
beforeAll(async () => { | ||
await sequelize.authenticate(); | ||
await sequelize.sync({ force: true }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await sequelize.close(); | ||
}); | ||
|
||
it('should add a new user', async () => { | ||
const newUser = { | ||
username: 'testuser', | ||
password: 'Test1234', | ||
name: 'Test', | ||
surname: 'User' | ||
}; | ||
|
||
const response = await request(app) | ||
.post('/user/add') | ||
.send(newUser); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body.username).toBe(newUser.username); | ||
|
||
// Check if the user exists in the database | ||
const user = await User.findOne({ where: { username: newUser.username } }); | ||
expect(user).toBeDefined(); | ||
|
||
// Check if the password is hashed | ||
const isPasswordCorrect = await bcrypt.compare(newUser.password, user.password); | ||
expect(isPasswordCorrect).toBe(true); | ||
|
||
// Check if statistics for the user are created | ||
const statistics = await Statistics.findOne({ where: { username: newUser.username } }); | ||
expect(statistics).toBeDefined(); | ||
}); | ||
|
||
it('should not add a user if username already exists', async () => { | ||
const existingUser = { | ||
username: 'existinguser', | ||
password: 'Test1234', | ||
name: 'Existing', | ||
surname: 'User' | ||
}; | ||
|
||
// Create the existing user in the database | ||
await User.create({ | ||
username: existingUser.username, | ||
password: await bcrypt.hash(existingUser.password, 10), | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
name: existingUser.name, | ||
surname: existingUser.surname | ||
}); | ||
|
||
// Try to add the existing user again | ||
const response = await request(app) | ||
.post('/user/add') | ||
.send(existingUser); | ||
|
||
expect(response.status).toBe(400); | ||
expect(response.body.error).toBe('An account with that username already exists'); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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,53 @@ | ||
const { Statistics, sequelize } = require('../../models/user-model.js'); | ||
const request = require('supertest'); | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const statisticsRoutes = require('../../routes/statistics-routes.js'); | ||
const { User } = require('../../models/user-model.js'); | ||
|
||
const app = express(); | ||
app.use(bodyParser.json()); | ||
app.use('/statistics', statisticsRoutes); | ||
|
||
describe('Statistics Routes', () => { | ||
beforeAll(async () => { | ||
await sequelize.authenticate(); | ||
await sequelize.sync({ force: true }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await sequelize.close(); | ||
}); | ||
|
||
it('should get user statistics by username', async () => { | ||
const newUser = { | ||
username: 'testuser', | ||
password: 'Test1234', | ||
name: 'Test', | ||
surname: 'User' | ||
}; | ||
|
||
// Create user for the statistics | ||
await User.create(newUser); | ||
|
||
const initialStatistics = { | ||
earned_money: 100, | ||
classic_correctly_answered_questions: 5, | ||
classic_incorrectly_answered_questions: 2, | ||
classic_total_time_played: 3600, | ||
classic_games_played: 3 | ||
}; | ||
|
||
await Statistics.create({ username: newUser.username, ...initialStatistics }); | ||
|
||
const response = await request(app) | ||
.get(`/statistics/api/${newUser.username}`); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body.earned_money).toEqual(initialStatistics.earned_money); | ||
expect(response.body.classic_correctly_answered_questions).toEqual(initialStatistics.classic_correctly_answered_questions); | ||
expect(response.body.classic_incorrectly_answered_questions).toEqual(initialStatistics.classic_incorrectly_answered_questions); | ||
expect(response.body.classic_total_time_played).toEqual(initialStatistics.classic_total_time_played); | ||
expect(response.body.classic_games_played).toEqual(initialStatistics.classic_games_played); | ||
}); | ||
}); |
Oops, something went wrong.