Skip to content

Commit

Permalink
feat: (RelayMessages): Mark emails that have been relayed. (#774) (#834)
Browse files Browse the repository at this point in the history
* feat (MessageRelay/UI):  Add indicator to UI when message has been relayed successfully.  (#774)

+ Relay recipients added to table MessageRelay

* task: test for relay controller action.

* swap Smtp4devServer usage with interface.

Smtp4devServer -> Stop, DeleteSession, DeleteSessions access modified to public to expose on interface.
  • Loading branch information
jafin committed Aug 11, 2021
1 parent c637220 commit 9c24b6e
Show file tree
Hide file tree
Showing 25 changed files with 1,102 additions and 612 deletions.
78 changes: 78 additions & 0 deletions Rnwood.Smtp4dev.Tests/Controllers/RelayMessageTests.cs
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using MimeKit;
using NSubstitute;
using Rnwood.Smtp4dev.ApiModel;
using Rnwood.Smtp4dev.Controllers;
using Rnwood.Smtp4dev.Data;
using Rnwood.Smtp4dev.Server;
using Rnwood.Smtp4dev.Tests.DBMigrations.Helpers;
using Xunit;
using Message = Rnwood.Smtp4dev.DbModel.Message;

namespace Rnwood.Smtp4dev.Tests.Controllers
{
public class MessagesControllerTests : IDisposable
{
private readonly MessagesController controller;
private readonly IMessagesRepository messagesRepository;
private readonly ISmtp4devServer server;
private readonly Smtp4devDbContext context;

public MessagesControllerTests()
{
messagesRepository = Substitute.For<IMessagesRepository>();
server = Substitute.For<ISmtp4devServer>();
controller = new MessagesController(messagesRepository, server);
var sqlLiteForTesting = new SqliteInMemory();
context = new Smtp4devDbContext(sqlLiteForTesting.ContextOptions);
InitRepo();
messagesRepository.GetMessages(Arg.Any<bool>())
.Returns(context.Messages);
messagesRepository.DbContext.Returns(context);
}

[Fact]
public void CanRelayMessageAndPersistResult()
{
// setup
var messageId = GetData().First().Id;

server.TryRelayMessage(Arg.Any<DbModel.Message>(), Arg.Any<MailboxAddress[]>()).Returns(new RelayResult(GetData().First())
{
RelayRecipients = new List<RelayRecipientResult>()
{ new RelayRecipientResult { Email = "relay@blah.com", RelayDate = DateTime.UtcNow } }
});

// act
var result = controller.RelayMessage(messageId,
new MessageRelayOptions() { OverrideRecipientAddresses = new[] { "test@foo.bar" } });

// expect ok result
result.Should().BeOfType<OkResult>();

var relay = context.MessageRelays.FirstOrDefault(mr => mr.MessageId == messageId);
//expect MessageRelay persisted.
relay.Should().NotBeNull();
}

private IEnumerable<Message> GetData()
{
return new List<Message>() { new Message() { Id = new Guid("7476cf62-03e4-4d58-93ac-1cd143ba8653") } };
}

private void InitRepo()
{
context.Messages.AddRange(GetData());
context.SaveChanges();
}

public void Dispose()
{
context?.Dispose();
}
}
}
2 changes: 2 additions & 0 deletions Rnwood.Smtp4dev.Tests/Rnwood.Smtp4dev.Tests.csproj
Expand Up @@ -13,12 +13,14 @@
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="MailKit" Version="2.13.0" />
<PackageReference Include="MedallionShell" Version="1.6.2" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
<PackageReference Include="ReflectionMagic" Version="4.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="Selenium.Support" Version="3.141.0" />
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="92.0.4515.10700" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.12" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 3 additions & 1 deletion Rnwood.Smtp4dev.Tests/TestMessagesRepository.cs
Expand Up @@ -23,7 +23,9 @@ public Task DeleteAllMessages()
return Task.CompletedTask;
}

public Task DeleteMessage(Guid id)
public Smtp4devDbContext DbContext => throw new NotImplementedException();

public Task DeleteMessage(Guid id)
{
Messages.RemoveAll(m => m.Id == id);
return Task.CompletedTask;
Expand Down
12 changes: 5 additions & 7 deletions Rnwood.Smtp4dev/ApiModel/MessageSummary.cs
@@ -1,9 +1,4 @@
using MimeKit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System;

namespace Rnwood.Smtp4dev.ApiModel
{
Expand All @@ -18,8 +13,11 @@ public MessageSummary(DbModel.Message dbMessage)
Subject = dbMessage.Subject;
AttachmentCount = dbMessage.AttachmentCount;
IsUnread = dbMessage.IsUnread;
IsRelayed = dbMessage.Relays.Count > 0;
}

public bool IsRelayed { get; set; }

public Guid Id { get; set; }

public string From { get; set; }
Expand All @@ -32,6 +30,6 @@ public MessageSummary(DbModel.Message dbMessage)

public bool IsUnread { get; set; }

string ICacheByKey.CacheKey => Id.ToString() + IsUnread.ToString();
string ICacheByKey.CacheKey => Id.ToString() + IsUnread + IsRelayed;
}
}
50 changes: 28 additions & 22 deletions Rnwood.Smtp4dev/ClientApp/src/ApiClient/MessageSummary.ts
@@ -1,24 +1,30 @@

export default class MessageSummary {
constructor(
id: string,
from: string,
to: string,
receivedDate: Date,
subject: string,
attachmentCount: number,
isUnread: boolean,
isRelayed: boolean,
) {
this.id = id;
this.from = from;
this.to = to;
this.receivedDate = receivedDate;
this.subject = subject;
this.attachmentCount = attachmentCount;
this.isUnread = isUnread;
this.isRelayed = isRelayed;
}

export default class MessageSummary {

constructor(id: string, from: string, to: string, receivedDate: Date, subject: string, attachmentCount: number, isUnread: boolean, ) {

this.id = id;
this.from = from;
this.to = to;
this.receivedDate = receivedDate;
this.subject = subject;
this.attachmentCount = attachmentCount;
this.isUnread = isUnread;
}


id: string;
from: string;
to: string;
receivedDate: Date;
subject: string;
attachmentCount: number;
isUnread: boolean;
id: string;
from: string;
to: string;
receivedDate: Date;
subject: string;
attachmentCount: number;
isUnread: boolean;
isRelayed: boolean;
}
4 changes: 2 additions & 2 deletions Rnwood.Smtp4dev/ClientApp/src/components/app/app.vue
Expand Up @@ -9,6 +9,8 @@
<script lang="ts">
import Vue from "vue";
import { Component } from "vue-property-decorator";
import '@fortawesome/fontawesome-free/css/all.css'
import '@fortawesome/fontawesome-free/js/all.js'
@Component({})
export default class AppComponent extends Vue {}
Expand All @@ -24,7 +26,5 @@ $--button-small-padding-vertical: 7px;
$--button-small-padding-horizontal: 12px;
$--input-small-padding-vertical: 7px;
@import "~element-ui/packages/theme-chalk/src/index";
</style>

0 comments on commit 9c24b6e

Please sign in to comment.