Files
server/test/Infrastructure.IntegrationTest/Billing/Repositories/OrganizationPlanMigrationCohortAssignmentRepositoryTests.cs
Alex Morask d009a52f43 [PM-36949] feat: Add OrganizationPlanMigrationCohort and Assignment tables with bare repositories (#7644)
* [PM-36949] Add OrganizationPlanMigrationCohort schema and Core domain types

Add the foundation for cohort-based plan migrations:
- Two tables: OrganizationPlanMigrationCohort and OrganizationPlanMigrationCohortAssignment
- Two views and nine stored procedures (four CRUD on cohort, four CRUD plus
  ReadByOrganizationId on assignment)
- Single Migrator script for MSSQL deployment
- Core entities, MigrationPath value object and its registry, and bare repository
  interfaces under Bit.Core.Billing.Organizations.PlanMigration

The cohort table holds the human-managed metadata (name, discount coupons,
MigrationPathId byte) and the assignment table records each organization's
position in the migration lifecycle (scheduled, migrated, churn-mitigated).
Both Update SPs follow the accept-but-don't-assign pattern: immutable columns
(OrganizationId, CohortId, CreatedAt) are parameters but not SET clauses.

* [PM-36949] Add Dapper repositories for plan migration cohort tables

OrganizationPlanMigrationCohortRepository inherits the base Repository<T, TId>
CRUD methods unchanged. OrganizationPlanMigrationCohortAssignmentRepository
also relies on the base for CRUD and adds GetByOrganizationIdAsync which
returns at most one row (the UNIQUE constraint on OrganizationId at the
database layer guarantees this).

* [PM-36949] Add EF Core configurations, repositories, and provider migrations for plan migration cohort tables

- EF models wrap the Core entities; the assignment model exposes nav properties
  for Organization and Cohort so the FK + cascade-delete is inferred by EF.
- EntityTypeConfiguration classes pin ID generation to application code
  (ValueGeneratedNever) and declare the UNIQUE indexes plus the composite
  (CohortId, ScheduledAt, MigratedAt) index.
- Repositories follow the OrganizationInstallationRepository template; the
  assignment repo adds GetByOrganizationIdAsync to mirror the SP exposed on
  the MSSQL side.
- DatabaseContext gets two DbSet properties; auto-discovery picks up the
  configuration classes.
- Generated migrations for MySQL, Postgres, and SQLite create matching schemas;
  EF truncates FK and index names on providers with 64-char identifier limits,
  which is consistent with the rest of the codebase.

* [PM-36949] Wire up DI and add tests for plan migration cohort repositories

Register both Dapper and EF Core repositories in their respective service
collection extensions, following the existing AddSingleton convention in
these files.

Add tests:
- MigrationPathIdsSnapshotTests guards the immortal byte IDs that downstream
  code pins on. The class- and method-level comments document why these
  values can never be renumbered.
- MigrationPathTests covers the FromId round-trip and the null-on-unknown
  behavior the registry promises to callers.
- OrganizationPlanMigrationCohortRepositoryTests exercises CRUD, the UNIQUE
  Name constraint, and verifies that ReplaceAsync ignores CreatedAt
  mutations (per the accept-but-don't-assign Update SP).
- OrganizationPlanMigrationCohortAssignmentRepositoryTests exercises CRUD,
  GetByOrganizationIdAsync, the UNIQUE OrganizationId constraint,
  cascade-delete from both Organization and Cohort, and verifies that
  ReplaceAsync ignores OrganizationId, CohortId, and CreatedAt mutations.

* [PM-36949] Use PlanType and MigrationPathId enums on MigrationPath

Replace the byte Id with a byte-backed MigrationPathId enum and replace
the string FromPlan/ToPlan fields with PlanType. Persistence is
unchanged -- EF normalises enum-backed properties to their underlying
type in the model snapshot, and Dapper handles enum-to-byte parameter
mapping automatically.

* [PM-36949] Add *.lscache to .gitignore

* [PM-36949] fix: Override ReplaceAsync on EF cohort repositories for immutability parity

The Dapper _Update SPs accept-but-don't-assign certain columns (CreatedAt
on cohort; OrganizationId, CohortId, and CreatedAt on assignment), but
the base EF Repository<T,TEntity,Guid>.ReplaceAsync uses SetValues which
writes every scalar. Override on both repos and mark the immutable
properties as IsModified = false so MySQL/Postgres/Sqlite match MSSQL
behavior. Mirrors the existing DeviceRepository.ReplaceAsync pattern.

* [PM-36949] fix: Bound cohort string columns and widen Name to 255 chars

Add [MaxLength] attributes to the three cohort string properties so the
EF providers (MySQL/Postgres/Sqlite) enforce the same limits as MSSQL,
where the columns were already NVARCHAR-capped. Widen Name from 64 to
255 chars across MSSQL DDL, both _Create/_Update SP signatures, the
Migrator script, the entity, and all three regenerated EF migrations.
Coupon codes stay at 64 (Stripe IDs are short).

* [PM-36949] test: Lock FromPlan and ToPlan per MigrationPath

The snapshot test class doc says "byte N means a specific FromPlan ->
ToPlan transition forever", but only the byte value was being asserted.
A silent refactor of the registry's PlanType references would not have
been caught. Add per-path FromPlan/ToPlan assertions to close the gap.

* [PM-36949] chore: Apply dotnet format

* [PM-36949] test: Use LaxDateTimeComparer for round-tripped DateTimes

Postgres timestamp and MySQL datetime(6) store microsecond precision (6
fractional digits), but .NET DateTime is 100ns ticks (7 digits). Exact
Assert.Equal fails by a single tick on round-trip. Switch the three
DateTime comparisons in ReplaceAsync_UpdatesMutableColumns_AndIgnoresImmutableOnes
to LaxDateTimeComparer.Default -- the same 2ms-tolerance comparer used
by SendRepositoryTests and InstallationRepositoryTests for the same
precision issue.

* [PM-36949] refactor: Rename DateTime columns to Date suffix per naming convention

Addresses PR review feedback. Also tightens DATETIME2(7) / NVARCHAR(N) spacing
per the SQL style guide, drops *.lscache from .gitignore (handled by #7648),
and regenerates EF migrations for MySQL, Postgres, and SQLite.

* Apply dotnet format to regenerated migrations

* [PM-36949] chore: Align NULL/NOT NULL columns in cohort tables
2026-05-18 12:34:55 -05:00

225 lines
10 KiB
C#

using Bit.Core.AdminConsole.Entities;
using Bit.Core.Billing.Organizations.PlanMigration.Entities;
using Bit.Core.Billing.Organizations.PlanMigration.Repositories;
using Bit.Core.Billing.Organizations.PlanMigration.ValueObjects;
using Bit.Core.Repositories;
using Bit.Infrastructure.IntegrationTest.AdminConsole;
using Bit.Infrastructure.IntegrationTest.Comparers;
using Xunit;
namespace Bit.Infrastructure.IntegrationTest.Billing.Repositories;
public class OrganizationPlanMigrationCohortAssignmentRepositoryTests
{
private static OrganizationPlanMigrationCohort CreateTestCohort() =>
new()
{
Name = $"cohort-{Guid.NewGuid()}",
MigrationPathId = MigrationPaths.Enterprise2020AnnualToCurrent.Id,
IsActive = true,
CreationDate = DateTime.UtcNow,
RevisionDate = DateTime.UtcNow,
};
private static OrganizationPlanMigrationCohortAssignment CreateTestAssignment(
Organization organization,
OrganizationPlanMigrationCohort cohort,
DateTime? scheduledAt = null,
DateTime? migratedAt = null,
DateTime? churnDiscountAppliedAt = null) =>
new()
{
OrganizationId = organization.Id,
CohortId = cohort.Id,
ScheduledDate = scheduledAt,
MigratedDate = migratedAt,
ChurnDiscountAppliedDate = churnDiscountAppliedAt,
CreationDate = DateTime.UtcNow,
RevisionDate = DateTime.UtcNow,
};
[Theory, DatabaseData]
public async Task CreateAsync_GetByIdAsync_RoundTrip(
IOrganizationPlanMigrationCohortAssignmentRepository assignmentRepository,
IOrganizationPlanMigrationCohortRepository cohortRepository,
IOrganizationRepository organizationRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var cohort = await cohortRepository.CreateAsync(CreateTestCohort());
var scheduledAt = DateTime.UtcNow;
var assignment = await assignmentRepository.CreateAsync(
CreateTestAssignment(organization, cohort, scheduledAt: scheduledAt));
var result = await assignmentRepository.GetByIdAsync(assignment.Id);
Assert.NotNull(result);
Assert.Equal(assignment.Id, result.Id);
Assert.Equal(organization.Id, result.OrganizationId);
Assert.Equal(cohort.Id, result.CohortId);
Assert.NotNull(result.ScheduledDate);
Assert.Null(result.MigratedDate);
Assert.Null(result.ChurnDiscountAppliedDate);
// Cleanup (cascade from organization will also remove the assignment, but be explicit)
await assignmentRepository.DeleteAsync(result);
await cohortRepository.DeleteAsync(cohort);
}
[Theory, DatabaseData]
public async Task CreateAsync_DuplicateOrganizationId_Throws(
IOrganizationPlanMigrationCohortAssignmentRepository assignmentRepository,
IOrganizationPlanMigrationCohortRepository cohortRepository,
IOrganizationRepository organizationRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var firstCohort = await cohortRepository.CreateAsync(CreateTestCohort());
var secondCohort = await cohortRepository.CreateAsync(CreateTestCohort());
var first = await assignmentRepository.CreateAsync(CreateTestAssignment(organization, firstCohort));
await Assert.ThrowsAnyAsync<Exception>(() =>
assignmentRepository.CreateAsync(CreateTestAssignment(organization, secondCohort)));
// Cleanup
await assignmentRepository.DeleteAsync(first);
await cohortRepository.DeleteAsync(firstCohort);
await cohortRepository.DeleteAsync(secondCohort);
}
[Theory, DatabaseData]
public async Task GetByOrganizationIdAsync_ReturnsAssignment(
IOrganizationPlanMigrationCohortAssignmentRepository assignmentRepository,
IOrganizationPlanMigrationCohortRepository cohortRepository,
IOrganizationRepository organizationRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var cohort = await cohortRepository.CreateAsync(CreateTestCohort());
var assignment = await assignmentRepository.CreateAsync(CreateTestAssignment(organization, cohort));
var result = await assignmentRepository.GetByOrganizationIdAsync(organization.Id);
Assert.NotNull(result);
Assert.Equal(assignment.Id, result.Id);
Assert.Equal(organization.Id, result.OrganizationId);
Assert.Equal(cohort.Id, result.CohortId);
// Cleanup
await assignmentRepository.DeleteAsync(result);
await cohortRepository.DeleteAsync(cohort);
}
[Theory, DatabaseData]
public async Task GetByOrganizationIdAsync_NonExistentOrganization_ReturnsNull(
IOrganizationPlanMigrationCohortAssignmentRepository assignmentRepository)
{
var result = await assignmentRepository.GetByOrganizationIdAsync(Guid.NewGuid());
Assert.Null(result);
}
[Theory, DatabaseData]
public async Task ReplaceAsync_UpdatesMutableColumns_AndIgnoresImmutableOnes(
IOrganizationPlanMigrationCohortAssignmentRepository assignmentRepository,
IOrganizationPlanMigrationCohortRepository cohortRepository,
IOrganizationRepository organizationRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var otherOrganization = await organizationRepository.CreateTestOrganizationAsync(identifier: "other");
var cohort = await cohortRepository.CreateAsync(CreateTestCohort());
var otherCohort = await cohortRepository.CreateAsync(CreateTestCohort());
var assignment = await assignmentRepository.CreateAsync(CreateTestAssignment(organization, cohort));
var baseline = await assignmentRepository.GetByIdAsync(assignment.Id);
Assert.NotNull(baseline);
var baselineCreationDate = baseline.CreationDate;
// Mutate the legitimately mutable columns and ALSO attempt to mutate the immutable
// columns. The Update SP accepts those parameters but does not assign them.
var migratedAt = DateTime.UtcNow;
var churnAt = DateTime.UtcNow.AddHours(-1);
baseline.ScheduledDate = DateTime.UtcNow.AddDays(-1);
baseline.MigratedDate = migratedAt;
baseline.ChurnDiscountAppliedDate = churnAt;
baseline.RevisionDate = DateTime.UtcNow;
baseline.OrganizationId = otherOrganization.Id; // Should be ignored
baseline.CohortId = otherCohort.Id; // Should be ignored
baseline.CreationDate = DateTime.UtcNow.AddYears(-10); // Should be ignored
await assignmentRepository.ReplaceAsync(baseline);
var result = await assignmentRepository.GetByIdAsync(assignment.Id);
Assert.NotNull(result);
Assert.NotNull(result.ScheduledDate);
Assert.NotNull(result.MigratedDate);
Assert.NotNull(result.ChurnDiscountAppliedDate);
// Postgres timestamp and MySQL datetime(6) both store microsecond precision; .NET
// DateTime has 100ns ticks. Round-tripping truncates the last digit, so compare with
// LaxDateTimeComparer rather than exact equality.
Assert.Equal(migratedAt, result.MigratedDate.Value, LaxDateTimeComparer.Default);
Assert.Equal(churnAt, result.ChurnDiscountAppliedDate.Value, LaxDateTimeComparer.Default);
// Immutable columns must not have moved.
Assert.Equal(organization.Id, result.OrganizationId);
Assert.Equal(cohort.Id, result.CohortId);
Assert.Equal(baselineCreationDate, result.CreationDate, LaxDateTimeComparer.Default);
// Cleanup
await assignmentRepository.DeleteAsync(result);
await cohortRepository.DeleteAsync(cohort);
await cohortRepository.DeleteAsync(otherCohort);
}
[Theory, DatabaseData]
public async Task DeleteAsync_RemovesAssignment(
IOrganizationPlanMigrationCohortAssignmentRepository assignmentRepository,
IOrganizationPlanMigrationCohortRepository cohortRepository,
IOrganizationRepository organizationRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var cohort = await cohortRepository.CreateAsync(CreateTestCohort());
var assignment = await assignmentRepository.CreateAsync(CreateTestAssignment(organization, cohort));
await assignmentRepository.DeleteAsync(assignment);
var result = await assignmentRepository.GetByIdAsync(assignment.Id);
Assert.Null(result);
// Cleanup
await cohortRepository.DeleteAsync(cohort);
}
[Theory, DatabaseData]
public async Task DeletingOrganization_CascadesToAssignment(
IOrganizationPlanMigrationCohortAssignmentRepository assignmentRepository,
IOrganizationPlanMigrationCohortRepository cohortRepository,
IOrganizationRepository organizationRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var cohort = await cohortRepository.CreateAsync(CreateTestCohort());
var assignment = await assignmentRepository.CreateAsync(CreateTestAssignment(organization, cohort));
await organizationRepository.DeleteAsync(organization);
var result = await assignmentRepository.GetByIdAsync(assignment.Id);
Assert.Null(result);
// Cleanup
await cohortRepository.DeleteAsync(cohort);
}
[Theory, DatabaseData]
public async Task DeletingCohort_CascadesToAssignment(
IOrganizationPlanMigrationCohortAssignmentRepository assignmentRepository,
IOrganizationPlanMigrationCohortRepository cohortRepository,
IOrganizationRepository organizationRepository)
{
var organization = await organizationRepository.CreateTestOrganizationAsync();
var cohort = await cohortRepository.CreateAsync(CreateTestCohort());
var assignment = await assignmentRepository.CreateAsync(CreateTestAssignment(organization, cohort));
await cohortRepository.DeleteAsync(cohort);
var result = await assignmentRepository.GetByIdAsync(assignment.Id);
Assert.Null(result);
}
}