Skip to content

Commit

Permalink
[WIP] test + fixing paths
Browse files Browse the repository at this point in the history
  • Loading branch information
pelazas committed Mar 7, 2024
1 parent 9887776 commit b136875
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 45 deletions.
40 changes: 40 additions & 0 deletions game/gameservice/game-service.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
12 changes: 3 additions & 9 deletions game/gameservice/gameLogic.js
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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) {
Expand Down
32 changes: 0 additions & 32 deletions game/gameservice/user-service.test.js

This file was deleted.

9 changes: 6 additions & 3 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
Expand Down
16 changes: 16 additions & 0 deletions users/authservice/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
2 changes: 1 addition & 1 deletion users/userservice/user-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ const userSchema = new mongoose.Schema({

const User = mongoose.model('User', userSchema);

module.exports = {User, userSchema}
module.exports = {User,userSchema};

0 comments on commit b136875

Please sign in to comment.