Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Arquisoft/wiq_es1a
Browse files Browse the repository at this point in the history
  • Loading branch information
iyanfdezz committed Apr 2, 2024
2 parents cc11bb1 + 5fbd4c4 commit b1699ea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 83 deletions.
40 changes: 13 additions & 27 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ app.get("/health", (req, res) => {
res.json({ status: "OK" });
});

returnError = (res, error) => {
res.status(error.response.status).json({ error: error.response.data.error });
}

app.post("/login", async (req, res) => {
try {
// Forward the login request to the authentication service
const authResponse = await axios.post(authServiceUrl + "/login", req.body);
res.json(authResponse.data);
} catch (error) {
res
.status(error.response.status)
.json({ error: error.response.data.error });
returnError(res, error);
}
});

Expand All @@ -49,9 +51,7 @@ app.post("/adduser", async (req, res) => {
);
res.json(userResponse.data);
} catch (error) {
res
.status(error.response.status)
.json({ error: error.response.data.error });
returnError(res, error);
}
});

Expand All @@ -64,9 +64,7 @@ app.get("/userInfo", async (req, res) => {
);
res.json(userResponse.data);
} catch (error) {
res
.status(error.response.status)
.json({ error: error.response.data.error });
returnError(res, error);
}
});

Expand All @@ -79,9 +77,7 @@ app.post("/saveGameList", async (req, res) => {
);
res.json(gameResponse.data);
} catch (error) {
res
.status(error.response.status)
.json({ error: error.response.data.error });
returnError(res, error);
}
});

Expand All @@ -94,9 +90,7 @@ app.get("/questions", async (req, res) => {
);
res.json(questionResponse.data);
} catch (error) {
res
.status(error.response.status)
.json({ error: error.response.data.error });
returnError(res, error);
}
});

Expand All @@ -109,9 +103,7 @@ app.post("/questions", async (req, res) => {
);
res.json(questionResponse.data);
} catch (error) {
res
.status(error.response.status)
.json({ error: error.response.data.error });
returnError(res, error);
}
});

Expand All @@ -123,9 +115,7 @@ app.get("/stats", async (req, res) => {
});
res.json(statsResponse.data);
} catch (error) {
res
.status(error.response.status)
.json({ error: error.response.data.error });
returnError(res, error);
}
});

Expand All @@ -138,9 +128,7 @@ app.post("/saveGame", async (req, res) => {
);
res.json(gameResponse.data);
} catch (error) {
res
.status(error.response.status)
.json({ error: error.response.data.error });
returnError(res, error);
}
});

Expand All @@ -151,9 +139,7 @@ app.get("/ranking", async (req, res) => {
});
res.json(statsResponse.data);
} catch (error) {
res
.status(error.response.status)
.json({ error: error.response.data.error });
returnError(res, error);
}
});

Expand Down
84 changes: 28 additions & 56 deletions webapp/src/components/Register/Register.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,42 @@ import { BrowserRouter as Router } from "react-router-dom";
jest.mock("axios");

describe("Register Component", () => {
const mockSuccessResponse = { data: "Usuario registrado exitosamente" };
const mockErrorResponse = { response: { data: { error: "Error al registrar usuario" } } };

const fillRegistrationForm = () => {
fireEvent.change(screen.getByLabelText("Introduce tu nombre:"), {
target: { value: "testUser" },
});
fireEvent.change(screen.getByLabelText("Introduce tu contraseña:"), {
target: { value: "testPassword" },
});
fireEvent.change(screen.getByLabelText("Vuelve a introducir la contraseña:"), {
target: { value: "testPassword" },
});
};

const submitRegistrationForm = async () => {
fireEvent.click(screen.getByText("Registrarse"));
await waitFor(() => {});
};

test("renders registration form correctly", () => {
render(
<Router>
<Register />
</Router>
);

// Verificar que los elementos del formulario se rendericen correctamente
expect(screen.getByText("Regístrate")).toBeInTheDocument();
expect(screen.getByLabelText("Introduce tu nombre:")).toBeInTheDocument();
expect(
screen.getByLabelText("Introduce tu contraseña:")
).toBeInTheDocument();
expect(
screen.getByLabelText("Vuelve a introducir la contraseña:")
).toBeInTheDocument();
expect(screen.getByLabelText("Introduce tu contraseña:")).toBeInTheDocument();
expect(screen.getByLabelText("Vuelve a introducir la contraseña:")).toBeInTheDocument();
expect(screen.getByText("Registrarse")).toBeInTheDocument();
expect(screen.getByText("¿Ya tienes cuenta?")).toBeInTheDocument();
});

test("handles user registration successfully", async () => {
const mockSuccessResponse = { data: "Usuario registrado exitosamente" };
axios.post.mockResolvedValueOnce(mockSuccessResponse);

render(
Expand All @@ -38,40 +52,17 @@ describe("Register Component", () => {
</Router>
);

// Simular la entrada de datos del usuario
fireEvent.change(screen.getByLabelText("Introduce tu nombre:"), {
target: { value: "testUser" },
});
fireEvent.change(screen.getByLabelText("Introduce tu contraseña:"), {
target: { value: "testPassword" },
});
fireEvent.change(
screen.getByLabelText("Vuelve a introducir la contraseña:"),
{
target: { value: "testPassword" },
}
);

// Simular clic en el botón de registro
fireEvent.click(screen.getByText("Registrarse"));
fillRegistrationForm();
await submitRegistrationForm();

// Esperar a que se complete la solicitud y se muestre la confirmación
await waitFor(() => {
expect(
screen.getByText("Usuario registrado exitosamente")
).toBeInTheDocument();
});

// Verificar que se haya llamado correctamente a la función post de axios
expect(screen.getByText("Usuario registrado exitosamente")).toBeInTheDocument();
expect(axios.post).toHaveBeenCalledWith("http://localhost:8000/adduser", {
username: "testUser",
password: "testPassword",
});
});

test("handles user registration failure", async () => {
const errorMessage = "Error al registrar usuario";
const mockErrorResponse = { response: { data: { error: errorMessage } } };
axios.post.mockRejectedValueOnce(mockErrorResponse);

render(
Expand All @@ -80,29 +71,10 @@ describe("Register Component", () => {
</Router>
);

// Simular la entrada de datos del usuario
fireEvent.change(screen.getByLabelText("Introduce tu nombre:"), {
target: { value: "testUser" },
});
fireEvent.change(screen.getByLabelText("Introduce tu contraseña:"), {
target: { value: "testPassword" },
});
fireEvent.change(
screen.getByLabelText("Vuelve a introducir la contraseña:"),
{
target: { value: "testPassword" },
}
);

// Simular clic en el botón de registro
fireEvent.click(screen.getByText("Registrarse"));

// Esperar a que se muestre el mensaje de error
await waitFor(() => {
expect(screen.getByText(`Error: ${errorMessage}`)).toBeInTheDocument();
});
fillRegistrationForm();
await submitRegistrationForm();

// Verificar que se haya llamado correctamente a la función post de axios
expect(screen.getByText("Error: Error al registrar usuario")).toBeInTheDocument();
expect(axios.post).toHaveBeenCalledWith("http://localhost:8000/adduser", {
username: "testUser",
password: "testPassword",
Expand Down

0 comments on commit b1699ea

Please sign in to comment.