|
| 1 | +// httpclient_auth_validation_test.go |
| 2 | +package httpclient |
| 3 | + |
| 4 | +import ( |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +// TestIsValidClientID tests the IsValidClientID function with various client ID inputs. |
| 12 | +// It verifies that valid UUIDs are correctly identified as such, and invalid formats |
| 13 | +// are appropriately flagged with an error message. Additionally, it checks that empty |
| 14 | +// client IDs are considered valid according to the updated logic. |
| 15 | +func TestIsValidClientID(t *testing.T) { |
| 16 | + tests := []struct { |
| 17 | + clientID string |
| 18 | + expected bool |
| 19 | + expectedMsg string |
| 20 | + }{ |
| 21 | + {"123e4567-e89b-12d3-a456-426614174000", true, ""}, |
| 22 | + {"invalid-uuid", false, "Client ID is not a valid UUID format."}, |
| 23 | + {"", true, ""}, // Empty client ID should be considered valid as per your updated logic |
| 24 | + } |
| 25 | + |
| 26 | + for _, tt := range tests { |
| 27 | + valid, msg := IsValidClientID(tt.clientID) |
| 28 | + assert.Equal(t, tt.expected, valid) |
| 29 | + assert.Equal(t, tt.expectedMsg, msg) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// TestIsValidClientSecret tests the IsValidClientSecret function with various client secret inputs. |
| 34 | +// It ensures that client secrets that meet the minimum length requirement and contain the necessary |
| 35 | +// character types are validated correctly. It also checks that short or invalid client secrets are |
| 36 | +// flagged appropriately, and that empty client secrets are considered valid as per the updated logic. |
| 37 | +func TestIsValidClientSecret(t *testing.T) { |
| 38 | + tests := []struct { |
| 39 | + clientSecret string |
| 40 | + expected bool |
| 41 | + expectedMsg string |
| 42 | + }{ |
| 43 | + {"ValidSecret123!", true, ""}, |
| 44 | + {"short", false, "Client secret must be at least 16 characters long."}, |
| 45 | + {"", true, ""}, // Empty client secret should be considered valid as per your updated logic |
| 46 | + } |
| 47 | + |
| 48 | + for _, tt := range tests { |
| 49 | + valid, msg := IsValidClientSecret(tt.clientSecret) |
| 50 | + assert.Equal(t, tt.expected, valid) |
| 51 | + assert.Equal(t, tt.expectedMsg, msg) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// TestIsValidUsername tests the IsValidUsername function with various username inputs. |
| 56 | +// This function verifies that usernames consisting of alphanumeric characters and password safe |
| 57 | +// special characters are considered valid. It also checks that usernames with unsafe characters |
| 58 | +// are correctly identified as invalid. |
| 59 | +func TestIsValidUsername(t *testing.T) { |
| 60 | + tests := []struct { |
| 61 | + username string |
| 62 | + expected bool |
| 63 | + expectedMsg string |
| 64 | + }{ |
| 65 | + {"user123", true, ""}, |
| 66 | + {"user!@#", true, ""}, |
| 67 | + {"<script>", false, "Username must contain only alphanumeric characters and password safe special characters (!@#$%^&*()_-+=[{]}\\|;:'\",<.>/?)."}, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tt := range tests { |
| 71 | + valid, msg := IsValidUsername(tt.username) |
| 72 | + assert.Equal(t, tt.expected, valid) |
| 73 | + assert.Equal(t, tt.expectedMsg, msg) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// TestIsValidPassword tests the IsValidPassword function with various password inputs. |
| 78 | +// It ensures that passwords meeting the minimum length requirement are validated correctly, |
| 79 | +// and that short passwords are appropriately flagged as invalid. |
| 80 | +func TestIsValidPassword(t *testing.T) { |
| 81 | + tests := []struct { |
| 82 | + password string |
| 83 | + expected bool |
| 84 | + expectedMsg string |
| 85 | + }{ |
| 86 | + {"Password1", true, ""}, |
| 87 | + {"short", false, "Password must be at least 8 characters long."}, |
| 88 | + } |
| 89 | + |
| 90 | + for _, tt := range tests { |
| 91 | + valid, msg := IsValidPassword(tt.password) |
| 92 | + assert.Equal(t, tt.expected, valid) |
| 93 | + assert.Equal(t, tt.expectedMsg, msg) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// TestDetermineAuthMethod tests the DetermineAuthMethod function with various authentication configurations. |
| 98 | +// It checks that the function correctly identifies the authentication method to be used based on the provided |
| 99 | +// credentials. Scenarios include valid OAuth credentials, valid bearer token credentials, and various combinations |
| 100 | +// of invalid or missing credentials. The function should return "oauth" for valid OAuth credentials, "bearer" for |
| 101 | +// valid bearer token credentials, and "unknown" with an error message for invalid or incomplete credentials. |
| 102 | +func TestDetermineAuthMethod(t *testing.T) { |
| 103 | + tests := []struct { |
| 104 | + authConfig AuthConfig |
| 105 | + expected string |
| 106 | + expectedErr error |
| 107 | + }{ |
| 108 | + {AuthConfig{ClientID: "123e4567-e89b-12d3-a456-426614174000", ClientSecret: "ValidSecret123!"}, "oauth", nil}, |
| 109 | + {AuthConfig{Username: "user123", Password: "Password1"}, "bearer", nil}, |
| 110 | + {AuthConfig{ClientID: "invalid-uuid", ClientSecret: "ValidSecret123!"}, "unknown", errors.New("No valid credentials provided. Client ID is not a valid UUID format.")}, |
| 111 | + {AuthConfig{}, "unknown", errors.New("No valid credentials provided.")}, // No credentials provided |
| 112 | + } |
| 113 | + |
| 114 | + for _, tt := range tests { |
| 115 | + method, err := DetermineAuthMethod(tt.authConfig) |
| 116 | + assert.Equal(t, tt.expected, method) |
| 117 | + if tt.expectedErr != nil { |
| 118 | + assert.EqualError(t, err, tt.expectedErr.Error()) |
| 119 | + } else { |
| 120 | + assert.NoError(t, err) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments