|
| 1 | +package httpclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestIsValidClientID(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + clientID string |
| 13 | + expected bool |
| 14 | + expectedMsg string |
| 15 | + }{ |
| 16 | + {"123e4567-e89b-12d3-a456-426614174000", true, ""}, |
| 17 | + {"invalid-uuid", false, "Client ID is not a valid UUID format."}, |
| 18 | + {"", true, ""}, // Empty client ID should be considered valid as per your updated logic |
| 19 | + } |
| 20 | + |
| 21 | + for _, tt := range tests { |
| 22 | + valid, msg := IsValidClientID(tt.clientID) |
| 23 | + assert.Equal(t, tt.expected, valid) |
| 24 | + assert.Equal(t, tt.expectedMsg, msg) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestIsValidClientSecret(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + clientSecret string |
| 31 | + expected bool |
| 32 | + expectedMsg string |
| 33 | + }{ |
| 34 | + {"ValidSecret123!", true, ""}, |
| 35 | + {"short", false, "Client secret must be at least 16 characters long."}, |
| 36 | + {"", true, ""}, // Empty client secret should be considered valid as per your updated logic |
| 37 | + } |
| 38 | + |
| 39 | + for _, tt := range tests { |
| 40 | + valid, msg := IsValidClientSecret(tt.clientSecret) |
| 41 | + assert.Equal(t, tt.expected, valid) |
| 42 | + assert.Equal(t, tt.expectedMsg, msg) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestIsValidUsername(t *testing.T) { |
| 47 | + tests := []struct { |
| 48 | + username string |
| 49 | + expected bool |
| 50 | + expectedMsg string |
| 51 | + }{ |
| 52 | + {"user123", true, ""}, |
| 53 | + {"user!@#", true, ""}, |
| 54 | + {"<script>", false, "Username must contain only alphanumeric characters and password safe special characters (!@#$%^&*()_-+=[{]}\\|;:'\",<.>/?)."}, |
| 55 | + } |
| 56 | + |
| 57 | + for _, tt := range tests { |
| 58 | + valid, msg := IsValidUsername(tt.username) |
| 59 | + assert.Equal(t, tt.expected, valid) |
| 60 | + assert.Equal(t, tt.expectedMsg, msg) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestIsValidPassword(t *testing.T) { |
| 65 | + tests := []struct { |
| 66 | + password string |
| 67 | + expected bool |
| 68 | + expectedMsg string |
| 69 | + }{ |
| 70 | + {"Password1", true, ""}, |
| 71 | + {"short", false, "Password must be at least 8 characters long."}, |
| 72 | + } |
| 73 | + |
| 74 | + for _, tt := range tests { |
| 75 | + valid, msg := IsValidPassword(tt.password) |
| 76 | + assert.Equal(t, tt.expected, valid) |
| 77 | + assert.Equal(t, tt.expectedMsg, msg) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestDetermineAuthMethod(t *testing.T) { |
| 82 | + tests := []struct { |
| 83 | + authConfig AuthConfig |
| 84 | + expected string |
| 85 | + expectedErr error |
| 86 | + }{ |
| 87 | + {AuthConfig{ClientID: "123e4567-e89b-12d3-a456-426614174000", ClientSecret: "ValidSecret123!"}, "oauth", nil}, |
| 88 | + {AuthConfig{Username: "user123", Password: "Password1"}, "bearer", nil}, |
| 89 | + {AuthConfig{ClientID: "invalid-uuid", ClientSecret: "ValidSecret123!"}, "unknown", errors.New("No valid credentials provided. Client ID is not a valid UUID format.")}, |
| 90 | + {AuthConfig{}, "unknown", errors.New("No valid credentials provided.")}, // No credentials provided |
| 91 | + } |
| 92 | + |
| 93 | + for _, tt := range tests { |
| 94 | + method, err := DetermineAuthMethod(tt.authConfig) |
| 95 | + assert.Equal(t, tt.expected, method) |
| 96 | + if tt.expectedErr != nil { |
| 97 | + assert.EqualError(t, err, tt.expectedErr.Error()) |
| 98 | + } else { |
| 99 | + assert.NoError(t, err) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments