(t *testing.T)
| 2390 | } |
| 2391 | |
| 2392 | func TestInsertUserServiceAccountConstraints(t *testing.T) { |
| 2393 | t.Parallel() |
| 2394 | |
| 2395 | db, _ := dbtestutil.NewDB(t) |
| 2396 | |
| 2397 | // Happy path: should succeed. |
| 2398 | t.Run("ServiceAccountWithEmptyEmailAndLoginNone", func(t *testing.T) { |
| 2399 | t.Parallel() |
| 2400 | |
| 2401 | ctx := testutil.Context(t, testutil.WaitLong) |
| 2402 | user, err := db.InsertUser(ctx, database.InsertUserParams{ |
| 2403 | Email: "", |
| 2404 | LoginType: database.LoginTypeNone, |
| 2405 | ID: uuid.New(), |
| 2406 | Username: "sa-ok", |
| 2407 | RBACRoles: []string{}, |
| 2408 | IsServiceAccount: true, |
| 2409 | }) |
| 2410 | require.NoError(t, err) |
| 2411 | require.True(t, user.IsServiceAccount) |
| 2412 | require.Empty(t, user.Email) |
| 2413 | }) |
| 2414 | |
| 2415 | // Service account with a non-empty email should be rejected |
| 2416 | // by the users_email_not_empty constraint. |
| 2417 | t.Run("ServiceAccountWithNonEmptyEmail", func(t *testing.T) { |
| 2418 | t.Parallel() |
| 2419 | |
| 2420 | ctx := testutil.Context(t, testutil.WaitLong) |
| 2421 | _, err := db.InsertUser(ctx, database.InsertUserParams{ |
| 2422 | Email: "sa@coder.com", |
| 2423 | LoginType: database.LoginTypeNone, |
| 2424 | ID: uuid.New(), |
| 2425 | Username: "sa-with-email", |
| 2426 | RBACRoles: []string{}, |
| 2427 | IsServiceAccount: true, |
| 2428 | }) |
| 2429 | require.Error(t, err) |
| 2430 | require.True(t, database.IsCheckViolation(err, database.CheckUsersEmailNotEmpty)) |
| 2431 | }) |
| 2432 | |
| 2433 | // A non-service-account with empty email should be rejected |
| 2434 | // by the users_email_not_empty constraint. |
| 2435 | t.Run("RegularUserWithEmptyEmail", func(t *testing.T) { |
| 2436 | t.Parallel() |
| 2437 | |
| 2438 | ctx := testutil.Context(t, testutil.WaitLong) |
| 2439 | _, err := db.InsertUser(ctx, database.InsertUserParams{ |
| 2440 | Email: "", |
| 2441 | LoginType: database.LoginTypePassword, |
| 2442 | ID: uuid.New(), |
| 2443 | Username: "regular-no-email", |
| 2444 | RBACRoles: []string{}, |
| 2445 | IsServiceAccount: false, |
| 2446 | }) |
| 2447 | require.Error(t, err) |
| 2448 | require.True(t, database.IsCheckViolation(err, database.CheckUsersEmailNotEmpty)) |
| 2449 | }) |
nothing calls this directly
no test coverage detected