From b47abbfc0b8d28aa83288e819351dbba3a70c6c6 Mon Sep 17 00:00:00 2001 From: Deepak Date: Sun, 31 Jan 2021 14:29:53 -0600 Subject: [PATCH] Adds put route tests for current plans --- routes/current_plan_put_test.go | 269 ++++++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 routes/current_plan_put_test.go diff --git a/routes/current_plan_put_test.go b/routes/current_plan_put_test.go new file mode 100644 index 0000000..84d7de3 --- /dev/null +++ b/routes/current_plan_put_test.go @@ -0,0 +1,269 @@ +package routes_test + +import ( + "bytes" + "context" + "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 TestPutSinglePlanNotFound(t *testing.T) { + // set up + assert := assert.New(t) + m := getEmptyModel() + router := routes.NewCurrentPlanRouter(m) + + plan := &models.Plan{} + m.AddPlan(plan, 3) + m.AddPlan(plan, 3) + + pp := &models.CurrentPlan{PlanID: 1} + data, _ := json.Marshal(pp) + req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", 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.StatusNotFound, status) +} + +func TestPutSinglePlanNonDup(t *testing.T) { + // set up + assert := assert.New(t) + m := getEmptyModel() + router := routes.NewCurrentPlanRouter(m) + + plan := &models.Plan{} + m.AddPlan(plan, 3) + m.AddPlan(plan, 3) + m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3) + + pp := &models.CurrentPlan{PlanID: 2} + data, _ := json.Marshal(pp) + req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", 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.StatusOK, status) +} + +func TestPutCurrentPlanUnauth(t *testing.T) { + // set up + assert := assert.New(t) + m := getEmptyModel() + + plan := &models.Plan{} + m.AddPlan(plan, 3) + m.AddPlan(plan, 3) + m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3) + + pp := &models.CurrentPlan{PlanID: 1} + data, _ := json.Marshal(pp) + req, _ := http.NewRequestWithContext(context.Background(), "PUT", "/", bytes.NewBuffer(data)) + req.Header.Set("Content-Type", "application/json") + + rr := httptest.NewRecorder() + router := routes.NewCurrentPlanRouter(m) + + // function under test + router.ServeHTTP(rr, req) + + // check results + status := rr.Code + assert.Equal(http.StatusUnauthorized, status) +} + +func TestExtraFieldJSONPutCurrentPlan(t *testing.T) { + // set up + assert := assert.New(t) + m := getEmptyModel() + + plan := &models.Plan{} + m.AddPlan(plan, 3) + m.AddPlan(plan, 3) + m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3) + + data := []byte(`{ + "plan_id": 5, + "sabotage": "omg" + }`) + req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", bytes.NewBuffer(data)) + req.Header.Set("Content-Type", "application/json") + + rr := httptest.NewRecorder() + router := routes.NewCurrentPlanRouter(m) + + // function under test + router.ServeHTTP(rr, req) + + // check results + status := rr.Code + assert.Equal(http.StatusBadRequest, status) + + expected := `Bad Request` + assert.Equal(expected, strings.TrimSpace(rr.Body.String())) +} + +func TestEmptyBodyJSONPutCurrentPlan(t *testing.T) { + // set up + assert := assert.New(t) + m := getEmptyModel() + + plan := &models.Plan{} + m.AddPlan(plan, 3) + m.AddPlan(plan, 3) + m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3) + + data := []byte(``) + req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", bytes.NewBuffer(data)) + req.Header.Set("Content-Type", "application/json") + + rr := httptest.NewRecorder() + router := routes.NewCurrentPlanRouter(m) + + // function under test + router.ServeHTTP(rr, req) + + // check results + status := rr.Code + assert.Equal(http.StatusBadRequest, status) + + expected := `Bad Request` + assert.Equal(expected, strings.TrimSpace(rr.Body.String())) +} + +func TestTwoBodyPutCurrentPlan(t *testing.T) { + // set up + assert := assert.New(t) + m := getEmptyModel() + + plan := &models.Plan{} + m.AddPlan(plan, 3) + m.AddPlan(plan, 3) + m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3) + + data := []byte(`{ + "plan_id": 5 + }, { + "plan_id": 7 + }`) + req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", bytes.NewBuffer(data)) + req.Header.Set("Content-Type", "application/json") + + rr := httptest.NewRecorder() + router := routes.NewCurrentPlanRouter(m) + + // function under test + router.ServeHTTP(rr, req) + + // check results + status := rr.Code + assert.Equal(http.StatusBadRequest, status) + + expected := `Bad Request` + assert.Equal(expected, strings.TrimSpace(rr.Body.String())) +} + +func TestErrorPutCurrentPlan(t *testing.T) { + // set up + assert := assert.New(t) + m := getErrorModel("error model") + + plan := &models.Plan{} + m.AddPlan(plan, 3) + m.AddPlan(plan, 3) + m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3) + + data := []byte(`{ + "plan_id": 5 + }`) + req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", bytes.NewBuffer(data)) + req.Header.Set("Content-Type", "application/json") + + rr := httptest.NewRecorder() + router := routes.NewCurrentPlanRouter(m) + + // function under test + router.ServeHTTP(rr, req) + + // check results + status := rr.Code + assert.Equal(http.StatusInternalServerError, status) + + expected := `Internal Server Error` + assert.Equal(expected, strings.TrimSpace(rr.Body.String())) +} + +func TestErrorOnRetrievePutCurrentPlan(t *testing.T) { + // set up + assert := assert.New(t) + m := getErrorOnGetModel("error model") + + plan := &models.Plan{} + m.AddPlan(plan, 3) + m.AddPlan(plan, 3) + m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3) + + data := []byte(`{ + "plan_id": 5 + }`) + req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", bytes.NewBuffer(data)) + req.Header.Set("Content-Type", "application/json") + + rr := httptest.NewRecorder() + router := routes.NewCurrentPlanRouter(m) + + // function under test + router.ServeHTTP(rr, req) + + // check results + status := rr.Code + assert.Equal(http.StatusInternalServerError, status) + + expected := `Internal Server Error` + assert.Equal(expected, strings.TrimSpace(rr.Body.String())) +} + +func TestErrorWriterPutCurrentPlan(t *testing.T) { + // set up + assert := assert.New(t) + m := getEmptyModel() + + plan := &models.Plan{} + m.AddPlan(plan, 3) + m.AddPlan(plan, 3) + m.AddCurrentPlan(&models.CurrentPlan{PlanID: 1}, 3) + + data := []byte(`{ + "plan_id": 5 + }`) + req, _ := http.NewRequestWithContext(sampleContext, "PUT", "/", bytes.NewBuffer(data)) + req.Header.Set("Content-Type", "application/json") + + rr := NewBadWriter() + router := routes.NewCurrentPlanRouter(m) + + // function under test + router.ServeHTTP(rr, req) + + // check results + status := rr.Code + assert.Equal(http.StatusInternalServerError, status) +}