MCPcopy Index your code
hub / github.com/coder/coder / TestInsertUserServiceAccountConstraints

Function TestInsertUserServiceAccountConstraints

coderd/database/querier_test.go:2392–2468  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2390}
2391
2392func 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 })

Callers

nothing calls this directly

Calls 8

NewDBFunction · 0.92
ContextFunction · 0.92
IsCheckViolationFunction · 0.92
RunMethod · 0.65
InsertUserMethod · 0.65
NewMethod · 0.65
EmptyMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected