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 #2289

Closed
tomsmith8 opened this issue Dec 24, 2024 · 4 comments · Fixed by #2296
Closed

[Unit Tests] - GetAdminPubkeys #2289

tomsmith8 opened this issue Dec 24, 2024 · 4 comments · Fixed by #2296
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for "GetAdminPubkeys"


Stakwork Run


Unit Test Code


File: /tmp/stakwork/sphinx-tribes/handlers/auth.go


package auth

import (
  "encoding/json"
  "net/http"
  "net/http/httptest"
  "testing"

  "github.com/stretchr/testify/assert"
)

// Mock configuration for testing
var config = struct {
  SuperAdmins interface{}
}{}

func TestGetAdminPubkeys(t *testing.T) {
  tests := []struct {
  	name           string
  	superAdmins    interface{}
  	expectedStatus int
  	expectedBody   string
  }{
  	{
  		name:           "Standard Input with Multiple Admin Keys",
  		superAdmins:    []string{"key1", "key2", "key3"},
  		expectedStatus: http.StatusOK,
  		expectedBody:   `{"pubkeys":["key1","key2","key3"]}`,
  	},
  	{
  		name:           "Standard Input with Single Admin Key",
  		superAdmins:    []string{"key1"},
  		expectedStatus: http.StatusOK,
  		expectedBody:   `{"pubkeys":["key1"]}`,
  	},
  	{
  		name:           "Empty Admin Keys List",
  		superAdmins:    []string{},
  		expectedStatus: http.StatusOK,
  		expectedBody:   `{"pubkeys":[]}`,
  	},
  	{
  		name:           "Maximum Number of Admin Keys",
  		superAdmins:    generateKeys(1000),
  		expectedStatus: http.StatusOK,
  		expectedBody:   generateExpectedBody(1000),
  	},
  	{
  		name:           "Null Admin Keys List",
  		superAdmins:    nil,
  		expectedStatus: http.StatusOK,
  		expectedBody:   `{"pubkeys":null}`,
  	},
  	{
  		name:           "Invalid Data Type for Admin Keys",
  		superAdmins:    "invalid_type",
  		expectedStatus: http.StatusOK,
  		expectedBody:   `{"pubkeys":[]}`,
  	},
  	{
  		name:           "Special Characters in Admin Keys",
  		superAdmins:    []string{"key1", "key@2", "key#3"},
  		expectedStatus: http.StatusOK,
  		expectedBody:   `{"pubkeys":["key1","key@2","key#3"]}`,
  	},
  	{
  		name:           "Unicode Characters in Admin Keys",
  		superAdmins:    []string{"ключ1", "キー2", "钥匙3"},
  		expectedStatus: http.StatusOK,
  		expectedBody:   `{"pubkeys":["ключ1","キー2","钥匙3"]}`,
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		// Set the mock configuration
  		config.SuperAdmins = tt.superAdmins

  		// Create a request to pass to our handler
  		req, err := http.NewRequest("GET", "/admin_pubkeys", nil)
  		assert.NoError(t, err)

  		// Create a ResponseRecorder to record the response
  		rr := httptest.NewRecorder()
  		handler := http.HandlerFunc(GetAdminPubkeys)

  		// Call the handler
  		handler.ServeHTTP(rr, req)

  		// Check the status code
  		assert.Equal(t, tt.expectedStatus, rr.Code)

  		// Check the response body
  		assert.JSONEq(t, tt.expectedBody, rr.Body.String())
  	})
  }
}

// Helper function to generate a list of keys
func generateKeys(n int) []string {
  keys := make([]string, n)
  for i := 0; i < n; i++ {
  	keys[i] = "key" + string(i)
  }
  return keys
}

// Helper function to generate expected JSON body for a large number of keys
func generateExpectedBody(n int) string {
  keys := generateKeys(n)
  jsonBytes, _ := json.Marshal(map[string][]string{"pubkeys": keys})
  return string(jsonBytes)
}
@MuhammadUmer44
Copy link
Contributor

MuhammadUmer44 commented Dec 24, 2024

@tomsmith8 assign me?

@saithsab877
Copy link
Contributor

@tomsmith8 I can help?

@MahtabBukhari
Copy link
Contributor

@tomsmith8 please assign

@AhsanFarooqDev
Copy link
Contributor

@tomsmith8 can I help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants