From 12b03482aedc852c249258436376b32de353b5b1 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 29 Sep 2023 15:40:07 +0000 Subject: [PATCH 1/8] Bumped version to 2023.9.1 (#3309) Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com> --- Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 952cacd42b..6820a03e37 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,7 +2,7 @@ net6.0 - 2023.9.0 + 2023.9.1 Bit.$(MSBuildProjectName) true enable From 46117b194e4607e220c30af35df8dd2fc247a749 Mon Sep 17 00:00:00 2001 From: Alex Morask <144709477+amorask-bitwarden@users.noreply.github.com> Date: Fri, 29 Sep 2023 12:29:43 -0400 Subject: [PATCH 2/8] Return discount info in Subscription responses [AC-1657] (#3278) * Return whether customer has Stripe discount applied from /api/accounts/subscription * Return whether customer has Stripe discount applied from /api/organizations/{id}/subscription --- .../Organizations/OrganizationResponseModel.cs | 2 ++ .../Models/Response/SubscriptionResponseModel.cs | 14 ++++++++++++++ src/Core/Models/Business/SubscriptionInfo.cs | 15 +++++++++++++++ .../Implementations/StripePaymentService.cs | 13 +++++++++++-- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Api/Models/Response/Organizations/OrganizationResponseModel.cs b/src/Api/Models/Response/Organizations/OrganizationResponseModel.cs index 11f5a795a4..f04c28c5e3 100644 --- a/src/Api/Models/Response/Organizations/OrganizationResponseModel.cs +++ b/src/Api/Models/Response/Organizations/OrganizationResponseModel.cs @@ -116,6 +116,7 @@ public class OrganizationSubscriptionResponseModel : OrganizationResponseModel { Subscription = subscription.Subscription != null ? new BillingSubscription(subscription.Subscription) : null; UpcomingInvoice = subscription.UpcomingInvoice != null ? new BillingSubscriptionUpcomingInvoice(subscription.UpcomingInvoice) : null; + Discount = subscription.Discount != null ? new BillingCustomerDiscount(subscription.Discount) : null; Expiration = DateTime.UtcNow.AddYears(1); // Not used, so just give it a value. if (hideSensitiveData) @@ -146,6 +147,7 @@ public class OrganizationSubscriptionResponseModel : OrganizationResponseModel public string StorageName { get; set; } public double? StorageGb { get; set; } + public BillingCustomerDiscount Discount { get; set; } public BillingSubscription Subscription { get; set; } public BillingSubscriptionUpcomingInvoice UpcomingInvoice { get; set; } diff --git a/src/Api/Models/Response/SubscriptionResponseModel.cs b/src/Api/Models/Response/SubscriptionResponseModel.cs index 4c0ee9338b..042da2e9e6 100644 --- a/src/Api/Models/Response/SubscriptionResponseModel.cs +++ b/src/Api/Models/Response/SubscriptionResponseModel.cs @@ -14,6 +14,7 @@ public class SubscriptionResponseModel : ResponseModel Subscription = subscription.Subscription != null ? new BillingSubscription(subscription.Subscription) : null; UpcomingInvoice = subscription.UpcomingInvoice != null ? new BillingSubscriptionUpcomingInvoice(subscription.UpcomingInvoice) : null; + Discount = subscription.Discount != null ? new BillingCustomerDiscount(subscription.Discount) : null; StorageName = user.Storage.HasValue ? CoreHelpers.ReadableBytesSize(user.Storage.Value) : null; StorageGb = user.Storage.HasValue ? Math.Round(user.Storage.Value / 1073741824D, 2) : 0; // 1 GB MaxStorageGb = user.MaxStorageGb; @@ -41,11 +42,24 @@ public class SubscriptionResponseModel : ResponseModel public short? MaxStorageGb { get; set; } public BillingSubscriptionUpcomingInvoice UpcomingInvoice { get; set; } public BillingSubscription Subscription { get; set; } + public BillingCustomerDiscount Discount { get; set; } public UserLicense License { get; set; } public DateTime? Expiration { get; set; } public bool UsingInAppPurchase { get; set; } } +public class BillingCustomerDiscount +{ + public BillingCustomerDiscount(SubscriptionInfo.BillingCustomerDiscount discount) + { + Id = discount.Id; + Active = discount.Active; + } + + public string Id { get; set; } + public bool Active { get; set; } +} + public class BillingSubscription { public BillingSubscription(SubscriptionInfo.BillingSubscription sub) diff --git a/src/Core/Models/Business/SubscriptionInfo.cs b/src/Core/Models/Business/SubscriptionInfo.cs index c72e291de0..87fe3157c2 100644 --- a/src/Core/Models/Business/SubscriptionInfo.cs +++ b/src/Core/Models/Business/SubscriptionInfo.cs @@ -5,10 +5,25 @@ namespace Bit.Core.Models.Business; public class SubscriptionInfo { + public BillingCustomerDiscount Discount { get; set; } public BillingSubscription Subscription { get; set; } public BillingUpcomingInvoice UpcomingInvoice { get; set; } public bool UsingInAppPurchase { get; set; } + public class BillingCustomerDiscount + { + public BillingCustomerDiscount() { } + + public BillingCustomerDiscount(Discount discount) + { + Id = discount.Id; + Active = discount.Start != null && discount.End == null; + } + + public string Id { get; } + public bool Active { get; } + } + public class BillingSubscription { public BillingSubscription(Subscription sub) diff --git a/src/Core/Services/Implementations/StripePaymentService.cs b/src/Core/Services/Implementations/StripePaymentService.cs index 4d2eb4ef85..33610bee72 100644 --- a/src/Core/Services/Implementations/StripePaymentService.cs +++ b/src/Core/Services/Implementations/StripePaymentService.cs @@ -1557,10 +1557,19 @@ public class StripePaymentService : IPaymentService { var subscriptionInfo = new SubscriptionInfo(); - if (subscriber.IsUser() && !string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId)) + if (!string.IsNullOrWhiteSpace(subscriber.GatewayCustomerId)) { var customer = await _stripeAdapter.CustomerGetAsync(subscriber.GatewayCustomerId); - subscriptionInfo.UsingInAppPurchase = customer.Metadata.ContainsKey("appleReceipt"); + + if (customer.Discount != null) + { + subscriptionInfo.Discount = new SubscriptionInfo.BillingCustomerDiscount(customer.Discount); + } + + if (subscriber.IsUser()) + { + subscriptionInfo.UsingInAppPurchase = customer.Metadata.ContainsKey("appleReceipt"); + } } if (!string.IsNullOrWhiteSpace(subscriber.GatewaySubscriptionId)) From 9af9d121bf5b96e1f96f1d1395d98a8942859ceb Mon Sep 17 00:00:00 2001 From: Todd Martin <106564991+trmartin4@users.noreply.github.com> Date: Fri, 29 Sep 2023 15:44:54 -0400 Subject: [PATCH 3/8] Updated client minimum version to reflect previous hotfixes. (#3311) --- src/Core/Constants.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/Constants.cs b/src/Core/Constants.cs index 6de35a6657..8dea0561f9 100644 --- a/src/Core/Constants.cs +++ b/src/Core/Constants.cs @@ -20,7 +20,7 @@ public static class Constants /// public const int OrganizationSelfHostSubscriptionGracePeriodDays = 60; - public const string CipherKeyEncryptionMinimumVersion = "2023.9.1"; + public const string CipherKeyEncryptionMinimumVersion = "2023.9.2"; } public static class TokenPurposes From cc68d843012900b722263f5ce35d02861affe3c8 Mon Sep 17 00:00:00 2001 From: Conner Turnbull <133619638+cturnbull-bitwarden@users.noreply.github.com> Date: Wed, 4 Oct 2023 12:05:44 -0400 Subject: [PATCH 4/8] Added billing patterns to codeowners (#3322) --- .github/CODEOWNERS | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 1d66ccb4b6..e55c457b15 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -18,4 +18,14 @@ src/Identity @bitwarden/team-auth-dev bitwarden_license/src/Scim @bitwarden/team-admin-console-dev bitwarden_license/src/test/Scim.IntegrationTest @bitwarden/team-admin-console-dev bitwarden_license/src/test/Scim.ScimTest @bitwarden/team-admin-console-dev -**/AdminConsole @bitwarden/team-admin-console-dev \ No newline at end of file +**/AdminConsole @bitwarden/team-admin-console-dev + +# Billing Team +**/*billing* @bitwarden/team-billing-dev +**/*subscription* @bitwarden/team-billing-dev +**/*stripe* @bitwarden/team-billing-dev +**/*paypal* @bitwarden/team-billing-dev +**/*braintree* @bitwarden/team-billing-dev +**/*bitpay* @bitwarden/team-billing-dev +**/*freshdesk* @bitwarden/team-billing-dev +**/*freshsales* @bitwarden/team-billing-dev \ No newline at end of file From 8c3ca2f1fa0b5496c616bf91dd3ea5000131eef7 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 14:58:13 -0400 Subject: [PATCH 5/8] Update bitwarden/gh-actions digest to f112580 (#3314) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --- .github/workflows/build.yml | 6 +++--- .github/workflows/container-registry-purge.yml | 2 +- .github/workflows/release.yml | 10 +++++----- .github/workflows/version-bump.yml | 4 ++-- .github/workflows/workflow-linter.yml | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b2acef85f1..c394585aa9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -277,7 +277,7 @@ jobs: - name: Retrieve github PAT secrets id: retrieve-secret-pat - uses: bitwarden/gh-actions/get-keyvault-secrets@fdcf1fcec3b04762ce48216cbf3af32498bed74c + uses: bitwarden/gh-actions/get-keyvault-secrets@f1125802b1ccae8c601d7c4f61ce39ea254b10c8 with: keyvault: "bitwarden-ci" secrets: "github-pat-bitwarden-devops-bot-repo-scope" @@ -528,7 +528,7 @@ jobs: - name: Retrieve github PAT secrets id: retrieve-secret-pat - uses: bitwarden/gh-actions/get-keyvault-secrets@fdcf1fcec3b04762ce48216cbf3af32498bed74c + uses: bitwarden/gh-actions/get-keyvault-secrets@f1125802b1ccae8c601d7c4f61ce39ea254b10c8 with: keyvault: "bitwarden-ci" secrets: "github-pat-bitwarden-devops-bot-repo-scope" @@ -603,7 +603,7 @@ jobs: - name: Retrieve secrets id: retrieve-secrets - uses: bitwarden/gh-actions/get-keyvault-secrets@fdcf1fcec3b04762ce48216cbf3af32498bed74c + uses: bitwarden/gh-actions/get-keyvault-secrets@f1125802b1ccae8c601d7c4f61ce39ea254b10c8 if: failure() with: keyvault: "bitwarden-ci" diff --git a/.github/workflows/container-registry-purge.yml b/.github/workflows/container-registry-purge.yml index e87a2edd05..3fef44b35a 100644 --- a/.github/workflows/container-registry-purge.yml +++ b/.github/workflows/container-registry-purge.yml @@ -92,7 +92,7 @@ jobs: - name: Retrieve secrets id: retrieve-secrets - uses: bitwarden/gh-actions/get-keyvault-secrets@fdcf1fcec3b04762ce48216cbf3af32498bed74c + uses: bitwarden/gh-actions/get-keyvault-secrets@f1125802b1ccae8c601d7c4f61ce39ea254b10c8 if: failure() with: keyvault: "bitwarden-ci" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c6dcbd03e7..73c6a779f2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -41,7 +41,7 @@ jobs: - name: Check Release Version id: version - uses: bitwarden/gh-actions/release-version-check@fdcf1fcec3b04762ce48216cbf3af32498bed74c + uses: bitwarden/gh-actions/release-version-check@f1125802b1ccae8c601d7c4f61ce39ea254b10c8 with: release-type: ${{ github.event.inputs.release_type }} project-type: dotnet @@ -89,7 +89,7 @@ jobs: - name: Download latest Release ${{ matrix.name }} asset if: ${{ github.event.inputs.release_type != 'Dry Run' }} - uses: bitwarden/gh-actions/download-artifacts@fdcf1fcec3b04762ce48216cbf3af32498bed74c + uses: bitwarden/gh-actions/download-artifacts@f1125802b1ccae8c601d7c4f61ce39ea254b10c8 with: workflow: build.yml workflow_conclusion: success @@ -98,7 +98,7 @@ jobs: - name: Dry Run - Download latest Release ${{ matrix.name }} asset if: ${{ github.event.inputs.release_type == 'Dry Run' }} - uses: bitwarden/gh-actions/download-artifacts@fdcf1fcec3b04762ce48216cbf3af32498bed74c + uses: bitwarden/gh-actions/download-artifacts@f1125802b1ccae8c601d7c4f61ce39ea254b10c8 with: workflow: build.yml workflow_conclusion: success @@ -274,7 +274,7 @@ jobs: steps: - name: Download latest Release Docker Stubs if: ${{ github.event.inputs.release_type != 'Dry Run' }} - uses: bitwarden/gh-actions/download-artifacts@fdcf1fcec3b04762ce48216cbf3af32498bed74c + uses: bitwarden/gh-actions/download-artifacts@f1125802b1ccae8c601d7c4f61ce39ea254b10c8 with: workflow: build.yml workflow_conclusion: success @@ -287,7 +287,7 @@ jobs: - name: Dry Run - Download latest Release Docker Stubs if: ${{ github.event.inputs.release_type == 'Dry Run' }} - uses: bitwarden/gh-actions/download-artifacts@fdcf1fcec3b04762ce48216cbf3af32498bed74c + uses: bitwarden/gh-actions/download-artifacts@f1125802b1ccae8c601d7c4f61ce39ea254b10c8 with: workflow: build.yml workflow_conclusion: success diff --git a/.github/workflows/version-bump.yml b/.github/workflows/version-bump.yml index d82a562502..01ac04ed5d 100644 --- a/.github/workflows/version-bump.yml +++ b/.github/workflows/version-bump.yml @@ -23,7 +23,7 @@ jobs: - name: Retrieve secrets id: retrieve-secrets - uses: bitwarden/gh-actions/get-keyvault-secrets@fdcf1fcec3b04762ce48216cbf3af32498bed74c + uses: bitwarden/gh-actions/get-keyvault-secrets@f1125802b1ccae8c601d7c4f61ce39ea254b10c8 with: keyvault: "bitwarden-ci" secrets: "github-gpg-private-key, github-gpg-private-key-passphrase" @@ -40,7 +40,7 @@ jobs: run: git switch -c version_bump_${{ github.event.inputs.version_number }} - name: Bump Version - Props - uses: bitwarden/gh-actions/version-bump@fdcf1fcec3b04762ce48216cbf3af32498bed74c + uses: bitwarden/gh-actions/version-bump@f1125802b1ccae8c601d7c4f61ce39ea254b10c8 with: version: ${{ github.event.inputs.version_number }} file_path: "Directory.Build.props" diff --git a/.github/workflows/workflow-linter.yml b/.github/workflows/workflow-linter.yml index d6f21ba717..49388c11f8 100644 --- a/.github/workflows/workflow-linter.yml +++ b/.github/workflows/workflow-linter.yml @@ -8,4 +8,4 @@ on: jobs: call-workflow: - uses: bitwarden/gh-actions/.github/workflows/workflow-linter.yml@fdcf1fcec3b04762ce48216cbf3af32498bed74c + uses: bitwarden/gh-actions/.github/workflows/workflow-linter.yml@f1125802b1ccae8c601d7c4f61ce39ea254b10c8 From 4dcd467a5fb8f99e9644da12dcafa36b14d870d2 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 14:59:23 -0400 Subject: [PATCH 6/8] Update crazy-max/ghaction-import-gpg action to v6 (#3315) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --- .github/workflows/version-bump.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/version-bump.yml b/.github/workflows/version-bump.yml index 01ac04ed5d..65fa38dd57 100644 --- a/.github/workflows/version-bump.yml +++ b/.github/workflows/version-bump.yml @@ -29,7 +29,7 @@ jobs: secrets: "github-gpg-private-key, github-gpg-private-key-passphrase" - name: Import GPG key - uses: crazy-max/ghaction-import-gpg@d6f3f49f3345e29369fe57596a3ca8f94c4d2ca7 # v5.4.0 + uses: crazy-max/ghaction-import-gpg@82a020f1f7f605c65dd2449b392a52c3fcfef7ef # v6.0.0 with: gpg_private_key: ${{ steps.retrieve-secrets.outputs.github-gpg-private-key }} passphrase: ${{ steps.retrieve-secrets.outputs.github-gpg-private-key-passphrase }} From 279d0ccf62fd05e7485c986e57e1f636881134f7 Mon Sep 17 00:00:00 2001 From: Vincent Salucci <26154748+vincentsalucci@users.noreply.github.com> Date: Thu, 5 Oct 2023 15:13:28 -0500 Subject: [PATCH 7/8] [AC-1669] Bug - Remove obsolete assignUserId from CollectionService.SaveAsync(...) (#3312) * fix: remove AssignUserId from CollectionService.SaveAsync, refs AC-1669 * fix: add manage access conditional before creating collection, refs AC-1669 * fix: move access logic for create/update, fix all tests, refs AC-1669 * fix: add CollectionAccessSelection fixture, update tests, update bad reqeuest message, refs AC-1669 * fix: format, refs AC-1669 * fix: update null params with specific arg.is null checks, refs Ac-1669 * fix: update attribute class name, refs AC-1669 --- src/Api/Controllers/CollectionsController.cs | 2 +- src/Core/Services/ICollectionService.cs | 2 +- .../Implementations/CollectionService.cs | 29 +++--- .../Controllers/CollectionsControllerTests.cs | 2 +- .../CollectionAccessSelectionFixtures.cs | 37 ++++++++ .../Services/CollectionServiceTests.cs | 91 ++++++++----------- 6 files changed, 93 insertions(+), 70 deletions(-) create mode 100644 test/Core.Test/AutoFixture/CollectionAccessSelectionFixtures.cs diff --git a/src/Api/Controllers/CollectionsController.cs b/src/Api/Controllers/CollectionsController.cs index 419ee8d816..e4010d0018 100644 --- a/src/Api/Controllers/CollectionsController.cs +++ b/src/Api/Controllers/CollectionsController.cs @@ -161,7 +161,7 @@ public class CollectionsController : Controller var groups = model.Groups?.Select(g => g.ToSelectionReadOnly()); var users = model.Users?.Select(g => g.ToSelectionReadOnly()); - await _collectionService.SaveAsync(collection, groups, users, _currentContext.UserId); + await _collectionService.SaveAsync(collection, groups, users); return new CollectionResponseModel(collection); } diff --git a/src/Core/Services/ICollectionService.cs b/src/Core/Services/ICollectionService.cs index 931993dacb..4d392a7722 100644 --- a/src/Core/Services/ICollectionService.cs +++ b/src/Core/Services/ICollectionService.cs @@ -5,7 +5,7 @@ namespace Bit.Core.Services; public interface ICollectionService { - Task SaveAsync(Collection collection, IEnumerable groups = null, IEnumerable users = null, Guid? assignUserId = null); + Task SaveAsync(Collection collection, IEnumerable groups = null, IEnumerable users = null); Task DeleteUserAsync(Collection collection, Guid organizationUserId); Task> GetOrganizationCollectionsAsync(Guid organizationId); } diff --git a/src/Core/Services/Implementations/CollectionService.cs b/src/Core/Services/Implementations/CollectionService.cs index 6525fdc210..b2beccbbce 100644 --- a/src/Core/Services/Implementations/CollectionService.cs +++ b/src/Core/Services/Implementations/CollectionService.cs @@ -41,7 +41,7 @@ public class CollectionService : ICollectionService } public async Task SaveAsync(Collection collection, IEnumerable groups = null, - IEnumerable users = null, Guid? assignUserId = null) + IEnumerable users = null) { var org = await _organizationRepository.GetByIdAsync(collection.OrganizationId); if (org == null) @@ -49,6 +49,16 @@ public class CollectionService : ICollectionService throw new BadRequestException("Organization not found"); } + var groupsList = groups?.ToList(); + var usersList = users?.ToList(); + var groupHasManageAccess = groupsList?.Any(g => g.Manage) ?? false; + var userHasManageAccess = usersList?.Any(u => u.Manage) ?? false; + if (!groupHasManageAccess && !userHasManageAccess) + { + throw new BadRequestException( + "At least one member or group must have can manage permission."); + } + if (collection.Id == default(Guid)) { if (org.MaxCollections.HasValue) @@ -61,26 +71,13 @@ public class CollectionService : ICollectionService } } - await _collectionRepository.CreateAsync(collection, org.UseGroups ? groups : null, users); - - // Assign a user to the newly created collection. - if (assignUserId.HasValue) - { - var orgUser = await _organizationUserRepository.GetByOrganizationAsync(org.Id, assignUserId.Value); - if (orgUser != null && orgUser.Status == Enums.OrganizationUserStatusType.Confirmed) - { - await _collectionRepository.UpdateUsersAsync(collection.Id, - new List { - new CollectionAccessSelection { Id = orgUser.Id, Manage = true} }); - } - } - + await _collectionRepository.CreateAsync(collection, org.UseGroups ? groupsList : null, usersList); await _eventService.LogCollectionEventAsync(collection, Enums.EventType.Collection_Created); await _referenceEventService.RaiseEventAsync(new ReferenceEvent(ReferenceEventType.CollectionCreated, org, _currentContext)); } else { - await _collectionRepository.ReplaceAsync(collection, org.UseGroups ? groups : null, users); + await _collectionRepository.ReplaceAsync(collection, org.UseGroups ? groupsList : null, usersList); await _eventService.LogCollectionEventAsync(collection, Enums.EventType.Collection_Updated); } } diff --git a/test/Api.Test/Controllers/CollectionsControllerTests.cs b/test/Api.Test/Controllers/CollectionsControllerTests.cs index d4e5aeac16..3bfaa8b02c 100644 --- a/test/Api.Test/Controllers/CollectionsControllerTests.cs +++ b/test/Api.Test/Controllers/CollectionsControllerTests.cs @@ -40,7 +40,7 @@ public class CollectionsControllerTests await sutProvider.GetDependency() .Received(1) .SaveAsync(Arg.Any(), Arg.Any>(), - Arg.Any>(), null); + Arg.Any>()); } [Theory, BitAutoData] diff --git a/test/Core.Test/AutoFixture/CollectionAccessSelectionFixtures.cs b/test/Core.Test/AutoFixture/CollectionAccessSelectionFixtures.cs new file mode 100644 index 0000000000..54b7fb034f --- /dev/null +++ b/test/Core.Test/AutoFixture/CollectionAccessSelectionFixtures.cs @@ -0,0 +1,37 @@ +using System.Reflection; +using AutoFixture; +using AutoFixture.Xunit2; +using Bit.Core.Models.Data; + +namespace Bit.Core.Test.AutoFixture; + +public class CollectionAccessSelectionCustomization : ICustomization +{ + public bool Manage { get; set; } + + public CollectionAccessSelectionCustomization(bool manage) + { + Manage = manage; + } + + public void Customize(IFixture fixture) + { + fixture.Customize(composer => composer + .With(o => o.Manage, Manage)); + } +} + +public class CollectionAccessSelectionCustomizeAttribute : CustomizeAttribute +{ + private readonly bool _manage; + + public CollectionAccessSelectionCustomizeAttribute(bool manage = false) + { + _manage = manage; + } + + public override ICustomization GetCustomization(ParameterInfo parameter) + { + return new CollectionAccessSelectionCustomization(_manage); + } +} diff --git a/test/Core.Test/Services/CollectionServiceTests.cs b/test/Core.Test/Services/CollectionServiceTests.cs index d5b5f15ccd..0ce0a90dc4 100644 --- a/test/Core.Test/Services/CollectionServiceTests.cs +++ b/test/Core.Test/Services/CollectionServiceTests.cs @@ -5,6 +5,7 @@ using Bit.Core.Exceptions; using Bit.Core.Models.Data; using Bit.Core.Repositories; using Bit.Core.Services; +using Bit.Core.Test.AutoFixture; using Bit.Core.Test.AutoFixture.OrganizationFixtures; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; @@ -18,23 +19,7 @@ namespace Bit.Core.Test.Services; public class CollectionServiceTest { [Theory, BitAutoData] - public async Task SaveAsync_DefaultId_CreatesCollectionInTheRepository(Collection collection, Organization organization, SutProvider sutProvider) - { - collection.Id = default; - sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); - var utcNow = DateTime.UtcNow; - - await sutProvider.Sut.SaveAsync(collection); - - await sutProvider.GetDependency().Received().CreateAsync(collection, null, null); - await sutProvider.GetDependency().Received() - .LogCollectionEventAsync(collection, EventType.Collection_Created); - Assert.True(collection.CreationDate - utcNow < TimeSpan.FromSeconds(1)); - Assert.True(collection.RevisionDate - utcNow < TimeSpan.FromSeconds(1)); - } - - [Theory, BitAutoData] - public async Task SaveAsync_DefaultIdWithUsers_CreatesCollectionInTheRepository(Collection collection, Organization organization, IEnumerable users, SutProvider sutProvider) + public async Task SaveAsync_DefaultIdWithUsers_CreatesCollectionInTheRepository(Collection collection, Organization organization, [CollectionAccessSelectionCustomize(true)] IEnumerable users, SutProvider sutProvider) { collection.Id = default; sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); @@ -42,7 +27,9 @@ public class CollectionServiceTest await sutProvider.Sut.SaveAsync(collection, null, users); - await sutProvider.GetDependency().Received().CreateAsync(collection, null, users); + await sutProvider.GetDependency().Received() + .CreateAsync(collection, Arg.Is>(l => l == null), + Arg.Is>(l => l.Any(i => i.Manage == true))); await sutProvider.GetDependency().Received() .LogCollectionEventAsync(collection, EventType.Collection_Created); Assert.True(collection.CreationDate - utcNow < TimeSpan.FromSeconds(1)); @@ -51,7 +38,7 @@ public class CollectionServiceTest [Theory, BitAutoData] public async Task SaveAsync_DefaultIdWithGroupsAndUsers_CreateCollectionWithGroupsAndUsersInRepository(Collection collection, - IEnumerable groups, IEnumerable users, Organization organization, SutProvider sutProvider) + [CollectionAccessSelectionCustomize(true)] IEnumerable groups, IEnumerable users, Organization organization, SutProvider sutProvider) { collection.Id = default; organization.UseGroups = true; @@ -60,7 +47,9 @@ public class CollectionServiceTest await sutProvider.Sut.SaveAsync(collection, groups, users); - await sutProvider.GetDependency().Received().CreateAsync(collection, groups, users); + await sutProvider.GetDependency().Received() + .CreateAsync(collection, Arg.Is>(l => l.Any(i => i.Manage == true)), + Arg.Any>()); await sutProvider.GetDependency().Received() .LogCollectionEventAsync(collection, EventType.Collection_Created); Assert.True(collection.CreationDate - utcNow < TimeSpan.FromSeconds(1)); @@ -68,15 +57,17 @@ public class CollectionServiceTest } [Theory, BitAutoData] - public async Task SaveAsync_NonDefaultId_ReplacesCollectionInRepository(Collection collection, Organization organization, SutProvider sutProvider) + public async Task SaveAsync_NonDefaultId_ReplacesCollectionInRepository(Collection collection, Organization organization, [CollectionAccessSelectionCustomize(true)] IEnumerable users, SutProvider sutProvider) { var creationDate = collection.CreationDate; sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); var utcNow = DateTime.UtcNow; - await sutProvider.Sut.SaveAsync(collection); + await sutProvider.Sut.SaveAsync(collection, null, users); - await sutProvider.GetDependency().Received().ReplaceAsync(collection, null, null); + await sutProvider.GetDependency().Received().ReplaceAsync(collection, + Arg.Is>(l => l == null), + Arg.Is>(l => l.Any(i => i.Manage == true))); await sutProvider.GetDependency().Received() .LogCollectionEventAsync(collection, EventType.Collection_Updated); Assert.Equal(collection.CreationDate, creationDate); @@ -84,39 +75,20 @@ public class CollectionServiceTest } [Theory, BitAutoData] - public async Task SaveAsync_OrganizationNotUseGroup_CreateCollectionWithoutGroupsInRepository(Collection collection, IEnumerable groups, + public async Task SaveAsync_OrganizationNotUseGroup_CreateCollectionWithoutGroupsInRepository(Collection collection, + IEnumerable groups, [CollectionAccessSelectionCustomize(true)] IEnumerable users, Organization organization, SutProvider sutProvider) { collection.Id = default; + organization.UseGroups = false; sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); var utcNow = DateTime.UtcNow; - await sutProvider.Sut.SaveAsync(collection, groups); + await sutProvider.Sut.SaveAsync(collection, groups, users); - await sutProvider.GetDependency().Received().CreateAsync(collection, null, null); - await sutProvider.GetDependency().Received() - .LogCollectionEventAsync(collection, EventType.Collection_Created); - Assert.True(collection.CreationDate - utcNow < TimeSpan.FromSeconds(1)); - Assert.True(collection.RevisionDate - utcNow < TimeSpan.FromSeconds(1)); - } - - [Theory, BitAutoData] - public async Task SaveAsync_DefaultIdWithUserId_UpdateUserInCollectionRepository(Collection collection, - Organization organization, OrganizationUser organizationUser, SutProvider sutProvider) - { - collection.Id = default; - organizationUser.Status = OrganizationUserStatusType.Confirmed; - sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); - sutProvider.GetDependency().GetByOrganizationAsync(organization.Id, organizationUser.Id) - .Returns(organizationUser); - var utcNow = DateTime.UtcNow; - - await sutProvider.Sut.SaveAsync(collection, null, null, organizationUser.Id); - - await sutProvider.GetDependency().Received().CreateAsync(collection, null, null); - await sutProvider.GetDependency().Received() - .GetByOrganizationAsync(organization.Id, organizationUser.Id); - await sutProvider.GetDependency().Received().UpdateUsersAsync(collection.Id, Arg.Any>()); + await sutProvider.GetDependency().Received().CreateAsync(collection, + Arg.Is>(l => l == null), + Arg.Is>(l => l.Any(i => i.Manage == true))); await sutProvider.GetDependency().Received() .LogCollectionEventAsync(collection, EventType.Collection_Created); Assert.True(collection.CreationDate - utcNow < TimeSpan.FromSeconds(1)); @@ -135,14 +107,31 @@ public class CollectionServiceTest } [Theory, BitAutoData] - public async Task SaveAsync_ExceedsOrganizationMaxCollections_ThrowsBadRequest(Collection collection, Organization organization, SutProvider sutProvider) + public async Task SaveAsync_NoManageAccess_ThrowsBadRequest(Collection collection, Organization organization, + [CollectionAccessSelectionCustomize] IEnumerable users, SutProvider sutProvider) + { + collection.Id = default; + sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); + + var ex = await Assert.ThrowsAsync(() => sutProvider.Sut.SaveAsync(collection, null, users)); + Assert.Contains("At least one member or group must have can manage permission.", ex.Message); + await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().CreateAsync(default); + await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().CreateAsync(default, default, default); + await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().ReplaceAsync(default); + await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().LogCollectionEventAsync(default, default); + } + + [Theory, BitAutoData] + public async Task SaveAsync_ExceedsOrganizationMaxCollections_ThrowsBadRequest(Collection collection, + Organization organization, [CollectionAccessSelectionCustomize(true)] IEnumerable users, + SutProvider sutProvider) { collection.Id = default; sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); sutProvider.GetDependency().GetCountByOrganizationIdAsync(organization.Id) .Returns(organization.MaxCollections.Value); - var ex = await Assert.ThrowsAsync(() => sutProvider.Sut.SaveAsync(collection)); + var ex = await Assert.ThrowsAsync(() => sutProvider.Sut.SaveAsync(collection, null, users)); Assert.Equal($@"You have reached the maximum number of collections ({organization.MaxCollections.Value}) for this organization.", ex.Message); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().CreateAsync(default); await sutProvider.GetDependency().DidNotReceiveWithAnyArgs().CreateAsync(default, default, default); From 3a71e7b08198f125f11e1066dcab630e3deff6e0 Mon Sep 17 00:00:00 2001 From: Daniel James Smith <2670567+djsmith85@users.noreply.github.com> Date: Fri, 6 Oct 2023 18:28:02 +0200 Subject: [PATCH 8/8] Add tech-leads as default owners (#3330) Co-authored-by: Daniel James Smith --- .github/CODEOWNERS | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index e55c457b15..7d87fce8f8 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -2,6 +2,10 @@ # # https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners +# The following owners will be the default owners for everything in the repo. +# Unless a later match takes precedence +* @bitwarden/tech-leads + # DevOps for Actions and other workflow changes. .github/workflows @bitwarden/dept-devops