|
1 | 1 | using System.Security.Claims; |
2 | 2 | using Bit.Api.AdminConsole.Controllers; |
| 3 | +using Bit.Api.AdminConsole.Models.Request.Organizations; |
3 | 4 | using Bit.Api.Auth.Models.Request.Accounts; |
4 | 5 | using Bit.Api.Models.Request.Organizations; |
5 | 6 | using Bit.Core; |
|
8 | 9 | using Bit.Core.AdminConsole.Enums.Provider; |
9 | 10 | using Bit.Core.AdminConsole.Models.Business; |
10 | 11 | using Bit.Core.AdminConsole.Models.Data.Organizations.Policies; |
| 12 | +using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationApiKeys.Interfaces; |
11 | 13 | using Bit.Core.AdminConsole.OrganizationFeatures.Organizations.Interfaces; |
12 | 14 | using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.Interfaces; |
13 | 15 | using Bit.Core.AdminConsole.OrganizationFeatures.Policies; |
@@ -257,4 +259,113 @@ await sutProvider.GetDependency<IOrganizationService>() |
257 | 259 | s.LimitItemDeletion == model.LimitItemDeletion && |
258 | 260 | s.AllowAdminAccessToAllCollectionItems == model.AllowAdminAccessToAllCollectionItems)); |
259 | 261 | } |
| 262 | + |
| 263 | + [Theory, BitAutoData] |
| 264 | + public async Task ApiKey_ScimType_InvalidSecret_ThrowsBadRequest( |
| 265 | + SutProvider<OrganizationsController> sutProvider, |
| 266 | + Organization organization, |
| 267 | + OrganizationApiKey organizationApiKey, |
| 268 | + User user) |
| 269 | + { |
| 270 | + organization.PlanType = PlanType.EnterpriseAnnually; |
| 271 | + var model = new OrganizationApiKeyRequestModel |
| 272 | + { |
| 273 | + Type = OrganizationApiKeyType.Scim, |
| 274 | + MasterPasswordHash = "invalid-hash" |
| 275 | + }; |
| 276 | + |
| 277 | + sutProvider.GetDependency<ICurrentContext>().ManageScim(organization.Id).Returns(true); |
| 278 | + sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization); |
| 279 | + sutProvider.GetDependency<IGetOrganizationApiKeyQuery>() |
| 280 | + .GetOrganizationApiKeyAsync(organization.Id, OrganizationApiKeyType.Scim) |
| 281 | + .Returns(organizationApiKey); |
| 282 | + |
| 283 | + var userService = sutProvider.GetDependency<IUserService>(); |
| 284 | + userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user); |
| 285 | + userService.VerifySecretAsync(user, model.Secret).Returns(false); |
| 286 | + |
| 287 | + await Assert.ThrowsAsync<BadRequestException>( |
| 288 | + () => sutProvider.Sut.ApiKey(organization.Id.ToString(), model)); |
| 289 | + } |
| 290 | + |
| 291 | + [Theory, BitAutoData] |
| 292 | + public async Task ApiKey_ScimType_ValidSecret_ReturnsApiKey( |
| 293 | + SutProvider<OrganizationsController> sutProvider, |
| 294 | + Organization organization, |
| 295 | + OrganizationApiKey organizationApiKey, |
| 296 | + User user) |
| 297 | + { |
| 298 | + organization.PlanType = PlanType.EnterpriseAnnually; |
| 299 | + var model = new OrganizationApiKeyRequestModel |
| 300 | + { |
| 301 | + Type = OrganizationApiKeyType.Scim, |
| 302 | + MasterPasswordHash = "valid-hash" |
| 303 | + }; |
| 304 | + |
| 305 | + sutProvider.GetDependency<ICurrentContext>().ManageScim(organization.Id).Returns(true); |
| 306 | + sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization); |
| 307 | + sutProvider.GetDependency<IGetOrganizationApiKeyQuery>() |
| 308 | + .GetOrganizationApiKeyAsync(organization.Id, OrganizationApiKeyType.Scim) |
| 309 | + .Returns(organizationApiKey); |
| 310 | + var userService = sutProvider.GetDependency<IUserService>(); |
| 311 | + userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user); |
| 312 | + userService.VerifySecretAsync(user, model.Secret).Returns(true); |
| 313 | + |
| 314 | + var result = await sutProvider.Sut.ApiKey(organization.Id.ToString(), model); |
| 315 | + |
| 316 | + Assert.Equal(organizationApiKey.ApiKey, result.ApiKey); |
| 317 | + } |
| 318 | + |
| 319 | + [Theory, BitAutoData] |
| 320 | + public async Task RotateApiKey_ScimType_InvalidSecret_ThrowsBadRequest( |
| 321 | + SutProvider<OrganizationsController> sutProvider, |
| 322 | + Organization organization, |
| 323 | + OrganizationApiKey organizationApiKey, |
| 324 | + User user) |
| 325 | + { |
| 326 | + var model = new OrganizationApiKeyRequestModel |
| 327 | + { |
| 328 | + Type = OrganizationApiKeyType.Scim, |
| 329 | + MasterPasswordHash = "invalid-hash" |
| 330 | + }; |
| 331 | + |
| 332 | + sutProvider.GetDependency<ICurrentContext>().ManageScim(organization.Id).Returns(true); |
| 333 | + sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization); |
| 334 | + sutProvider.GetDependency<IGetOrganizationApiKeyQuery>() |
| 335 | + .GetOrganizationApiKeyAsync(organization.Id, OrganizationApiKeyType.Scim) |
| 336 | + .Returns(organizationApiKey); |
| 337 | + var userService = sutProvider.GetDependency<IUserService>(); |
| 338 | + userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user); |
| 339 | + userService.VerifySecretAsync(user, model.Secret).Returns(false); |
| 340 | + |
| 341 | + await Assert.ThrowsAsync<BadRequestException>( |
| 342 | + () => sutProvider.Sut.RotateApiKey(organization.Id.ToString(), model)); |
| 343 | + } |
| 344 | + |
| 345 | + [Theory, BitAutoData] |
| 346 | + public async Task RotateApiKey_ScimType_ValidSecret_ReturnsApiKey( |
| 347 | + SutProvider<OrganizationsController> sutProvider, |
| 348 | + Organization organization, |
| 349 | + OrganizationApiKey organizationApiKey, |
| 350 | + User user) |
| 351 | + { |
| 352 | + var model = new OrganizationApiKeyRequestModel |
| 353 | + { |
| 354 | + Type = OrganizationApiKeyType.Scim, |
| 355 | + MasterPasswordHash = "valid-hash" |
| 356 | + }; |
| 357 | + |
| 358 | + sutProvider.GetDependency<ICurrentContext>().ManageScim(organization.Id).Returns(true); |
| 359 | + sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization); |
| 360 | + sutProvider.GetDependency<IGetOrganizationApiKeyQuery>() |
| 361 | + .GetOrganizationApiKeyAsync(organization.Id, OrganizationApiKeyType.Scim) |
| 362 | + .Returns(organizationApiKey); |
| 363 | + var userService = sutProvider.GetDependency<IUserService>(); |
| 364 | + userService.GetUserByPrincipalAsync(Arg.Any<ClaimsPrincipal>()).Returns(user); |
| 365 | + userService.VerifySecretAsync(user, model.Secret).Returns(true); |
| 366 | + |
| 367 | + var result = await sutProvider.Sut.RotateApiKey(organization.Id.ToString(), model); |
| 368 | + |
| 369 | + Assert.Equal(organizationApiKey.ApiKey, result.ApiKey); |
| 370 | + } |
260 | 371 | } |
0 commit comments