From 103b43bc27fe44daefe1e685e680a8d9265c3efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20M=C3=A9ndez=20Murias?= Date: Sun, 7 Apr 2024 23:26:15 +0200 Subject: [PATCH] Changes on tests --- gatewayservice/gateway-service.js | 4 --- gatewayservice/gateway-service.test.js | 43 ++++++++++++++++++++++++-- webapp/src/test/AddUser.test.js | 36 +++++++++++++++++++++ webapp/src/test/PostGame.test.js | 32 +++++++++++++++++++ 4 files changed, 109 insertions(+), 6 deletions(-) diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 8a428834..73402ba5 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -188,10 +188,6 @@ app.post('/addgame', async (req, res) => { app.get('/getParticipation/:userId', async (req, res) => { try { const userId = req.params.userId; - if (!userId) { - res.status(404).json({ error: 'User ID not provided' }); - return; - } const apiUrl = `${gameServiceUrl}/getParticipation/${userId}`; try{ const gameResponse = await axios.get(apiUrl); diff --git a/gatewayservice/gateway-service.test.js b/gatewayservice/gateway-service.test.js index 61adf000..056d90bd 100644 --- a/gatewayservice/gateway-service.test.js +++ b/gatewayservice/gateway-service.test.js @@ -26,6 +26,17 @@ describe('Gateway Service', () => { } }); } + else if (url.endsWith('/addgame')) { + return Promise.resolve({ + data: { + user: userId, + pAcertadas: 5, + pFalladas: 3, + totalTime: 1200, + gameMode: 'normal' + } + }); + } }); axios.get.mockImplementation((url) => { if (url.endsWith('/api/questions/create')) { @@ -69,6 +80,23 @@ describe('Gateway Service', () => { return Promise.resolve({ data: { username: 'testuser' } }); } }); + + + it('should return 401 Unauthorized if authorization token is missing', async () => { + const response = await request(app).post('/addgame'); + expect(response.statusCode).toBe(401); + expect(response.body).toEqual({ error: 'Unauthorized' }); + }); + + it('should return 401 Unauthorized if authorization token is invalid', async () => { + const mockedToken = 'invalidToken'; + axios.get.mockRejectedValueOnce({ response: { status: 401 } }); + const response = await request(app) + .post('/addgame') + .set('Authorization', `Bearer ${mockedToken}`); + expect(response.statusCode).toBe(401); + expect(response.body).toEqual({ error: 'Unauthorized' }); + }); // Test /health endpoint it('should give information of the status', async () => { const response = await request(app) @@ -89,13 +117,24 @@ describe('Gateway Service', () => { }); // Test /verify endpoint it('should verify authorization token with auth service', async () => { + const mockedToken = 'mockedToken'; + const authResponse = { username: 'testuser' }; + axios.get.mockResolvedValueOnce({ data: authResponse }); const response = await request(app) .get('/verify') - .set('Authorization', 'Bearer mockedToken'); + .set('Authorization', `Bearer ${mockedToken}`); expect(response.statusCode).toBe(200); - expect(response.body).toHaveProperty('username', 'testuser'); + expect(response.body).toEqual(authResponse); + }); + it("should return 401 Unauthorized for unauthorized request", async () => { + const response = await request(app).get("/verify"); + expect(response.statusCode).toBe(401); + expect(response.body).toEqual({ error: "Unauthorized" }); }); + + + // Test /adduser endpoint it('should forward add user request to user service', async () => { const response = await request(app) diff --git a/webapp/src/test/AddUser.test.js b/webapp/src/test/AddUser.test.js index 9df46e59..d0c0f062 100644 --- a/webapp/src/test/AddUser.test.js +++ b/webapp/src/test/AddUser.test.js @@ -135,4 +135,40 @@ describe('AddUser component', () => { fireEvent.click(button5); }); + it('should handle successful user registration and login', async () => { + render( + {},clearSessionData: jest.fn() }}> + {}}/> + + ); + + const usernameInput = screen.getByLabelText(/Username/i); + const passwordInput = screen.getByLabelText('Password'); + const confirmPasswordInput = screen.getByLabelText(/Confirm/i); + const addUserButton = screen.getByRole('button', { name: /SIGN UP/i }); + + // Simulate user input + fireEvent.change(usernameInput, { target: { value: 'testUser' } }); + fireEvent.change(passwordInput, { target: { value: 'testPassword' } }); + fireEvent.change(confirmPasswordInput, { target: { value: 'testPassword' } }); + + // Mock the axios.post request to simulate a successful response + mockAxios.onPost('http://localhost:8000/adduser').reply(200); + mockAxios.onPost(`http://localhost:8000/login`).reply(200, { + createdAt: '2022-01-01', + username: 'testUser', + token: 'testToken', + profileImage: 'testProfileImage', + userId: 'testUserId', + }); + + // Trigger the add user button click + fireEvent.click(addUserButton); + + // Wait for the Snackbar to be open + await waitFor(() => { + expect(screen.queryByText(/Passwords do not match/i)).toBeNull(); + }); + }); + }); diff --git a/webapp/src/test/PostGame.test.js b/webapp/src/test/PostGame.test.js index b6c65f3f..71381ed0 100644 --- a/webapp/src/test/PostGame.test.js +++ b/webapp/src/test/PostGame.test.js @@ -87,4 +87,36 @@ describe('PostGame component', () => { // Check if the snackbar is displayed expect(await screen.findByText('Game saved successfully')).toBeInTheDocument(); }); + test('closes the snackbar correctly', () => { + // Mock the SessionContext value + const sessionData = { + userId: 'mockedUserId', + token: 'mockedToken' + }; + + // Mock the setOpenSnackbar function + const setOpenSnackbar = jest.fn(); + const handleCloseSnackbar = jest.fn(); + render( + + + + ); + + // Set the initial state of the snackbar to open + act(() => { + setOpenSnackbar(false); + }); + + // Call the handleCloseSnackbar function + act(() => { + handleCloseSnackbar(); + }); + + // Check if setOpenSnackbar is false + expect(setOpenSnackbar).toHaveBeenCalledWith(false); + + // Check if the snackbar is closed + expect(screen.queryByText('Game saved successfully')).not.toBeInTheDocument(); + }); }); \ No newline at end of file