Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] - GetIsAdmin #2298

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions handlers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,115 @@ func TestGetIsAdmin(t *testing.T) {

assert.Equal(t, http.StatusOK, rr.Code)
})

t.Run("Should test that empty public key returns unauthorized", func(t *testing.T) {
req, err := http.NewRequest("GET", "/admin/auth", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(aHandler.GetIsAdmin)

ctx := context.WithValue(req.Context(), auth.ContextKey, "")
req = req.WithContext(ctx)

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusUnauthorized, rr.Code)
var responseBody string
json.NewDecoder(rr.Body).Decode(&responseBody)
assert.Equal(t, "Not a super admin: handler", responseBody)
})

t.Run("Should test that nil context value returns unauthorized", func(t *testing.T) {
req, err := http.NewRequest("GET", "/admin/auth", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(aHandler.GetIsAdmin)

ctx := context.WithValue(req.Context(), auth.ContextKey, nil)
req = req.WithContext(ctx)

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusUnauthorized, rr.Code)
var responseBody string
json.NewDecoder(rr.Body).Decode(&responseBody)
assert.Equal(t, "Not a super admin: handler", responseBody)
})

t.Run("Should test that free pass enabled allows any user", func(t *testing.T) {

originalAdmins := config.SuperAdmins
config.SuperAdmins = []string{config.AdminDevFreePass}
defer func() {
config.SuperAdmins = originalAdmins
}()

req, err := http.NewRequest("GET", "/admin/auth", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(aHandler.GetIsAdmin)

ctx := context.WithValue(req.Context(), auth.ContextKey, "any_pubkey")
req = req.WithContext(ctx)

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
var responseBody string
json.NewDecoder(rr.Body).Decode(&responseBody)
assert.Equal(t, "Log in successful", responseBody)
})

t.Run("Should test that invalid context value type returns unauthorized", func(t *testing.T) {
req, err := http.NewRequest("GET", "/admin/auth", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(aHandler.GetIsAdmin)

ctx := context.WithValue(req.Context(), auth.ContextKey, 12345)
req = req.WithContext(ctx)

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusUnauthorized, rr.Code)
var responseBody string
json.NewDecoder(rr.Body).Decode(&responseBody)
assert.Equal(t, "Not a super admin: handler", responseBody)
})

t.Run("Should test multiple admins configuration", func(t *testing.T) {

originalAdmins := config.SuperAdmins
config.SuperAdmins = []string{"admin1", "admin2", "admin3"}
defer func() {
config.SuperAdmins = originalAdmins
}()

req, err := http.NewRequest("GET", "/admin/auth", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(aHandler.GetIsAdmin)

ctx := context.WithValue(req.Context(), auth.ContextKey, "admin2")
req = req.WithContext(ctx)

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
var responseBody string
json.NewDecoder(rr.Body).Decode(&responseBody)
assert.Equal(t, "Log in successful", responseBody)
})
}

func TestRefreshToken(t *testing.T) {
Expand Down
Loading