Compare commits
4 Commits
4d093ed99a
...
c9675b1573
| Author | SHA1 | Date | |
|---|---|---|---|
|
c9675b1573
|
|||
|
63a9e2ff58
|
|||
|
28325c8d7b
|
|||
|
93a5e9c1ba
|
@@ -33,6 +33,10 @@ func getMeFunc(m *models.Model) http.HandlerFunc {
|
||||
|
||||
user, err := m.UserByUsername(username, userID)
|
||||
if err != nil {
|
||||
if models.IsNotFoundError(err) {
|
||||
notFoundHandler(w, r)
|
||||
return
|
||||
}
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
158
routes/currentUser_test.go
Normal file
158
routes/currentUser_test.go
Normal file
@@ -0,0 +1,158 @@
|
||||
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"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEmptyCurrentUser(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
router := routes.NewCurrentUserRouter(m)
|
||||
req, _ := http.NewRequestWithContext(tokens.GetContextForUserValues(3, "testing"), "GET", "/", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusNotFound, status)
|
||||
|
||||
}
|
||||
|
||||
func TestSingleUser(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
idToUse := 1
|
||||
username := "testing_username"
|
||||
displayName := "testing_name"
|
||||
password := "pass"
|
||||
m.CreateUser(&models.CreateUserRequest{Username: username, DisplayName: displayName, Password: password})
|
||||
|
||||
router := routes.NewCurrentUserRouter(m)
|
||||
req, _ := http.NewRequestWithContext(tokens.GetContextForUserValues(idToUse, username), "GET", "/", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusOK, status)
|
||||
expected := `{
|
||||
"user_id": 1,
|
||||
"username": "testing_username",
|
||||
"display_name": "testing_name"
|
||||
}`
|
||||
assert.JSONEq(expected, rr.Body.String())
|
||||
contentType := rr.Header().Get("Content-Type")
|
||||
assert.Equal("application/json", contentType)
|
||||
|
||||
}
|
||||
|
||||
func TestSingleUserEmptyContext(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
username := "testing_username"
|
||||
displayName := "testing_name"
|
||||
password := "pass"
|
||||
m.CreateUser(&models.CreateUserRequest{Username: username, DisplayName: displayName, Password: password})
|
||||
|
||||
router := routes.NewCurrentUserRouter(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 TestSingleUserContextNoUserID(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
idToUse := 1
|
||||
username := "testing_username"
|
||||
displayName := "testing_name"
|
||||
password := "pass"
|
||||
m.CreateUser(&models.CreateUserRequest{Username: username, DisplayName: displayName, Password: password})
|
||||
|
||||
router := routes.NewCurrentUserRouter(m)
|
||||
req, _ := http.NewRequestWithContext(tokens.SetUserID(context.Background(), idToUse), "GET", "/", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusUnauthorized, status)
|
||||
|
||||
}
|
||||
|
||||
func TestErrorUserContextNoUserID(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getErrorModel("Here's an error.")
|
||||
|
||||
idToUse := 1
|
||||
|
||||
router := routes.NewCurrentUserRouter(m)
|
||||
req, _ := http.NewRequestWithContext(tokens.GetContextForUserValues(idToUse, "username"), "GET", "/", nil)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusInternalServerError, status)
|
||||
|
||||
}
|
||||
|
||||
func TestSingleUserErrorWriter(t *testing.T) {
|
||||
// set up
|
||||
assert := assert.New(t)
|
||||
m := getEmptyModel()
|
||||
|
||||
idToUse := 1
|
||||
username := "testing_username"
|
||||
displayName := "testing_name"
|
||||
password := "pass"
|
||||
m.CreateUser(&models.CreateUserRequest{Username: username, DisplayName: displayName, Password: password})
|
||||
|
||||
router := routes.NewCurrentUserRouter(m)
|
||||
req, _ := http.NewRequestWithContext(tokens.GetContextForUserValues(idToUse, username), "GET", "/", nil)
|
||||
|
||||
rr := NewBadWriter()
|
||||
|
||||
// function under test
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
// check results
|
||||
status := rr.Code
|
||||
assert.Equal(http.StatusInternalServerError, status)
|
||||
|
||||
}
|
||||
@@ -67,6 +67,11 @@ func GetUserID(ctx context.Context) (int, error) {
|
||||
return int(userID), nil
|
||||
}
|
||||
|
||||
// SetUserID sets the username field on a context, necessary because the key is an unexported custom type.
|
||||
func SetUserID(ctx context.Context, id int) context.Context {
|
||||
return context.WithValue(ctx, userIDCtxKey, int64(id))
|
||||
}
|
||||
|
||||
// GetUsername does something similar to GetUserID.
|
||||
func GetUsername(ctx context.Context) (string, error) {
|
||||
username, ok := ctx.Value(usernameCtxKey).(string)
|
||||
|
||||
@@ -37,3 +37,13 @@ func TestBadContext(t *testing.T) {
|
||||
assert.NotNil(err)
|
||||
|
||||
}
|
||||
|
||||
func TestSetContext(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
idToUse := 3
|
||||
ctx := tokens.SetUserID(context.Background(), 3)
|
||||
receivedID, err := tokens.GetUserID(ctx)
|
||||
assert.Nil(err)
|
||||
assert.EqualValues(idToUse, receivedID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user