Adds tests for post current plan
This commit is contained in:
parent
eb8838ab75
commit
4905d18222
228
routes/current_plan_post_test.go
Normal file
228
routes/current_plan_post_test.go
Normal file
@ -0,0 +1,228 @@
|
|||||||
|
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 TestPostSinglePlan(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
router := routes.NewCurrentPlanRouter(m)
|
||||||
|
|
||||||
|
plan := &models.Plan{}
|
||||||
|
m.AddPlan(plan, 3)
|
||||||
|
|
||||||
|
pp := &models.CurrentPlan{PlanID: 1}
|
||||||
|
data, _ := json.Marshal(pp)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostCurrentPlanUnauth(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
|
||||||
|
plan := &models.Plan{}
|
||||||
|
m.AddPlan(plan, 3)
|
||||||
|
|
||||||
|
pp := &models.CurrentPlan{PlanID: 1}
|
||||||
|
data, _ := json.Marshal(pp)
|
||||||
|
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", 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 TestExtraFieldJSONCurrentPlan(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
|
||||||
|
plan := &models.Plan{}
|
||||||
|
m.AddPlan(plan, 3)
|
||||||
|
|
||||||
|
data := []byte(`{
|
||||||
|
"plan_id": 5,
|
||||||
|
"sabotage": "omg"
|
||||||
|
}`)
|
||||||
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", 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 TestEmptyBodyJSONCurrentPlan(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
|
||||||
|
plan := &models.Plan{}
|
||||||
|
m.AddPlan(plan, 3)
|
||||||
|
|
||||||
|
data := []byte(``)
|
||||||
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", 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 TestTwoBodyCurrentPlan(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
|
||||||
|
plan := &models.Plan{}
|
||||||
|
m.AddPlan(plan, 3)
|
||||||
|
|
||||||
|
data := []byte(`{
|
||||||
|
"plan_id": 5
|
||||||
|
}, {
|
||||||
|
"plan_id": 7
|
||||||
|
}`)
|
||||||
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", 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 TestErrorCreateCurrentPlan(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getErrorModel("error model")
|
||||||
|
|
||||||
|
plan := &models.Plan{}
|
||||||
|
m.AddPlan(plan, 3)
|
||||||
|
|
||||||
|
data := []byte(`{
|
||||||
|
"plan_id": 5
|
||||||
|
}`)
|
||||||
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", 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 TestErrorOnRetrieveCreateCurrentPlan(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getErrorOnGetModel("error model")
|
||||||
|
|
||||||
|
plan := &models.Plan{}
|
||||||
|
m.AddPlan(plan, 3)
|
||||||
|
|
||||||
|
data := []byte(`{
|
||||||
|
"plan_id": 5
|
||||||
|
}`)
|
||||||
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", 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 TestErrorWriterCreateCurrentPlan(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
|
||||||
|
plan := &models.Plan{}
|
||||||
|
m.AddPlan(plan, 3)
|
||||||
|
|
||||||
|
data := []byte(`{
|
||||||
|
"plan_id": 5
|
||||||
|
}`)
|
||||||
|
req, _ := http.NewRequestWithContext(sampleContext, "POST", "/", 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)
|
||||||
|
}
|
117
routes/current_plan_test.go
Normal file
117
routes/current_plan_test.go
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
package routes_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"gitea.deepak.science/deepak/gogmagog/models"
|
||||||
|
"gitea.deepak.science/deepak/gogmagog/routes"
|
||||||
|
// "gitea.deepak.science/deepak/gogmagog/tokens"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEmptyCurrentPlan(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
router := routes.NewCurrentPlanRouter(m)
|
||||||
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusNotFound, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmptyCurrentPlanUnauth(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
router := routes.NewCurrentPlanRouter(m)
|
||||||
|
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusUnauthorized, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrorCurrentPlan(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
m := getErrorModel("Model always errors")
|
||||||
|
|
||||||
|
router := routes.NewCurrentPlanRouter(m)
|
||||||
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||||
|
|
||||||
|
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 TestEmptyCurrentPlanErrorWriter(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
m := getEmptyModel()
|
||||||
|
m.AddCurrentPlan(&models.CurrentPlan{PlanID: 3}, 3)
|
||||||
|
|
||||||
|
router := routes.NewCurrentPlanRouter(m)
|
||||||
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||||
|
|
||||||
|
rr := NewBadWriter()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusInternalServerError, status)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSingleCurrentPlan(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
m.AddCurrentPlan(&models.CurrentPlan{PlanID: 3}, 3)
|
||||||
|
|
||||||
|
router := routes.NewCurrentPlanRouter(m)
|
||||||
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/", nil)
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusOK, status)
|
||||||
|
// We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp.
|
||||||
|
expected := `{
|
||||||
|
"plan_id": 3,
|
||||||
|
"user_id": 3
|
||||||
|
}`
|
||||||
|
assert.JSONEq(expected, rr.Body.String())
|
||||||
|
contentType := rr.Header().Get("Content-Type")
|
||||||
|
assert.Equal("application/json", contentType)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user