Skip to content

Commit bfe2e77

Browse files
authored
[PM-30615] Fix Public API List Collections returning Default Collections (#6841)
1 parent 93e2c97 commit bfe2e77

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/Api/Public/Controllers/CollectionsController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ public async Task<IActionResult> List()
6767
{
6868
var collections = await _collectionRepository.GetManyByOrganizationIdWithAccessAsync(_currentContext.OrganizationId.Value);
6969

70-
var collectionResponses = collections.Select(c =>
71-
new CollectionResponseModel(c.Item1, c.Item2.Groups));
70+
var collectionResponses = collections
71+
.Where(c => c.Item1.Type != CollectionType.DefaultUserCollection)
72+
.Select(c => new CollectionResponseModel(c.Item1, c.Item2.Groups));
7273

7374
var response = new ListResponseModel<CollectionResponseModel>(collectionResponses);
7475
return new JsonResult(response);

test/Api.IntegrationTest/Controllers/Public/CollectionsControllerTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Bit.Core.AdminConsole.Entities;
77
using Bit.Core.AdminConsole.Repositories;
88
using Bit.Core.Billing.Enums;
9+
using Bit.Core.Entities;
910
using Bit.Core.Enums;
1011
using Bit.Core.Models.Data;
1112
using Bit.Core.Platform.Push;
@@ -114,4 +115,64 @@ public async Task CreateCollectionWithMultipleUsersAndVariedPermissions_Success(
114115
Assert.NotEmpty(result.Item2.Groups);
115116
Assert.NotEmpty(result.Item2.Users);
116117
}
118+
119+
[Fact]
120+
public async Task List_ExcludesDefaultUserCollections_IncludesGroupsAndUsers()
121+
{
122+
// Arrange
123+
var collectionRepository = _factory.GetService<ICollectionRepository>();
124+
var groupRepository = _factory.GetService<IGroupRepository>();
125+
126+
var defaultCollection = new Collection
127+
{
128+
OrganizationId = _organization.Id,
129+
Name = "My Items",
130+
Type = CollectionType.DefaultUserCollection
131+
};
132+
await collectionRepository.CreateAsync(defaultCollection, null, null);
133+
134+
var group = await groupRepository.CreateAsync(new Group
135+
{
136+
OrganizationId = _organization.Id,
137+
Name = "Test Group",
138+
ExternalId = $"test-group-{Guid.NewGuid()}",
139+
});
140+
141+
var (_, user) = await OrganizationTestHelpers.CreateNewUserWithAccountAsync(
142+
_factory,
143+
_organization.Id,
144+
OrganizationUserType.User);
145+
146+
var sharedCollection = await OrganizationTestHelpers.CreateCollectionAsync(
147+
_factory,
148+
_organization.Id,
149+
"Shared Collection with Access",
150+
externalId: "shared-collection-with-access",
151+
groups:
152+
[
153+
new CollectionAccessSelection { Id = group.Id, ReadOnly = false, HidePasswords = false, Manage = true }
154+
],
155+
users:
156+
[
157+
new CollectionAccessSelection { Id = user.Id, ReadOnly = true, HidePasswords = true, Manage = false }
158+
]);
159+
160+
// Act
161+
var response = await _client.GetFromJsonAsync<ListResponseModel<CollectionResponseModel>>("public/collections");
162+
163+
// Assert
164+
Assert.NotNull(response);
165+
166+
Assert.DoesNotContain(response.Data, c => c.Id == defaultCollection.Id);
167+
168+
var collectionResponse = response.Data.First(c => c.Id == sharedCollection.Id);
169+
Assert.NotNull(collectionResponse.Groups);
170+
Assert.Single(collectionResponse.Groups);
171+
172+
var groupResponse = collectionResponse.Groups.First();
173+
Assert.Equal(group.Id, groupResponse.Id);
174+
Assert.False(groupResponse.ReadOnly);
175+
Assert.False(groupResponse.HidePasswords);
176+
Assert.True(groupResponse.Manage);
177+
}
117178
}

0 commit comments

Comments
 (0)