221 lines
5.0 KiB
Go
221 lines
5.0 KiB
Go
package routes_test
|
|
|
|
import (
|
|
"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"
|
|
)
|
|
|
|
var sampleContext = tokens.GetContextForUserValues(3, "testing")
|
|
|
|
func TestEmptyPlans(t *testing.T) {
|
|
// set up
|
|
assert := assert.New(t)
|
|
m := getEmptyModel()
|
|
router := routes.NewPlanRouter(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)
|
|
expected := `[]`
|
|
|
|
assert.JSONEq(expected, rr.Body.String())
|
|
contentType := rr.Header().Get("Content-Type")
|
|
assert.Equal("application/json", contentType)
|
|
}
|
|
|
|
func TestOnePlan(t *testing.T) {
|
|
// set up
|
|
assert := assert.New(t)
|
|
planDescription := "2021-01-01"
|
|
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
|
m := getEmptyModel()
|
|
m.AddPlan(p, 3)
|
|
router := routes.NewPlanRouter(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": 1,
|
|
"plan_description": "2021-01-01",
|
|
"user_id": 3
|
|
}
|
|
]`
|
|
assert.JSONEq(expected, rr.Body.String())
|
|
contentType := rr.Header().Get("Content-Type")
|
|
assert.Equal("application/json", contentType)
|
|
}
|
|
|
|
func TestErrorPlan(t *testing.T) {
|
|
// set up
|
|
assert := assert.New(t)
|
|
|
|
m := getErrorModel("Model always errors")
|
|
|
|
router := routes.NewPlanRouter(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)
|
|
expected := `Internal Server Error`
|
|
assert.Equal(expected, strings.TrimSpace(rr.Body.String()))
|
|
}
|
|
|
|
func TestEmptyPlanErrorWriter(t *testing.T) {
|
|
// set up
|
|
assert := assert.New(t)
|
|
|
|
m := getEmptyModel()
|
|
|
|
router := routes.NewPlanRouter(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 TestOnePlanByID(t *testing.T) {
|
|
// set up
|
|
assert := assert.New(t)
|
|
planDescription := "2021-01-01"
|
|
p := &models.Plan{PlanID: 6, PlanDescription: planDescription, UserID: 3}
|
|
m := getEmptyModel()
|
|
m.AddPlan(p, 3)
|
|
router := routes.NewPlanRouter(m)
|
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/1", nil)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
// function under test
|
|
router.ServeHTTP(rr, req)
|
|
|
|
// check results
|
|
status := rr.Code
|
|
assert.Equal(http.StatusOK, status)
|
|
|
|
expected := `{
|
|
"plan_id": 1,
|
|
"plan_description": "2021-01-01",
|
|
"user_id": 3
|
|
}`
|
|
assert.JSONEq(expected, rr.Body.String())
|
|
contentType := rr.Header().Get("Content-Type")
|
|
assert.Equal("application/json", contentType)
|
|
}
|
|
|
|
func TestErrorPlanByID(t *testing.T) {
|
|
// set up
|
|
assert := assert.New(t)
|
|
|
|
m := getErrorModel("Model always errors")
|
|
|
|
router := routes.NewPlanRouter(m)
|
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/5", 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 TestEmptyPlanErrorWriterByID(t *testing.T) {
|
|
// set up
|
|
assert := assert.New(t)
|
|
planDescription := "2021-01-01"
|
|
p := &models.Plan{PlanID: 1, PlanDescription: planDescription}
|
|
m := getEmptyModel()
|
|
m.AddPlan(p, 3)
|
|
|
|
router := routes.NewPlanRouter(m)
|
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/1", nil)
|
|
|
|
rr := NewBadWriter()
|
|
|
|
// function under test
|
|
router.ServeHTTP(rr, req)
|
|
|
|
// check results
|
|
status := rr.Code
|
|
assert.Equal(http.StatusInternalServerError, status)
|
|
|
|
}
|
|
|
|
func TestNotFoundPlanByIDText(t *testing.T) {
|
|
// set up
|
|
assert := assert.New(t)
|
|
|
|
m := getEmptyModel()
|
|
|
|
router := routes.NewPlanRouter(m)
|
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/wo", nil)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
// function under test
|
|
router.ServeHTTP(rr, req)
|
|
|
|
// check results
|
|
status := rr.Code
|
|
assert.Equal(http.StatusNotFound, status)
|
|
|
|
}
|
|
func TestNotFoundPlanByIDEmpty(t *testing.T) {
|
|
// set up
|
|
assert := assert.New(t)
|
|
|
|
m := getEmptyModel()
|
|
|
|
router := routes.NewPlanRouter(m)
|
|
req, _ := http.NewRequestWithContext(sampleContext, "GET", "/1", nil)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
// function under test
|
|
router.ServeHTTP(rr, req)
|
|
|
|
// check results
|
|
status := rr.Code
|
|
assert.Equal(http.StatusNotFound, status)
|
|
|
|
}
|