-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a read only view of linked player identities for their team owners …
- Loading branch information
1 parent
1b39a6a
commit c2b79ea
Showing
21 changed files
with
358 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
Stoolball.Web.UnitTests/Teams/LinkedPlayersForIdentityControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using Stoolball.Data.Abstractions; | ||
using Stoolball.Security; | ||
using Stoolball.Statistics; | ||
using Stoolball.Teams; | ||
using Stoolball.Web.Navigation; | ||
using Stoolball.Web.Statistics.Models; | ||
using Stoolball.Web.Teams; | ||
using Xunit; | ||
|
||
namespace Stoolball.Web.UnitTests.Teams | ||
{ | ||
public class LinkedPlayersForIdentityControllerTests : UmbracoBaseTest | ||
{ | ||
private readonly Mock<IPlayerDataSource> _playerDataSource = new(); | ||
private readonly Mock<IAuthorizationPolicy<Team>> _authorizationPolicy = new(); | ||
private readonly Mock<ITeamBreadcrumbBuilder> _breadcrumbBuilder = new(); | ||
|
||
private LinkedPlayersForIdentityController CreateController() | ||
{ | ||
return new LinkedPlayersForIdentityController( | ||
Mock.Of<ILogger<LinkedPlayersForIdentityController>>(), | ||
CompositeViewEngine.Object, | ||
UmbracoContextAccessor.Object, | ||
_authorizationPolicy.Object, | ||
_playerDataSource.Object, | ||
_breadcrumbBuilder.Object) | ||
{ | ||
ControllerContext = ControllerContext | ||
}; | ||
} | ||
|
||
private PlayerIdentity SetupMocks() | ||
{ | ||
var identity = new PlayerIdentity | ||
{ | ||
PlayerIdentityId = Guid.NewGuid(), | ||
PlayerIdentityName = "John Smith", | ||
Team = new Team(), | ||
Player = new Player { PlayerId = Guid.NewGuid(), PlayerRoute = "/players/example-player" } | ||
}; | ||
_playerDataSource.Setup(x => x.ReadPlayerIdentityByRoute(Request.Object.Path)).ReturnsAsync(identity); | ||
_playerDataSource.Setup(x => x.ReadPlayerByRoute(identity.Player.PlayerRoute, null)).ReturnsAsync(identity.Player); | ||
_authorizationPolicy.Setup(x => x.IsAuthorized(identity.Team)).Returns(Task.FromResult(new Dictionary<AuthorizedAction, bool> { { AuthorizedAction.EditTeam, true } })); | ||
return identity; | ||
} | ||
|
||
[Fact] | ||
public async Task Route_not_matching_identity_returns_404() | ||
{ | ||
_playerDataSource.Setup(x => x.ReadPlayerIdentityByRoute(Request.Object.Path)).Returns(Task.FromResult<PlayerIdentity?>(null)); | ||
|
||
using (var controller = CreateController()) | ||
{ | ||
var result = await controller.Index(); | ||
|
||
Assert.IsType<NotFoundResult>(result); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Route_matching_identity_sets_authorization() | ||
{ | ||
_ = SetupMocks(); | ||
|
||
using (var controller = CreateController()) | ||
{ | ||
var result = await controller.Index(); | ||
|
||
var model = (LinkedPlayersViewModel)((ViewResult)result).Model; | ||
|
||
Assert.True(model.Authorization.CurrentMemberIsAuthorized[AuthorizedAction.EditTeam]); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Route_matching_identity_returns_player_and_identity() | ||
{ | ||
var identity = SetupMocks(); | ||
|
||
using (var controller = CreateController()) | ||
{ | ||
var result = await controller.Index(); | ||
|
||
var model = (LinkedPlayersViewModel)((ViewResult)result).Model; | ||
|
||
Assert.Equal(identity, model.ContextIdentity); | ||
Assert.Equal(identity.Player, model.Player); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Route_matching_identity_sets_page_title() | ||
{ | ||
var identity = SetupMocks(); | ||
|
||
using (var controller = CreateController()) | ||
{ | ||
var result = await controller.Index(); | ||
|
||
var model = (LinkedPlayersViewModel)((ViewResult)result).Model; | ||
|
||
Assert.Equal($"Players linked to the statistics for {identity.PlayerIdentityName}", model.Metadata.PageTitle); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Route_matching_identity_sets_breadcrumbs() | ||
{ | ||
var identity = SetupMocks(); | ||
|
||
using (var controller = CreateController()) | ||
{ | ||
var result = await controller.Index(); | ||
|
||
var model = (LinkedPlayersViewModel)((ViewResult)result).Model; | ||
|
||
_breadcrumbBuilder.Verify(x => x.BuildBreadcrumbsForEditPlayers(model.Breadcrumbs, identity.Team!), Times.Once()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
Stoolball.Web/Statistics/Models/LinkedPlayersForMemberViewModel.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Stoolball.Statistics; | ||
using Stoolball.Web.Models; | ||
using Umbraco.Cms.Core.Models.PublishedContent; | ||
using Umbraco.Cms.Core.Services; | ||
|
||
namespace Stoolball.Web.Statistics.Models | ||
{ | ||
public class LinkedPlayersViewModel : BaseViewModel | ||
{ | ||
public LinkedPlayersViewModel(IPublishedContent? contentModel = null, IUserService? userService = null) : base(contentModel, userService) | ||
{ | ||
} | ||
|
||
public Player? Player { get; set; } | ||
|
||
public PlayerIdentity? ContextIdentity { get; set; } | ||
|
||
public string PreferredNextRoute { get; set; } = Constants.Pages.AccountUrl; | ||
|
||
public string LinkedByHeading { get; set; } = "Linked by"; | ||
public string LinkedByMemberLabel { get; set; } = PlayerIdentityLinkedBy.Member.ToString(); | ||
public string LinkedByClubOrTeamLabel { get; set; } = PlayerIdentityLinkedBy.ClubOrTeam.ToString(); | ||
public string LinkedByStoolballEnglandLabel { get; set; } = PlayerIdentityLinkedBy.StoolballEngland.ToString(); | ||
|
||
public bool CanUnlinkIdentitiesLinkedByMember { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.ViewEngines; | ||
using Microsoft.Extensions.Logging; | ||
using Stoolball.Data.Abstractions; | ||
using Stoolball.Security; | ||
using Stoolball.Teams; | ||
using Stoolball.Web.Navigation; | ||
using Stoolball.Web.Routing; | ||
using Stoolball.Web.Security; | ||
using Stoolball.Web.Statistics.Models; | ||
using Umbraco.Cms.Core.Web; | ||
using Umbraco.Cms.Web.Common.Controllers; | ||
|
||
namespace Stoolball.Web.Teams | ||
{ | ||
public class LinkedPlayersForIdentityController : RenderController, IRenderControllerAsync | ||
{ | ||
private readonly IAuthorizationPolicy<Team> _authorizationPolicy; | ||
private readonly IPlayerDataSource _playerDataSource; | ||
private readonly ITeamBreadcrumbBuilder _breadcrumbBuilder; | ||
|
||
public LinkedPlayersForIdentityController(ILogger<LinkedPlayersForIdentityController> logger, | ||
ICompositeViewEngine compositeViewEngine, | ||
IUmbracoContextAccessor umbracoContextAccessor, | ||
IAuthorizationPolicy<Team> authorizationPolicy, | ||
IPlayerDataSource playerDataSource, | ||
ITeamBreadcrumbBuilder breadcrumbBuilder) | ||
: base(logger, compositeViewEngine, umbracoContextAccessor) | ||
{ | ||
_authorizationPolicy = authorizationPolicy ?? throw new ArgumentNullException(nameof(authorizationPolicy)); | ||
_playerDataSource = playerDataSource ?? throw new ArgumentNullException(nameof(playerDataSource)); | ||
_breadcrumbBuilder = breadcrumbBuilder ?? throw new ArgumentNullException(nameof(breadcrumbBuilder)); | ||
} | ||
|
||
[HttpGet] | ||
[ContentSecurityPolicy] | ||
public async new Task<IActionResult> Index() | ||
{ | ||
var model = new LinkedPlayersViewModel(CurrentPage) | ||
{ | ||
ContextIdentity = await _playerDataSource.ReadPlayerIdentityByRoute(Request.Path) | ||
}; | ||
|
||
if (model.ContextIdentity?.Team == null || model.ContextIdentity?.Player == null) | ||
{ | ||
return NotFound(); | ||
} | ||
else | ||
{ | ||
model.Player = await _playerDataSource.ReadPlayerByRoute(model.ContextIdentity.Player!.PlayerRoute!); | ||
|
||
model.Authorization.CurrentMemberIsAuthorized = await _authorizationPolicy.IsAuthorized(model.ContextIdentity.Team); | ||
|
||
model.Metadata.PageTitle = "Players linked to the statistics for " + model.ContextIdentity.PlayerIdentityName; | ||
|
||
_breadcrumbBuilder.BuildBreadcrumbsForEditPlayers(model.Breadcrumbs, model.ContextIdentity.Team); | ||
|
||
return CurrentTemplate(model); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.