(t *testing.T)
| 98 | } |
| 99 | |
| 100 | func TestUserAPIKeys(t *testing.T) { // nolint:gocyclo |
| 101 | p := &storetest.Population{} |
| 102 | |
| 103 | admin := p.NewUser() |
| 104 | admin.Admin = true |
| 105 | adminKey, _ := p.NewAPIKey(admin.GetEntityIdentifiers(), ttnpb.Right_RIGHT_ALL) |
| 106 | adminCreds := rpcCreds(adminKey) |
| 107 | |
| 108 | usr1 := p.NewUser() |
| 109 | usr1Key, _ := p.NewAPIKey(usr1.GetEntityIdentifiers(), |
| 110 | ttnpb.Right_RIGHT_USER_INFO, |
| 111 | ttnpb.Right_RIGHT_USER_APPLICATIONS_LIST, |
| 112 | ) |
| 113 | limitedKey, _ := p.NewAPIKey(usr1.GetEntityIdentifiers(), |
| 114 | ttnpb.Right_RIGHT_USER_INFO, |
| 115 | ttnpb.Right_RIGHT_USER_SETTINGS_BASIC, |
| 116 | ttnpb.Right_RIGHT_USER_SETTINGS_API_KEYS, |
| 117 | ) |
| 118 | limitedCreds := rpcCreds(limitedKey) |
| 119 | |
| 120 | t.Parallel() |
| 121 | a, ctx := test.New(t) |
| 122 | |
| 123 | testWithIdentityServer(t, func(is *IdentityServer, cc *grpc.ClientConn) { |
| 124 | is.config.AdminRights.All = true |
| 125 | |
| 126 | reg := ttnpb.NewUserAccessClient(cc) |
| 127 | |
| 128 | // GetAPIKey that doesn't exist. |
| 129 | got, err := reg.GetAPIKey(ctx, &ttnpb.GetUserAPIKeyRequest{ |
| 130 | UserIds: usr1.GetIds(), |
| 131 | KeyId: "does-not-exist", |
| 132 | }, limitedCreds) |
| 133 | if a.So(err, should.NotBeNil) { |
| 134 | a.So(errors.IsNotFound(err), should.BeTrue) |
| 135 | } |
| 136 | a.So(got, should.BeNil) |
| 137 | |
| 138 | // UpdateAPIKey that doesn't exist. |
| 139 | updated, err := reg.UpdateAPIKey(ctx, &ttnpb.UpdateUserAPIKeyRequest{ |
| 140 | UserIds: usr1.GetIds(), |
| 141 | ApiKey: &ttnpb.APIKey{ |
| 142 | Id: "does-not-exist", |
| 143 | }, |
| 144 | FieldMask: ttnpb.FieldMask("name"), |
| 145 | }, limitedCreds) |
| 146 | if a.So(err, should.NotBeNil) { |
| 147 | a.So(errors.IsNotFound(err), should.BeTrue) |
| 148 | } |
| 149 | a.So(updated, should.BeNil) |
| 150 | |
| 151 | // CreateAPIKey with rights that caller doesn't have. |
| 152 | apiKey, err := reg.CreateAPIKey(ctx, &ttnpb.CreateUserAPIKeyRequest{ |
| 153 | UserIds: usr1.GetIds(), |
| 154 | Name: "api-key-name", |
| 155 | Rights: []ttnpb.Right{ttnpb.Right_RIGHT_USER_ALL}, |
| 156 | }, limitedCreds) |
| 157 | if a.So(err, should.NotBeNil) { |
nothing calls this directly
no test coverage detected