Skip to content

Commit 8ab7f6d

Browse files
test: Enhance ProfileControllerTests with activation code failure handling and linked players mapping
1 parent 8b65a7e commit 8ab7f6d

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

src/XtremeIdiots.Portal.Web.Tests/Controllers/ProfileControllerTests.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,92 @@ public async Task ActivateConnectedPlayerCode_WhenProfileFound_CallsActivationEn
134134
.Verify(x => x.ConnectedPlayers.V1.ActivateConnectedPlayerActivationCode(
135135
It.Is<ActivateConnectedPlayerActivationCodeDto>(dto => dto.UserProfileId == userProfileId),
136136
It.IsAny<CancellationToken>()), Times.Once);
137+
138+
Assert.Contains("Activation code generated", sut.TempData["Alerts"]?.ToString() ?? string.Empty, StringComparison.Ordinal);
139+
}
140+
141+
[Fact]
142+
public async Task ActivateConnectedPlayerCode_WhenActivationFails_AddsFailureAlert()
143+
{
144+
// Arrange
145+
var userProfileId = Guid.NewGuid();
146+
var userProfile = CreateUserProfileDto(userProfileId);
147+
148+
mockRepositoryApiClient
149+
.Setup(x => x.UserProfiles.V1.GetUserProfileByXtremeIdiotsId(It.IsAny<string>(), It.IsAny<CancellationToken>()))
150+
.ReturnsAsync(new ApiResult<UserProfileDto>(HttpStatusCode.OK, new ApiResponse<UserProfileDto>(userProfile)));
151+
152+
mockRepositoryApiClient
153+
.Setup(x => x.ConnectedPlayers.V1.ActivateConnectedPlayerActivationCode(It.IsAny<ActivateConnectedPlayerActivationCodeDto>(), It.IsAny<CancellationToken>()))
154+
.ReturnsAsync(new ApiResult<ConnectedPlayerActivationCodeDto>(HttpStatusCode.BadRequest));
155+
156+
var user = new ClaimsPrincipal(
157+
new ClaimsIdentity([
158+
new Claim(UserProfileClaimType.XtremeIdiotsId, "123456")
159+
], "TestAuth"));
160+
161+
var sut = CreateSut(user);
162+
163+
// Act
164+
var result = await sut.ActivateConnectedPlayerCode();
165+
166+
// Assert
167+
var redirectResult = Assert.IsType<RedirectToActionResult>(result);
168+
Assert.Equal(nameof(ProfileController.Manage), redirectResult.ActionName);
169+
Assert.Contains("Failed to generate activation code", sut.TempData["Alerts"]?.ToString() ?? string.Empty, StringComparison.Ordinal);
170+
}
171+
172+
[Fact]
173+
public async Task Manage_WhenProfileFound_MapsActivationAndLinkedPlayersWithCappedFlag()
174+
{
175+
// Arrange
176+
var userProfileId = Guid.NewGuid();
177+
var userProfile = CreateUserProfileDto(userProfileId);
178+
179+
mockRepositoryApiClient
180+
.Setup(x => x.UserProfiles.V1.GetUserProfileByXtremeIdiotsId(It.IsAny<string>(), It.IsAny<CancellationToken>()))
181+
.ReturnsAsync(new ApiResult<UserProfileDto>(HttpStatusCode.OK, new ApiResponse<UserProfileDto>(userProfile)));
182+
183+
var activationCode = CreateActivationCodeDto(userProfileId, "QW12ER");
184+
mockRepositoryApiClient
185+
.Setup(x => x.ConnectedPlayers.V1.GetActiveConnectedPlayerActivationCode(userProfileId, It.IsAny<CancellationToken>()))
186+
.ReturnsAsync(new ApiResult<ConnectedPlayerActivationCodeDto>(HttpStatusCode.OK, new ApiResponse<ConnectedPlayerActivationCodeDto>(activationCode)));
187+
188+
var linkedPlayers = new CollectionModel<ConnectedPlayerDto>([
189+
CreateConnectedPlayerDto(userProfileId, "Alpha", true, null),
190+
CreateConnectedPlayerDto(userProfileId, "Bravo", false, DateTime.UtcNow.AddDays(-1))
191+
]);
192+
var linkedPlayersResponse = new ApiResponse<CollectionModel<ConnectedPlayerDto>>(linkedPlayers)
193+
{
194+
Pagination = new ApiPagination(totalCount: 150, filteredCount: 150, skip: 0, top: 100)
195+
};
196+
197+
mockRepositoryApiClient
198+
.Setup(x => x.ConnectedPlayers.V1.GetConnectedPlayersByUserProfile(userProfileId, 0, 100, It.IsAny<CancellationToken>()))
199+
.ReturnsAsync(new ApiResult<CollectionModel<ConnectedPlayerDto>>(HttpStatusCode.OK, linkedPlayersResponse));
200+
201+
var user = new ClaimsPrincipal(
202+
new ClaimsIdentity([
203+
new Claim(UserProfileClaimType.XtremeIdiotsId, "123456")
204+
], "TestAuth"));
205+
206+
var sut = CreateSut(user);
207+
208+
// Act
209+
var result = await sut.Manage();
210+
211+
// Assert
212+
var viewResult = Assert.IsType<ViewResult>(result);
213+
var model = Assert.IsType<ProfileManageViewModel>(viewResult.Model);
214+
215+
Assert.Equal(userProfileId, model.UserProfileId);
216+
Assert.NotNull(model.ActiveActivationCode);
217+
Assert.Equal("QW12ER", model.ActiveActivationCode!.Code);
218+
Assert.Equal(2, model.LinkedPlayers.Count);
219+
Assert.Equal(150, model.TotalLinkedPlayers);
220+
Assert.True(model.IsLinkedPlayersCapped);
221+
Assert.Contains(model.LinkedPlayers, x => x.Username == "Alpha" && x.IsActive);
222+
Assert.Contains(model.LinkedPlayers, x => x.Username == "Bravo" && !x.IsActive);
137223
}
138224

139225
private static UserProfileDto CreateUserProfileDto(Guid userProfileId)
@@ -165,4 +251,24 @@ private static ConnectedPlayerActivationCodeDto CreateActivationCodeDto(Guid use
165251

166252
return JsonConvert.DeserializeObject<ConnectedPlayerActivationCodeDto>(json)!;
167253
}
254+
255+
private static ConnectedPlayerDto CreateConnectedPlayerDto(Guid userProfileId, string username, bool isActive, DateTime? unlinkedAtUtc)
256+
{
257+
var json = JsonConvert.SerializeObject(new
258+
{
259+
ConnectedPlayerProfileId = Guid.NewGuid(),
260+
PlayerId = Guid.NewGuid(),
261+
UserProfileId = userProfileId,
262+
GameType = GameType.CallOfDuty4,
263+
Username = username,
264+
LinkMethod = ConnectedPlayerLinkMethod.ActivationCode,
265+
LinkedAtUtc = DateTime.UtcNow.AddDays(-2),
266+
LinkedByUserProfileId = userProfileId,
267+
UnlinkedAtUtc = unlinkedAtUtc,
268+
UnlinkedByUserProfileId = unlinkedAtUtc.HasValue ? (Guid?)userProfileId : null,
269+
IsActive = isActive
270+
});
271+
272+
return JsonConvert.DeserializeObject<ConnectedPlayerDto>(json)!;
273+
}
168274
}

0 commit comments

Comments
 (0)