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

Function TestAuthorization

coderd/aibridgedserver/aibridgedserver_test.go:53–220  ·  view source on GitHub ↗

TestAuthorization validates the authorization logic. No other tests are explicitly defined in this package because aibridgedserver is tested via integration tests in the aibridged package (see aibridged/aibridged_integration_test.go).

(t *testing.T)

Source from the content-addressed store, hash-verified

51// No other tests are explicitly defined in this package because aibridgedserver is
52// tested via integration tests in the aibridged package (see aibridged/aibridged_integration_test.go).
53func TestAuthorization(t *testing.T) {
54 t.Parallel()
55
56 cases := []struct {
57 name string
58 // Key will be set to the same key passed to mocksFn if unset.
59 key string
60 // mocksFn is called with a valid API key and user. If the test needs
61 // invalid values, it should just mutate them directly.
62 mocksFn func(db *dbmock.MockStore, apiKey database.APIKey, user database.User)
63 expectedErr error
64 }{
65 {
66 name: "invalid key format",
67 key: "foo",
68 expectedErr: aibridgedserver.ErrInvalidKey,
69 },
70 {
71 name: "unknown key",
72 expectedErr: aibridgedserver.ErrUnknownKey,
73 mocksFn: func(db *dbmock.MockStore, apiKey database.APIKey, user database.User) {
74 db.EXPECT().GetAPIKeyByID(gomock.Any(), apiKey.ID).Times(1).Return(database.APIKey{}, sql.ErrNoRows)
75 },
76 },
77 {
78 name: "expired",
79 expectedErr: aibridgedserver.ErrExpired,
80 mocksFn: func(db *dbmock.MockStore, apiKey database.APIKey, user database.User) {
81 apiKey.ExpiresAt = dbtime.Now().Add(-time.Hour)
82 db.EXPECT().GetAPIKeyByID(gomock.Any(), apiKey.ID).Times(1).Return(apiKey, nil)
83 },
84 },
85 {
86 name: "invalid key secret",
87 expectedErr: aibridgedserver.ErrInvalidKey,
88 mocksFn: func(db *dbmock.MockStore, apiKey database.APIKey, user database.User) {
89 apiKey.HashedSecret = []byte("differentsecret")
90 db.EXPECT().GetAPIKeyByID(gomock.Any(), apiKey.ID).Times(1).Return(apiKey, nil)
91 },
92 },
93 {
94 name: "unknown user",
95 expectedErr: aibridgedserver.ErrUnknownUser,
96 mocksFn: func(db *dbmock.MockStore, apiKey database.APIKey, user database.User) {
97 db.EXPECT().GetAPIKeyByID(gomock.Any(), apiKey.ID).Times(1).Return(apiKey, nil)
98 db.EXPECT().GetUserByID(gomock.Any(), user.ID).Times(1).Return(database.User{}, sql.ErrNoRows)
99 },
100 },
101 {
102 name: "deleted user",
103 expectedErr: aibridgedserver.ErrDeletedUser,
104 mocksFn: func(db *dbmock.MockStore, apiKey database.APIKey, user database.User) {
105 user.Deleted = true
106 db.EXPECT().GetAPIKeyByID(gomock.Any(), apiKey.ID).Times(1).Return(apiKey, nil)
107 db.EXPECT().GetUserByID(gomock.Any(), user.ID).Times(1).Return(user, nil)
108 },
109 },
110 {

Callers

nothing calls this directly

Calls 15

EXPECTMethod · 0.95
IsAuthorizedMethod · 0.95
NowFunction · 0.92
NewMockStoreFunction · 0.92
LoggerFunction · 0.92
StringFunction · 0.92
GenerateSecretFunction · 0.92
NewServerFunction · 0.92
GetAPIKeyByIDMethod · 0.65
AddMethod · 0.65
GetUserByIDMethod · 0.65
RunMethod · 0.65

Tested by

no test coverage detected