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-5966] Fix Entity Framework query for MySQL #5170

Merged
merged 7 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,13 @@
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);

//Get domains that have not been verified after 72 hours
var domains = await dbContext.OrganizationDomains
.Where(x => (DateTime.UtcNow - x.CreationDate).Days == 4
&& x.VerifiedDate == null)
var threeDaysOldUnverifiedDomains = await dbContext.OrganizationDomains
.Where(x => x.CreationDate.Date == DateTime.UtcNow.AddDays(-4).Date
&& x.VerifiedDate == null)

Check warning on line 152 in src/Infrastructure.EntityFramework/Repositories/OrganizationDomainRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/Infrastructure.EntityFramework/Repositories/OrganizationDomainRepository.cs#L150-L152

Added lines #L150 - L152 were not covered by tests
.AsNoTracking()
.ToListAsync();

return Mapper.Map<List<Core.Entities.OrganizationDomain>>(domains);
return Mapper.Map<List<Core.Entities.OrganizationDomain>>(threeDaysOldUnverifiedDomains);

Check warning on line 156 in src/Infrastructure.EntityFramework/Repositories/OrganizationDomainRepository.cs

View check run for this annotation

Codecov / codecov/patch

src/Infrastructure.EntityFramework/Repositories/OrganizationDomainRepository.cs#L156

Added line #L156 was not covered by tests
}

public async Task<bool> DeleteExpiredAsync(int expirationPeriod)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
๏ปฟusing Bit.Core.AdminConsole.Entities;
using Bit.Core.Entities;
using Bit.Core.Repositories;
using Xunit;

namespace Bit.Infrastructure.IntegrationTest.Repositories;

public class OrganizationDomainRepositoryTests
{
[DatabaseTheory, DatabaseData]
public async Task GetExpiredOrganizationDomainsAsync_ShouldReturn3DaysOldUnverifiedDomains(
IUserRepository userRepository,
IOrganizationRepository organizationRepository,
IOrganizationDomainRepository organizationDomainRepository)
{
// Arrange
var id = Guid.NewGuid();

var user1 = await userRepository.CreateAsync(new User
{
Name = "Test User 1",
Email = $"test+{id}@example.com",
ApiKey = "TEST",
SecurityStamp = "stamp",
});

var organization1 = await organizationRepository.CreateAsync(new Organization
{
Name = $"Test Org {id}",
BillingEmail = user1.Email,
Plan = "Test",
PrivateKey = "privatekey",

});

var organizationDomain1 = new OrganizationDomain
{
OrganizationId = organization1.Id,
DomainName = $"domain2+{id}@example.com",
Txt = "btw+12345"
};
var dummyInterval = 1;
organizationDomain1.SetNextRunDate(dummyInterval);

var beforeValidationDate = DateTime.UtcNow.AddDays(-4).Date;

await organizationDomainRepository.CreateAsync(organizationDomain1);
var organization2 = await organizationRepository.CreateAsync(new Organization
{
Name = $"Test Org {id}",
BillingEmail = user1.Email,
Plan = "Test",
PrivateKey = "privatekey",
CreationDate = beforeValidationDate
});
var organizationDomain2 = new OrganizationDomain
{
OrganizationId = organization2.Id,
DomainName = $"domain2+{id}@example.com",
Txt = "btw+12345",
CreationDate = beforeValidationDate
};
organizationDomain2.SetNextRunDate(dummyInterval);
await organizationDomainRepository.CreateAsync(organizationDomain2);

// Act
var domains = await organizationDomainRepository.GetExpiredOrganizationDomainsAsync();

// Assert
var expectedDomain1 = domains.FirstOrDefault(domain => domain.DomainName == organizationDomain1.DomainName);
Assert.NotNull(expectedDomain1);

var expectedDomain2 = domains.FirstOrDefault(domain => domain.DomainName == organizationDomain2.DomainName);
Assert.NotNull(expectedDomain2);
}

[DatabaseTheory, DatabaseData]
public async Task GetExpiredOrganizationDomainsAsync_ShouldNotReturnDomainsUnder3DaysOld(
IUserRepository userRepository,
IOrganizationRepository organizationRepository,
IOrganizationDomainRepository organizationDomainRepository)
{
// Arrange
var id = Guid.NewGuid();

var user = await userRepository.CreateAsync(new User
{
Name = "Test User",
Email = $"test+{id}@example.com",
ApiKey = "TEST",
SecurityStamp = "stamp",
});

var organization = await organizationRepository.CreateAsync(new Organization
{
Name = $"Test Org {id}",
BillingEmail = user.Email,
Plan = "Test",
PrivateKey = "privatekey",

});

var beforeValidationDate = DateTime.UtcNow.AddDays(-1).Date;
var organizationDomain = new OrganizationDomain
{
OrganizationId = organization.Id,
DomainName = $"domain{id}@example.com",
Txt = "btw+12345",
CreationDate = beforeValidationDate
};
var dummyInterval = 1;
organizationDomain.SetNextRunDate(dummyInterval);
await organizationDomainRepository.CreateAsync(organizationDomain);

// Act
var domains = await organizationDomainRepository.GetExpiredOrganizationDomainsAsync();

// Assert
var expectedDomain2 = domains.FirstOrDefault(domain => domain.DomainName == organizationDomain.DomainName);
Assert.Null(expectedDomain2);
}

[DatabaseTheory, DatabaseData]
public async Task GetExpiredOrganizationDomainsAsync_ShouldNotReturnVerifiedDomains(
IUserRepository userRepository,
IOrganizationRepository organizationRepository,
IOrganizationDomainRepository organizationDomainRepository)
{
// Arrange
var id = Guid.NewGuid();

var user = await userRepository.CreateAsync(new User
{
Name = "Test User 1",
Email = $"test+{id}@example.com",
ApiKey = "TEST",
SecurityStamp = "stamp",
});

var organization1 = await organizationRepository.CreateAsync(new Organization
{
Name = $"Test Org {id}",
BillingEmail = user.Email,
Plan = "Test",
PrivateKey = "privatekey",

});

var organizationDomain1 = new OrganizationDomain
{
OrganizationId = organization1.Id,
DomainName = $"domain2+{id}@example.com",
Txt = "btw+12345"
};
organizationDomain1.SetVerifiedDate();
var dummyInterval = 1;

organizationDomain1.SetNextRunDate(dummyInterval);

await organizationDomainRepository.CreateAsync(organizationDomain1);

var organization2 = await organizationRepository.CreateAsync(new Organization
{
Name = $"Test Org {id}",
BillingEmail = user.Email,
Plan = "Test",
PrivateKey = "privatekey",
});

var organizationDomain2 = new OrganizationDomain
{
OrganizationId = organization2.Id,
DomainName = $"domain2+{id}@example.com",
Txt = "btw+12345"
};
organizationDomain2.SetNextRunDate(dummyInterval);
organizationDomain2.SetVerifiedDate();

await organizationDomainRepository.CreateAsync(organizationDomain2);

// Act
var domains = await organizationDomainRepository.GetExpiredOrganizationDomainsAsync();

// Assert
var expectedDomain1 = domains.FirstOrDefault(domain => domain.DomainName == organizationDomain1.DomainName);
Assert.Null(expectedDomain1);

var expectedDomain2 = domains.FirstOrDefault(domain => domain.DomainName == organizationDomain2.DomainName);
Assert.Null(expectedDomain2);
}
}
Loading