-
Notifications
You must be signed in to change notification settings - Fork 1
/
SubmitService.Tests.cs
345 lines (295 loc) · 17.1 KB
/
SubmitService.Tests.cs
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
333
334
335
336
337
338
339
340
341
342
343
344
345
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Comments.Common;
using Comments.Models;
using Comments.Services;
using Comments.Test.Infrastructure;
using Comments.ViewModels;
using Microsoft.EntityFrameworkCore;
using Shouldly;
using Xunit;
using Comment = Comments.Models.Comment;
namespace Comments.Test.UnitTests
{
public class SubmitServiceTests : Infrastructure.TestBase
{
[Fact]
public async Task Update_Comment_When_Submitted()
{
//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 consultationContext = new ConsultationsContext(_options, userService, _fakeEncryption);
var submitService = new SubmitService(consultationContext, userService, _consultationService);
var commentService = new CommentService(consultationContext, userService, _consultationService, _fakeHttpContextAccessor);
var locationId = AddLocation(sourceURI, _context);
var commentId = AddComment(locationId, "Comment text", userId, (int)StatusName.Draft, _context);
//Act
var commentsAndQuestions = await commentService.GetCommentsAndQuestions(sourceURI, _urlHelper);
var (rowsUpdated, _) = await submitService.Submit(new ViewModels.Submission(commentsAndQuestions.Comments, new List<ViewModels.Answer>()));
var (comment, _) = commentService.GetComment(commentId);
var commentsSubmissionData = _context.SubmissionComment.Where(s => s.CommentId == commentId)
.Include(s => s.Submission).First();
//Assert
rowsUpdated.ShouldBe(3);
comment.StatusId.ShouldBe((int)StatusName.Submitted);
comment.Status.StatusId.ShouldBe((int)StatusName.Submitted);
commentsSubmissionData.CommentId.ShouldBe(commentId);
commentsSubmissionData.Submission.SubmissionByUserId.ShouldBe(userId);
}
[Fact]
public async Task Update_Answer_When_Submitted()
{
//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 consultationContext = new ConsultationsContext(_options, userService, _fakeEncryption);
var submitService = new SubmitService(consultationContext, userService, _consultationService);
var commentService = new CommentService(consultationContext, userService, _consultationService, _fakeHttpContextAccessor);
var answerService = new AnswerService(consultationContext, userService);
var locationId = AddLocation(sourceURI, _context);
var questionTypeId = 99;
var questionId = AddQuestion(locationId, questionTypeId, "Question Label");
var answerId = AddAnswer(questionId, userId, "Answer Label");
//Act
var commentsAndQuestions = await commentService.GetCommentsAndQuestions(sourceURI, _urlHelper);
var (rowsUpdated, _) = await submitService.Submit(new ViewModels.Submission(new List<ViewModels.Comment>(), commentsAndQuestions.Questions.First().Answers));
var (answer, _) = answerService.GetAnswer(answerId);
var answerSubmissionData = _context.SubmissionAnswer.Where(s => s.AnswerId == answerId)
.Include(s => s.Submission).First();
//Assert
rowsUpdated.ShouldBe(3);
answer.StatusId.ShouldBe((int)StatusName.Submitted);
answer.Status.StatusId.ShouldBe((int)StatusName.Submitted);
answerSubmissionData.AnswerId.ShouldBe(answerId);
answerSubmissionData.Submission.SubmissionByUserId.ShouldBe(userId);
}
[Fact]
public void Get_Users_Submission()
{
//Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
var userId = Guid.NewGuid().ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var consultationContext = new ConsultationsContext(_options, userService, _fakeEncryption);
AddSubmittedCommentsAndAnswers(sourceURI, "Comment Label", "Question Label", "Answer Label", userId, consultationContext);
var sourceURIs = new List<string>
{
ConsultationsUri.ConvertToConsultationsUri("/1/0/Review", CommentOn.Consultation)
};
//Act
var results = consultationContext.GetAllCommentsAndQuestionsForDocument(sourceURIs, true);
//Assert
results.First().Comment.First().SubmissionComment.Count.ShouldBe(1);
results.First().Comment.First().Status.ShouldNotBeNull();
}
[Theory]
[InlineData(null, false)]
[InlineData("", false)]
[InlineData(" ", false)]
[InlineData("consultations://./consultation/10/document/1/chapter/introduction", true)]
[InlineData("consultations://./consultation/10/document/2/chapter/introduction", true)]
[InlineData("consultations://./consultation/10/document/1/chapter/anotherchaptertitle", true)]
[InlineData("consultations://./consultation/10/document/1", true)]
[InlineData("consultations://./consultation/10", true)]
[InlineData("consultations://./consultation/1", false)]
[InlineData("consultations://./consultation/2", false)]
public void Has_Submitted_Comments_Or_Answers_For_Chapter_SourceURI(string consultationSourceURI, bool expectedResult)
{
//Arrange
var userId = Guid.NewGuid().ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var consultationContext = new ConsultationsContext(_options, userService, _fakeEncryption);
var consultationService = new ConsultationService(consultationContext, null, null, userService);
//var submitService = new SubmitService(consultationContext, userService, _consultationService);
AddSubmittedCommentsAndAnswers("consultations://./consultation/10/document/1/chapter/introduction", "Comment Label", "Question Label", "Answer Label", userId, consultationContext);
//Act
var actualResult = consultationService.GetSubmittedDate(consultationSourceURI).HasValue;
//Assert
actualResult.ShouldBe(expectedResult);
}
[Theory]
[InlineData(null, false)]
[InlineData("", false)]
[InlineData(" ", false)]
[InlineData("consultations://./consultation/10/document/10/chapter/introduction", true)]
[InlineData("consultations://./consultation/10/document/2/chapter/introduction", true)]
[InlineData("consultations://./consultation/10/document/10/chapter/anotherchaptertitle", true)]
[InlineData("consultations://./consultation/10/document/10", true)]
[InlineData("consultations://./consultation/10/document/1", true)]
[InlineData("consultations://./consultation/10", true)]
[InlineData("consultations://./consultation/1", false)]
[InlineData("consultations://./consultation/2", false)]
public void Has_Submitted_Comments_Or_Answers_For_Document_SourceURI(string consultationSourceURI, bool expectedResult)
{
//Arrange
var userId = Guid.NewGuid().ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var consultationContext = new ConsultationsContext(_options, userService, _fakeEncryption);
//var submitService = new SubmitService(consultationContext, userService, _consultationService);
var consultationService = new Services.ConsultationService(consultationContext, null, null, userService);
AddSubmittedCommentsAndAnswers("consultations://./consultation/10/document/10", "Comment Label", "Question Label", "Answer Label", userId, consultationContext);
//Act
var actualResult = consultationService.GetSubmittedDate(consultationSourceURI).HasValue;
//Assert
actualResult.ShouldBe(expectedResult);
}
[Theory]
[InlineData(null, false)]
[InlineData("", false)]
[InlineData(" ", false)]
[InlineData("consultations://./consultation/10/document/1/chapter/introduction", true)]
[InlineData("consultations://./consultation/10/document/2/chapter/introduction", true)]
[InlineData("consultations://./consultation/10/document/1/chapter/anotherchaptertitle", true)]
[InlineData("consultations://./consultation/10/document/1", true)]
[InlineData("consultations://./consultation/10", true)]
[InlineData("consultations://./consultation/1", false)]
[InlineData("consultations://./consultation/2", false)]
public void Has_Submitted_Comments_Or_Answers_For_Consultation_SourceURI(string consultationSourceURI, bool expectedResult)
{
//Arrange
var userId = Guid.NewGuid().ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var consultationContext = new ConsultationsContext(_options, userService, _fakeEncryption);
var consultationService = new ConsultationService(consultationContext, null, null, userService);
//var submitService = new SubmitService(consultationContext, userService, _consultationService);
AddSubmittedCommentsAndAnswers("consultations://./consultation/10", "Comment Label", "Question Label", "Answer Label", userId, consultationContext);
//Act
var actualResult = consultationService.GetSubmittedDate(consultationSourceURI).HasValue;
//Assert
actualResult.ShouldBe(expectedResult);
}
[Theory]
[InlineData(null, false)]
[InlineData("", false)]
[InlineData(" ", false)]
[InlineData("consultations://./consultation/10/document/1/chapter/introduction", true)]
[InlineData("consultations://./consultation/10/document/2/chapter/introduction", true)]
[InlineData("consultations://./consultation/10/document/1/chapter/anotherchaptertitle", true)]
[InlineData("consultations://./consultation/10/document/1", true)]
[InlineData("consultations://./consultation/10", true)]
[InlineData("consultations://./consultation/1", false)]
[InlineData("consultations://./consultation/2", false)]
public void Has_Submitted_Comments_Or_Answers_For_Consultation_SourceURI_With_Only_Comments(string consultationSourceURI, bool expectedResult)
{
//Arrange
var userId = Guid.NewGuid().ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var consultationContext = new ConsultationsContext(_options, userService, _fakeEncryption);
var consultationService = new ConsultationService(consultationContext, null, null, userService);
AddSubmittedComments("consultations://./consultation/10", "Comment Label", "Question Label", "Answer Label", userId, consultationContext);
//Act
var actualResult = consultationService.GetSubmittedDate(consultationSourceURI).HasValue;
//Assert
actualResult.ShouldBe(expectedResult);
}
[Theory]
[InlineData(null, false)]
[InlineData("", false)]
[InlineData(" ", false)]
[InlineData("consultations://./consultation/10/document/1/chapter/introduction", true)]
[InlineData("consultations://./consultation/10/document/2/chapter/introduction", true)]
[InlineData("consultations://./consultation/10/document/1/chapter/anotherchaptertitle", true)]
[InlineData("consultations://./consultation/10/document/1", true)]
[InlineData("consultations://./consultation/10", true)]
[InlineData("consultations://./consultation/1", false)]
[InlineData("consultations://./consultation/2", false)]
public void Has_Submitted_Comments_Or_Answers_For_Consultation_SourceURI_With_Only_Answers(string consultationSourceURI, bool expectedResult)
{
//Arrange
var userId = Guid.NewGuid().ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var consultationContext = new ConsultationsContext(_options, userService, _fakeEncryption);
var consultationService = new ConsultationService(consultationContext, null, null, userService);
AddSubmittedQuestionsWithAnswers("consultations://./consultation/10", "Comment Label", "Question Label", "Answer Label", userId, consultationContext);
//Act
var actualResult = consultationService.GetSubmittedDate(consultationSourceURI).HasValue;
//Assert
actualResult.ShouldBe(expectedResult);
}
[Fact]
public async Task SubmitToLead_CommentShouldBeDuplicatedAndStatusSetOnParentRecord()
{
//Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
var consultationId = 1;
var organisationId = 1;
var authorisationSession = Guid.NewGuid();
var commentText = "Comment text";
var organisationAuthorisationId = TestBaseDBHelpers.AddOrganisationAuthorisationWithLocation(organisationId, consultationId, _context);
var organisationUserId = TestBaseDBHelpers.AddOrganisationUser(_context, organisationAuthorisationId, authorisationSession, null);
var userService = FakeUserService.Get(true, "Benjamin Button", null, TestUserType.NotAuthenticated, false, organisationUserId);
var consultationContext = new ConsultationsContext(_options, userService, _fakeEncryption);
var submitService = new SubmitService(consultationContext, userService, _consultationService);
var commentService = new CommentService(consultationContext, userService, _consultationService, _fakeHttpContextAccessor);
var locationId = AddLocation(sourceURI, _context);
var commentId = AddComment(locationId, commentText, null, (int)StatusName.Draft, _context, organisationUserId, null, organisationId);
//Act
var commentsAndQuestions = await commentService.GetCommentsAndQuestions(sourceURI, _urlHelper);
var (rowsUpdated, _, context) = await submitService.SubmitToLead(new ViewModels.SubmissionToLead(commentsAndQuestions.Comments, new List<ViewModels.Answer>(), "testemail@nice.org.uk", true, "Organisation"));
//Assert
rowsUpdated.ShouldBe(3);
context.Comment.First().StatusId.ShouldBe((int)StatusName.SubmittedToLead);
context.Comment.First().OrganisationId.ShouldBe(organisationId);
context.Comment.First().ParentCommentId.ShouldBe(null);
context.Comment.First().OrganisationUserId.ShouldBe(organisationUserId);
context.Comment.First().CreatedByUserId.ShouldBe(null);
context.Comment.First().ChildComments.First().StatusId.ShouldBe((int)StatusName.Draft);
context.Comment.First().ChildComments.First().OrganisationId.ShouldBe(organisationId);
context.Comment.First().ChildComments.First().ParentCommentId.ShouldBe(commentId);
context.Comment.First().ChildComments.First().CommentText.ShouldBe(commentText);
context.Comment.First().ChildComments.First().OrganisationUserId.ShouldBe(organisationUserId);
context.Comment.First().ChildComments.First().CreatedByUserId.ShouldBe(null);
}
[Fact]
public async Task SubmitToLead_AnswerShouldBeDuplicatedAndStatusSetOnParentRecord()
{
//Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
var questionTypeId = 99;
var consultationId = 1;
var organisationId = 1;
var authorisationSession = Guid.NewGuid();
var answerText = "Answer Label";
var organisationAuthorisationId = TestBaseDBHelpers.AddOrganisationAuthorisationWithLocation(organisationId, consultationId, _context);
var organisationUserId = TestBaseDBHelpers.AddOrganisationUser(_context, organisationAuthorisationId, authorisationSession, null);
var userService = FakeUserService.Get(true, "Benjamin Button", null, TestUserType.NotAuthenticated, false, organisationUserId);
var consultationContext = new ConsultationsContext(_options, userService, _fakeEncryption);
var submitService = new SubmitService(consultationContext, userService, _consultationService);
var commentService = new CommentService(consultationContext, userService, _consultationService, _fakeHttpContextAccessor);
var locationId = AddLocation(sourceURI, _context);
var questionId = AddQuestion(locationId, questionTypeId, "Question Label");
var answerId = AddAnswer(questionId, null, answerText, (int)StatusName.Draft, _context, organisationUserId, null, organisationId);
//Act
var commentsAndQuestions = await commentService.GetCommentsAndQuestions(sourceURI, _urlHelper);
var (rowsUpdated, _, context) = await submitService.SubmitToLead(new ViewModels.SubmissionToLead(new List<ViewModels.Comment>(), commentsAndQuestions.Questions.First().Answers, "testemail@nice.org.uk", true, "Organisation"));
//Assert
rowsUpdated.ShouldBe(3);
context.Answer.First().StatusId.ShouldBe((int)StatusName.SubmittedToLead);
context.Answer.First().OrganisationId.ShouldBe(organisationId);
context.Answer.First().ParentAnswerId.ShouldBe(null);
context.Answer.First().OrganisationUserId.ShouldBe(organisationUserId);
context.Answer.First().CreatedByUserId.ShouldBe(null);
context.Answer.First().ChildAnswers.First().StatusId.ShouldBe((int)StatusName.Draft);
context.Answer.First().ChildAnswers.First().OrganisationId.ShouldBe(organisationId);
context.Answer.First().ChildAnswers.First().ParentAnswerId.ShouldBe(answerId);
context.Answer.First().ChildAnswers.First().AnswerText.ShouldBe(answerText);
context.Answer.First().ChildAnswers.First().OrganisationUserId.ShouldBe(organisationUserId);
context.Answer.First().ChildAnswers.First().CreatedByUserId.ShouldBe(null);
}
}
}