TestInTX is not perfect, just checks that it properly checks auth.
(t *testing.T)
| 86 | |
| 87 | // TestInTX is not perfect, just checks that it properly checks auth. |
| 88 | func TestInTX(t *testing.T) { |
| 89 | t.Parallel() |
| 90 | |
| 91 | var ( |
| 92 | ctrl = gomock.NewController(t) |
| 93 | db = dbmock.NewMockStore(ctrl) |
| 94 | mTx = dbmock.NewMockStore(ctrl) // to record the 'in tx' calls |
| 95 | faker = gofakeit.New(0) |
| 96 | w = testutil.Fake(t, faker, database.Workspace{}) |
| 97 | actor = rbac.Subject{ |
| 98 | ID: uuid.NewString(), |
| 99 | Roles: rbac.RoleIdentifiers{rbac.RoleOwner()}, |
| 100 | Groups: []string{}, |
| 101 | Scope: rbac.ScopeAll, |
| 102 | } |
| 103 | ctx = dbauthz.As(context.Background(), actor) |
| 104 | ) |
| 105 | |
| 106 | db.EXPECT().Wrappers().Times(1).Return([]string{}) // called by dbauthz.New |
| 107 | q := dbauthz.New(db, &coderdtest.RecordingAuthorizer{ |
| 108 | Wrapped: (&coderdtest.FakeAuthorizer{}).AlwaysReturn(xerrors.New("custom error")), |
| 109 | }, slog.Make(), coderdtest.AccessControlStorePointer()) |
| 110 | |
| 111 | db.EXPECT().InTx(gomock.Any(), gomock.Any()).Times(1).DoAndReturn( |
| 112 | func(f func(database.Store) error, _ *database.TxOptions) error { |
| 113 | return f(mTx) |
| 114 | }, |
| 115 | ) |
| 116 | mTx.EXPECT().Wrappers().Times(1).Return([]string{}) |
| 117 | mTx.EXPECT().GetWorkspaceByID(gomock.Any(), gomock.Any()).Times(1).Return(w, nil) |
| 118 | err := q.InTx(func(tx database.Store) error { |
| 119 | // The inner tx should use the parent's authz |
| 120 | _, err := tx.GetWorkspaceByID(ctx, w.ID) |
| 121 | return err |
| 122 | }, nil) |
| 123 | require.ErrorContains(t, err, "custom error", "must be our custom error") |
| 124 | require.ErrorAs(t, err, &dbauthz.NotAuthorizedError{}, "must be an authorized error") |
| 125 | require.True(t, dbauthz.IsNotAuthorizedError(err), "must be an authorized error") |
| 126 | } |
| 127 | |
| 128 | // TestNew should not double wrap a querier. |
| 129 | func TestNew(t *testing.T) { |
nothing calls this directly
no test coverage detected