Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
handle deleting with invalid id
Browse files Browse the repository at this point in the history
  • Loading branch information
oprogramador committed Aug 10, 2020
1 parent 2257985 commit ff4d440
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 18 additions & 1 deletion e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestAddingReadingAddDeleting(t *testing.T) {
}

// Thanks to that, DELETE is idempotent
func TestDeletingNonExistentUserWithValidIdIsPermitted(t *testing.T) {
func TestDeletingNonExistentUserWithValidId(t *testing.T) {
server, _, _, _ := setupServer()
ts := httptest.NewServer(server)
defer ts.Close()
Expand All @@ -82,3 +82,20 @@ func TestDeletingNonExistentUserWithValidIdIsPermitted(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "", string(bodyBytes))
}

func TestDeletingWithInvalidId(t *testing.T) {
server, _, _, _ := setupServer()
ts := httptest.NewServer(server)
defer ts.Close()

req, err := http.NewRequest(http.MethodDelete, ts.URL+"/user/invalid", nil)
assert.Nil(t, err)
client := &http.Client{}
resp, err := client.Do(req)

assert.Nil(t, err)
assert.Equal(t, 400, resp.StatusCode)
bodyBytes, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, "invalid id", string(bodyBytes))
}
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func deleteUser(ctx context.Context, usersCollection *mongo.Collection) func(gin

idPrimitive, err := primitive.ObjectIDFromHex(id)
if err != nil {
log.Fatalln(err)
ginContext.String(400, "invalid id")
}

data, err := usersCollection.DeleteOne(ctx, bson.D{
Expand Down

0 comments on commit ff4d440

Please sign in to comment.