Skip to content

Commit

Permalink
test(api) add more invalid body tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aguszorza committed Apr 3, 2024
1 parent 1b7d681 commit 2318389
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
18 changes: 18 additions & 0 deletions API/controllers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func getUserToken(email string, password string) string {
return acc.Token
}

func TestCreateUserInvalidBody(t *testing.T) {
e2e.TestInvalidBody(t, "POST", "/api/users", "Invalid request: wrong format body")
}

// Tests domain bulk creation (/api/users/bulk)
func TestCreateBulkUsersInvalidBody(t *testing.T) {
requestBody := []byte(`[
Expand Down Expand Up @@ -84,6 +88,11 @@ func TestCreateBulkUsers(t *testing.T) {
assert.False(t, exists)
}

// Tests Login
func TestLoginInvalidBody(t *testing.T) {
e2e.TestInvalidBody(t, "POST", "/api/login", "Invalid request")
}

func TestLoginWrongPassword(t *testing.T) {
requestBody := []byte(`{
"email": "[email protected]",
Expand Down Expand Up @@ -272,6 +281,11 @@ func TestDeleteWithInvalidIdReturnsError(t *testing.T) {
}

// Tests modify user role
func TestModifyUserInvalidBody(t *testing.T) {
userId := models.GetUserByEmail("[email protected]").ID.Hex()
e2e.TestInvalidBody(t, "PATCH", "/api/users/"+userId, "Invalid request")
}

func TestModifyRoleWithMoreDataReturnsError(t *testing.T) {
// we get the user ID
userId := models.GetUserByEmail("[email protected]").ID.Hex()
Expand Down Expand Up @@ -381,6 +395,10 @@ func TestModifyRoleSuccess(t *testing.T) {
}

// Tests modify and reset user password
func TestModifyPasswordInvalidBody(t *testing.T) {
e2e.TestInvalidBody(t, "POST", "/api/users/password/change", "Invalid request")
}

func TestModifyPasswordNotEnoughArguments(t *testing.T) {
userToken := getUserToken("[email protected]", "fake_password")
requestBody := []byte(`{
Expand Down
13 changes: 4 additions & 9 deletions API/controllers/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,11 @@ func init() {
}

func testInvalidBody(t *testing.T, httpMethod string, endpoint string) {
invalidBody := []byte(`{`)

recorder := e2e.MakeRequest(httpMethod, endpoint, invalidBody)
assert.Equal(t, http.StatusBadRequest, recorder.Code)
e2e.TestInvalidBody(t, httpMethod, endpoint, "Error while decoding request body")
}

var response map[string]interface{}
json.Unmarshal(recorder.Body.Bytes(), &response)
message, exists := response["message"].(string)
assert.True(t, exists)
assert.Equal(t, "Error while decoding request body", message)
func TestCreateEntityInvalidBody(t *testing.T) {
testInvalidBody(t, "POST", "/api/sites")
}

// Tests domain bulk creation (/api/domains/bulk)
Expand Down
4 changes: 4 additions & 0 deletions API/controllers/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var project = map[string]any{
}
var projectId string

func TestCreateProjectInvalidBody(t *testing.T) {
e2e.TestInvalidBody(t, "POST", "/api/projects", "Invalid request")
}

func TestCreateProject(t *testing.T) {
json.Marshal(project)
requestBody, _ := json.Marshal(project)
Expand Down
15 changes: 15 additions & 0 deletions API/test/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"p3/models"
"p3/router"
_ "p3/test/integration"
"testing"

"github.com/elliotchance/pie/v2"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)

var appRouter *mux.Router
Expand Down Expand Up @@ -85,3 +87,16 @@ func GetObjects(queryParams string) (*httptest.ResponseRecorder, []map[string]an

return response, objects
}

func TestInvalidBody(t *testing.T, httpMethod string, endpoint string, errorMessage string) {
invalidBody := []byte(`{`)

recorder := MakeRequest(httpMethod, endpoint, invalidBody)
assert.Equal(t, http.StatusBadRequest, recorder.Code)

var response map[string]interface{}
json.Unmarshal(recorder.Body.Bytes(), &response)
message, exists := response["message"].(string)
assert.True(t, exists)
assert.Equal(t, errorMessage, message)
}

0 comments on commit 2318389

Please sign in to comment.