From b1368759f49c3ae140f69e056f4341c942157bf3 Mon Sep 17 00:00:00 2001 From: carlospelazas Date: Thu, 7 Mar 2024 11:13:54 +0100 Subject: [PATCH] [WIP] test + fixing paths --- game/gameservice/game-service.test.js | 40 +++++++++++++++++++++++++++ game/gameservice/gameLogic.js | 12 ++------ game/gameservice/user-service.test.js | 32 --------------------- gatewayservice/gateway-service.js | 9 ++++-- users/authservice/auth-service.js | 16 +++++++++++ users/userservice/user-model.js | 2 +- 6 files changed, 66 insertions(+), 45 deletions(-) create mode 100644 game/gameservice/game-service.test.js delete mode 100644 game/gameservice/user-service.test.js diff --git a/game/gameservice/game-service.test.js b/game/gameservice/game-service.test.js new file mode 100644 index 0000000..68cbfdb --- /dev/null +++ b/game/gameservice/game-service.test.js @@ -0,0 +1,40 @@ +const request = require('supertest'); +const { MongoMemoryServer } = require('mongodb-memory-server'); + +let mongoServer; +let app; + +beforeAll(async () => { + mongoServer = await MongoMemoryServer.create(); + const mongoUri = mongoServer.getUri(); + process.env.MONGODB_URI = mongoUri; + app = require('./gameservice'); +}); + +afterAll(async () => { + app.close(); + await mongoServer.stop(); +}); + +describe('Game service', () => { + + // mock data + const questions = [{ _id: 'question1_id' }, { _id: 'question2_id' }]; + const users = [{ _id: 'user1_id' }, { _id: 'user2_id' }]; + + it('should create a game and update user lastGame field', async () => { + const response = await request(app).post('/creategame').send({questions,users}); + expect(response.status).toBe(200); + expect(response.body).toHaveProperty('_id'); + + const gameId = response.body._id; + const gameFromDB = await mongoose.model('Game').findById(gameId); + const user1FromDB = await mongoose.model('User').findById('user1_id'); + const user2FromDB = await mongoose.model('User').findById('user2_id'); + + // Assertions for the database state + expect(gameFromDB).toBeTruthy(); + expect(user1FromDB.lastGame.toString()).toBe(gameId); + expect(user2FromDB.lastGame.toString()).toBe(gameId); + }); +}); \ No newline at end of file diff --git a/game/gameservice/gameLogic.js b/game/gameservice/gameLogic.js index d77df09..55f0b98 100644 --- a/game/gameservice/gameLogic.js +++ b/game/gameservice/gameLogic.js @@ -1,12 +1,10 @@ const Game = require("./game-model") const mongoose = require('mongoose'); -const {userSchema} = require("../../users/userservice/user-model"); -const {question4AnswersSchema} = require("../qgservice/Question4Answers"); +const {User} = require("../../users/userservice/user-model"); +const {Question4Answers} = require("../qgservice/Question4Answers"); async function createGame(questions, users) { try { - var Question4Answers = mongoose.model('Question4Answers', question4AnswersSchema); - var User = mongoose.model('User', userSchema); // Create a new Game instance const game = new Game({ _id: new mongoose.Types.ObjectId(), @@ -17,11 +15,7 @@ async function createGame(questions, users) { // Save the game to the database const savedGame = await game.save(); - users.forEach(async u => { - await User.findOneAndUpdate({ _id: u._id }, { lastGame: savedGame._id } ); - }); - - let populatedGame = await Game.findById(savedGame._id).populate('players').populate('questions'); + let populatedGame = await Game.findById(savedGame._id).populate('players').populate('questions').exec(); return populatedGame; } catch (error) { diff --git a/game/gameservice/user-service.test.js b/game/gameservice/user-service.test.js deleted file mode 100644 index 462169c..0000000 --- a/game/gameservice/user-service.test.js +++ /dev/null @@ -1,32 +0,0 @@ -/* -const request = require('supertest'); -const { MongoMemoryServer } = require('mongodb-memory-server'); - -let mongoServer; -let app; - -beforeAll(async () => { - mongoServer = await MongoMemoryServer.create(); - const mongoUri = mongoServer.getUri(); - process.env.MONGODB_URI = mongoUri; - app = require('./qg-service'); -}); - -afterAll(async () => { - app.close(); - await mongoServer.stop(); -}); - -describe('User Service', () => { - it('should add a new user on POST /adduser', async () => { - const newUser = { - username: 'testuser', - password: 'testpassword', - }; - - const response = await request(app).post('/adduser').send(newUser); - expect(response.status).toBe(200); - expect(response.body).toHaveProperty('username', 'testuser'); - }); -}); -*/ \ No newline at end of file diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index d716c0e..9b69373 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -56,10 +56,13 @@ app.get('/questionsGame', async (req, res) => { } }); -app.post('/addgame', async (req, res) => { +app.post('/createGame', async (req, res) => { try { - const response = await axios.post(gameServiceUrl+'/createGame', req.body); - res.json(response.data); + const createGameResponse = await axios.post(gameServiceUrl+'/creategame', req.body); + console.log("ESTOY AQUI") + const updateUserResponse = await axios.post(userServiceUrl+'/updateLastGame', createGameResponse.data); + + res.json(createGameResponse.data); } catch (error) { res.status(500).json({ error: 'Internal server error' }); } diff --git a/users/authservice/auth-service.js b/users/authservice/auth-service.js index 9764f08..8aaabf9 100644 --- a/users/authservice/auth-service.js +++ b/users/authservice/auth-service.js @@ -48,6 +48,22 @@ app.post('/login', async (req, res) => { } }); +app.post("/updateLastGame", async (req, res) => { + try { + const { _id, players } = req.body; + players.map(async (p) => { + const user = await User.findById(p._id); + user.lastGame = _id; + await user.save(); + console.log(user); + return user; + }) + res.json(user); + } catch (error) { + res.status(500).json({ error: 'Internal Server Error' }); + } +}) + // Start the server const server = app.listen(port, () => { console.log(`Auth Service listening at http://localhost:${port}`); diff --git a/users/userservice/user-model.js b/users/userservice/user-model.js index 862b0a7..560120b 100644 --- a/users/userservice/user-model.js +++ b/users/userservice/user-model.js @@ -29,4 +29,4 @@ const userSchema = new mongoose.Schema({ const User = mongoose.model('User', userSchema); -module.exports = {User, userSchema} \ No newline at end of file +module.exports = {User,userSchema}; \ No newline at end of file