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
package auth import ( "errors" "net/http" "net/http/httptest" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) // Mock dependencies type MockDecoder struct { mock.Mock } func (m *MockDecoder) DecodeJwt(token string) (map[string]interface{}, error) { args := m.Called(token) return args.Get(0).(map[string]interface{}), args.Error(1) } type MockVerifier struct { mock.Mock } func (m *MockVerifier) VerifyTribeUUID(token string, checkTimestamp bool) (string, error) { args := m.Called(token, checkTimestamp) return args.String(0), args.Error(1) } type MockAdminChecker struct { mock.Mock } func (m *MockAdminChecker) AdminCheck(pubkey string) bool { args := m.Called(pubkey) return args.Bool(0) } type MockFreePass struct { mock.Mock } func (m *MockFreePass) IsFreePass() bool { args := m.Called() return args.Bool(0) } // Test function func TestPubKeyContextSuperAdmin(t *testing.T) { mockDecoder := new(MockDecoder) mockVerifier := new(MockVerifier) mockAdminChecker := new(MockAdminChecker) mockFreePass := new(MockFreePass) // Helper function to create a new request createRequest := func(token string) *http.Request { req := httptest.NewRequest(http.MethodGet, "http://example.com", nil) req.Header.Set("x-jwt", token) return req } // Helper function to execute the handler executeHandler := func(req *http.Request) *httptest.ResponseRecorder { rr := httptest.NewRecorder() handler := PubKeyContextSuperAdmin(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })) handler.ServeHTTP(rr, req) return rr } // Test cases tests := []struct { name string token string setupMocks func() expectedStatus int expectedBody string }{ { name: "Valid JWT Token with Super Admin Privileges", token: "valid.jwt.token", setupMocks: func() { mockDecoder.On("DecodeJwt", "valid.jwt.token").Return(map[string]interface{}{"pubkey": "superadmin"}, nil) mockAdminChecker.On("AdminCheck", "superadmin").Return(true) mockFreePass.On("IsFreePass").Return(false) }, expectedStatus: http.StatusOK, }, { name: "Valid Tribe UUID Token with Super Admin Privileges", token: "valid.tribe.uuid", setupMocks: func() { mockVerifier.On("VerifyTribeUUID", "valid.tribe.uuid", true).Return("superadmin", nil) mockAdminChecker.On("AdminCheck", "superadmin").Return(true) mockFreePass.On("IsFreePass").Return(false) }, expectedStatus: http.StatusOK, }, { name: "Empty Token in Request", token: "", setupMocks: func() { mockFreePass.On("IsFreePass").Return(false) }, expectedStatus: http.StatusUnauthorized, expectedBody: "Unauthorized\n", }, { name: "Token with Only a Dot", token: ".", setupMocks: func() { mockFreePass.On("IsFreePass").Return(false) }, expectedStatus: http.StatusUnauthorized, expectedBody: "Unauthorized\n", }, { name: "Expired JWT Token", token: "expired.jwt.token", setupMocks: func() { mockDecoder.On("DecodeJwt", "expired.jwt.token").Return(map[string]interface{}{"pubkey": "superadmin", "exp": time.Now().Add(-time.Hour).Unix()}, nil) mockFreePass.On("IsFreePass").Return(false) }, expectedStatus: http.StatusUnauthorized, expectedBody: "Unauthorized\n", }, { name: "Invalid JWT Token Format", token: "invalid.jwt.token", setupMocks: func() { mockDecoder.On("DecodeJwt", "invalid.jwt.token").Return(nil, errors.New("invalid token")) mockFreePass.On("IsFreePass").Return(false) }, expectedStatus: http.StatusUnauthorized, expectedBody: "Unauthorized\n", }, { name: "Invalid Tribe UUID Token", token: "invalid.tribe.uuid", setupMocks: func() { mockVerifier.On("VerifyTribeUUID", "invalid.tribe.uuid", true).Return("", errors.New("invalid token")) mockFreePass.On("IsFreePass").Return(false) }, expectedStatus: http.StatusUnauthorized, expectedBody: "Unauthorized\n", }, { name: "Non-Super Admin JWT Token", token: "non.superadmin.jwt", setupMocks: func() { mockDecoder.On("DecodeJwt", "non.superadmin.jwt").Return(map[string]interface{}{"pubkey": "user"}, nil) mockAdminChecker.On("AdminCheck", "user").Return(false) mockFreePass.On("IsFreePass").Return(false) }, expectedStatus: http.StatusUnauthorized, expectedBody: "Unauthorized\n", }, { name: "Non-Super Admin Tribe UUID Token", token: "non.superadmin.tribe", setupMocks: func() { mockVerifier.On("VerifyTribeUUID", "non.superadmin.tribe", true).Return("user", nil) mockAdminChecker.On("AdminCheck", "user").Return(false) mockFreePass.On("IsFreePass").Return(false) }, expectedStatus: http.StatusUnauthorized, expectedBody: "Unauthorized\n", }, { name: "Free Pass Configuration", token: "any.token", setupMocks: func() { mockFreePass.On("IsFreePass").Return(true) }, expectedStatus: http.StatusOK, }, { name: "Token with Invalid Signature", token: "invalid.signature.token", setupMocks: func() { mockDecoder.On("DecodeJwt", "invalid.signature.token").Return(nil, errors.New("invalid signature")) mockFreePass.On("IsFreePass").Return(false) }, expectedStatus: http.StatusUnauthorized, expectedBody: "Unauthorized\n", }, { name: "Token with Missing Claims", token: "missing.claims.token", setupMocks: func() { mockDecoder.On("DecodeJwt", "missing.claims.token").Return(map[string]interface{}{}, nil) mockFreePass.On("IsFreePass").Return(false) }, expectedStatus: http.StatusUnauthorized, expectedBody: "Unauthorized\n", }, { name: "Token with Incorrect Algorithm", token: "incorrect.algorithm.token", setupMocks: func() { mockDecoder.On("DecodeJwt", "incorrect.algorithm.token").Return(nil, errors.New("incorrect algorithm")) mockFreePass.On("IsFreePass").Return(false) }, expectedStatus: http.StatusUnauthorized, expectedBody: "Unauthorized\n", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tt.setupMocks() req := createRequest(tt.token) rr := executeHandler(req) assert.Equal(t, tt.expectedStatus, rr.Code) if tt.expectedBody != "" { assert.Equal(t, tt.expectedBody, rr.Body.String()) } mockDecoder.AssertExpectations(t) mockVerifier.AssertExpectations(t) mockAdminChecker.AssertExpectations(t) mockFreePass.AssertExpectations(t) }) } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Unit Test Coverage for "PubKeyContextSuperAdmin"
Stakwork Run
Unit Test Code
The text was updated successfully, but these errors were encountered: