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] - GetAdminPubkeys #2296

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
153 changes: 151 additions & 2 deletions handlers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -46,12 +47,160 @@ func TestGetAdminPubkeys(t *testing.T) {

expected := `{"pubkeys":["test"]}`
if strings.TrimRight(rr.Body.String(), "\n") != expected {

t.Errorf("handler returned unexpected body: expected %s pubkeys %s is there a space after?", expected, rr.Body.String())
}
})
}

t.Run("Should handle multiple admin pubkeys", func(t *testing.T) {
os.Setenv("ADMINS", "test1,test2,test3")
os.Setenv("RELAY_URL", "RelayUrl")
os.Setenv("RELAY_AUTH_KEY", "RelayAuthKey")
config.InitConfig()

req, err := http.NewRequest("GET", "/admin_pubkeys", nil)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(GetAdminPubkeys)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
expected := `{"pubkeys":["test1","test2","test3"]}`
assert.JSONEq(t, expected, strings.TrimRight(rr.Body.String(), "\n"))
})

t.Run("Should handle empty admin pubkeys", func(t *testing.T) {
os.Setenv("ADMINS", "")
os.Setenv("RELAY_URL", "RelayUrl")
os.Setenv("RELAY_AUTH_KEY", "RelayAuthKey")
config.InitConfig()

req, err := http.NewRequest("GET", "/admin_pubkeys", nil)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(GetAdminPubkeys)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
expected := `{"pubkeys":[]}`
assert.JSONEq(t, expected, strings.TrimRight(rr.Body.String(), "\n"))
})

t.Run("Should handle admin pubkeys with special characters", func(t *testing.T) {
os.Setenv("ADMINS", "test@123,test#456,test$789")
os.Setenv("RELAY_URL", "RelayUrl")
os.Setenv("RELAY_AUTH_KEY", "RelayAuthKey")
config.InitConfig()

req, err := http.NewRequest("GET", "/admin_pubkeys", nil)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(GetAdminPubkeys)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
expected := `{"pubkeys":["test@123","test#456","test$789"]}`
assert.JSONEq(t, expected, strings.TrimRight(rr.Body.String(), "\n"))
})

t.Run("Should handle admin pubkeys with spaces", func(t *testing.T) {
os.Setenv("ADMINS", "test 123, test 456 , test 789")
os.Setenv("RELAY_URL", "RelayUrl")
os.Setenv("RELAY_AUTH_KEY", "RelayAuthKey")
config.InitConfig()

req, err := http.NewRequest("GET", "/admin_pubkeys", nil)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(GetAdminPubkeys)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
expected := `{"pubkeys":["test 123","test 456","test 789"]}`
assert.JSONEq(t, expected, strings.TrimRight(rr.Body.String(), "\n"))
})

t.Run("Should handle invalid HTTP method", func(t *testing.T) {
os.Setenv("ADMINS", "test")
os.Setenv("RELAY_URL", "RelayUrl")
os.Setenv("RELAY_AUTH_KEY", "RelayAuthKey")
config.InitConfig()

req, err := http.NewRequest(http.MethodPost, "/admin_pubkeys", nil)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(GetAdminPubkeys)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
expected := `{"pubkeys":["test"]}`
assert.JSONEq(t, expected, strings.TrimRight(rr.Body.String(), "\n"))
})
t.Run("Maximum Number of Admin Keys", func(t *testing.T) {

var keys []string
for i := 0; i < 1000; i++ {
keys = append(keys, fmt.Sprintf("key%d", i))
}
os.Setenv("ADMINS", strings.Join(keys, ","))
os.Setenv("RELAY_URL", "RelayUrl")
os.Setenv("RELAY_AUTH_KEY", "RelayAuthKey")
config.InitConfig()

req, err := http.NewRequest("GET", "/admin_pubkeys", nil)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(GetAdminPubkeys)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
var response map[string][]string
err = json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Equal(t, 1000, len(response["pubkeys"]))
})

t.Run("Null Admin Keys List", func(t *testing.T) {
os.Unsetenv("ADMINS")
os.Setenv("RELAY_URL", "RelayUrl")
os.Setenv("RELAY_AUTH_KEY", "RelayAuthKey")
config.InitConfig()

req, err := http.NewRequest("GET", "/admin_pubkeys", nil)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(GetAdminPubkeys)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
expected := `{"pubkeys":[]}`
assert.JSONEq(t, expected, strings.TrimRight(rr.Body.String(), "\n"))
})

t.Run("Unicode Characters in Admin Keys", func(t *testing.T) {
os.Setenv("ADMINS", "ключ1,キー2,钥匙3")
os.Setenv("RELAY_URL", "RelayUrl")
os.Setenv("RELAY_AUTH_KEY", "RelayAuthKey")
config.InitConfig()

req, err := http.NewRequest("GET", "/admin_pubkeys", nil)
assert.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(GetAdminPubkeys)
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
expected := `{"pubkeys":["ключ1","キー2","钥匙3"]}`
assert.JSONEq(t, expected, strings.TrimRight(rr.Body.String(), "\n"))
})
}
func TestCreateConnectionCode(t *testing.T) {
teardownSuite := SetupSuite(t)
defer teardownSuite(t)
Expand Down
Loading