From 8f6ff028b4538ac3720fd14c429b6dc9ae9b352d Mon Sep 17 00:00:00 2001 From: Gilles TOURREAU Date: Tue, 20 Aug 2024 14:04:44 +0200 Subject: [PATCH] Remove the concept of AsAdministrator(). --- .../SqlServer.cs | 3 - .../SqlServerDatabaseExtensions.cs | 10 -- .../SqlServerDatabaseInitializer.cs | 7 +- .../SqlServerDatabaseExtensionsTest.cs | 14 +-- ... => SqlServerDatabaseInitializerDacPac.cs} | 8 +- ...rDatabaseInitializerDacPacWithLoginTest.cs | 70 ----------- ... SqlServerDatabaseInitializerDbContext.cs} | 8 +- ...tabaseInitializerDbContextWithLoginTest.cs | 112 ------------------ .../SqlServerTest.cs | 2 +- 9 files changed, 13 insertions(+), 221 deletions(-) rename tests/UnitTests.Databases.SqlServer.Tests/{SqlServerDatabaseInitializerDacPacWithoutLogin.cs => SqlServerDatabaseInitializerDacPac.cs} (87%) delete mode 100644 tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDacPacWithLoginTest.cs rename tests/UnitTests.Databases.SqlServer.Tests/{SqlServerDatabaseInitializerDbContextWithoutLogin.cs => SqlServerDatabaseInitializerDbContext.cs} (90%) delete mode 100644 tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDbContextWithLoginTest.cs diff --git a/src/UnitTests.Databases.SqlServer/SqlServer.cs b/src/UnitTests.Databases.SqlServer/SqlServer.cs index 9a3dc1c..70f3ce8 100644 --- a/src/UnitTests.Databases.SqlServer/SqlServer.cs +++ b/src/UnitTests.Databases.SqlServer/SqlServer.cs @@ -27,9 +27,6 @@ public SqlServer(string connectionString) var connectionStringMaster = new SqlConnectionStringBuilder(connectionString); connectionStringMaster.InitialCatalog = "master"; - connectionStringMaster.IntegratedSecurity = true; - connectionStringMaster.Remove("User ID"); - connectionStringMaster.Remove("Password"); this.Master = new SqlServerDatabase(this, connectionStringMaster.ToString()); } diff --git a/src/UnitTests.Databases.SqlServer/SqlServerDatabaseExtensions.cs b/src/UnitTests.Databases.SqlServer/SqlServerDatabaseExtensions.cs index b29ce00..6b71fe5 100644 --- a/src/UnitTests.Databases.SqlServer/SqlServerDatabaseExtensions.cs +++ b/src/UnitTests.Databases.SqlServer/SqlServerDatabaseExtensions.cs @@ -70,16 +70,6 @@ Type t when Array.Exists(AuthorizedNonStringTypes, at => at == t) => builder.Add return database.ExecuteNonQuery(statement); } - public static SqlServerDatabase AsAdministrator(this SqlServerDatabase database) - { - var databaseConnectionString = new SqlConnectionStringBuilder(database.ConnectionString); - - var masterConnectionString = new SqlConnectionStringBuilder(database.Server.Master.ConnectionString); - masterConnectionString.InitialCatalog = databaseConnectionString.InitialCatalog; - - return new SqlServerDatabase(database.Server, masterConnectionString.ToString()); - } - private sealed class SqlInsertStatementBuilder { private readonly string tableName; diff --git a/src/UnitTests.Databases.SqlServer/SqlServerDatabaseInitializer.cs b/src/UnitTests.Databases.SqlServer/SqlServerDatabaseInitializer.cs index cbb3b40..e4cf4e9 100644 --- a/src/UnitTests.Databases.SqlServer/SqlServerDatabaseInitializer.cs +++ b/src/UnitTests.Databases.SqlServer/SqlServerDatabaseInitializer.cs @@ -39,7 +39,7 @@ IF SUSER_ID ('{connectionStringBuilder.UserID}') IS NULL database = server.GetDatabase(connectionStringBuilder.InitialCatalog); } - ClearAllData(database.AsAdministrator()); + ClearAllData(database); return database; } @@ -58,10 +58,7 @@ public SqlServerDatabase Initialize(TContext context) { server.DeleteDatabase(connectionStringBuilder.InitialCatalog); - // Change the connection with administrator rights. - context.Database.SetConnectionString(database.AsAdministrator().ConnectionString); context.Database.EnsureCreated(); - context.Database.SetConnectionString(connectionString); if (!string.IsNullOrWhiteSpace(connectionStringBuilder.UserID)) { @@ -73,7 +70,7 @@ IF SUSER_ID ('{connectionStringBuilder.UserID}') IS NULL this.isDeployed = true; } - ClearAllData(database.AsAdministrator()); + ClearAllData(database); return database; } diff --git a/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseExtensionsTest.cs b/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseExtensionsTest.cs index f0b0919..4b8392c 100644 --- a/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseExtensionsTest.cs +++ b/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseExtensionsTest.cs @@ -11,16 +11,6 @@ public class SqlServerDatabaseExtensionsTest { private const string ConnectionString = $"Data Source=(localDB)\\posinfo-unit-tests; Initial Catalog={nameof(SqlServerDatabaseExtensionsTest)}; Integrated Security=True"; - [Fact] - public void AsAdministrator() - { - var server = new SqlServer(ConnectionString); - - var database = server.GetDatabase("TheDatabase"); - - database.AsAdministrator().ConnectionString.Should().Be("Data Source=(localDB)\\posinfo-unit-tests;Initial Catalog=TheDatabase;Integrated Security=True"); - } - [Fact] public void InsertInto_EnableIdentity() { @@ -28,7 +18,7 @@ public void InsertInto_EnableIdentity() var database = server.CreateEmptyDatabase("SqlServerDatabaseExtensionsTest"); - database.AsAdministrator().ExecuteNonQuery(@" + database.ExecuteNonQuery(@" CREATE TABLE TableTest ( Id INT NOT NULL IDENTITY(1, 1), @@ -68,7 +58,7 @@ public void InsertInto_DisableIdentity() var database = server.CreateEmptyDatabase("SqlServerDatabaseExtensionsTest"); - database.AsAdministrator().ExecuteNonQuery(@" + database.ExecuteNonQuery(@" CREATE TABLE TableTest ( Id INT NOT NULL IDENTITY(1, 1), diff --git a/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDacPacWithoutLogin.cs b/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDacPac.cs similarity index 87% rename from tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDacPacWithoutLogin.cs rename to tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDacPac.cs index d65a02f..a8cddf7 100644 --- a/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDacPacWithoutLogin.cs +++ b/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDacPac.cs @@ -1,5 +1,5 @@ //----------------------------------------------------------------------- -// +// // Copyright (c) P.O.S Informatique. All rights reserved. // //----------------------------------------------------------------------- @@ -7,13 +7,13 @@ namespace PosInformatique.UnitTests.Databases.SqlServer.Tests { [Collection("PosInformatique.UnitTests.Databases.SqlServer.Tests")] - public class SqlServerDatabaseInitializerDacPacWithoutLogin : IClassFixture + public class SqlServerDatabaseInitializerDacPac : IClassFixture { - private const string ConnectionString = $"Data Source=(localDB)\\posinfo-unit-tests; Initial Catalog={nameof(SqlServerDatabaseInitializerDacPacWithoutLogin)}; Integrated Security=True"; + private const string ConnectionString = $"Data Source=(localDB)\\posinfo-unit-tests; Initial Catalog={nameof(SqlServerDatabaseInitializerDacPac)}; Integrated Security=True"; private readonly SqlServerDatabase database; - public SqlServerDatabaseInitializerDacPacWithoutLogin(SqlServerDatabaseInitializer initializer) + public SqlServerDatabaseInitializerDacPac(SqlServerDatabaseInitializer initializer) { this.database = initializer.Initialize("UnitTests.Databases.SqlServer.Tests.DacPac.dacpac", ConnectionString); diff --git a/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDacPacWithLoginTest.cs b/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDacPacWithLoginTest.cs deleted file mode 100644 index fc279cf..0000000 --- a/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDacPacWithLoginTest.cs +++ /dev/null @@ -1,70 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) P.O.S Informatique. All rights reserved. -// -//----------------------------------------------------------------------- - -namespace PosInformatique.UnitTests.Databases.SqlServer.Tests -{ - [Collection("PosInformatique.UnitTests.Databases.SqlServer.Tests")] - public class SqlServerDatabaseInitializerDacPacWithLoginTest : IClassFixture - { - private const string ConnectionString = $"Data Source=(localDB)\\posinfo-unit-tests; Initial Catalog={nameof(SqlServerDatabaseInitializerDacPacWithLoginTest)}; User ID=ServiceAccountLogin; Password=P@ssw0rd"; - - private readonly SqlServerDatabase database; - - public SqlServerDatabaseInitializerDacPacWithLoginTest(SqlServerDatabaseInitializer initializer) - { - this.database = initializer.Initialize("UnitTests.Databases.SqlServer.Tests.DacPac.dacpac", ConnectionString); - - var table = this.database.ExecuteQuery("SELECT * FROM MyTable"); - - table.Rows.Should().BeEmpty(); - - this.database.InsertInto("MyTable", new { Id = 1, Name = "Name 1" }); - this.database.InsertInto("MyTable", new { Id = 2, Name = "Name 2" }); - } - - [Fact] - public void Test1() - { - var currentUser = this.database.ExecuteQuery("SELECT SUSER_NAME()"); - currentUser.Rows[0][0].Should().Be("ServiceAccountLogin"); - - // Check the constructor has been called - var table = this.database.ExecuteQuery("SELECT * FROM MyTable"); - - table.Rows.Should().HaveCount(2); - - table.Rows[0]["Id"].Should().Be(1); - table.Rows[0]["Name"].Should().Be("Name 1"); - - table.Rows[1]["Id"].Should().Be(2); - table.Rows[1]["Name"].Should().Be("Name 2"); - - // Insert a row which should not be use in other tests. - this.database.InsertInto("MyTable", new { Id = 99, Name = "Should not be here for the next unit test" }); - } - - [Fact] - public void Test2() - { - var currentUser = this.database.ExecuteQuery("SELECT SUSER_NAME()"); - currentUser.Rows[0][0].Should().Be("ServiceAccountLogin"); - - // Check the constructor has been called - var table = this.database.ExecuteQuery("SELECT * FROM MyTable"); - - table.Rows.Should().HaveCount(2); - - table.Rows[0]["Id"].Should().Be(1); - table.Rows[0]["Name"].Should().Be("Name 1"); - - table.Rows[1]["Id"].Should().Be(2); - table.Rows[1]["Name"].Should().Be("Name 2"); - - // Insert a row which should not be use in other tests. - this.database.InsertInto("MyTable", new { Id = 99, Name = "Should not be here for the next unit test" }); - } - } -} \ No newline at end of file diff --git a/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDbContextWithoutLogin.cs b/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDbContext.cs similarity index 90% rename from tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDbContextWithoutLogin.cs rename to tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDbContext.cs index 91cc324..42efd1c 100644 --- a/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDbContextWithoutLogin.cs +++ b/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDbContext.cs @@ -1,5 +1,5 @@ //----------------------------------------------------------------------- -// +// // Copyright (c) P.O.S Informatique. All rights reserved. // //----------------------------------------------------------------------- @@ -9,13 +9,13 @@ namespace PosInformatique.UnitTests.Databases.SqlServer.Tests using Microsoft.EntityFrameworkCore; [Collection("PosInformatique.UnitTests.Databases.SqlServer.Tests")] - public class SqlServerDatabaseInitializerDbContextWithoutLogin : IClassFixture + public class SqlServerDatabaseInitializerDbContext : IClassFixture { - private const string ConnectionString = $"Data Source=(localDB)\\posinfo-unit-tests; Initial Catalog={nameof(SqlServerDatabaseInitializerDbContextWithoutLogin)}; Integrated Security=True"; + private const string ConnectionString = $"Data Source=(localDB)\\posinfo-unit-tests; Initial Catalog={nameof(SqlServerDatabaseInitializerDbContext)}; Integrated Security=True"; private readonly SqlServerDatabase database; - public SqlServerDatabaseInitializerDbContextWithoutLogin(SqlServerDatabaseInitializer initializer) + public SqlServerDatabaseInitializerDbContext(SqlServerDatabaseInitializer initializer) { var optionsBuilder = new DbContextOptionsBuilder() .UseSqlServer(ConnectionString); diff --git a/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDbContextWithLoginTest.cs b/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDbContextWithLoginTest.cs deleted file mode 100644 index e377820..0000000 --- a/tests/UnitTests.Databases.SqlServer.Tests/SqlServerDatabaseInitializerDbContextWithLoginTest.cs +++ /dev/null @@ -1,112 +0,0 @@ -//----------------------------------------------------------------------- -// -// Copyright (c) P.O.S Informatique. All rights reserved. -// -//----------------------------------------------------------------------- - -namespace PosInformatique.UnitTests.Databases.SqlServer.Tests -{ - using Microsoft.EntityFrameworkCore; - - [Collection("PosInformatique.UnitTests.Databases.SqlServer.Tests")] - public class SqlServerDatabaseInitializerDbContextWithLoginTest : IClassFixture - { - private const string ConnectionString = $"Data Source=(localDB)\\posinfo-unit-tests; Initial Catalog={nameof(SqlServerDatabaseInitializerDbContextWithLoginTest)}; User ID=ServiceAccountLogin; Password=P@ssw0rd"; - - private readonly SqlServerDatabase database; - - public SqlServerDatabaseInitializerDbContextWithLoginTest(SqlServerDatabaseInitializer initializer) - { - var optionsBuilder = new DbContextOptionsBuilder() - .UseSqlServer(ConnectionString); - - using var context = new DbContextTest(optionsBuilder.Options); - - this.database = initializer.Initialize(context); - - this.database.AsAdministrator().ExecuteNonQuery("IF NOT EXISTS (SELECT 1 FROM [sys].[sysusers] WHERE name = 'ServiceAccountUser') CREATE USER [ServiceAccountUser] FOR LOGIN [ServiceAccountLogin]"); - this.database.AsAdministrator().ExecuteNonQuery("GRANT CONNECT TO [ServiceAccountUser]"); - this.database.AsAdministrator().ExecuteNonQuery("GRANT SELECT, INSERT ON [MyTable] TO [ServiceAccountUser]"); - - var table = this.database.ExecuteQuery("SELECT * FROM MyTable"); - - table.Rows.Should().BeEmpty(); - - this.database.InsertInto("MyTable", new { Id = 1, Name = "Name 1" }); - this.database.InsertInto("MyTable", new { Id = 2, Name = "Name 2" }); - } - - [Fact] - public void Test1() - { - var currentUser = this.database.ExecuteQuery("SELECT SUSER_NAME()"); - currentUser.Rows[0][0].Should().Be("ServiceAccountLogin"); - - // Check the constructor has been called - var table = this.database.ExecuteQuery("SELECT * FROM MyTable"); - - table.Rows.Should().HaveCount(2); - - table.Rows[0]["Id"].Should().Be(1); - table.Rows[0]["Name"].Should().Be("Name 1"); - - table.Rows[1]["Id"].Should().Be(2); - table.Rows[1]["Name"].Should().Be("Name 2"); - - // Insert a row which should not be use in other tests. - this.database.InsertInto("MyTable", new { Id = 99, Name = "Should not be here for the next unit test" }); - } - - [Fact] - public void Test2() - { - var currentUser = this.database.ExecuteQuery("SELECT SUSER_NAME()"); - currentUser.Rows[0][0].Should().Be("ServiceAccountLogin"); - - // Check the constructor has been called - var table = this.database.ExecuteQuery("SELECT * FROM MyTable"); - - table.Rows.Should().HaveCount(2); - - table.Rows[0]["Id"].Should().Be(1); - table.Rows[0]["Name"].Should().Be("Name 1"); - - table.Rows[1]["Id"].Should().Be(2); - table.Rows[1]["Name"].Should().Be("Name 2"); - - // Insert a row which should not be use in other tests. - this.database.InsertInto("MyTable", new { Id = 99, Name = "Should not be here for the next unit test" }); - } - - private sealed class DbContextTest : DbContext - { - public DbContextTest(DbContextOptions options) - : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder.Entity(t => - { - t.ToTable("MyTable"); - - t.Property(t => t.Id) - .ValueGeneratedNever(); - - t.Property(t => t.Name) - .IsRequired() - .HasMaxLength(50) - .IsUnicode(false); - }); - } - } - - private sealed class Table - { - public int Id { get; set; } - - public string Name { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/UnitTests.Databases.SqlServer.Tests/SqlServerTest.cs b/tests/UnitTests.Databases.SqlServer.Tests/SqlServerTest.cs index 1e15a97..6ac632e 100644 --- a/tests/UnitTests.Databases.SqlServer.Tests/SqlServerTest.cs +++ b/tests/UnitTests.Databases.SqlServer.Tests/SqlServerTest.cs @@ -9,7 +9,7 @@ namespace PosInformatique.UnitTests.Databases.SqlServer.Tests public class SqlServerTest { [Theory] - [InlineData("Data Source=TheServer; Initial Catalog=TheDB; User ID=TheID; Password=ThePassword", "Data Source=TheServer;Initial Catalog=master;Integrated Security=True")] + [InlineData("Data Source=TheServer; Initial Catalog=TheDB; User ID=TheID; Password=ThePassword", "Data Source=TheServer;Initial Catalog=master;User ID=TheID;Password=ThePassword")] [InlineData("Data Source=TheServer; Initial Catalog=TheDB; Integrated Security=True", "Data Source=TheServer;Initial Catalog=master;Integrated Security=True")] public void Constructor(string connectionString, string expectedMasterConnectionString) {