(t *testing.T)
| 211 | } |
| 212 | |
| 213 | func TestAuthManager_authenticate_Success(t *testing.T) { |
| 214 | // Create a test server that returns a successful authentication response |
| 215 | server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 216 | if r.URL.Path == "/access/ticket" && r.Method == http.MethodPost { |
| 217 | // Verify the request |
| 218 | assert.Equal(t, "application/x-www-form-urlencoded", r.Header.Get("Content-Type")) |
| 219 | assert.Equal(t, "pvetui", r.Header.Get("User-Agent")) |
| 220 | |
| 221 | // Parse form data |
| 222 | err := r.ParseForm() |
| 223 | require.NoError(t, err) |
| 224 | assert.Equal(t, "testuser", r.Form.Get("username")) |
| 225 | assert.Equal(t, "testpass", r.Form.Get("password")) |
| 226 | |
| 227 | response := map[string]interface{}{ |
| 228 | "data": map[string]interface{}{ |
| 229 | "ticket": "auth-ticket", |
| 230 | "CSRFPreventionToken": "auth-csrf", |
| 231 | "username": "testuser", |
| 232 | }, |
| 233 | } |
| 234 | |
| 235 | w.Header().Set("Content-Type", "application/json") |
| 236 | _ = json.NewEncoder(w).Encode(response) |
| 237 | |
| 238 | return |
| 239 | } |
| 240 | |
| 241 | http.NotFound(w, r) |
| 242 | })) |
| 243 | defer server.Close() |
| 244 | |
| 245 | httpClient := &HTTPClient{ |
| 246 | baseURL: server.URL, |
| 247 | client: server.Client(), |
| 248 | } |
| 249 | logger := testutils.NewTestLogger() |
| 250 | |
| 251 | authManager := NewAuthManagerWithPassword(httpClient, "testuser", "testpass", logger) |
| 252 | |
| 253 | token, err := authManager.authenticate(context.Background()) |
| 254 | require.NoError(t, err) |
| 255 | assert.NotNil(t, token) |
| 256 | assert.Equal(t, "auth-ticket", token.Ticket) |
| 257 | assert.Equal(t, "auth-csrf", token.CSRFToken) |
| 258 | assert.Equal(t, "testuser", token.Username) |
| 259 | assert.True(t, token.IsValid()) |
| 260 | |
| 261 | // Verify the token is cached |
| 262 | assert.Equal(t, token, authManager.authToken) |
| 263 | } |
| 264 | |
| 265 | func TestAuthManager_authenticate_HTTPError(t *testing.T) { |
| 266 | // Create a test server that returns an error |
nothing calls this directly
no test coverage detected