Skip to content

Commit

Permalink
Change status code for POST with user already in database (#137)
Browse files Browse the repository at this point in the history
The 400 error when POSTing to the database when the user is already
inside can surprise developers during debugging because everytime a
user logs in, the POST is made regardless of whether the user is inside
the database or not.

Let's change the status code to 200 to avoid showing it as an error
while still differentiating it from the status code 201 when the user
details are added for the first time.
  • Loading branch information
yhtMinceraft1010X authored Oct 19, 2023
1 parent 408fde0 commit ccaa8e1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion services/user-service/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ indexRouter.post("/", function (req: express.Request, res: express.Response) {
.createUser(req.body)
.then((result) => {
if (result === null) {
res.status(400).append("Is-User-Already-Found", "true").end();
res.status(200).append("Is-User-Already-Found", "true").end();
} else {
res.status(201).json(result);
}
Expand Down
6 changes: 3 additions & 3 deletions services/user-service/systemtest/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const updatePayload = { matchDifficulty: 1 };

describe('/index', () => {
describe('Sample App Workflow', () => {
it('Step 1: Add user 1 to database should pass', async () => {
it('Step 1: Add user 1 to database should pass with status 201', async () => {
// The function being tested
const response = await request(app).post('/api/user-service').send(fullNewUser);
expect(response.status).toStrictEqual(201);
Expand Down Expand Up @@ -42,9 +42,9 @@ describe('/index', () => {
expect(response.body).toStrictEqual(updatedNewUser);
})

it('Step 5: Attempt to add duplicate user 1 to database should fail with error', async () => {
it('Step 5: Attempt to add duplicate user 1 to database should give status 200', async () => {
const response = await request(app).post('/api/user-service').send(fullNewUser);
expect(response.status).toStrictEqual(400);
expect(response.status).toStrictEqual(200);
})

it('Step 6: Delete user 1 from database', async () => {
Expand Down
2 changes: 1 addition & 1 deletion services/user-service/test/routes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('/index', () => {

// The function being tested
const response = await request(app).post('/').send(fullNewUser);
expect(response.status).toStrictEqual(400);
expect(response.status).toStrictEqual(200);
})

it('[POST] to / when database is unavailable', async () => {
Expand Down

0 comments on commit ccaa8e1

Please sign in to comment.