Skip to content

Commit 522ea57

Browse files
author
Jijie Chen
committedSep 13, 2017
Supported deleting all messages; re-organized message test cases; UI improvements
1 parent 9bd415c commit 522ea57

File tree

11 files changed

+359
-185
lines changed

11 files changed

+359
-185
lines changed
 

‎src/Papercut.Module.WebUI/Controllers/MessageController.cs ‎src/Papercut.Module.WebUI/Controllers/MessagesController.cs

+19-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ namespace Papercut.Module.WebUI.Controllers
3333
using System.Collections.Generic;
3434
using System.IO;
3535

36-
public class MessageController : ApiController
36+
using Common.Extensions;
37+
38+
public class MessagesController : ApiController
3739
{
3840
readonly MessageRepository messageRepository;
3941
readonly MimeMessageLoader messageLoader;
4042

41-
public MessageController(MessageRepository messageRepository, MimeMessageLoader messageLoader)
43+
public MessagesController(MessageRepository messageRepository, MimeMessageLoader messageLoader)
4244
{
4345
this.messageRepository = messageRepository;
4446
this.messageLoader = messageLoader;
@@ -63,6 +65,21 @@ public HttpResponseMessage GetAll(int limit = 10, int start = 0)
6365
});
6466
}
6567

68+
[HttpDelete]
69+
public HttpResponseMessage DeleteAll()
70+
{
71+
messageRepository.LoadMessages()
72+
.ForEach(msg =>
73+
{
74+
try
75+
{
76+
messageRepository.DeleteMessage(msg);
77+
}catch {}
78+
});
79+
80+
return Request.CreateResponse(HttpStatusCode.OK);
81+
}
82+
6683
[HttpGet]
6784
public HttpResponseMessage Get(string id)
6885
{

‎src/Papercut.Module.WebUI/Papercut.Module.WebUI.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
</Reference>
8383
</ItemGroup>
8484
<ItemGroup>
85-
<Compile Include="Controllers\MessageController.cs" />
85+
<Compile Include="Controllers\MessagesController.cs" />
8686
<Compile Include="Controllers\HealthController.cs" />
8787
<Compile Include="Controllers\StaticContentController.cs" />
8888
<Compile Include="Helpers\FileHelper.cs" />

‎src/Papercut.Module.WebUI/RouteConfig.cs

+14-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Papercut.Module.WebUI
44
{
5+
using System.Net.Http;
6+
using System.Web.Http.Routing;
7+
58
using Autofac;
69
using Autofac.Integration.WebApi;
710

@@ -15,23 +18,29 @@ public static void Init(HttpConfiguration config, ILifetimeScope scope)
1518

1619
config.Routes.MapHttpRoute("load all messages",
1720
"api/messages",
18-
new {controller = "Message", action = "GetAll"});
21+
new {controller = "Messages", action = "GetAll"},
22+
new { HttpMethod = new HttpMethodConstraint(HttpMethod.Get)});
23+
24+
config.Routes.MapHttpRoute("delete all messages",
25+
"api/messages",
26+
new { controller = "Messages", action = "DeleteAll" },
27+
new { HttpMethod = new HttpMethodConstraint(HttpMethod.Delete) });
1928

2029
config.Routes.MapHttpRoute("load message detail",
2130
"api/messages/{id}",
22-
new {controller = "Message", action = "Get"});
31+
new {controller = "Messages", action = "Get"});
2332

2433
config.Routes.MapHttpRoute("download section by content id",
2534
"api/messages/{messageId}/contents/{contentId}",
26-
new {controller = "Message", action = "DownloadSectionContent" });
35+
new {controller = "Messages", action = "DownloadSectionContent" });
2736

2837
config.Routes.MapHttpRoute("download section by index",
2938
"api/messages/{messageId}/sections/{index}",
30-
new {controller = "Message", action = "DownloadSection" });
39+
new {controller = "Messages", action = "DownloadSection" });
3140

3241
config.Routes.MapHttpRoute("download raw message palyload",
3342
"api/messages/{messageId}/raw",
34-
new { controller = "Message", action = "DownloadRaw" });
43+
new { controller = "Messages", action = "DownloadRaw" });
3544

3645
config.Routes.MapHttpRoute("Serve other requests as static content",
3746
"{*anything}",

‎src/Papercut.Module.WebUI/assets/css/style.css

+12-31
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,7 @@ a:focus, a:hover{
2121
padding-left: 0;
2222
}
2323

24-
/**************/
25-
/* Search bar */
26-
#search {
27-
width: 500px;
28-
}
29-
@media (max-width: 991px) {
30-
#search {
31-
width: 420px;
32-
}
33-
}
24+
3425

3526
/* http://stackoverflow.com/questions/18838964/add-bootstrap-glyphicon-to-input-box */
3627
.left-inner-addon {
@@ -62,24 +53,15 @@ a:focus, a:hover{
6253
color: red;
6354
}
6455

65-
/*******/
66-
/* Jim */
67-
.jim-well {
68-
margin-top: 20px;
56+
.nav > li > a.btn-delete-all{
57+
position: absolute;
58+
right: 0;
59+
top: 5px;
60+
display: none;
6961
}
7062

71-
.jim-well h3 {
72-
margin-top: 0;
73-
}
74-
75-
.jim-config {
76-
margin-top: 3px;
77-
padding: 5px;
78-
font-size: 0.9em;
79-
}
80-
81-
.jim-config .jim-config-item {
82-
margin-bottom: 2px;
63+
.nav > li:hover > a.btn-delete-all{
64+
display: block;
8365
}
8466

8567
/***********/
@@ -179,15 +161,13 @@ a:focus, a:hover{
179161
margin-top: 10px;
180162
}
181163

182-
.headers tbody td {
183-
width: 100%;
184-
}
185-
186-
.headers tbody th {
164+
.headers tbody td.header-name {
187165
white-space: nowrap;
188166
text-align: right;
189167
color: #666;
190168
font-weight: normal;
169+
width: 80px;
170+
padding-right: 10px;
191171
}
192172

193173
.haeders table td, .headers table th {
@@ -201,5 +181,6 @@ a:focus, a:hover{
201181
label.mail-address{
202182
display: inline-block;
203183
margin-right: 5px;
184+
margin-bottom: 0;
204185
font-weight: normal;
205186
}

‎src/Papercut.Module.WebUI/assets/index.html

+15-8
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040
<div class="row">
4141
<div class="col-md-2 col-sm-3">
4242
<ul class="nav nav-pills nav-stacked">
43-
<li>
43+
<li class="inbox">
4444
<a href="#" ng-click="backToInboxFirst()">
4545
Inbox ({{totalMessages}})
4646
</a>
47+
<a title="Delete all messages" href="#" class="btn-delete-all glyphicon glyphicon-trash" ng-click="deleteAll()"></a>
4748
</li>
4849
</ul>
4950
</div>
@@ -108,35 +109,41 @@
108109
<table>
109110
<tbody>
110111
<tr>
111-
<th>From</th>
112+
<td class="header-name">From</td>
112113
<td><span ng-if="!!preview.From[0].Name">{{preview.From[0].Name}}(</span>{{ preview.From[0].Address }}<span ng-if="!!preview.From[0].Name">)</span></td>
113114
</tr>
114115
<tr>
115-
<th>To</th>
116+
<td class="header-name">To</td>
116117
<td>
117118
<label class="mail-address" ng-repeat="to in preview.To">
118-
<span ng-if="!!to.Name">{{to.Name}}(</span> {{ to.Address }}<span ng-if="!!to.Name">)</span>
119+
<span ng-if="!!to.Name">{{to.Name}}(</span>{{ to.Address }}<span ng-if="!!to.Name">)</span>
119120
</label>
120121
</td>
121122
</tr>
122123
<tr ng-if="!!(preview.Cc && preview.Cc.length)">
123-
<th>CC</th>
124+
<td class="header-name">CC</td>
124125
<td>
125126
<label class="mail-address" ng-repeat="c in preview.Cc">
126127
<span ng-if="!!c.Name">{{c.Name}}(</span> {{ c.Address }}<span ng-if="!!c.Name">)</span>
127128
</label>
128129
</td>
129130
</tr>
130131
<tr ng-if="!!(preview.BCc && preview.BCc.length)">
131-
<th>BCC</th>
132+
<td class="header-name">BCC</td>
132133
<td>
133134
<label class="mail-address" ng-repeat="b in preview.BCc">
134135
<span ng-if="!!b.Name">{{b.Name}}(</span> {{ b.Address }}<span ng-if="!!b.Name">)</span>
135136
</label>
136137
</td>
137138
</tr>
139+
<tr ng-if="!!preview.Date">
140+
<td class="header-name">Date</td>
141+
<td>
142+
{{preview.Date}}
143+
</td>
144+
</tr>
138145
<tr>
139-
<th>Subject</th>
146+
<td class="header-name">Subject</td>
140147
<td><strong>{{ preview.Subject }}</strong></td>
141148
</tr>
142149
</tbody>
@@ -176,7 +183,7 @@
176183
<tr ng-repeat="section in preview.Sections">
177184
<td>{{section.MediaType}}</td>
178185
<td>{{section.FileName}}</td>
179-
<td><a title="Click to download and save section content" ng-href="/api/messages/{{preview.Id}}/sections/{{$index}}" target="_blank" class="glyphicon glyphicon-download-alt"></a></td>
186+
<td><a title="Download and save section content" ng-href="/api/messages/{{preview.Id}}/sections/{{$index}}" target="_blank" class="glyphicon glyphicon-download-alt"></a></td>
180187
</tr>
181188
</tbody>
182189
</table>

‎src/Papercut.Module.WebUI/assets/js/controllers.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ papercutApp.controller('MailCtrl', function ($scope, $http, $sce, $timeout, $int
5454
url += "?limit=" + $scope.itemsPerPage;
5555
}
5656

57-
$http.get(url).success(function (data) {
58-
$scope.messages = data.Messages;
59-
$scope.totalMessages = data.TotalMessageCount;
60-
$scope.countMessages = data.Messages.length;
57+
$http.get(url).then(function (resp) {
58+
$scope.messages = resp.data.Messages;
59+
$scope.totalMessages = resp.data.TotalMessageCount;
60+
$scope.countMessages = resp.data.Messages.length;
6161
$scope.startMessages = $scope.startIndex;
6262
e.done();
6363
});
@@ -94,17 +94,30 @@ papercutApp.controller('MailCtrl', function ($scope, $http, $sce, $timeout, $int
9494
$scope.refresh();
9595
};
9696

97+
$scope.deleteAll = function () {
98+
if(!window.confirm('Are you sure to delete all messages?')){
99+
return;
100+
}
101+
102+
$http.delete('/api/messages').finally(function () {
103+
$scope.refresh();
104+
});
105+
};
106+
97107
$scope.selectMessage = function (message) {
98108
if ($scope.cache[message.Id]) {
99109
$scope.preview = $scope.cache[message.Id];
100110
} else {
101111
$scope.preview = message;
102112
var e = startEvent("Loading message", message.Id, "glyphicon-download-alt");
103-
$http.get('/api/messages/' + message.Id).success(function (data) {
104-
$scope.cache[message.Id] = data;
113+
$http.get('/api/messages/' + message.Id).then(function (resp) {
114+
$scope.cache[message.Id] = resp.data;
115+
116+
resp.data.previewHTML = $sce.trustAsHtml(resp.data.HtmlBody);
117+
$scope.preview = resp.data;
105118

106-
data.previewHTML = $sce.trustAsHtml(data.HtmlBody);
107-
$scope.preview = data;
119+
var dateHeader = resp.data.Headers.find(function(h){return h.Name === 'Date'});
120+
$scope.preview.Date = dateHeader === null ? null : dateHeader.Value;
108121
e.done();
109122
});
110123
}

‎test/Papercut.Module.WebUI.Tests/Base/ApiTestBase.cs

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ protected HttpResponseMessage Get(string uri)
8686
return Client.GetAsync(uri).Result;
8787
}
8888

89+
protected HttpResponseMessage Delete(string uri)
90+
{
91+
return Client.DeleteAsync(uri).Result;
92+
}
93+
8994
protected T Get<T>(string uri)
9095
{
9196
return Get(uri).Content.ReadAsAsync<T>().Result;

‎test/Papercut.Module.WebUI.Tests/MessageFacts/LoadMessageFacts.cs

-130
Original file line numberDiff line numberDiff line change
@@ -184,136 +184,6 @@ public void ShouldContainHeadersInMessageDetail()
184184
Assert.AreEqual("extended value", headers.First(h => h.Name == "X-Extended").Value);
185185
}
186186

187-
[Test, Order(5)]
188-
public void ShouldLoadDeatailWithSections()
189-
{
190-
var existedMail = new MimeMessage
191-
{
192-
Body = new Multipart
193-
{
194-
new MimePart(new ContentType("image", "jpeg") {Charset = Encoding.UTF8.EncodingName})
195-
{
196-
FileName = "sample.pdf",
197-
ContentId = Guid.Empty.ToString()
198-
}
199-
}
200-
};
201-
this._messageRepository.SaveMessage(fs => existedMail.WriteTo(fs));
202-
203-
var messageId = Get<MessageListResponse>("/api/messages").Messages.First().Id;
204-
205-
var detail = Get<MimeMessageEntry.DetailDto>($"/api/messages/{messageId}");
206-
Assert.AreEqual(messageId, detail.Id);
207-
208-
var sections = detail.Sections;
209-
Assert.AreEqual(1, sections.Count);
210-
Assert.AreEqual(Guid.Empty.ToString(), sections.First().Id);
211-
Assert.AreEqual("image/jpeg", sections.First().MediaType);
212-
Assert.AreEqual("sample.pdf", sections.First().FileName);
213-
}
214-
215-
[Test, Order(6)]
216-
public void ShouldDownloadSectionByIndex()
217-
{
218-
var existedMail = new MimeMessage
219-
{
220-
Body = new Multipart
221-
{
222-
new MimePart(new ContentType("image", "jpeg") {Charset = Encoding.UTF8.EncodingName})
223-
{
224-
FileName = "sample.pdf",
225-
ContentObject = new ContentObject(
226-
new MemoryStream(Encoding.UTF8.GetBytes("Content")), ContentEncoding.Binary)
227-
}
228-
}
229-
};
230-
this._messageRepository.SaveMessage(fs => existedMail.WriteTo(fs));
231-
232-
var messageId = Get<MessageListResponse>("/api/messages").Messages.First().Id;
233-
234-
var response = Get($"/api/messages/{messageId}/sections/0");
235-
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
236-
237-
var disposition = response.Content.Headers.ContentDisposition;
238-
Assert.AreEqual(DispositionTypeNames.Attachment, disposition.DispositionType);
239-
Assert.AreEqual("sample.pdf", disposition.FileName);
240-
Assert.AreEqual("image/jpeg", response.Content.Headers.ContentType.MediaType);
241-
}
242-
243-
[Test, Order(7)]
244-
public void ShouldDownloadSectionByContentId()
245-
{
246-
var contentId = Guid.NewGuid().ToString();
247-
var existedMail = new MimeMessage
248-
{
249-
Body = new Multipart
250-
{
251-
new MimePart(new ContentType("image", "jpeg") {Charset = Encoding.UTF8.EncodingName})
252-
{
253-
FileName = "sample.pdf",
254-
ContentId = contentId,
255-
ContentObject = new ContentObject(
256-
new MemoryStream(Encoding.UTF8.GetBytes("Content")), ContentEncoding.Binary)
257-
}
258-
}
259-
};
260-
this._messageRepository.SaveMessage(fs => existedMail.WriteTo(fs));
261-
262-
var messageId = Get<MessageListResponse>("/api/messages").Messages.First().Id;
263-
264-
var response = Get($"/api/messages/{messageId}/contents/{contentId}");
265-
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
266-
267-
var disposition = response.Content.Headers.ContentDisposition;
268-
Assert.AreEqual(DispositionTypeNames.Attachment, disposition.DispositionType);
269-
Assert.AreEqual("sample.pdf", disposition.FileName);
270-
Assert.AreEqual("image/jpeg", response.Content.Headers.ContentType.MediaType);
271-
}
272-
273-
[Test, Order(8)]
274-
public void ShouldDownloadRawMessage()
275-
{
276-
var existedMail = new MimeMessage(
277-
new [] { new MailboxAddress("from@from.com") },
278-
new[] { new MailboxAddress("to@to.com") },
279-
"Sample email",
280-
new Multipart
281-
{
282-
new MimePart(new ContentType("text", "html") {Charset = Encoding.UTF8.EncodingName})
283-
{
284-
ContentObject = new ContentObject(new MemoryStream(Encoding.UTF8.GetBytes("Content example")), ContentEncoding.Binary)
285-
}
286-
});
287-
var savePath = this._messageRepository.SaveMessage(fs => existedMail.WriteTo(fs));
288-
var messageId = Path.GetFileName(savePath);
289-
290-
var response = Get($"/api/messages/{messageId}/raw");
291-
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
292-
293-
var disposition = response.Content.Headers.ContentDisposition;
294-
Assert.AreEqual(DispositionTypeNames.Attachment, disposition.DispositionType);
295-
Assert.AreEqual(messageId, disposition.FileName);
296-
297-
298-
MimeMessage downloadMessage;
299-
using (var raw = response.Content.ReadAsStreamAsync().Result)
300-
{
301-
downloadMessage = MimeMessage.Load(ParserOptions.Default, raw);
302-
}
303-
Assert.AreEqual("from@from.com", ((MailboxAddress) downloadMessage.From.First()).Address);
304-
Assert.AreEqual("to@to.com", ((MailboxAddress)downloadMessage.To.First()).Address);
305-
Assert.AreEqual("Sample email", downloadMessage.Subject);
306-
307-
using (var ms = new MemoryStream())
308-
{
309-
var bodyContent = (downloadMessage.BodyParts.Single() as MimePart).ContentObject;
310-
bodyContent.DecodeTo(ms);
311-
ms.Seek(0, SeekOrigin.Begin);
312-
313-
Assert.AreEqual("Content example", new StreamReader(ms).ReadToEnd());
314-
}
315-
}
316-
317187
class MessageListResponse
318188
{
319189
public MessageListResponse()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Papercut
2+
//
3+
// Copyright © 2008 - 2012 Ken Robertson
4+
// Copyright © 2013 - 2017 Jaben Cargman
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
19+
namespace Papercut.Module.WebUI.Test.MessageFacts
20+
{
21+
using System;
22+
using System.Collections.Generic;
23+
using System.IO;
24+
using System.Linq;
25+
using System.Net;
26+
using System.Net.Mime;
27+
using System.Text;
28+
using System.Threading;
29+
30+
using Autofac;
31+
using Base;
32+
using Message;
33+
using MimeKit;
34+
using Models;
35+
36+
using NUnit.Framework;
37+
38+
using ContentType = MimeKit.ContentType;
39+
40+
public class MessageMetaFacts : ApiTestBase
41+
{
42+
readonly MessageRepository _messageRepository;
43+
44+
public MessageMetaFacts()
45+
{
46+
this._messageRepository = Scope.Resolve<MessageRepository>();
47+
}
48+
49+
[Test, Order(1)]
50+
public void ShouldDeleteAllMessages()
51+
{
52+
CreateMessage();
53+
CreateMessage();
54+
55+
var response = Delete("/api/messages");
56+
57+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
58+
59+
var allMessages = _messageRepository.LoadMessages();
60+
Assert.AreEqual(0, allMessages.Count);
61+
}
62+
63+
[Test, Order(2)]
64+
public void ShouldDownloadRawMessage()
65+
{
66+
var savePath = CreateMessage();
67+
var messageId = Path.GetFileName(savePath);
68+
69+
var response = Get($"/api/messages/{messageId}/raw");
70+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
71+
72+
var disposition = response.Content.Headers.ContentDisposition;
73+
Assert.AreEqual(DispositionTypeNames.Attachment, disposition.DispositionType);
74+
Assert.AreEqual(messageId, disposition.FileName);
75+
76+
77+
MimeMessage downloadMessage;
78+
using (var raw = response.Content.ReadAsStreamAsync().Result)
79+
{
80+
downloadMessage = MimeMessage.Load(ParserOptions.Default, raw);
81+
}
82+
Assert.AreEqual("from@from.com", ((MailboxAddress) downloadMessage.From.First()).Address);
83+
Assert.AreEqual("to@to.com", ((MailboxAddress)downloadMessage.To.First()).Address);
84+
Assert.AreEqual("Sample email", downloadMessage.Subject);
85+
86+
using (var ms = new MemoryStream())
87+
{
88+
var bodyContent = (downloadMessage.BodyParts.Single() as MimePart).ContentObject;
89+
bodyContent.DecodeTo(ms);
90+
ms.Seek(0, SeekOrigin.Begin);
91+
92+
Assert.AreEqual("Content example", new StreamReader(ms).ReadToEnd());
93+
}
94+
}
95+
96+
private string CreateMessage()
97+
{
98+
var existedMail = new MimeMessage(
99+
new[] { new MailboxAddress("from@from.com") },
100+
new[] { new MailboxAddress("to@to.com") },
101+
"Sample email",
102+
new Multipart
103+
{
104+
new MimePart(new ContentType("text", "html") {Charset = Encoding.UTF8.EncodingName})
105+
{
106+
ContentObject = new ContentObject(new MemoryStream(Encoding.UTF8.GetBytes("Content example")), ContentEncoding.Binary)
107+
}
108+
});
109+
110+
return this._messageRepository.SaveMessage(fs => existedMail.WriteTo(fs));
111+
}
112+
113+
class MessageListResponse
114+
{
115+
public MessageListResponse()
116+
{
117+
Messages = new List<MimeMessageEntry.RefDto>();
118+
}
119+
120+
public int TotalMessageCount { get; set; }
121+
public List<MimeMessageEntry.RefDto> Messages { get; set; }
122+
}
123+
}
124+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Papercut
2+
//
3+
// Copyright © 2008 - 2012 Ken Robertson
4+
// Copyright © 2013 - 2017 Jaben Cargman
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
19+
namespace Papercut.Module.WebUI.Test.MessageFacts
20+
{
21+
using System;
22+
using System.Collections.Generic;
23+
using System.IO;
24+
using System.Linq;
25+
using System.Net;
26+
using System.Net.Mime;
27+
using System.Text;
28+
using System.Threading;
29+
30+
using Autofac;
31+
using Base;
32+
using Message;
33+
using MimeKit;
34+
using Models;
35+
36+
using NUnit.Framework;
37+
38+
using ContentType = MimeKit.ContentType;
39+
40+
public class MessageSectionsFacts : ApiTestBase
41+
{
42+
readonly MessageRepository _messageRepository;
43+
44+
public MessageSectionsFacts()
45+
{
46+
this._messageRepository = Scope.Resolve<MessageRepository>();
47+
}
48+
49+
[Test, Order(1)]
50+
public void ShouldLoadDeatailWithSections()
51+
{
52+
var existedMail = new MimeMessage
53+
{
54+
Body = new Multipart
55+
{
56+
new MimePart(new ContentType("image", "jpeg") {Charset = Encoding.UTF8.EncodingName})
57+
{
58+
FileName = "sample.pdf",
59+
ContentId = Guid.Empty.ToString()
60+
}
61+
}
62+
};
63+
this._messageRepository.SaveMessage(fs => existedMail.WriteTo(fs));
64+
65+
var messageId = Get<MessageListResponse>("/api/messages").Messages.First().Id;
66+
67+
var detail = Get<MimeMessageEntry.DetailDto>($"/api/messages/{messageId}");
68+
Assert.AreEqual(messageId, detail.Id);
69+
70+
var sections = detail.Sections;
71+
Assert.AreEqual(1, sections.Count);
72+
Assert.AreEqual(Guid.Empty.ToString(), sections.First().Id);
73+
Assert.AreEqual("image/jpeg", sections.First().MediaType);
74+
Assert.AreEqual("sample.pdf", sections.First().FileName);
75+
}
76+
77+
[Test, Order(2)]
78+
public void ShouldDownloadSectionByIndex()
79+
{
80+
var existedMail = new MimeMessage
81+
{
82+
Body = new Multipart
83+
{
84+
new MimePart(new ContentType("image", "jpeg") {Charset = Encoding.UTF8.EncodingName})
85+
{
86+
FileName = "sample.pdf",
87+
ContentObject = new ContentObject(
88+
new MemoryStream(Encoding.UTF8.GetBytes("Content")), ContentEncoding.Binary)
89+
}
90+
}
91+
};
92+
this._messageRepository.SaveMessage(fs => existedMail.WriteTo(fs));
93+
94+
var messageId = Get<MessageListResponse>("/api/messages").Messages.First().Id;
95+
96+
var response = Get($"/api/messages/{messageId}/sections/0");
97+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
98+
99+
var disposition = response.Content.Headers.ContentDisposition;
100+
Assert.AreEqual(DispositionTypeNames.Attachment, disposition.DispositionType);
101+
Assert.AreEqual("sample.pdf", disposition.FileName);
102+
Assert.AreEqual("image/jpeg", response.Content.Headers.ContentType.MediaType);
103+
}
104+
105+
[Test, Order(3)]
106+
public void ShouldDownloadSectionByContentId()
107+
{
108+
var contentId = Guid.NewGuid().ToString();
109+
var existedMail = new MimeMessage
110+
{
111+
Body = new Multipart
112+
{
113+
new MimePart(new ContentType("image", "jpeg") {Charset = Encoding.UTF8.EncodingName})
114+
{
115+
FileName = "sample.pdf",
116+
ContentId = contentId,
117+
ContentObject = new ContentObject(
118+
new MemoryStream(Encoding.UTF8.GetBytes("Content")), ContentEncoding.Binary)
119+
}
120+
}
121+
};
122+
this._messageRepository.SaveMessage(fs => existedMail.WriteTo(fs));
123+
124+
var messageId = Get<MessageListResponse>("/api/messages").Messages.First().Id;
125+
126+
var response = Get($"/api/messages/{messageId}/contents/{contentId}");
127+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
128+
129+
var disposition = response.Content.Headers.ContentDisposition;
130+
Assert.AreEqual(DispositionTypeNames.Attachment, disposition.DispositionType);
131+
Assert.AreEqual("sample.pdf", disposition.FileName);
132+
Assert.AreEqual("image/jpeg", response.Content.Headers.ContentType.MediaType);
133+
}
134+
135+
class MessageListResponse
136+
{
137+
public MessageListResponse()
138+
{
139+
Messages = new List<MimeMessageEntry.RefDto>();
140+
}
141+
142+
public int TotalMessageCount { get; set; }
143+
public List<MimeMessageEntry.RefDto> Messages { get; set; }
144+
}
145+
}
146+
}

‎test/Papercut.Module.WebUI.Tests/Papercut.Module.WebUI.Tests.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
<ItemGroup>
7373
<Compile Include="Base\ApiTestBase.cs" />
7474
<Compile Include="Base\ServerPathTemplateProviderService.cs" />
75+
<Compile Include="MessageFacts\MessageMetaFacts.cs" />
76+
<Compile Include="MessageFacts\MessageSectionsFacts.cs" />
7577
<Compile Include="MessageFacts\LoadMessageFacts.cs" />
7678
<Compile Include="WebServerFacts\WebUiWebServerApiFact.cs" />
7779
<Compile Include="Properties\AssemblyInfo.cs" />

0 commit comments

Comments
 (0)
Please sign in to comment.