-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommentService.Tests.cs
More file actions
332 lines (262 loc) · 16.3 KB
/
Copy pathCommentService.Tests.cs
File metadata and controls
332 lines (262 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System;
using Comments.Models;
using Comments.Services;
using Comments.Test.Infrastructure;
using Xunit;
using Shouldly;
using System.Linq;
using System.Threading.Tasks;
using Comments.ViewModels;
using Comment = Comments.Models.Comment;
using Location = Comments.Models.Location;
namespace Comments.Test.UnitTests
{
public class CommentServiceTests : Infrastructure.TestBase
{
[Fact]
public void Comments_Get()
{
// Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
var commentText = Guid.NewGuid().ToString();
var userId = Guid.Empty.ToString();
var locationId = AddLocation(sourceURI);
var commentId = AddComment(locationId, commentText, createdByUserId: userId);
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var context = new ConsultationsContext(_options, userService, _fakeEncryption);
//var submitService = new SubmitService(context, userService, _consultationService);
var commentService = new CommentService(context, userService, _consultationService, _fakeHttpContextAccessor);
// Act
var (comment, _) = commentService.GetComment(commentId);
//Assert
comment.CommentText.ShouldBe(commentText);
}
[Fact]
public void Comments_Get_Record_Not_Found()
{
// Arrange
ResetDatabase();
var userId = Guid.Empty.ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var context = new ConsultationsContext(_options, userService, _fakeEncryption);
var commentService = new CommentService(context, userService, _consultationService, _fakeHttpContextAccessor);
// Act
var viewModel = commentService.GetComment(1);
//Assert
viewModel.validate.NotFound.ShouldBeTrue();
viewModel.comment.ShouldBeNull();
}
[Fact]
public void Comments_CanBeEdited()
{
//Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
var commentText = Guid.NewGuid().ToString();
var userId = Guid.Empty.ToString();
var locationId = AddLocation(sourceURI);
var commentId = AddComment(locationId, commentText, createdByUserId: userId);
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var context = new ConsultationsContext(_options, userService, _fakeEncryption);
var commentService = new CommentService(context, userService, _consultationService, _fakeHttpContextAccessor);
var viewModel = commentService.GetComment(commentId);
var updatedCommentText = Guid.NewGuid().ToString();
viewModel.comment.CommentText = updatedCommentText;
//Act
commentService.EditComment(commentId, viewModel.comment);
viewModel = commentService.GetComment(commentId);
//Assert
viewModel.comment.CommentText.ShouldBe(updatedCommentText);
}
[Fact]
public void Comments_CanBeDeleted()
{
//Arrange
ResetDatabase();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
var commentText = Guid.NewGuid().ToString();
var userId = Guid.Empty.ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var locationId = AddLocation(sourceURI);
var commentId = AddComment(locationId, commentText, createdByUserId: userId);
var context = new ConsultationsContext(_options, userService, _fakeEncryption);
//var submitService = new SubmitService(context, userService, _consultationService);
var commentService = new CommentService(context, userService, _consultationService, _fakeHttpContextAccessor);
//Act
commentService.DeleteComment(commentId);
var viewModel = commentService.GetComment(commentId);
//Assert
viewModel.comment.ShouldBeNull();
viewModel.validate.NotFound.ShouldBeTrue();
}
[Fact]
public async Task Comments_CanBeCreated()
{
//Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
var locationId = AddLocation(sourceURI);
var userId = Guid.Empty.ToString();
var commentText = Guid.NewGuid().ToString();
var location = new Location(sourceURI, null, null, null, null, null, null, null, null, null, null, null);
var comment = new Comment(locationId, userId, commentText, userId, location, 1, null);
var viewModel = new ViewModels.Comment(location, comment);
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var context = new ConsultationsContext(_options, userService, _fakeEncryption);
//var submitService = new SubmitService(context, userService, _consultationService);
var commentService = new CommentService(context, userService, _consultationService, _fakeHttpContextAccessor);
//Act
var result = await commentService.CreateComment(viewModel);
//Assert
result.comment.CommentText.ShouldBe(commentText);
}
[Fact]
public async Task No_Comments_returned_when_not_logged_in()
{
// Arrange
ResetDatabase();
var context = new ConsultationsContext(_options, _fakeUserService, _fakeEncryption);
//var submitService = new SubmitService(context, _fakeUserService, _consultationService);
var commentService = new CommentService(new ConsultationsContext(_options, _fakeUserService, _fakeEncryption), FakeUserService.Get(isAuthenticated: false), _consultationService, _fakeHttpContextAccessor);
// Act
var viewModel = await commentService.GetCommentsAndQuestions("consultations://./consultation/1/document/1/chapter/introduction", _urlHelper);
//Assert
viewModel.IsAuthorised.ShouldBeFalse();
viewModel.Comments.Count().ShouldBe(0);
viewModel.Questions.Count().ShouldBe(0);
}
[Fact]
public async Task Only_own_Comments_returned_when_logged_in()
{
// Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var userId = Guid.NewGuid().ToString();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var context = new ConsultationsContext(_options, userService, _fakeEncryption);
//var submitService = new SubmitService(context, userService, _consultationService);
var commentService = new CommentService(new ConsultationsContext(_options, userService, _fakeEncryption), userService, _consultationService, _fakeHttpContextAccessor);
var locationId = AddLocation(sourceURI);
var expectedCommentId = AddComment(locationId, "current user's comment", createdByUserId: userId);
AddComment(locationId, "another user's comment", createdByUserId: Guid.NewGuid().ToString());
// Act
var viewModel = await commentService.GetCommentsAndQuestions("consultations://./consultation/1/document/1/chapter/introduction", _urlHelper);
//Assert
viewModel.Comments.Single().CommentId.ShouldBe(expectedCommentId);
}
[Fact]
public async Task CommentsQuestionsAndAnswers_OnlyReturnOwnComments()
{
// Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var URI = "consultations://./consultation/1/document/1/chapter/intro";
var someText = Guid.NewGuid().ToString();
var createdByUserId = Guid.Empty.ToString();
var anotherUserId = Guid.NewGuid().ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: createdByUserId);
AddCommentsAndQuestionsAndAnswers(URI, "My Comment", someText, someText, createdByUserId, (int)StatusName.Draft, _context);
AddCommentsAndQuestionsAndAnswers(URI, "Another Users Comment", someText, someText, anotherUserId, (int)StatusName.Draft, _context);
var context = new ConsultationsContext(_options, userService, _fakeEncryption);
//var submitService = new SubmitService(context, _fakeUserService, _consultationService);
var commentService = new CommentService(context, _fakeUserService, _consultationService, _fakeHttpContextAccessor);
// Act
var viewModel = await commentService.GetCommentsAndQuestions(URI, _urlHelper);
//Assert
//commentService.GetComment(1).comment.CommentText.ShouldBe("My Comment");
//commentService.GetComment(2).validate.NotFound.ShouldBeTrue();
viewModel.Comments.Count().ShouldBe(1);
}
[Fact]
public async Task CommentsQuestionsAndAnswers_ReturnAllOwnCommentsForReviewingConsultation()
{
// Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var ChapterIntroURI = "consultations://./consultation/1/document/1/chapter/introduction";
var ChaperOverviewURI = "consultations://./consultation/1/document/1/chapter/overview";
var DocumentOneURI = "consultations://./consultation/1/document/1";
var DocumentTwoURI = "consultations://./consultation/1/document/2/chapter/guidelines";
var ConsultationOneURI = "consultations://./consultation/1";
var ConsultationTwoURI = "consultations://./consultation/2/document/1/chapter/ERROR";
var commentText = Guid.NewGuid().ToString();
var questionText = Guid.NewGuid().ToString();
var answerText = Guid.NewGuid().ToString();
var createdByUserId = Guid.Empty.ToString();
var anotherUserId = Guid.NewGuid().ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: createdByUserId);
AddCommentsAndQuestionsAndAnswers(ChapterIntroURI, commentText, questionText, answerText, createdByUserId, (int)StatusName.Draft, _context);
AddCommentsAndQuestionsAndAnswers(ChaperOverviewURI, commentText, questionText, answerText, createdByUserId, (int)StatusName.Draft, _context);
AddCommentsAndQuestionsAndAnswers(DocumentOneURI, commentText, questionText, answerText, createdByUserId, (int)StatusName.Draft, _context);
AddCommentsAndQuestionsAndAnswers(ConsultationOneURI, commentText, questionText, answerText, createdByUserId, (int)StatusName.Draft, _context);
AddCommentsAndQuestionsAndAnswers(DocumentTwoURI, commentText, questionText, answerText, createdByUserId, (int)StatusName.Draft, _context);
AddCommentsAndQuestionsAndAnswers(ConsultationTwoURI, commentText, questionText, answerText, createdByUserId, (int)StatusName.Draft, _context);
AddCommentsAndQuestionsAndAnswers(ChaperOverviewURI, "Another Users Comment", questionText, answerText, anotherUserId, (int)StatusName.Draft, _context);
var context = new ConsultationsContext(_options, userService, _fakeEncryption);
//var submitService = new SubmitService(context, _fakeUserService, _consultationService);
var commentService = new CommentService(context, _fakeUserService, _consultationService, _fakeHttpContextAccessor);
// Act
var viewModel = await commentService.GetCommentsAndQuestions("/1/review", _urlHelper);
//Assert
//commentService.GetComment(6).comment.ShouldNotBeNull();
//commentService.GetComment(7).validate.NotFound.ShouldBeTrue();
viewModel.Comments.Count().ShouldBe(5);
}
[Fact]
public async Task Only_own_Comments_returned_for_comment_list_when_has_organisation_session_cookie_exists()
{
// Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var sessionId = Guid.NewGuid();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
const int organisationUserId = 1;
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: null, organisationUserId: organisationUserId);
var context = new ConsultationsContext(_options, userService, _fakeEncryption);
var commentService = new CommentService(new ConsultationsContext(_options, userService, _fakeEncryption), userService, _consultationService, _fakeHttpContextAccessor);
var locationId = AddLocation(sourceURI);
var expectedCommentId = AddComment(locationId, "current user's comment", createdByUserId: null, organisationUserId: organisationUserId);
AddComment(locationId, "another user's comment logged in using auth", createdByUserId: Guid.NewGuid().ToString());
AddComment(locationId, "another user's comment logged in with cookie", createdByUserId: null, organisationUserId: 9999);
// Act
var viewModel = await commentService.GetCommentsAndQuestions("/1/1/introduction", _urlHelper);
//Assert
viewModel.Comments.Single().CommentId.ShouldBe(expectedCommentId);
}
[Fact] public void Only_return_comments_from_my_organisation_which_have_been_submitted_to_lead()
{
// Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var sessionId = Guid.NewGuid();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
const int organisationUserId = 1;
const int organisationId = 1;
const int otherUsersorganisationUserId = 2;
const string commentTextThatShouldBeReturned = "another user from my organisations comment submitted to lead";
const string emailAddress = "theotherusersemail@organisation.com";
var organisationUser = new OrganisationUser() { EmailAddress = emailAddress, OrganisationUserId = otherUsersorganisationUserId };
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: null, organisationUserId: organisationUserId);
var context = new ConsultationsContext(_options, userService, _fakeEncryption);
var commentService = new CommentService(new ConsultationsContext(_options, userService, _fakeEncryption), userService, _consultationService, _fakeHttpContextAccessor);
var locationId = AddLocation(sourceURI);
AddComment(locationId, "current user's comment", createdByUserId: null, organisationUserId: organisationUserId, organisationId: organisationId);
AddComment(locationId, "another user from my organisations comment not submitted", createdByUserId: null, status: (int)StatusName.Draft, organisationUserId: 9999, organisationId: organisationId);
AddComment(locationId, commentTextThatShouldBeReturned, createdByUserId: null, status: (int)StatusName.SubmittedToLead, organisationUserId: otherUsersorganisationUserId, organisationId: organisationId, organisationUser: organisationUser);
AddComment(locationId, "another user from a different organisation comment", createdByUserId: null, status: (int)StatusName.SubmittedToLead, organisationUserId: 7777, organisationId: 2);
AddComment(locationId, "an individual users comment", createdByUserId: "1", status: (int)StatusName.Submitted);
// Act
var viewModel = commentService.GetCommentsAndQuestionsFromOtherOrganisationCommenters("/1/1/introduction", _urlHelper);
//Assert
viewModel.Comments.Count.Equals(1);
var comment = viewModel.Comments.Single();
comment.CommentText.ShouldBe(commentTextThatShouldBeReturned);
comment.CommenterEmail.ShouldBe(emailAddress);
}
}
}