Skip to content

Commit

Permalink
Some tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
UO287687 committed Apr 7, 2024
1 parent ea04eee commit c63b5c2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ app.post('/adduser', async (req, res) => {
// Check if required fields are present in the request body
validateRequiredFields(req, ['username', 'password', 'profileImage']);
// Check if the user already exists
const existingUser = await User.findOne({ username: req.body.username });
const existingUser = await User.findOne({ username: req.body.username.toString() });
if (existingUser) {
res.status(400).json({ error: "User already exist" });
return;
Expand Down
33 changes: 33 additions & 0 deletions webapp/src/test/Login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,37 @@ describe('Login component', () => {
expect(screen.queryByText(/Hello testUser!/i)).toBeNull();
expect(screen.queryByText(/Your account was created on/i)).toBeNull();
});
it('should call autologin function when sessionData has token', async () => {
const goToMock = jest.fn();
const sessionData = { token: 'testToken' };

render(
<SessionContext.Provider value={{ ...mockValue, sessionData }}>
<Login goTo={goToMock} />
</SessionContext.Provider>
);

await waitFor(() => {
expect(mockAxios.history.get.length).toBe(1);
expect(mockAxios.history.get[0].url).toBe('http://localhost:8000/verify');
expect(mockAxios.history.get[0].headers.Authorization).toBe('Bearer testToken');
});
});

it('should not call autologin function when sessionData does not have token', async () => {
const goToMock = jest.fn();
const sessionData = {};

render(
<SessionContext.Provider value={{ ...mockValue, sessionData }}>
<Login goTo={goToMock} />
</SessionContext.Provider>
);

await waitFor(() => {
expect(mockAxios.history.get.length).toBe(0);
});

expect(goToMock).not.toHaveBeenCalled();
});
});

0 comments on commit c63b5c2

Please sign in to comment.