test(handlers): RequireAuth distinguishes missing context from user id zero
This commit is contained in:
@@ -569,33 +569,41 @@ func TestParseUintParam(t *testing.T) {
|
|||||||
func TestRequireAuth(t *testing.T) {
|
func TestRequireAuth(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
userID uint
|
setUserKey bool
|
||||||
|
userIDValue uint
|
||||||
expectedID uint
|
expectedID uint
|
||||||
expectedOK bool
|
expectedOK bool
|
||||||
expectedStatus int
|
expectedStatus int
|
||||||
expectedError string
|
expectedError string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "authenticated user",
|
name: "authenticated user",
|
||||||
userID: 123,
|
setUserKey: true,
|
||||||
expectedID: 123,
|
userIDValue: 123,
|
||||||
expectedOK: true,
|
expectedID: 123,
|
||||||
expectedStatus: 0,
|
expectedOK: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "unauthenticated user (no userID)",
|
name: "unauthenticated user (missing context)",
|
||||||
userID: 0,
|
setUserKey: false,
|
||||||
expectedID: 0,
|
expectedID: 0,
|
||||||
expectedOK: false,
|
expectedOK: false,
|
||||||
expectedStatus: http.StatusUnauthorized,
|
expectedStatus: http.StatusUnauthorized,
|
||||||
expectedError: "Authentication required",
|
expectedError: "Authentication required",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "authenticated user with large ID",
|
name: "authenticated user with id zero",
|
||||||
userID: 4294967295,
|
setUserKey: true,
|
||||||
expectedID: 4294967295,
|
userIDValue: 0,
|
||||||
expectedOK: true,
|
expectedID: 0,
|
||||||
expectedStatus: 0,
|
expectedOK: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "authenticated user with large ID",
|
||||||
|
setUserKey: true,
|
||||||
|
userIDValue: 4294967295,
|
||||||
|
expectedID: 4294967295,
|
||||||
|
expectedOK: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -604,7 +612,10 @@ func TestRequireAuth(t *testing.T) {
|
|||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
r := httptest.NewRequest("GET", "/", nil)
|
r := httptest.NewRequest("GET", "/", nil)
|
||||||
|
|
||||||
ctx := context.WithValue(r.Context(), middleware.UserIDKey, tt.userID)
|
ctx := context.Background()
|
||||||
|
if tt.setUserKey {
|
||||||
|
ctx = context.WithValue(ctx, middleware.UserIDKey, tt.userIDValue)
|
||||||
|
}
|
||||||
r = r.WithContext(ctx)
|
r = r.WithContext(ctx)
|
||||||
|
|
||||||
userID, ok := RequireAuth(w, r)
|
userID, ok := RequireAuth(w, r)
|
||||||
|
|||||||
Reference in New Issue
Block a user