Skip to content

Commit

Permalink
Merge pull request #2296 from MuhammadUmer44/GetAdminPubkeys-unit-test
Browse files Browse the repository at this point in the history
[Unit Tests] - GetAdminPubkeys
  • Loading branch information
elraphty authored Dec 30, 2024
2 parents f587e74 + 335dd4c commit 585a895
Showing 1 changed file with 151 additions and 2 deletions.
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

0 comments on commit 585a895

Please sign in to comment.