-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix(general): Fix FK error when deleting a session for which a messag…
- Loading branch information
Showing
8 changed files
with
333 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using MimeKit; | ||
using NSubstitute; | ||
using Rnwood.Smtp4dev.Controllers; | ||
using Rnwood.Smtp4dev.Data; | ||
using Rnwood.Smtp4dev.Hubs; | ||
using Rnwood.Smtp4dev.Server; | ||
using Rnwood.Smtp4dev.Server.Settings; | ||
using Rnwood.Smtp4dev.Tests.DBMigrations.Helpers; | ||
using Rnwood.SmtpServer; | ||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Rnwood.Smtp4dev.Tests.Data | ||
{ | ||
public class DataModelTests | ||
{ | ||
[Fact] | ||
public async Task CanDeleteSessionWhenMessageExist() | ||
{ | ||
var sqlLiteForTesting = new SqliteInMemory(); | ||
var context = new Smtp4devDbContext(sqlLiteForTesting.ContextOptions); | ||
|
||
DbModel.Session session = new DbModel.Session(); | ||
context.Add(session); | ||
DbModel.Message testMessage1 = await GetTestMessage("Message subject1"); | ||
testMessage1.Session = session; | ||
|
||
context.Add(testMessage1); | ||
await context.SaveChangesAsync(); | ||
|
||
context.Remove(session); | ||
await context.SaveChangesAsync(); | ||
|
||
Assert.Null(testMessage1.Session); | ||
} | ||
|
||
private static async Task<DbModel.Message> GetTestMessage(string subject, string from = "from@from.com", string to = "to@to.com") | ||
{ | ||
MimeMessage mimeMessage = new MimeMessage(); | ||
mimeMessage.From.Add(InternetAddress.Parse(from)); | ||
mimeMessage.To.Add(InternetAddress.Parse(to)); | ||
|
||
mimeMessage.Subject = subject; | ||
BodyBuilder bodyBuilder = new BodyBuilder(); | ||
bodyBuilder.HtmlBody = "<html>Hi</html>"; | ||
bodyBuilder.TextBody = "Hi"; | ||
|
||
mimeMessage.Body = bodyBuilder.ToMessageBody(); | ||
|
||
MemoryMessageBuilder memoryMessageBuilder = new MemoryMessageBuilder(); | ||
memoryMessageBuilder.Recipients.Add(to); | ||
memoryMessageBuilder.From = from; | ||
memoryMessageBuilder.ReceivedDate = DateTime.Now; | ||
using (var messageData = await memoryMessageBuilder.WriteData()) | ||
{ | ||
mimeMessage.WriteTo(messageData); | ||
} | ||
|
||
IMessage message = await memoryMessageBuilder.ToMessage(); | ||
|
||
var dbMessage = await new MessageConverter().ConvertAsync(message, [to]); | ||
dbMessage.Mailbox = new DbModel.Mailbox { Name = MailboxOptions.DEFAULTNAME }; | ||
|
||
return dbMessage; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
Rnwood.Smtp4dev/Migrations/20240515191815_FixSessionMessageCascasing.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
Rnwood.Smtp4dev/Migrations/20240515191815_FixSessionMessageCascasing.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
#nullable disable | ||
|
||
namespace Rnwood.Smtp4dev.Migrations | ||
{ | ||
/// <inheritdoc /> | ||
public partial class FixSessionMessageCascasing : Migration | ||
{ | ||
/// <inheritdoc /> | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropForeignKey( | ||
name: "FK_Messages_Sessions_SessionId", | ||
table: "Messages"); | ||
|
||
migrationBuilder.AddForeignKey( | ||
name: "FK_Messages_Sessions_SessionId", | ||
table: "Messages", | ||
column: "SessionId", | ||
principalTable: "Sessions", | ||
principalColumn: "Id", | ||
onDelete: ReferentialAction.SetNull); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropForeignKey( | ||
name: "FK_Messages_Sessions_SessionId", | ||
table: "Messages"); | ||
|
||
migrationBuilder.AddForeignKey( | ||
name: "FK_Messages_Sessions_SessionId", | ||
table: "Messages", | ||
column: "SessionId", | ||
principalTable: "Sessions", | ||
principalColumn: "Id"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters