Adds put route tests for current plans
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
This commit is contained in:
parent
4905d18222
commit
b47abbfc0b
269
routes/current_plan_put_test.go
Normal file
269
routes/current_plan_put_test.go
Normal file
@ -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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user