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" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) // Mocking TokenAuth and ExpireInHours for testing purposes type MockTokenAuth struct { mock.Mock } func (m *MockTokenAuth) Encode(claims map[string]interface{}) (interface{}, string, error) { args := m.Called(claims) return args.Get(0), args.String(1), args.Error(2) } func MockExpireInHours(hours int) int64 { return int64(hours * 3600) } func TestEncodeJwt(t *testing.T) { mockTokenAuth := new(MockTokenAuth) originalTokenAuth := TokenAuth originalExpireInHours := ExpireInHours // Replace the real implementations with mocks TokenAuth = mockTokenAuth ExpireInHours = MockExpireInHours defer func() { // Restore the original implementations after tests TokenAuth = originalTokenAuth ExpireInHours = originalExpireInHours }() t.Run("Valid Public Key", func(t *testing.T) { pubkey := "validPublicKey123" expectedToken := "validTokenString" mockTokenAuth.On("Encode", mock.Anything).Return(nil, expectedToken, nil) token, err := EncodeJwt(pubkey) assert.NoError(t, err) assert.Equal(t, expectedToken, token) mockTokenAuth.AssertExpectations(t) }) t.Run("Empty Public Key", func(t *testing.T) { pubkey := "" expectedError := "public key cannot be empty" mockTokenAuth.On("Encode", mock.Anything).Return(nil, "", errors.New(expectedError)) token, err := EncodeJwt(pubkey) assert.Error(t, err) assert.Equal(t, expectedError, err.Error()) assert.Equal(t, "", token) mockTokenAuth.AssertExpectations(t) }) t.Run("Maximum Length Public Key", func(t *testing.T) { pubkey := string(make([]byte, 1024)) expectedToken := "validTokenString" mockTokenAuth.On("Encode", mock.Anything).Return(nil, expectedToken, nil) token, err := EncodeJwt(pubkey) assert.NoError(t, err) assert.Equal(t, expectedToken, token) mockTokenAuth.AssertExpectations(t) }) t.Run("Minimum Length Public Key", func(t *testing.T) { pubkey := "a" expectedToken := "validTokenString" mockTokenAuth.On("Encode", mock.Anything).Return(nil, expectedToken, nil) token, err := EncodeJwt(pubkey) assert.NoError(t, err) assert.Equal(t, expectedToken, token) mockTokenAuth.AssertExpectations(t) }) t.Run("Invalid Characters in Public Key", func(t *testing.T) { pubkey := "invalid!@#$%^&*()" expectedError := "invalid characters in public key" mockTokenAuth.On("Encode", mock.Anything).Return(nil, "", errors.New(expectedError)) token, err := EncodeJwt(pubkey) assert.Error(t, err) assert.Equal(t, expectedError, err.Error()) assert.Equal(t, "", token) mockTokenAuth.AssertExpectations(t) }) t.Run("TokenAuth Encode Failure", func(t *testing.T) { pubkey := "validPublicKey123" expectedError := "encoding failed" mockTokenAuth.On("Encode", mock.Anything).Return(nil, "", errors.New(expectedError)) token, err := EncodeJwt(pubkey) assert.Error(t, err) assert.Equal(t, expectedError, err.Error()) assert.Equal(t, "", token) mockTokenAuth.AssertExpectations(t) }) t.Run("Expired Token", func(t *testing.T) { pubkey := "validPublicKey123" expectedToken := "expiredTokenString" mockTokenAuth.On("Encode", mock.Anything).Return(nil, expectedToken, nil) ExpireInHours = func(hours int) int64 { return -1 } // Simulate past expiration token, err := EncodeJwt(pubkey) assert.NoError(t, err) assert.Equal(t, expectedToken, token) mockTokenAuth.AssertExpectations(t) }) t.Run("Future Expiration Date", func(t *testing.T) { pubkey := "validPublicKey123" expectedToken := "futureTokenString" mockTokenAuth.On("Encode", mock.Anything).Return(nil, expectedToken, nil) ExpireInHours = func(hours int) int64 { return 9999999999 } // Simulate far future expiration token, err := EncodeJwt(pubkey) assert.NoError(t, err) assert.Equal(t, expectedToken, token) mockTokenAuth.AssertExpectations(t) }) t.Run("Whitespace Public Key", func(t *testing.T) { pubkey := " " expectedError := "public key cannot be just whitespace" mockTokenAuth.On("Encode", mock.Anything).Return(nil, "", errors.New(expectedError)) token, err := EncodeJwt(pubkey) assert.Error(t, err) assert.Equal(t, expectedError, err.Error()) assert.Equal(t, "", token) mockTokenAuth.AssertExpectations(t) }) t.Run("Null Public Key", func(t *testing.T) { var pubkey interface{} = nil expectedError := "public key cannot be null" mockTokenAuth.On("Encode", mock.Anything).Return(nil, "", errors.New(expectedError)) token, err := EncodeJwt(pubkey) assert.Error(t, err) assert.Equal(t, expectedError, err.Error()) assert.Equal(t, "", token) mockTokenAuth.AssertExpectations(t) }) }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Unit Test Coverage for "EncodeJwt"
Stakwork Run
Unit Test Code
The text was updated successfully, but these errors were encountered: