package routes_test import ( "bytes" "encoding/json" "gitea.deepak.science/deepak/gogmagog/models" "gitea.deepak.science/deepak/gogmagog/routes" "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "strings" "testing" ) func TestCreatePlanRoute(t *testing.T) { // set up assert := assert.New(t) planDescription := "2021-01-01" userID := 3 p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: int64(userID)} m := getEmptyModel() m.AddPlan(p, userID) router := routes.NewPlanRouter(m) data, _ := json.Marshal(p) req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data)) req.Header.Set("Content-Type", "application/json") rr := httptest.NewRecorder() // function under test router.ServeHTTP(rr, req) // check results status := rr.Code assert.Equal(http.StatusCreated, status) // We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp. expected := `{ "created_plan": { "plan_id": 2, "plan_description": "2021-01-01", "user_id": 3 }, "id": 2 }` assert.JSONEq(expected, rr.Body.String()) contentType := rr.Header().Get("Content-Type") assert.Equal("application/json", contentType) } func TestPureJSON(t *testing.T) { // set up assert := assert.New(t) planDescription := "2021-01-01" userID := 3 p := &models.Plan{PlanID: 1, PlanDescription: planDescription, UserID: int64(userID)} m := getEmptyModel() m.AddPlan(p, userID) router := routes.NewPlanRouter(m) data := []byte(`{ "plan_description": "2021-01-01", "plan_id": 1, "user_id": 3 }`) req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data)) req.Header.Set("Content-Type", "application/json") rr := httptest.NewRecorder() // function under test router.ServeHTTP(rr, req) // check results status := rr.Code assert.Equal(http.StatusCreated, status) // We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp. expected := `{ "created_plan": { "plan_id": 2, "user_id": 3, "plan_description": "2021-01-01" }, "id": 2 }` assert.JSONEq(expected, rr.Body.String()) contentType := rr.Header().Get("Content-Type") assert.Equal("application/json", contentType) } func TestExtraFieldJSON(t *testing.T) { // set up assert := assert.New(t) planDescription := "2021-01-01" userID := 3 p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: int64(userID)} m := getEmptyModel() m.AddPlan(p, userID) router := routes.NewPlanRouter(m) data := []byte(`{ "plan_description": "2021-01-01", "plan_id": 5, "plan_sabotage": "omg" }`) req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data)) req.Header.Set("Content-Type", "application/json") rr := httptest.NewRecorder() // function under test router.ServeHTTP(rr, req) // check results status := rr.Code assert.Equal(http.StatusBadRequest, status) // We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp. expected := `Bad Request` assert.Equal(expected, strings.TrimSpace(rr.Body.String())) } func TestEmptyBody(t *testing.T) { // set up assert := assert.New(t) planDescription := "2021-01-01" userID := 3 p := &models.Plan{PlanID: 6, PlanDescription: planDescription} m := getEmptyModel() m.AddPlan(p, userID) router := routes.NewPlanRouter(m) data := []byte(``) req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data)) req.Header.Set("Content-Type", "application/json") rr := httptest.NewRecorder() // function under test router.ServeHTTP(rr, req) // check results status := rr.Code assert.Equal(http.StatusBadRequest, status) // We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp. expected := `Bad Request` assert.Equal(expected, strings.TrimSpace(rr.Body.String())) } func TestTwoBody(t *testing.T) { // set up assert := assert.New(t) planDescription := "2021-01-01" p := &models.Plan{PlanID: 6, PlanDescription: planDescription} m := getEmptyModel() m.AddPlan(p, 3) router := routes.NewPlanRouter(m) data := []byte(`{ "plan_description": "2021-01-01", "plan_id": 5 }, { "plan_description": "2021-01-01", "plan_id": 6 }`) req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data)) req.Header.Set("Content-Type", "application/json") rr := httptest.NewRecorder() // function under test router.ServeHTTP(rr, req) // check results status := rr.Code assert.Equal(http.StatusBadRequest, status) // We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp. expected := `Bad Request` assert.Equal(expected, strings.TrimSpace(rr.Body.String())) } func TestErrorCreatePlan(t *testing.T) { // set up assert := assert.New(t) m := getErrorModel("Model always errors") router := routes.NewPlanRouter(m) planDescription := "2021-01-01" p := &models.Plan{PlanID: 6, PlanDescription: planDescription} data, _ := json.Marshal(p) req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data)) req.Header.Set("Content-Type", "application/json") rr := httptest.NewRecorder() // function under test router.ServeHTTP(rr, req) // check results status := rr.Code assert.Equal(http.StatusInternalServerError, status) // We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp. expected := `Internal Server Error` assert.Equal(expected, strings.TrimSpace(rr.Body.String())) } func TestErrorOnRetrieveCreatePlan(t *testing.T) { // set up assert := assert.New(t) m := getErrorOnGetModel("Model always errors") router := routes.NewPlanRouter(m) planDescription := "2021-01-01" p := &models.Plan{PlanID: 6, PlanDescription: planDescription} data, _ := json.Marshal(p) req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data)) req.Header.Set("Content-Type", "application/json") rr := httptest.NewRecorder() // function under test router.ServeHTTP(rr, req) // check results status := rr.Code assert.Equal(http.StatusInternalServerError, status) // We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp. expected := `Internal Server Error` assert.Equal(expected, strings.TrimSpace(rr.Body.String())) } func TestErrorWriterCreatePlan(t *testing.T) { // set up assert := assert.New(t) planDescription := "2021-01-01" p := &models.Plan{PlanID: 6, PlanDescription: planDescription} m := getEmptyModel() m.AddPlan(p, 3) router := routes.NewPlanRouter(m) data, _ := json.Marshal(p) req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", bytes.NewBuffer(data)) req.Header.Set("Content-Type", "application/json") rr := NewBadWriter() // function under test router.ServeHTTP(rr, req) // check results status := rr.Code assert.Equal(http.StatusInternalServerError, status) }