Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-3626] Add Collections & Organizations Tests #68

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,34 @@ public async Task UpdateCollectionsAsync(Guid cipherId, Guid userId, IEnumerable
.Select(c => c.OrganizationId)
.FirstAsync();

var availableCollections = await (from c in dbContext.Collections
join o in dbContext.Organizations on c.OrganizationId equals o.Id
join ou in dbContext.OrganizationUsers
on new { OrganizationId = o.Id, UserId = (Guid?)userId } equals
new { ou.OrganizationId, ou.UserId }
join cu in dbContext.CollectionUsers
on new { ou.AccessAll, CollectionId = c.Id, OrganizationUserId = ou.Id } equals
new { AccessAll = false, cu.CollectionId, cu.OrganizationUserId } into cu_g
from cu in cu_g.DefaultIfEmpty()
join gu in dbContext.GroupUsers
on new { CollectionId = (Guid?)cu.CollectionId, ou.AccessAll, OrganizationUserId = ou.Id } equals
new { CollectionId = (Guid?)null, AccessAll = false, gu.OrganizationUserId } into gu_g
from gu in gu_g.DefaultIfEmpty()
join g in dbContext.Groups on gu.GroupId equals g.Id into g_g
from g in g_g.DefaultIfEmpty()
join cg in dbContext.CollectionGroups
on new { g.AccessAll, CollectionId = c.Id, gu.GroupId } equals
new { AccessAll = false, cg.CollectionId, cg.GroupId } into cg_g
from cg in cg_g.DefaultIfEmpty()
where o.Id == organizationId && o.Enabled && ou.Status == OrganizationUserStatusType.Confirmed
&& (ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly)
select c.Id).ToListAsync();

var collectionCiphers = await (from cc in dbContext.CollectionCiphers
where cc.CipherId == cipherId
select cc).ToListAsync();
var availableCollections = await (
from c in dbContext.Collections
join o in dbContext.Organizations on c.OrganizationId equals o.Id
join ou in dbContext.OrganizationUsers
on new { OrganizationId = o.Id, UserId = (Guid?)userId } equals
new { ou.OrganizationId, ou.UserId }
join cu in dbContext.CollectionUsers
on new { ou.AccessAll, CollectionId = c.Id, OrganizationUserId = ou.Id } equals
new { AccessAll = false, cu.CollectionId, cu.OrganizationUserId } into cu_g
from cu in cu_g.DefaultIfEmpty()
join gu in dbContext.GroupUsers
on new { CollectionId = (Guid?)cu.CollectionId, ou.AccessAll, OrganizationUserId = ou.Id } equals
new { CollectionId = (Guid?)null, AccessAll = false, gu.OrganizationUserId } into gu_g
from gu in gu_g.DefaultIfEmpty()
join g in dbContext.Groups on gu.GroupId equals g.Id into g_g
from g in g_g.DefaultIfEmpty()
join cg in dbContext.CollectionGroups
on new { g.AccessAll, CollectionId = c.Id, gu.GroupId } equals
new { AccessAll = false, cg.CollectionId, cg.GroupId } into cg_g
from cg in cg_g.DefaultIfEmpty()
where o.Id == organizationId && o.Enabled && ou.Status == OrganizationUserStatusType.Confirmed
&& (ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly)
select c.Id).ToListAsync();

var collectionCiphers = await (
from cc in dbContext.CollectionCiphers
where cc.CipherId == cipherId
select cc).ToListAsync();

foreach (var requestedCollectionId in collectionIds)
{
Expand Down Expand Up @@ -181,44 +183,49 @@ public async Task UpdateCollectionsForCiphersAsync(IEnumerable<Guid> cipherIds,
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var availableCollections = from c in dbContext.Collections
join o in dbContext.Organizations
on c.OrganizationId equals o.Id
join ou in dbContext.OrganizationUsers
on o.Id equals ou.OrganizationId
where ou.UserId == userId
join cu in dbContext.CollectionUsers
on ou.Id equals cu.OrganizationUserId into cu_g
from cu in cu_g.DefaultIfEmpty()
where !ou.AccessAll && cu.CollectionId == c.Id
join gu in dbContext.GroupUsers
on ou.Id equals gu.OrganizationUserId into gu_g
from gu in gu_g.DefaultIfEmpty()
where cu.CollectionId == null && !ou.AccessAll
join g in dbContext.Groups
on gu.GroupId equals g.Id into g_g
from g in g_g.DefaultIfEmpty()
join cg in dbContext.CollectionGroups
on gu.GroupId equals cg.GroupId into cg_g
from cg in cg_g.DefaultIfEmpty()
where !g.AccessAll && cg.CollectionId == c.Id &&
(o.Id == organizationId && o.Enabled && ou.Status == OrganizationUserStatusType.Confirmed &&
(ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly))
select new { c, o, ou, cu, gu, g, cg };
var count = await availableCollections.CountAsync();
if (await availableCollections.CountAsync() < 1)
var availableCollections = (
from c in dbContext.Collections
join o in dbContext.Organizations
on c.OrganizationId equals o.Id
join ou in dbContext.OrganizationUsers
on new { OrganizationId = o.Id, UserId = (Guid?)userId }
equals new { ou.OrganizationId, ou.UserId }
join cu in dbContext.CollectionUsers
on new { ou.AccessAll, CollectionId = c.Id }
equals new { AccessAll = false, cu.CollectionId } into cu_g
from cu in cu_g.DefaultIfEmpty()
join gu in dbContext.GroupUsers
on new { CollectionId = (Guid?)cu.CollectionId, ou.AccessAll, OrganizationUserId = ou.Id }
equals new { CollectionId = (Guid?)null, AccessAll = false, gu.OrganizationUserId } into gu_g
from gu in gu_g.DefaultIfEmpty()
join g in dbContext.Groups
on gu.GroupId equals g.Id into g_g
from g in g_g.DefaultIfEmpty()
join cg in dbContext.CollectionGroups
on new { g.AccessAll, CollectionId = c.Id, gu.GroupId }
equals new { AccessAll = false, cg.CollectionId, cg.GroupId } into cg_g
from cg in cg_g.DefaultIfEmpty()
where o.Id == organizationId
&& o.Enabled
&& ou.Status == OrganizationUserStatusType.Confirmed
&& (ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly)
select c.Id
);

if (!await availableCollections.AnyAsync())
{
return;
}

var insertData = from collectionId in collectionIds
from cipherId in cipherIds
where availableCollections.Select(x => x.c.Id).Contains(collectionId)
select new Models.CollectionCipher
{
CollectionId = collectionId,
CipherId = cipherId,
};
var insertData = (
from collectionId in collectionIds
from cipherId in cipherIds
where availableCollections.Contains(collectionId)
select new Models.CollectionCipher
{
CollectionId = collectionId,
CipherId = cipherId,
});
await dbContext.AddRangeAsync(insertData);
await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(organizationId);
await dbContext.SaveChangesAsync();
Expand Down
37 changes: 17 additions & 20 deletions src/Infrastructure.EntityFramework/Repositories/GroupRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ public async Task DeleteUserAsync(Guid groupId, Guid organizationUserId)
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = await (
from cg in dbContext.CollectionGroups
where cg.GroupId == id
select cg).ToListAsync();
var collections = query.Select(c => new CollectionAccessSelection
{
Id = c.CollectionId,
ReadOnly = c.ReadOnly,
HidePasswords = c.HidePasswords,
}).ToList();
var collections = await dbContext.CollectionGroups
.Where(cg => cg.GroupId == id)
.Select(cg => new CollectionAccessSelection
{
Id = cg.CollectionId,
ReadOnly = cg.ReadOnly,
HidePasswords = cg.HidePasswords,
})
.ToListAsync();

return new Tuple<Core.Entities.Group, ICollection<CollectionAccessSelection>>(
grp, collections);
}
Expand Down Expand Up @@ -120,10 +120,9 @@ from cg in dbContext.CollectionGroups
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = from g in dbContext.Groups
where groupIds.Contains(g.Id)
select g;
var groups = await query.ToListAsync();
var groups = await dbContext.Groups
.Where(g => groupIds.Contains(g.Id))
.ToListAsync();
return Mapper.Map<List<Core.Entities.Group>>(groups);
}
}
Expand All @@ -149,12 +148,10 @@ public async Task<ICollection<Guid>> GetManyIdsByUserIdAsync(Guid organizationUs
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query =
from gu in dbContext.GroupUsers
where gu.OrganizationUserId == organizationUserId
select gu;
var groupIds = await query.Select(x => x.GroupId).ToListAsync();
return groupIds;
return await dbContext.GroupUsers
.Where(gu => gu.OrganizationUserId == organizationUserId)
.Select(gu => gu.GroupId)
.ToListAsync();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
using AutoFixture.Kernel;
using Bit.Core.Entities;
using Bit.Infrastructure.EFIntegration.Test.AutoFixture.Relays;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;

namespace Bit.Infrastructure.EFIntegration.Test.AutoFixture;

Expand All @@ -29,21 +27,3 @@ public object Create(object request, ISpecimenContext context)
return obj;
}
}

internal class EfCollection : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new IgnoreVirtualMembersCustomization());
fixture.Customizations.Add(new GlobalSettingsBuilder());
fixture.Customizations.Add(new CollectionBuilder());
fixture.Customizations.Add(new OrganizationBuilder());
fixture.Customizations.Add(new EfRepositoryListBuilder<CollectionRepository>());
fixture.Customizations.Add(new EfRepositoryListBuilder<OrganizationRepository>());
}
}

internal class EfCollectionCustomize : BitCustomizeAttribute
{
public override ICustomization GetCustomization() => new EfCollection();
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using AutoFixture;
using AutoFixture.Kernel;
using Bit.Core.Entities;
using Bit.Core.Test.AutoFixture.UserFixtures;
using Bit.Infrastructure.EFIntegration.Test.AutoFixture.Relays;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;

namespace Bit.Infrastructure.EFIntegration.Test.AutoFixture;

Expand All @@ -30,30 +27,3 @@ public object Create(object request, ISpecimenContext context)
return obj;
}
}

internal class EfDevice : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new IgnoreVirtualMembersCustomization());
fixture.Customizations.Add(new GlobalSettingsBuilder());
fixture.Customizations.Add(new DeviceBuilder());
fixture.Customizations.Add(new UserBuilder());
fixture.Customizations.Add(new EfRepositoryListBuilder<DeviceRepository>());
fixture.Customizations.Add(new EfRepositoryListBuilder<UserRepository>());
}
}

internal class EfDeviceAutoDataAttribute : CustomAutoDataAttribute
{
public EfDeviceAutoDataAttribute() : base(new SutProviderCustomization(), new EfDevice())
{ }
}

internal class InlineEfDeviceAutoDataAttribute : InlineCustomAutoDataAttribute
{
public InlineEfDeviceAutoDataAttribute(params object[] values) : base(new[] { typeof(SutProviderCustomization),
typeof(EfDevice) }, values)
{ }
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,6 @@ public object Create(object request, ISpecimenContext context)
}
}

internal class EfPolicy : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new IgnoreVirtualMembersCustomization());
fixture.Customizations.Add(new GlobalSettingsBuilder());
fixture.Customizations.Add(new PolicyBuilder());
fixture.Customizations.Add(new OrganizationBuilder());
fixture.Customizations.Add(new EfRepositoryListBuilder<PolicyRepository>());
fixture.Customizations.Add(new EfRepositoryListBuilder<OrganizationRepository>());
}
}

internal class EfPolicyApplicableToUser : ICustomization
{
public void Customize(IFixture fixture)
Expand All @@ -59,21 +46,8 @@ public void Customize(IFixture fixture)
}
}

internal class EfPolicyAutoDataAttribute : CustomAutoDataAttribute
{
public EfPolicyAutoDataAttribute() : base(new SutProviderCustomization(), new EfPolicy())
{ }
}

internal class EfPolicyApplicableToUserInlineAutoDataAttribute : InlineCustomAutoDataAttribute
{
public EfPolicyApplicableToUserInlineAutoDataAttribute(params object[] values) : base(new[] { typeof(SutProviderCustomization), typeof(EfPolicyApplicableToUser) }, values)
{ }
}

internal class InlineEfPolicyAutoDataAttribute : InlineCustomAutoDataAttribute
{
public InlineEfPolicyAutoDataAttribute(params object[] values) : base(new[] { typeof(SutProviderCustomization),
typeof(EfPolicy) }, values)
{ }
}
Loading