Skip to content

Commit 5ad1c9d

Browse files
authored
Merge pull request #114 from deploymenttheory/dev
Added testing for auth validation
2 parents 016747b + b380afe commit 5ad1c9d

File tree

5 files changed

+131
-35
lines changed

5 files changed

+131
-35
lines changed

httpclient/httpclient_auth_bearer_token.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,6 @@ import (
1414
"go.uber.org/zap"
1515
)
1616

17-
// // BearerTokenAuthCredentials represents the username and password for basic authentication.
18-
// type BearerTokenAuthCredentials struct {
19-
// Username string
20-
// Password string
21-
// }
22-
23-
// // SetBearerTokenAuthCredentials sets the BearerTokenAuthCredentials (Username and Password)
24-
// // for the client instance. These credentials are used for obtaining and refreshing
25-
// // bearer tokens for authentication.
26-
// func (c *Client) SetBearerTokenAuthCredentials(credentials BearerTokenAuthCredentials) {
27-
// c.BearerTokenAuthCredentials = credentials
28-
// }
29-
3017
// ObtainToken fetches and sets an authentication token using the stored basic authentication credentials.
3118
func (c *Client) ObtainToken(log logger.Logger) error {
3219

httpclient/httpclient_auth_oauth.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,6 @@ type OAuthResponse struct {
2626
Error string `json:"error,omitempty"`
2727
}
2828

29-
// // OAuthCredentials contains the client ID and client secret required for OAuth authentication.
30-
// type OAuthCredentials struct {
31-
// ClientID string
32-
// ClientSecret string
33-
// }
34-
35-
// // SetOAuthCredentials sets the OAuth credentials (Client ID and Client Secret)
36-
// // for the client instance. These credentials are used for obtaining and refreshing
37-
// // OAuth tokens for authentication.
38-
// func (c *Client) SetOAuthCredentials(credentials OAuthCredentials) {
39-
// c.OAuthCredentials = credentials
40-
// }
41-
4229
// ObtainOAuthToken fetches an OAuth access token using the provided OAuthCredentials (Client ID and Client Secret).
4330
// It updates the client's Token and Expiry fields with the obtained values.
4431
func (c *Client) ObtainOAuthToken(credentials AuthConfig) error {

httpclient/httpclient_auth_validation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// httpclient_auth_validation.go
12
package httpclient
23

34
import (
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}

httpclient/httpclient_client.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ type Client struct {
2424
AuthMethod string // Specifies the authentication method: "bearer" or "oauth"
2525
Token string // Authentication Token
2626
OverrideBaseDomain string // Base domain override used when the default in the api handler isn't suitable
27-
//OAuthCredentials OAuthCredentials // ClientID / Client Secret
28-
//BearerTokenAuthCredentials BearerTokenAuthCredentials // Username and Password for Basic Authentication
29-
Expiry time.Time // Expiry time set for the auth token
30-
httpClient *http.Client
31-
tokenLock sync.Mutex
32-
clientConfig ClientConfig
33-
Logger logger.Logger
34-
ConcurrencyMgr *ConcurrencyManager
35-
PerfMetrics PerformanceMetrics
27+
Expiry time.Time // Expiry time set for the auth token
28+
httpClient *http.Client
29+
tokenLock sync.Mutex
30+
clientConfig ClientConfig
31+
Logger logger.Logger
32+
ConcurrencyMgr *ConcurrencyManager
33+
PerfMetrics PerformanceMetrics
3634
}
3735

3836
// Config holds configuration options for the HTTP Client.

0 commit comments

Comments
 (0)