Compare commits
2 Commits
c28939d2b8
...
4d093ed99a
| Author | SHA1 | Date | |
|---|---|---|---|
|
4d093ed99a
|
|||
|
77e6e6bc04
|
@@ -6,7 +6,6 @@ import (
|
|||||||
"gitea.deepak.science/deepak/gogmagog/tokens"
|
"gitea.deepak.science/deepak/gogmagog/tokens"
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
@@ -24,7 +23,6 @@ func getAllPlansFunc(m *models.Model) http.HandlerFunc {
|
|||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := tokens.GetUserID(r.Context())
|
userID, err := tokens.GetUserID(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
|
||||||
unauthorizedHandler(w, r)
|
unauthorizedHandler(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -45,7 +43,6 @@ func getPlanByIDFunc(m *models.Model) http.HandlerFunc {
|
|||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := tokens.GetUserID(r.Context())
|
userID, err := tokens.GetUserID(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
|
||||||
unauthorizedHandler(w, r)
|
unauthorizedHandler(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -82,7 +79,6 @@ func postPlanFunc(m *models.Model) http.HandlerFunc {
|
|||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, err := tokens.GetUserID(r.Context())
|
userID, err := tokens.GetUserID(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
|
||||||
unauthorizedHandler(w, r)
|
unauthorizedHandler(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
95
routes/plans_unauthorized_test.go
Normal file
95
routes/plans_unauthorized_test.go
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package routes_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"gitea.deepak.science/deepak/gogmagog/models"
|
||||||
|
"gitea.deepak.science/deepak/gogmagog/routes"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEmptyPlanEmptyContext(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
router := routes.NewPlanRouter(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 TestOnePlanEmptyContext(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
||||||
|
m := getEmptyModel()
|
||||||
|
m.AddPlan(p, 3)
|
||||||
|
router := routes.NewPlanRouter(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 TestOnePlanByIDEmptyContext(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
planDate, _ := time.Parse("2006-01-02", "2021-01-01")
|
||||||
|
p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3}
|
||||||
|
m := getEmptyModel()
|
||||||
|
m.AddPlan(p, 3)
|
||||||
|
router := routes.NewPlanRouter(m)
|
||||||
|
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/1", nil)
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusUnauthorized, status)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPureJSONEmptyContext(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
router := routes.NewPlanRouter(m)
|
||||||
|
data := []byte(`{
|
||||||
|
"plan_date": "2021-01-01T00:00:00Z",
|
||||||
|
"plan_id": 1,
|
||||||
|
"user_id": 3
|
||||||
|
}`)
|
||||||
|
req, _ := http.NewRequestWithContext(context.Background(), "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.StatusUnauthorized, status)
|
||||||
|
}
|
||||||
39
tokens/middleware_context_test.go
Normal file
39
tokens/middleware_context_test.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package tokens_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"gitea.deepak.science/deepak/gogmagog/tokens"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoodContext(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
idToUse := 3
|
||||||
|
username := "username"
|
||||||
|
|
||||||
|
ctx := tokens.GetContextForUserValues(idToUse, username)
|
||||||
|
|
||||||
|
receivedID, err := tokens.GetUserID(ctx)
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.EqualValues(idToUse, receivedID)
|
||||||
|
|
||||||
|
receivedUsername, err := tokens.GetUsername(ctx)
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal(username, receivedUsername)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBadContext(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
_, err := tokens.GetUserID(ctx)
|
||||||
|
assert.NotNil(err)
|
||||||
|
|
||||||
|
_, err = tokens.GetUsername(ctx)
|
||||||
|
assert.NotNil(err)
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user