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)
| 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). |
| 53 | func 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 | { |
nothing calls this directly
no test coverage detected