Skip to content

Commit 66df1d0

Browse files
committed
Updating tests
1 parent 20ffea7 commit 66df1d0

File tree

3 files changed

+78
-34
lines changed

3 files changed

+78
-34
lines changed

OpenAPI/LearningHub.Nhs.OpenApi.Tests/Controllers/ResourceControllerTests.cs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ namespace LearningHub.Nhs.OpenApi.Tests.Controllers
1818
using Moq;
1919
using Newtonsoft.Json;
2020
using Xunit;
21+
using Microsoft.AspNetCore.Http;
22+
using Microsoft.AspNetCore.Mvc;
23+
using System.Security.Claims;
2124

22-
public sealed class ResourceControllerTests : IDisposable
25+
public sealed class ResourceControllerTests
2326
{
2427
private readonly Mock<ISearchService> searchService;
2528
private readonly Mock<IResourceService> resourceService;
@@ -87,6 +90,7 @@ await Assert.ThrowsAsync<HttpResponseException>(
8790
public async Task SearchEndpointUsesDefaultLimitGivenInConfig()
8891
{
8992
// Given
93+
int? currentUserId = null; //E.g if hitting endpoint with ApiKey auth
9094
this.GivenSearchServiceSucceedsButFindsNoItems();
9195
this.GivenDefaultLimitForFindwiseSearchIs(12);
9296
this.resourceController = new ResourceController(
@@ -99,7 +103,7 @@ public async Task SearchEndpointUsesDefaultLimitGivenInConfig()
99103

100104
// Then
101105
this.searchService.Verify(
102-
service => service.Search(It.Is<ResourceSearchRequest>(request => request.Limit == 12)));
106+
service => service.Search(It.Is<ResourceSearchRequest>(request => request.Limit == 12), currentUserId));
103107
}
104108

105109
[Fact]
@@ -177,13 +181,49 @@ await Assert.ThrowsAsync<HttpResponseException>(
177181
exception.StatusCode.Should().Be(HttpStatusCode.BadRequest);
178182
}
179183

184+
[Fact]
185+
public void CurrentUserIdSetByAuth()
186+
{
187+
// Arrange
188+
ResourceController resourceController = new ResourceController(
189+
this.searchService.Object,
190+
this.resourceService.Object,
191+
this.findwiseConfigOptions.Object
192+
);
193+
194+
195+
// This Id is the development accountId
196+
int currentUserId = 57541;
197+
198+
// Create claims identity with the specified user id
199+
var claims = new List<Claim>
200+
{
201+
new Claim(ClaimTypes.NameIdentifier, currentUserId.ToString()),
202+
};
203+
var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation"); // Set the authentication type to "Federation"
204+
205+
// Create claims principal with the claims identity
206+
var claimsPrincipal = new ClaimsPrincipal(identity);
207+
208+
// Create a mock HttpContext and set it to the ControllerContext
209+
var httpContext = new DefaultHttpContext { User = claimsPrincipal };
210+
var controllerContext = new ControllerContext { HttpContext = httpContext };
211+
resourceController.ControllerContext = controllerContext;
212+
213+
// Act
214+
215+
// Assert that the CurrentUserId property of the resourceController matches the currentUserId
216+
Assert.Equal(currentUserId, resourceController.CurrentUserId);
217+
}
218+
180219
[Theory]
181220
[InlineData(1)]
182221
[InlineData(20)]
183222
[InlineData(46)]
184223
public async Task SearchEndpointUsesPassedInLimitIfGiven(int limit)
185224
{
186225
// Given
226+
int? currentUserId = null; //E.g if hitting endpoint with ApiKey auth
187227
this.GivenSearchServiceSucceedsButFindsNoItems();
188228
this.GivenDefaultLimitForFindwiseSearchIs(20);
189229
this.resourceController = new ResourceController(
@@ -196,12 +236,7 @@ public async Task SearchEndpointUsesPassedInLimitIfGiven(int limit)
196236

197237
// Then
198238
this.searchService.Verify(
199-
service => service.Search(It.Is<ResourceSearchRequest>(request => request.Limit == limit)));
200-
}
201-
202-
public void Dispose()
203-
{
204-
this.resourceController?.Dispose();
239+
service => service.Search(It.Is<ResourceSearchRequest>(request => request.Limit == limit), currentUserId));
205240
}
206241

207242
private void GivenDefaultLimitForFindwiseSearchIs(int limit)
@@ -212,14 +247,17 @@ private void GivenDefaultLimitForFindwiseSearchIs(int limit)
212247

213248
private void GivenSearchServiceFailsWithStatus(FindwiseRequestStatus status)
214249
{
215-
this.searchService.Setup(ss => ss.Search(It.IsAny<ResourceSearchRequest>())).ReturnsAsync(
250+
int? currentUserId = null; //E.g if hitting endpoint with ApiKey auth
251+
this.searchService.Setup(ss => ss.Search(It.IsAny<ResourceSearchRequest>(), currentUserId)).ReturnsAsync(
216252
new ResourceSearchResultModel(new List<ResourceMetadataViewModel>(), status, 0));
217253
}
218254

219255
private void GivenSearchServiceSucceedsButFindsNoItems()
220256
{
221-
this.searchService.Setup(ss => ss.Search(It.IsAny<ResourceSearchRequest>())).ReturnsAsync(
257+
int? currentUserId = null; //E.g if hitting endpoint with ApiKey auth
258+
this.searchService.Setup(ss => ss.Search(It.IsAny<ResourceSearchRequest>(), currentUserId)).ReturnsAsync(
222259
new ResourceSearchResultModel(new List<ResourceMetadataViewModel>(), FindwiseRequestStatus.Success, 0));
223260
}
261+
224262
}
225263
}

OpenAPI/LearningHub.Nhs.OpenApi.Tests/Services/Services/ResourceServiceTests.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ public class ResourceServiceTests
2222
private readonly Mock<ILearningHubService> learningHubService;
2323
private readonly ResourceService resourceService;
2424
private readonly Mock<IResourceRepository> resourceRepository;
25+
private readonly int currentUserId;
2526

2627
public ResourceServiceTests()
2728
{
29+
//This Id is the development accountId
30+
this.currentUserId = 57541;
31+
2832
this.learningHubService = new Mock<ILearningHubService>();
2933
this.resourceRepository = new Mock<IResourceRepository>();
3034
this.resourceService = new ResourceService(this.learningHubService.Object, this.resourceRepository.Object, new NullLogger<ResourceService>());
@@ -63,7 +67,7 @@ public async Task SingleResourceEndpointReturnsTheCorrectInformationIfThereIsAMa
6367
.ReturnsAsync(this.ResourceReferenceList.GetRange(0, 1));
6468

6569
// When
66-
var x = await this.resourceService.GetResourceReferenceByOriginalId(1);
70+
var x = await this.resourceService.GetResourceReferenceByOriginalId(1, null);
6771

6872
// Then
6973
x.Rating.Should().Be(3);
@@ -80,7 +84,7 @@ public async Task SingleResourceReturnsA404IfTheresNoResourceReferenceWithAMatch
8084
.ReturnsAsync(new List<ResourceReference>());
8185

8286
// When / Then
83-
var exception = await Assert.ThrowsAsync<HttpResponseException>(async () => await this.resourceService.GetResourceReferenceByOriginalId(999));
87+
var exception = await Assert.ThrowsAsync<HttpResponseException>(async () => await this.resourceService.GetResourceReferenceByOriginalId(999, null));
8488
exception.StatusCode.Should().Be(HttpStatusCode.NotFound);
8589
exception.ResponseBody.Should().Be("No matching resource reference");
8690
}
@@ -93,7 +97,7 @@ public async Task SingleResourceEndpointReturnsAResourceMetadataViewModelObjectW
9397
.ReturnsAsync(this.ResourceReferenceList.GetRange(1, 1));
9498

9599
// When
96-
var x = await this.resourceService.GetResourceReferenceByOriginalId(2);
100+
var x = await this.resourceService.GetResourceReferenceByOriginalId(2, null);
97101

98102
// Then
99103
x.Title.Should().Be("No current resource version");
@@ -108,7 +112,7 @@ public async Task SingleResourceEndpointReturnsAMessageSayingNoCatalogueIfThereI
108112
.ReturnsAsync(this.ResourceReferenceList.GetRange(2, 1));
109113

110114
// When
111-
var x = await this.resourceService.GetResourceReferenceByOriginalId(3);
115+
var x = await this.resourceService.GetResourceReferenceByOriginalId(3, null);
112116

113117
// Then
114118
x.Catalogue.Name.Should().Be("No catalogue for resource reference");
@@ -122,7 +126,7 @@ public async Task SingleResourceEndpointReturnsAMessageSayingNoCatalogueIfThereI
122126
.ReturnsAsync(this.ResourceReferenceList.GetRange(3, 1));
123127

124128
// When
125-
var x = await this.resourceService.GetResourceReferenceByOriginalId(4);
129+
var x = await this.resourceService.GetResourceReferenceByOriginalId(4, null);
126130

127131
// Then
128132
x.Catalogue.Name.Should().Be("No catalogue for resource reference");
@@ -136,7 +140,7 @@ public async Task SingleResourceEndpointReturnsAMessageSayingNoCatalogueIfThereI
136140
.ReturnsAsync(this.ResourceReferenceList.GetRange(5, 1));
137141

138142
// When
139-
var x = await this.resourceService.GetResourceReferenceByOriginalId(6);
143+
var x = await this.resourceService.GetResourceReferenceByOriginalId(6, null);
140144

141145
// Then
142146
x.Catalogue.Name.Should().Be("No catalogue for resource reference");
@@ -150,7 +154,7 @@ public async Task SingleResourceEndpointReturnsAZeroForRatingIfTheresNoRatingSum
150154
.ReturnsAsync(this.ResourceReferenceList.GetRange(7, 1));
151155

152156
// When
153-
var x = await this.resourceService.GetResourceReferenceByOriginalId(8);
157+
var x = await this.resourceService.GetResourceReferenceByOriginalId(8, null);
154158

155159
// Then
156160
x.Catalogue.Name.Should().Be("catalogue3");
@@ -165,7 +169,7 @@ public async Task SingleResourceEndpointThrowsAnErrorAndReturnsABlankStringIfThe
165169
.ReturnsAsync(this.ResourceReferenceList.GetRange(8, 1));
166170

167171
// When
168-
var x = await this.resourceService.GetResourceReferenceByOriginalId(9);
172+
var x = await this.resourceService.GetResourceReferenceByOriginalId(9, null);
169173

170174
// Then
171175
x.ResourceType.Should().Be(string.Empty);
@@ -179,7 +183,7 @@ public async Task SingleResourceEndpointThrowsAnErrorIfThereIsMoreThanOneResourc
179183
.ReturnsAsync(this.ResourceReferenceList.GetRange(9, 2));
180184

181185
// When / Then
182-
await Assert.ThrowsAsync<InvalidOperationException>(async () => await this.resourceService.GetResourceReferenceByOriginalId(10));
186+
await Assert.ThrowsAsync<InvalidOperationException>(async () => await this.resourceService.GetResourceReferenceByOriginalId(10, null));
183187
}
184188

185189
/*[Fact]
@@ -198,7 +202,7 @@ public async Task BulkEndpointReturnsAllMatchingResources()
198202
.ReturnsAsync(this.ResourceReferenceList.GetRange(0, 2));
199203

200204
// When
201-
var x = await this.resourceService.GetResourceReferencesByOriginalIds(idsToLookUp);
205+
var x = await this.resourceService.GetResourceReferencesByOriginalIds(idsToLookUp, null);
202206

203207
// Then
204208
x.ResourceReferences.Count.Should().Be(2);
@@ -220,7 +224,7 @@ public async Task BulkEndpointReturnsA404IfThereAreNoMatchingResources()
220224
.ReturnsAsync(new List<ResourceReference>());
221225

222226
// When
223-
var x = await this.resourceService.GetResourceReferencesByOriginalIds(idsToLookUp);
227+
var x = await this.resourceService.GetResourceReferencesByOriginalIds(idsToLookUp, null);
224228

225229
// Then
226230
x.UnmatchedResourceReferenceIds.Count.Should().Be(2);
@@ -237,7 +241,7 @@ public async Task BulkEndpointReturnsResourcesWithIncompleteInformation()
237241
.ReturnsAsync(this.ResourceReferenceList.GetRange(0, 4));
238242

239243
// When
240-
var x = await this.resourceService.GetResourceReferencesByOriginalIds(idsToLookUp);
244+
var x = await this.resourceService.GetResourceReferencesByOriginalIds(idsToLookUp, null);
241245

242246
// Then
243247
x.ResourceReferences.Count.Should().Be(4);
@@ -257,7 +261,7 @@ public async Task BulkEndpointReturnsUnmatchedResourcesWithMatchedResources()
257261
.ReturnsAsync(this.ResourceReferenceList.GetRange(0, 1));
258262

259263
// When
260-
var x = await this.resourceService.GetResourceReferencesByOriginalIds(idsToLookUp);
264+
var x = await this.resourceService.GetResourceReferencesByOriginalIds(idsToLookUp, null);
261265

262266
// Then
263267
x.ResourceReferences.Count.Should().Be(1);
@@ -277,7 +281,7 @@ public async Task ResourceServiceReturnsTheOriginalResourceReferenceIdAsTheRefId
277281
.ReturnsAsync(this.ResourceReferenceList.GetRange(5, 2));
278282

279283
// When
280-
var x = await this.resourceService.GetResourceReferencesByOriginalIds(list);
284+
var x = await this.resourceService.GetResourceReferencesByOriginalIds(list, null);
281285

282286
// Then
283287
x.ResourceReferences[0].RefId.Should().Be(6);
@@ -293,7 +297,7 @@ public async Task ResourceServiceReturnsTheOriginalResourceReferenceIdAsTheRefId
293297
.ReturnsAsync(this.ResourceReferenceList.GetRange(5, 1));
294298

295299
// When
296-
var x = await this.resourceService.GetResourceReferenceByOriginalId(6);
300+
var x = await this.resourceService.GetResourceReferenceByOriginalId(6, null);
297301

298302
// Then
299303
x.RefId.Should().Be(6);
@@ -308,7 +312,7 @@ public async Task ResourceServiceReturnsThatARestrictedCatalogueIsRestricted()
308312
.ReturnsAsync(this.ResourceReferenceList.GetRange(8, 1));
309313

310314
// When
311-
var x = await this.resourceService.GetResourceReferenceByOriginalId(9);
315+
var x = await this.resourceService.GetResourceReferenceByOriginalId(9, null);
312316

313317
// Then
314318
x.Catalogue.IsRestricted.Should().BeTrue();
@@ -323,7 +327,7 @@ public async Task ResourceServiceReturnsThatAnUnrestrictedCatalogueIsUnrestricte
323327
.ReturnsAsync(this.ResourceReferenceList.GetRange(7, 1));
324328

325329
// When
326-
var x = await this.resourceService.GetResourceReferenceByOriginalId(8);
330+
var x = await this.resourceService.GetResourceReferenceByOriginalId(8, null);
327331

328332
// Then
329333
x.Catalogue.IsRestricted.Should().BeFalse();

OpenAPI/LearningHub.Nhs.OpenApi.Tests/Services/Services/SearchServiceTests.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace LearningHub.Nhs.OpenApi.Tests.Services.Services
66
using FizzWare.NBuilder;
77
using FluentAssertions;
88
using FluentAssertions.Execution;
9+
using LearningHub.Nhs.Models.Entities.Activity;
910
using LearningHub.Nhs.Models.Entities.Resource;
1011
using LearningHub.Nhs.Models.Enums;
1112
using LearningHub.Nhs.Models.Search;
@@ -73,7 +74,7 @@ public async Task SearchPassesQueryOnToFindwise()
7374
.ReturnsAsync(FindwiseResultModel.Failure(FindwiseRequestStatus.Timeout));
7475

7576
// When
76-
await this.searchService.Search(searchRequest);
77+
await this.searchService.Search(searchRequest, null);
7778

7879
// Then
7980
this.findwiseClient.Verify(fc => fc.Search(searchRequest));
@@ -90,7 +91,7 @@ public async Task SearchReturnsTotalHitsAndSearchResult()
9091
this.GivenFindwiseReturnsSuccessfulResponse(74, Enumerable.Range(1, 34));
9192

9293
// When
93-
var searchResult = await this.searchService.Search(searchRequest);
94+
var searchResult = await this.searchService.Search(searchRequest, null);
9495

9596
// Then
9697
searchResult.Resources.Count.Should().Be(34);
@@ -137,7 +138,7 @@ public async Task SearchResultsReturnExpectedValues()
137138
this.GivenFindwiseReturnsSuccessfulResponse(2, new[] { 1, 2, 3 });
138139

139140
// When
140-
var searchResult = await this.searchService.Search(searchRequest);
141+
var searchResult = await this.searchService.Search(searchRequest, null);
141142

142143
// Then
143144
searchResult.Resources.Count.Should().Be(2);
@@ -180,7 +181,7 @@ public async Task SearchReturnsResourcesInOrderMatchingFindwise()
180181
.ReturnsAsync(resources);
181182

182183
// When
183-
var searchResultModel = await this.searchService.Search(new ResourceSearchRequest("text", 0, 10));
184+
var searchResultModel = await this.searchService.Search(new ResourceSearchRequest("text", 0, 10), null);
184185

185186
// Then
186187
searchResultModel.Resources.Select(r => r.ResourceId).Should().ContainInOrder(new[] { 1, 3, 2 });
@@ -194,7 +195,6 @@ public async Task SearchReplacesNullPropertiesOfResourceWithDefaultValues()
194195
{
195196
Builder<Resource>.CreateNew()
196197
.With(r => r.Id = 1)
197-
.With(r => r.CurrentResourceVersion = null)
198198
.With(
199199
r => r.ResourceReference = new[]
200200
{
@@ -212,7 +212,7 @@ public async Task SearchReplacesNullPropertiesOfResourceWithDefaultValues()
212212
this.GivenFindwiseReturnsSuccessfulResponse(1, new[] { 1 });
213213

214214
// When
215-
var searchResult = await this.searchService.Search(new ResourceSearchRequest("text", 0, 10));
215+
var searchResult = await this.searchService.Search(new ResourceSearchRequest("text", 0, 10), null);
216216

217217
// Then
218218
using var scope = new AssertionScope();
@@ -233,7 +233,9 @@ public async Task SearchReplacesNullPropertiesOfResourceWithDefaultValues()
233233
string.Empty,
234234
expectedResourceReferences,
235235
"Article",
236-
0));
236+
0,
237+
0,
238+
new List<MajorVersionIdActivityStatusDescription>(){ }));
237239
}
238240

239241
private void GivenFindwiseReturnsSuccessfulResponse(int totalHits, IEnumerable<int> resourceIds)

0 commit comments

Comments
 (0)