|
4 | 4 | "database/sql" |
5 | 5 | "encoding/json" |
6 | 6 | "testing" |
| 7 | + "time" |
7 | 8 |
|
8 | 9 | "github.com/google/uuid" |
9 | 10 | "github.com/jmoiron/sqlx" |
@@ -208,3 +209,211 @@ func TestGetProfile_NullValues(t *testing.T) { |
208 | 209 | assert.Equal(t, "null-provider", profile.Name) |
209 | 210 | } |
210 | 211 | } |
| 212 | + |
| 213 | +func TestGetAllProfiles_Success(t *testing.T) { |
| 214 | + db, mock, err := sqlmock.New() |
| 215 | + assert.NoError(t, err) |
| 216 | + defer db.Close() |
| 217 | + |
| 218 | + sqlxDB := sqlx.NewDb(db, "sqlmock") |
| 219 | + store := NewStore(sqlxDB) |
| 220 | + |
| 221 | + id1 := uuid.New() |
| 222 | + id2 := uuid.New() |
| 223 | + now := time.Now() |
| 224 | + msg := "timeout reaching token_endpoint" |
| 225 | + |
| 226 | + // Must match the exact 19-column order in GetAllProfiles SELECT: |
| 227 | + // id, name, client_id, client_secret, auth_url, token_url, issuer, |
| 228 | + // enable_discovery, scopes, auth_type, auth_header, |
| 229 | + // api_base_url, user_info_endpoint, params, description, category, |
| 230 | + // last_health_check_at, health_status, health_message |
| 231 | + rows := sqlmock.NewRows([]string{ |
| 232 | + "id", "name", "client_id", "client_secret", "auth_url", "token_url", "issuer", |
| 233 | + "enable_discovery", "scopes", "auth_type", "auth_header", |
| 234 | + "api_base_url", "user_info_endpoint", "params", "description", "category", |
| 235 | + "last_health_check_at", "health_status", "health_message", |
| 236 | + }).AddRow( |
| 237 | + id1.String(), "google", ptr("cid"), ptr("csec"), ptr("https://auth"), ptr("https://token"), nil, |
| 238 | + true, []byte("{email,profile}"), "oauth2", "", |
| 239 | + "https://api.google.com", "/userinfo", nil, "Google OAuth", "Identity", |
| 240 | + now, "healthy", nil, |
| 241 | + ).AddRow( |
| 242 | + id2.String(), "stripe", nil, nil, nil, nil, nil, |
| 243 | + false, []byte("{}"), "api_key", "Authorization", |
| 244 | + "https://api.stripe.com", "/v1/account", nil, "Stripe API", "Payments", |
| 245 | + now, "unhealthy", &msg, |
| 246 | + ) |
| 247 | + |
| 248 | + mock.ExpectQuery(`SELECT .* FROM provider_profiles`).WillReturnRows(rows) |
| 249 | + |
| 250 | + profiles, err := store.GetAllProfiles() |
| 251 | + assert.NoError(t, err) |
| 252 | + assert.Len(t, profiles, 2) |
| 253 | + |
| 254 | + // Verify first profile health fields |
| 255 | + assert.Equal(t, id1, profiles[0].ID) |
| 256 | + assert.Equal(t, "google", profiles[0].Name) |
| 257 | + assert.Equal(t, "healthy", profiles[0].HealthStatus) |
| 258 | + assert.NotNil(t, profiles[0].LastHealthCheckAt) |
| 259 | + assert.Nil(t, profiles[0].HealthMessage) |
| 260 | + |
| 261 | + // Verify second profile health fields |
| 262 | + assert.Equal(t, id2, profiles[1].ID) |
| 263 | + assert.Equal(t, "stripe", profiles[1].Name) |
| 264 | + assert.Equal(t, "unhealthy", profiles[1].HealthStatus) |
| 265 | + assert.NotNil(t, profiles[1].HealthMessage) |
| 266 | + assert.Equal(t, "timeout reaching token_endpoint", *profiles[1].HealthMessage) |
| 267 | + |
| 268 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 269 | +} |
| 270 | + |
| 271 | +func TestGetAllProfiles_Empty(t *testing.T) { |
| 272 | + db, mock, err := sqlmock.New() |
| 273 | + assert.NoError(t, err) |
| 274 | + defer db.Close() |
| 275 | + |
| 276 | + sqlxDB := sqlx.NewDb(db, "sqlmock") |
| 277 | + store := NewStore(sqlxDB) |
| 278 | + |
| 279 | + rows := sqlmock.NewRows([]string{ |
| 280 | + "id", "name", "client_id", "client_secret", "auth_url", "token_url", "issuer", |
| 281 | + "enable_discovery", "scopes", "auth_type", "auth_header", |
| 282 | + "api_base_url", "user_info_endpoint", "params", "description", "category", |
| 283 | + "last_health_check_at", "health_status", "health_message", |
| 284 | + }) |
| 285 | + |
| 286 | + mock.ExpectQuery(`SELECT .* FROM provider_profiles`).WillReturnRows(rows) |
| 287 | + |
| 288 | + profiles, err := store.GetAllProfiles() |
| 289 | + assert.NoError(t, err) |
| 290 | + assert.Nil(t, profiles) // append to nil slice returns nil |
| 291 | + |
| 292 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 293 | +} |
| 294 | + |
| 295 | +func TestGetAllProfiles_QueryError(t *testing.T) { |
| 296 | + db, mock, err := sqlmock.New() |
| 297 | + assert.NoError(t, err) |
| 298 | + defer db.Close() |
| 299 | + |
| 300 | + sqlxDB := sqlx.NewDb(db, "sqlmock") |
| 301 | + store := NewStore(sqlxDB) |
| 302 | + |
| 303 | + mock.ExpectQuery(`SELECT .* FROM provider_profiles`). |
| 304 | + WillReturnError(sql.ErrConnDone) |
| 305 | + |
| 306 | + profiles, err := store.GetAllProfiles() |
| 307 | + assert.Error(t, err) |
| 308 | + assert.Nil(t, profiles) |
| 309 | + assert.Contains(t, err.Error(), "failed to query all profiles") |
| 310 | + |
| 311 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 312 | +} |
| 313 | + |
| 314 | +func TestUpdateHealthStatus_Success(t *testing.T) { |
| 315 | + db, mock, err := sqlmock.New() |
| 316 | + assert.NoError(t, err) |
| 317 | + defer db.Close() |
| 318 | + |
| 319 | + sqlxDB := sqlx.NewDb(db, "sqlmock") |
| 320 | + store := NewStore(sqlxDB) |
| 321 | + |
| 322 | + providerID := uuid.New() |
| 323 | + msg := "token_url 503" |
| 324 | + |
| 325 | + // Verify the UPDATE is called with (status, message, id) in correct order |
| 326 | + mock.ExpectExec(`UPDATE provider_profiles SET health_status = \$1, health_message = \$2, last_health_check_at = NOW\(\) WHERE id = \$3`). |
| 327 | + WithArgs("unhealthy", &msg, providerID). |
| 328 | + WillReturnResult(sqlmock.NewResult(0, 1)) |
| 329 | + |
| 330 | + err = store.UpdateHealthStatus(providerID, "unhealthy", &msg) |
| 331 | + assert.NoError(t, err) |
| 332 | + |
| 333 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 334 | +} |
| 335 | + |
| 336 | +func TestUpdateHealthStatus_NilMessage(t *testing.T) { |
| 337 | + db, mock, err := sqlmock.New() |
| 338 | + assert.NoError(t, err) |
| 339 | + defer db.Close() |
| 340 | + |
| 341 | + sqlxDB := sqlx.NewDb(db, "sqlmock") |
| 342 | + store := NewStore(sqlxDB) |
| 343 | + |
| 344 | + providerID := uuid.New() |
| 345 | + |
| 346 | + mock.ExpectExec(`UPDATE provider_profiles SET health_status = \$1, health_message = \$2, last_health_check_at = NOW\(\) WHERE id = \$3`). |
| 347 | + WithArgs("healthy", nil, providerID). |
| 348 | + WillReturnResult(sqlmock.NewResult(0, 1)) |
| 349 | + |
| 350 | + err = store.UpdateHealthStatus(providerID, "healthy", nil) |
| 351 | + assert.NoError(t, err) |
| 352 | + |
| 353 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 354 | +} |
| 355 | + |
| 356 | +func TestUpdateHealthStatus_DBError(t *testing.T) { |
| 357 | + db, mock, err := sqlmock.New() |
| 358 | + assert.NoError(t, err) |
| 359 | + defer db.Close() |
| 360 | + |
| 361 | + sqlxDB := sqlx.NewDb(db, "sqlmock") |
| 362 | + store := NewStore(sqlxDB) |
| 363 | + |
| 364 | + providerID := uuid.New() |
| 365 | + |
| 366 | + mock.ExpectExec(`UPDATE provider_profiles SET health_status`). |
| 367 | + WithArgs("unhealthy", nil, providerID). |
| 368 | + WillReturnError(sql.ErrConnDone) |
| 369 | + |
| 370 | + err = store.UpdateHealthStatus(providerID, "unhealthy", nil) |
| 371 | + assert.Error(t, err) |
| 372 | + assert.Contains(t, err.Error(), "failed to update provider health status") |
| 373 | + |
| 374 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 375 | +} |
| 376 | + |
| 377 | +func TestGetHealthStatus_Success(t *testing.T) { |
| 378 | + db, mock, err := sqlmock.New() |
| 379 | + assert.NoError(t, err) |
| 380 | + defer db.Close() |
| 381 | + |
| 382 | + sqlxDB := sqlx.NewDb(db, "sqlmock") |
| 383 | + store := NewStore(sqlxDB) |
| 384 | + |
| 385 | + providerID := uuid.New() |
| 386 | + rows := sqlmock.NewRows([]string{"health_status"}).AddRow("unhealthy") |
| 387 | + |
| 388 | + mock.ExpectQuery(`SELECT COALESCE\(health_status, 'unknown'\) FROM provider_profiles WHERE id = \$1`). |
| 389 | + WithArgs(providerID). |
| 390 | + WillReturnRows(rows) |
| 391 | + |
| 392 | + status, err := store.GetHealthStatus(providerID) |
| 393 | + assert.NoError(t, err) |
| 394 | + assert.Equal(t, "unhealthy", status) |
| 395 | + |
| 396 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 397 | +} |
| 398 | + |
| 399 | +func TestGetHealthStatus_NotFound(t *testing.T) { |
| 400 | + db, mock, err := sqlmock.New() |
| 401 | + assert.NoError(t, err) |
| 402 | + defer db.Close() |
| 403 | + |
| 404 | + sqlxDB := sqlx.NewDb(db, "sqlmock") |
| 405 | + store := NewStore(sqlxDB) |
| 406 | + |
| 407 | + providerID := uuid.New() |
| 408 | + |
| 409 | + mock.ExpectQuery(`SELECT COALESCE\(health_status, 'unknown'\) FROM provider_profiles WHERE id = \$1`). |
| 410 | + WithArgs(providerID). |
| 411 | + WillReturnError(sql.ErrNoRows) |
| 412 | + |
| 413 | + status, err := store.GetHealthStatus(providerID) |
| 414 | + assert.Error(t, err) |
| 415 | + assert.Equal(t, "", status) |
| 416 | + assert.Contains(t, err.Error(), "failed to get provider health status") |
| 417 | + |
| 418 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 419 | +} |
0 commit comments