-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnswerService.Tests.cs
More file actions
283 lines (226 loc) · 12.2 KB
/
Copy pathAnswerService.Tests.cs
File metadata and controls
283 lines (226 loc) · 12.2 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
using System;
using System.Linq;
using System.Threading.Tasks;
using Comments.Models;
using Comments.Services;
using Comments.Test.Infrastructure;
using Comments.ViewModels;
using Shouldly;
using Xunit;
using Answer = Comments.Models.Answer;
using Location = Comments.Models.Location;
using Question = Comments.Models.Question;
using QuestionType = Comments.Models.QuestionType;
using TestBase = Comments.Test.Infrastructure.TestBase;
namespace Comments.Test.UnitTests
{
public class AnswerServiceTests : TestBase
{
[Fact]
public void Answer_Get()
{
//Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var answerText = Guid.NewGuid().ToString();
var userId = Guid.Empty.ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
SetupTestDataInDB();
var question = GetQuestion();
var answerId = AddAnswer(question.QuestionId, userId, answerText);
//Act
var viewModel = new AnswerService(new ConsultationsContext(_options, userService, _fakeEncryption), userService).GetAnswer(answerId);
//Assert
viewModel.answer.AnswerText.ShouldBe(answerText);
}
[Fact]
public void Answer_Get_Record_Not_Found()
{
//Arrange
ResetDatabase();
var userId = Guid.Empty.ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var answerService = new AnswerService(new ConsultationsContext(_options, userService, _fakeEncryption), userService);
//Act
var viewModel = answerService.GetAnswer(1);
//Assert
viewModel.validate.NotFound.ShouldBeTrue();
viewModel.answer.ShouldBeNull();
}
[Fact]
public void Answer_Get_Record_Not_Logged_In()
{
//Arrange
ResetDatabase();
var userService = FakeUserService.Get(isAuthenticated: false);
//Act
var viewModel = new AnswerService(new ConsultationsContext(_options, userService, _fakeEncryption), userService).GetAnswer(1);
//Assert
viewModel.validate.Unauthenticated.ShouldBeTrue();
}
[Fact]
public void Answer_CanBeEdited()
{
//Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var answerText = Guid.NewGuid().ToString();
var userId = Guid.Empty.ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
SetupTestDataInDB();
var question = GetQuestion();
var answerId = AddAnswer(question.QuestionId, userId, answerText);
var answerService = new AnswerService(new ConsultationsContext(_options, userService, _fakeEncryption), userService);
var viewModel = answerService.GetAnswer(answerId);
var updatedAnswerText = Guid.NewGuid().ToString();
viewModel.answer.AnswerText = updatedAnswerText;
//Act
var result = answerService.EditAnswer(answerId, viewModel.answer);
viewModel = answerService.GetAnswer(answerId);
//Assert
result.rowsUpdated.ShouldBe(1);
viewModel.answer.AnswerText.ShouldBe(updatedAnswerText);
}
[Fact]
public void Answer_CanBeDeleted()
{
//Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var answerText = Guid.NewGuid().ToString();
var userId = Guid.Empty.ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
SetupTestDataInDB();
var question = GetQuestion();
var answerId = AddAnswer(question.QuestionId, userId, answerText);
var answerService = new AnswerService(new ConsultationsContext(_options, userService, _fakeEncryption), userService);
//Act
var result = answerService.DeleteAnswer(answerId);
var viewModel = answerService.GetAnswer(answerId);
//Assert
result.rowsUpdated.ShouldBe(1);
viewModel.answer.ShouldBeNull();
}
[Fact]
public void Answer_Record_To_Be_Deleted_Not_Found()
{
//Arrange
ResetDatabase();
var answerId = 1;
var userId = Guid.Empty.ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var answerService = new AnswerService(new ConsultationsContext(_options, userService, _fakeEncryption), userService);
//Act
var result = answerService.DeleteAnswer(answerId);
//Assert
result.validate.NotFound.ShouldBeTrue();
}
[Fact]
public void Answer_CanBeCreated()
{
//Arrange
ResetDatabase();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
var answerText = Guid.NewGuid().ToString();
var questionText = Guid.NewGuid().ToString();
var description = Guid.NewGuid().ToString();
var userId = Guid.Empty.ToString();
AddStatus(StatusName.Draft.ToString(), (int)StatusName.Draft);
var locationId = AddLocation(sourceURI);
var questionTypeId = AddQuestionType(description, false, true);
var questionId = AddQuestion(locationId, questionTypeId, questionText);
var questionType = new QuestionType(description, false, true, null);
var location = new Location(sourceURI, null, null, null, null, null, null, null, null, null, null, null);
var question = new Question(locationId, questionText, questionTypeId, location, questionType, null);
var answer = new Answer(questionId, userId, answerText, false, question, (int)StatusName.Draft, null);
var viewModel = new ViewModels.Answer(answer);
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var answerService = new AnswerService(new ConsultationsContext(_options, userService, _fakeEncryption), userService);
//Act
var result = answerService.CreateAnswer(viewModel);
//Assert
result.answer.AnswerId.ShouldBeGreaterThan(0);
result.answer.AnswerText.ShouldBe(answerText);
result.answer.AnswerBoolean.ShouldBe(false);
}
[Fact]
public void No_Answers_returned_when_not_logged_in()
{
// Arrange
ResetDatabase();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
var commentText = Guid.NewGuid().ToString();
var answerText = Guid.NewGuid().ToString();
var questionText = Guid.NewGuid().ToString();
var userId = Guid.Empty.ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
AddCommentsAndQuestionsAndAnswers(sourceURI, commentText, questionText, answerText, userId);
// Act
var viewModel = new AnswerService(new ConsultationsContext(_options, _fakeUserService, _fakeEncryption), FakeUserService.Get(isAuthenticated: false)).GetAnswer(1);
//Assert
viewModel.validate.Unauthenticated.ShouldBeTrue();
viewModel.answer.ShouldBeNull();
}
[Fact]
public async Task Only_own_Answers_returned_when_logged_in()
{
//Arrange
ResetDatabase();
_context.Database.EnsureCreated();
var sourceURI = "consultations://./consultation/1/document/1/chapter/introduction";
var questionText = Guid.NewGuid().ToString();
var userId = Guid.NewGuid().ToString();
var userService = FakeUserService.Get(isAuthenticated: true, displayName: "Benjamin Button", userId: userId);
var locationId = AddLocation(sourceURI);
var questionTypeId = 99;
var questionId = AddQuestion(locationId, questionTypeId, questionText);
var expectedAnswerId = AddAnswer(questionId, userId, "current user's answer");
AddAnswer(questionId, Guid.NewGuid().ToString(), "another user's answer");
var context = new ConsultationsContext(_options, userService, _fakeEncryption);
//var submitService = new SubmitService(context, userService, _consultationService);
var commentService = new CommentService(context, userService, _consultationService, _fakeHttpContextAccessor);
// Act
var viewModel = await commentService.GetCommentsAndQuestions(sourceURI, _urlHelper);
var questionViewModel = viewModel.Questions.SingleOrDefault(q => q.QuestionId.Equals(questionId));
//Assert
questionViewModel.Answers.Single().AnswerId.ShouldBe(expectedAnswerId);
}
[Fact]
public void Only_return_answers_from_my_organisation_which_have_been_submitted_to_lead_in_correct_order()
{
// 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 int differentUsersOrganisationUserID = 3;
const int questionTypeId = 50;
const string answerTextThatShouldBeReturned = "another user from my organisations answer submitted to lead";
const string emailAddress = "theotherusersemail@organisation.com";
var organisationUser = new OrganisationUser() { EmailAddress = emailAddress, OrganisationUserId = otherUsersorganisationUserId };
var differentOrganisationUser = new OrganisationUser() { EmailAddress = "email@organisation.com", OrganisationUserId = differentUsersOrganisationUserID };
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);
AddQuestionType("Text question", hasBooleanAnswer: false, hasTextAnswer: true, questionTypeId: questionTypeId);
var questionId = AddQuestion(locationId, questionTypeId, "Some question text");
AddAnswer(questionId, userId: null, "current user's answer", organisationUserId: organisationUserId, organisationId: organisationId);
AddAnswer(questionId, userId: null, "another user from my organisations answer not submitted", organisationUserId: 9999, organisationId: organisationId, status: (int)StatusName.Draft);
AddAnswer(questionId, userId: null, "first answer submitted", organisationUserId: differentUsersOrganisationUserID, organisationId: organisationId, status: (int)StatusName.SubmittedToLead, organisationUser: differentOrganisationUser, lastModifiedDate: DateTime.MinValue);
AddAnswer(questionId, userId: null, answerTextThatShouldBeReturned, organisationUserId: otherUsersorganisationUserId, organisationId: organisationId, status: (int)StatusName.SubmittedToLead, organisationUser: organisationUser);
AddAnswer(questionId, userId: null, "another user from a different organisation answer", status: (int)StatusName.SubmittedToLead, organisationUserId: organisationUserId, organisationId: 2);
AddAnswer(questionId, userId: "1", "an individual users answer", status: (int)StatusName.SubmittedToLead);
// Act
var viewModel = commentService.GetCommentsAndQuestionsFromOtherOrganisationCommenters("/1/1/introduction", _urlHelper);
//Assert
var answers = viewModel.Questions.Single().Answers;
answers.Count.ShouldBe(2);
answers.First().AnswerText.ShouldBe(answerTextThatShouldBeReturned);
answers.First().CommenterEmail.ShouldBe(emailAddress);
}
}
}