Skip to content

Commit

Permalink
Update unit and integration tests
Browse files Browse the repository at this point in the history
Within application_handlers_test.go, delete every call on Binder()

Within endpoint_handlers_test.go, delete every instance of Binder()

Within rhc_connection_handlers_test.go, change TestRhcConnectionEditNotFound to test for not found error if given an invalid ID

Within source_handlers_test.go, delete every instance of binder()
  • Loading branch information
Ellen-Yi-Dong committed Sep 7, 2022
1 parent 5e52dde commit 4802bd4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 59 deletions.
33 changes: 3 additions & 30 deletions application_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/redhatinsights/platform-go-middlewares/identity"
"io"
"net/http"
"reflect"
Expand All @@ -29,7 +30,6 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/redhatinsights/platform-go-middlewares/identity"
)

func TestSourceApplicationSubcollectionList(t *testing.T) {
Expand Down Expand Up @@ -635,8 +635,8 @@ func TestApplicationCreateGood(t *testing.T) {
}

id, _ := strconv.ParseInt(app.ID, 10, 64)
dao, _ := getApplicationDao(c)
_, _ = dao.Delete(&id)
applicationDao, _ := getApplicationDao(c)
_, _ = applicationDao.Delete(&id)
}

func TestApplicationCreateMissingSourceId(t *testing.T) {
Expand Down Expand Up @@ -1932,10 +1932,6 @@ func TestApplicationEditPaused(t *testing.T) {
},
)

// Make sure we are using the "NoUnknownFieldsBinder".
backupBinder := c.Echo().Binder
c.Echo().Binder = &NoUnknownFieldsBinder{}

c.SetParamNames("id")
c.SetParamValues("1")
c.Request().Header.Add("Content-Type", "application/json;charset=utf-8")
Expand Down Expand Up @@ -1974,9 +1970,6 @@ func TestApplicationEditPaused(t *testing.T) {
if err != nil {
t.Error(err)
}

// Restore the binder to not affect any other tests.
c.Echo().Binder = backupBinder
}

// TestApplicationEditPausedUnitInvalidFields tests that a "bad request" response is returned when a paused application
Expand All @@ -2002,10 +1995,6 @@ func TestApplicationEditPausedIntegration(t *testing.T) {
},
)

// Make sure we are using the "NoUnknownFieldsBinder".
backupBinder := c.Echo().Binder
c.Echo().Binder = &NoUnknownFieldsBinder{}

c.SetParamNames("id")
c.SetParamValues("1")
c.Request().Header.Add("Content-Type", "application/json;charset=utf-8")
Expand Down Expand Up @@ -2044,9 +2033,6 @@ func TestApplicationEditPausedIntegration(t *testing.T) {
if err != nil {
t.Error(err)
}

// Restore the binder to not affect any other tests.
c.Echo().Binder = backupBinder
}

// TestApplicationEditPaused tests that an application can be edited even if it is paused, if the payload is right.
Expand Down Expand Up @@ -2074,10 +2060,6 @@ func TestApplicationEditPausedUnit(t *testing.T) {
},
)

// Make sure we are using the "NoUnknownFieldsBinder".
backupBinder := c.Echo().Binder
c.Echo().Binder = &NoUnknownFieldsBinder{}

c.SetParamNames("id")
c.SetParamValues("1")
c.Request().Header.Add("Content-Type", "application/json;charset=utf-8")
Expand All @@ -2102,8 +2084,6 @@ func TestApplicationEditPausedUnit(t *testing.T) {
t.Errorf("Wrong return code, expected %v got %v", http.StatusOK, rec.Code)
}

// Restore the binder to not affect any other tests.
c.Echo().Binder = backupBinder
}

// TestApplicationEditPausedUnitInvalidFields tests that a "bad request" response is returned when a paused application
Expand All @@ -2127,10 +2107,6 @@ func TestApplicationEditPausedUnitInvalidFields(t *testing.T) {
},
)

// Make sure we don't accept the "Extra" field we set up above
backupBinder := c.Echo().Binder
c.Echo().Binder = &NoUnknownFieldsBinder{}

c.SetParamNames("id")
c.SetParamValues("1")
c.Request().Header.Add("Content-Type", "application/json;charset=utf-8")
Expand Down Expand Up @@ -2170,9 +2146,6 @@ func TestApplicationEditPausedUnitInvalidFields(t *testing.T) {
if rec.Code != http.StatusBadRequest {
t.Errorf("Wrong return code, expected %v got %v", http.StatusBadRequest, rec.Code)
}

// Restore the binder to not affect any other tests.
c.Echo().Binder = backupBinder
}

func TestApplicationDeleteWithOwnership(t *testing.T) {
Expand Down
14 changes: 0 additions & 14 deletions endpoint_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,10 +1133,6 @@ func TestEndpointEditPaused(t *testing.T) {
},
)

// Make sure we are using the "NoUnknownFieldsBinder".
backupBinder := c.Echo().Binder
c.Echo().Binder = &NoUnknownFieldsBinder{}

c.SetParamNames("id")
c.SetParamValues("1")
c.Request().Header.Add("Content-Type", "application/json;charset=utf-8")
Expand Down Expand Up @@ -1167,9 +1163,6 @@ func TestEndpointEditPaused(t *testing.T) {

// Restore the original "getEndpointDao" function.
getEndpointDao = backupGetEndpointDao

// Restore the binder to not affect any other tests.
c.Echo().Binder = backupBinder
}

// TestEndpointEditPausedInvalidFields tests that a "bad request" response is returned when a paused endpoint is tried
Expand All @@ -1193,10 +1186,6 @@ func TestEndpointEditPausedInvalidFields(t *testing.T) {
},
)

// Make sure we don't accept the "Scheme" field we set up above.
backupBinder := c.Echo().Binder
c.Echo().Binder = &NoUnknownFieldsBinder{}

c.SetParamNames("id")
c.SetParamValues("1")
c.Request().Header.Add("Content-Type", "application/json;charset=utf-8")
Expand Down Expand Up @@ -1241,9 +1230,6 @@ func TestEndpointEditPausedInvalidFields(t *testing.T) {

// Restore the original "getEndpointDao" function.
getEndpointDao = backupGetEndpointDao

// Restore the binder to not affect any other tests.
c.Echo().Binder = backupBinder
}

func TestEndpointListAuthentications(t *testing.T) {
Expand Down
14 changes: 12 additions & 2 deletions rhc_connection_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,19 @@ func TestRhcConnectionEditInvalidParam(t *testing.T) {
func TestRhcConnectionEditNotFound(t *testing.T) {
invalidId := "12345"

requestBody := model.RhcConnectionEditRequest{
Extra: nil,
}

body, err := json.Marshal(requestBody)
if err != nil {
t.Error("Could not marshal JSON")
}

c, rec := request.CreateTestContext(
http.MethodPatch,
"/api/sources/v3.1/rhc_connections/"+invalidId,
nil,
bytes.NewReader(body),
map[string]interface{}{
"tenantID": int64(1),
},
Expand All @@ -663,7 +672,8 @@ func TestRhcConnectionEditNotFound(t *testing.T) {
c.SetParamValues(invalidId)

notFoundRhcConnectionEdit := ErrorHandlingContext(RhcConnectionEdit)
err := notFoundRhcConnectionEdit(c)
err = notFoundRhcConnectionEdit(c)

if err != nil {
t.Error(err)
}
Expand Down
13 changes: 0 additions & 13 deletions source_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2565,9 +2565,6 @@ func TestSourceEditPausedIntegration(t *testing.T) {
"tenantID": int64(1),
},
)
// Make sure we are using the "NoUnknownFieldsBinder".
backupBinder := c.Echo().Binder
c.Echo().Binder = &NoUnknownFieldsBinder{}

c.SetParamNames("id")
c.SetParamValues("1")
Expand Down Expand Up @@ -2600,9 +2597,6 @@ func TestSourceEditPausedIntegration(t *testing.T) {
if !strings.Contains(got, want) {
t.Errorf(`unexpected body returned. Want "%s" contained in what we got "%s"`, want, got)
}

// Restore the binder to not affect any other tests.
c.Echo().Binder = backupBinder
}

// TestSourceEditPausedUnit tests that a "bad request" response is returned when a paused source is tried to be updated
Expand Down Expand Up @@ -2640,10 +2634,6 @@ func TestSourceEditPausedUnit(t *testing.T) {
pausedAt := time.Now()
fixtures.TestSourceData[0].PausedAt = &pausedAt

// Make sure we don't accept the "Name" field we set up above.
backupBinder := c.Echo().Binder
c.Echo().Binder = &NoUnknownFieldsBinder{}

badRequestSourceEdit := ErrorHandlingContext(SourceEdit)
err := badRequestSourceEdit(c)

Expand All @@ -2669,9 +2659,6 @@ func TestSourceEditPausedUnit(t *testing.T) {
if rec.Code != http.StatusBadRequest {
t.Errorf("Wrong return code, expected %v got %v", http.StatusBadRequest, rec.Code)
}

// Restore the binder to not affect any other tests.
c.Echo().Binder = backupBinder
}

// HELPERS:
Expand Down

0 comments on commit 4802bd4

Please sign in to comment.