|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.AspNetCore.Builder; |
| 5 | +using Microsoft.Data.Sqlite; |
| 6 | +using Microsoft.EntityFrameworkCore; |
| 7 | +using Microsoft.EntityFrameworkCore.Diagnostics; |
| 8 | +using Microsoft.Extensions.Configuration; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | + |
| 11 | +namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test; |
| 12 | + |
| 13 | +public class VersionThreeSchemaTest : IClassFixture<ScratchDatabaseFixture> |
| 14 | +{ |
| 15 | + private readonly ApplicationBuilder _builder; |
| 16 | + |
| 17 | + public VersionThreeSchemaTest(ScratchDatabaseFixture fixture) |
| 18 | + { |
| 19 | + var services = new ServiceCollection(); |
| 20 | + |
| 21 | + services |
| 22 | + .AddSingleton<IConfiguration>(new ConfigurationBuilder().Build()) |
| 23 | + .AddDbContext<VersionTwoDbContext>(o => |
| 24 | + o.UseSqlite(fixture.Connection) |
| 25 | + .ConfigureWarnings(b => b.Log(CoreEventId.ManyServiceProvidersCreatedWarning))) |
| 26 | + .AddIdentity<IdentityUser, IdentityRole>(o => |
| 27 | + { |
| 28 | + // MaxKeyLength does not need to be set in version 3 |
| 29 | + o.Stores.SchemaVersion = IdentitySchemaVersions.Version3; |
| 30 | + }) |
| 31 | + .AddEntityFrameworkStores<VersionTwoDbContext>(); |
| 32 | + |
| 33 | + services.AddLogging(); |
| 34 | + |
| 35 | + _builder = new ApplicationBuilder(services.BuildServiceProvider()); |
| 36 | + var scope = _builder.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope(); |
| 37 | + var db = scope.ServiceProvider.GetRequiredService<VersionTwoDbContext>(); |
| 38 | + db.Database.EnsureCreated(); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void EnsureDefaultSchema() |
| 43 | + { |
| 44 | + using var scope = _builder.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope(); |
| 45 | + var db = scope.ServiceProvider.GetRequiredService<VersionTwoDbContext>(); |
| 46 | + VerifyVersion3Schema(db); |
| 47 | + } |
| 48 | + |
| 49 | + internal static void VerifyVersion3Schema(DbContext dbContext) |
| 50 | + { |
| 51 | + using var sqlConn = (SqliteConnection)dbContext.Database.GetDbConnection(); |
| 52 | + sqlConn.Open(); |
| 53 | + Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUsers", "Id", "UserName", "Email", "PasswordHash", "SecurityStamp", |
| 54 | + "EmailConfirmed", "PhoneNumber", "PhoneNumberConfirmed", "TwoFactorEnabled", "LockoutEnabled", |
| 55 | + "LockoutEnd", "AccessFailedCount", "ConcurrencyStamp", "NormalizedUserName", "NormalizedEmail")); |
| 56 | + Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetRoles", "Id", "Name", "NormalizedName", "ConcurrencyStamp")); |
| 57 | + Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserRoles", "UserId", "RoleId")); |
| 58 | + Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserClaims", "Id", "UserId", "ClaimType", "ClaimValue")); |
| 59 | + Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserLogins", "UserId", "ProviderKey", "LoginProvider", "ProviderDisplayName")); |
| 60 | + Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserTokens", "UserId", "LoginProvider", "Name", "Value")); |
| 61 | + Assert.True(DbUtil.VerifyColumns(sqlConn, "AspNetUserPasskeys", "UserId", "CredentialId", "PublicKey", "Name", "CreatedAt", |
| 62 | + "SignCount", "Transports", "IsUserVerified", "IsBackupEligible", "IsBackedUp", "AttestationObject", |
| 63 | + "ClientDataJson")); |
| 64 | + |
| 65 | + Assert.True(DbUtil.VerifyMaxLength(dbContext, "AspNetUsers", 256, "UserName", "Email", "NormalizedUserName", "NormalizedEmail", "PhoneNumber")); |
| 66 | + Assert.True(DbUtil.VerifyMaxLength(dbContext, "AspNetRoles", 256, "Name", "NormalizedName")); |
| 67 | + Assert.True(DbUtil.VerifyMaxLength(dbContext, "AspNetUserLogins", 128, "LoginProvider", "ProviderKey")); |
| 68 | + Assert.True(DbUtil.VerifyMaxLength(dbContext, "AspNetUserTokens", 128, "LoginProvider", "Name")); |
| 69 | + |
| 70 | + DbUtil.VerifyIndex(sqlConn, "AspNetRoles", "RoleNameIndex", isUnique: true); |
| 71 | + DbUtil.VerifyIndex(sqlConn, "AspNetUsers", "UserNameIndex", isUnique: true); |
| 72 | + DbUtil.VerifyIndex(sqlConn, "AspNetUsers", "EmailIndex"); |
| 73 | + } |
| 74 | +} |
0 commit comments