We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Stakwork Run
File: /tmp/stakwork/sphinx-tribes/handlers/auth.go
package auth import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) // Mocking the auth package functions type MockAuth struct { mock.Mock } func (m *MockAuth) AdminCheck(pubkey string) bool { args := m.Called(pubkey) return args.Bool(0) } func (m *MockAuth) IsFreePass() bool { args := m.Called() return args.Bool(0) } func TestGetIsAdmin(t *testing.T) { mockAuth := new(MockAuth) originalAdminCheck := auth.AdminCheck originalIsFreePass := auth.IsFreePass // Replace the real functions with mocks auth.AdminCheck = mockAuth.AdminCheck auth.IsFreePass = mockAuth.IsFreePass defer func() { // Restore the original functions after tests auth.AdminCheck = originalAdminCheck auth.IsFreePass = originalIsFreePass }() tests := []struct { name string pubKeyFromAuth interface{} adminCheckResult bool isFreePassResult bool expectedStatus int expectedBody string }{ { name: "Admin User with Free Pass Disabled", pubKeyFromAuth: "admin1", adminCheckResult: true, isFreePassResult: false, expectedStatus: http.StatusOK, expectedBody: "Log in successful", }, { name: "Non-Admin User with Free Pass Disabled", pubKeyFromAuth: "notAnAdmin", adminCheckResult: false, isFreePassResult: false, expectedStatus: http.StatusUnauthorized, expectedBody: "Not a super admin: handler", }, { name: "Admin User with Free Pass Enabled", pubKeyFromAuth: "admin1", adminCheckResult: true, isFreePassResult: true, expectedStatus: http.StatusOK, expectedBody: "Log in successful", }, { name: "Non-Admin User with Free Pass Enabled", pubKeyFromAuth: "notAnAdmin", adminCheckResult: false, isFreePassResult: true, expectedStatus: http.StatusOK, expectedBody: "Log in successful", }, { name: "Empty Public Key", pubKeyFromAuth: "", adminCheckResult: false, isFreePassResult: false, expectedStatus: http.StatusUnauthorized, expectedBody: "Not a super admin: handler", }, { name: "Null Context Value", pubKeyFromAuth: nil, adminCheckResult: false, isFreePassResult: false, expectedStatus: http.StatusUnauthorized, expectedBody: "Not a super admin: handler", }, { name: "Invalid Context Key Type", pubKeyFromAuth: 12345, adminCheckResult: false, isFreePassResult: false, expectedStatus: http.StatusUnauthorized, expectedBody: "Not a super admin: handler", }, { name: "AdminCheck Returns Unexpected Value", pubKeyFromAuth: "unexpected", adminCheckResult: false, // Simulate unexpected non-boolean behavior isFreePassResult: false, expectedStatus: http.StatusUnauthorized, expectedBody: "Not a super admin: handler", }, { name: "Free Pass Function Throws Error", pubKeyFromAuth: "errorCase", adminCheckResult: false, isFreePassResult: false, // Simulate error in IsFreePass expectedStatus: http.StatusUnauthorized, expectedBody: "Not a super admin: handler", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { mockAuth.On("AdminCheck", mock.Anything).Return(tt.adminCheckResult) mockAuth.On("IsFreePass").Return(tt.isFreePassResult) req := httptest.NewRequest(http.MethodGet, "/admin/auth", nil) ctx := context.WithValue(req.Context(), auth.ContextKey, tt.pubKeyFromAuth) req = req.WithContext(ctx) rr := httptest.NewRecorder() handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ah := &authHandler{} ah.GetIsAdmin(w, r) }) handler.ServeHTTP(rr, req) assert.Equal(t, tt.expectedStatus, rr.Code) var responseBody string json.NewDecoder(rr.Body).Decode(&responseBody) assert.Equal(t, tt.expectedBody, responseBody) mockAuth.AssertExpectations(t) }) } }
The text was updated successfully, but these errors were encountered:
@tomsmith8 assign me?
Sorry, something went wrong.
@tomsmith8 I can help?
@tomsmith8 can I help?
MirzaHanan
Successfully merging a pull request may close this issue.
Unit Test Coverage for "GetIsAdmin"
Stakwork Run
Unit Test Code
File: /tmp/stakwork/sphinx-tribes/handlers/auth.go
The text was updated successfully, but these errors were encountered: