Manager: add assert function for testing JSON responses

This makes it much easier to test an API response actually matches the
expected JSON values.
This commit is contained in:
Sybren A. Stüvel 2022-04-15 16:14:17 +02:00
parent 5f019044ce
commit d30befa2d7
2 changed files with 52 additions and 16 deletions

View File

@ -5,12 +5,15 @@ package api_impl
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"gorm.io/gorm" "gorm.io/gorm"
"git.blender.org/flamenco/internal/manager/api_impl/mocks" "git.blender.org/flamenco/internal/manager/api_impl/mocks"
@ -64,6 +67,7 @@ func (mf *mockedFlamenco) prepareMockedJSONRequest(requestBody interface{}) echo
} }
// prepareMockedJSONRequest returns an `echo.Context` that has an empty request body attached to it. // prepareMockedJSONRequest returns an `echo.Context` that has an empty request body attached to it.
// `body` may be `nil` to indicate "no body".
func (mf *mockedFlamenco) prepareMockedRequest(body io.Reader) echo.Context { func (mf *mockedFlamenco) prepareMockedRequest(body io.Reader) echo.Context {
e := echo.New() e := echo.New()
@ -74,6 +78,39 @@ func (mf *mockedFlamenco) prepareMockedRequest(body io.Reader) echo.Context {
return c return c
} }
func getRecordedResponse(echoCtx echo.Context) *http.Response {
writer := echoCtx.Response().Writer
resp, ok := writer.(*httptest.ResponseRecorder)
if !ok {
panic(fmt.Sprintf("response writer was not a `*httptest.ResponseRecorder` but a %T", writer))
}
return resp.Result()
}
// assertJSONResponse asserts that a recorded response is JSON with the given HTTP status code.
func assertJSONResponse(t *testing.T, echoCtx echo.Context, expectStatusCode int, expectBody interface{}) {
resp := getRecordedResponse(echoCtx)
assert.Equal(t, expectStatusCode, resp.StatusCode)
contentType := resp.Header.Get(echo.HeaderContentType)
if !assert.Equal(t, "application/json; charset=UTF-8", contentType) {
t.Fatalf("response not JSON but %q, not going to compare body", contentType)
return
}
expectJSON, err := json.Marshal(expectBody)
if !assert.NoError(t, err) {
t.FailNow()
}
actualJSON, err := io.ReadAll(resp.Body)
if !assert.NoError(t, err) {
t.FailNow()
}
assert.JSONEq(t, string(expectJSON), string(actualJSON))
}
func testWorker() persistence.Worker { func testWorker() persistence.Worker {
return persistence.Worker{ return persistence.Worker{
Model: gorm.Model{ID: 1}, Model: gorm.Model{ID: 1},

View File

@ -4,9 +4,7 @@ package api_impl
import ( import (
"context" "context"
"encoding/json"
"net/http" "net/http"
"net/http/httptest"
"testing" "testing"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
@ -39,9 +37,15 @@ func TestTaskScheduleHappy(t *testing.T) {
err := mf.flamenco.ScheduleTask(echo) err := mf.flamenco.ScheduleTask(echo)
assert.NoError(t, err) assert.NoError(t, err)
resp := echo.Response().Writer.(*httptest.ResponseRecorder) // Check the response
assert.Equal(t, http.StatusOK, resp.Code) assignedTask := api.AssignedTask{
// TODO: check that the returned JSON actually matches what we expect. Uuid: task.UUID,
Job: job.UUID,
Commands: []api.Command{},
}
assertJSONResponse(t, echo, http.StatusOK, assignedTask)
resp := getRecordedResponse(echo)
assert.Equal(t, http.StatusOK, resp.StatusCode)
} }
func TestTaskScheduleNonActiveStatus(t *testing.T) { func TestTaskScheduleNonActiveStatus(t *testing.T) {
@ -60,8 +64,8 @@ func TestTaskScheduleNonActiveStatus(t *testing.T) {
err := mf.flamenco.ScheduleTask(echoCtx) err := mf.flamenco.ScheduleTask(echoCtx)
assert.NoError(t, err) assert.NoError(t, err)
resp := echoCtx.Response().Writer.(*httptest.ResponseRecorder) resp := getRecordedResponse(echoCtx)
assert.Equal(t, http.StatusConflict, resp.Code) assert.Equal(t, http.StatusConflict, resp.StatusCode)
} }
func TestTaskScheduleOtherStatusRequested(t *testing.T) { func TestTaskScheduleOtherStatusRequested(t *testing.T) {
@ -80,13 +84,8 @@ func TestTaskScheduleOtherStatusRequested(t *testing.T) {
err := mf.flamenco.ScheduleTask(echoCtx) err := mf.flamenco.ScheduleTask(echoCtx)
assert.NoError(t, err) assert.NoError(t, err)
resp := echoCtx.Response().Writer.(*httptest.ResponseRecorder) expectBody := api.WorkerStateChange{StatusRequested: api.WorkerStatusAsleep}
assert.Equal(t, http.StatusLocked, resp.Code) assertJSONResponse(t, echoCtx, http.StatusLocked, expectBody)
responseBody := api.WorkerStateChange{}
err = json.Unmarshal(resp.Body.Bytes(), &responseBody)
assert.NoError(t, err)
assert.Equal(t, worker.StatusRequested, responseBody.StatusRequested)
} }
func TestWorkerSignoffTaskRequeue(t *testing.T) { func TestWorkerSignoffTaskRequeue(t *testing.T) {
@ -137,6 +136,6 @@ func TestWorkerSignoffTaskRequeue(t *testing.T) {
err := mf.flamenco.SignOff(echo) err := mf.flamenco.SignOff(echo)
assert.NoError(t, err) assert.NoError(t, err)
resp := echo.Response().Writer.(*httptest.ResponseRecorder) resp := getRecordedResponse(echo)
assert.Equal(t, http.StatusNoContent, resp.Code) assert.Equal(t, http.StatusNoContent, resp.StatusCode)
} }