|
| 1 | +using Microsoft.AspNetCore.Authorization; |
| 2 | +using System.Security.Claims; |
| 3 | +using XtremeIdiots.Portal.Repository.Abstractions.Constants.V1; |
| 4 | +using XtremeIdiots.Portal.Web.Auth.Constants; |
| 5 | +using XtremeIdiots.Portal.Web.Auth.Handlers; |
| 6 | +using XtremeIdiots.Portal.Web.Auth.Requirements; |
| 7 | + |
| 8 | +namespace XtremeIdiots.Portal.Web.Tests.Auth.Handlers; |
| 9 | + |
| 10 | +public class AdminActionsAuthHandlerTests |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public async Task HandleAsync_Create_SucceedsForCod4ClaimOnCod4xResource() |
| 14 | + { |
| 15 | + var requirement = new AdminActionsCreate(); |
| 16 | + var user = CreateUser(new Claim(UserProfileClaimType.GameAdmin, GameType.CallOfDuty4.ToString())); |
| 17 | + var context = new AuthorizationHandlerContext([requirement], user, (GameType.CallOfDuty4x, AdminActionType.Ban)); |
| 18 | + |
| 19 | + var sut = new AdminActionsAuthHandler(); |
| 20 | + await sut.HandleAsync(context); |
| 21 | + |
| 22 | + Assert.True(context.HasSucceeded); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public async Task HandleAsync_Create_SucceedsForCod4PermissionClaimOnCod4xResource() |
| 27 | + { |
| 28 | + var requirement = new AdminActionsCreate(); |
| 29 | + var user = CreateUser(new Claim(AuthPolicies.AdminActions_Create, GameType.CallOfDuty4.ToString())); |
| 30 | + var context = new AuthorizationHandlerContext([requirement], user, (GameType.CallOfDuty4x, AdminActionType.Ban)); |
| 31 | + |
| 32 | + var sut = new AdminActionsAuthHandler(); |
| 33 | + await sut.HandleAsync(context); |
| 34 | + |
| 35 | + Assert.True(context.HasSucceeded); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public async Task HandleAsync_Create_SucceedsForCod4xClaimOnCod4Resource() |
| 40 | + { |
| 41 | + var requirement = new AdminActionsCreate(); |
| 42 | + var user = CreateUser(new Claim(UserProfileClaimType.GameAdmin, GameType.CallOfDuty4x.ToString())); |
| 43 | + var context = new AuthorizationHandlerContext([requirement], user, (GameType.CallOfDuty4, AdminActionType.Ban)); |
| 44 | + |
| 45 | + var sut = new AdminActionsAuthHandler(); |
| 46 | + await sut.HandleAsync(context); |
| 47 | + |
| 48 | + Assert.True(context.HasSucceeded); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task HandleAsync_Create_DoesNotSucceedForDifferentGame() |
| 53 | + { |
| 54 | + var requirement = new AdminActionsCreate(); |
| 55 | + var user = CreateUser(new Claim(UserProfileClaimType.GameAdmin, GameType.Insurgency.ToString())); |
| 56 | + var context = new AuthorizationHandlerContext([requirement], user, (GameType.CallOfDuty4x, AdminActionType.Ban)); |
| 57 | + |
| 58 | + var sut = new AdminActionsAuthHandler(); |
| 59 | + await sut.HandleAsync(context); |
| 60 | + |
| 61 | + Assert.False(context.HasSucceeded); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public async Task HandleAsync_EditObservation_SucceedsForCod4ModeratorOnCod4xWhenOwner() |
| 66 | + { |
| 67 | + var requirement = new AdminActionsEdit(); |
| 68 | + var ownerId = "owner-1"; |
| 69 | + var user = CreateUser( |
| 70 | + new Claim(UserProfileClaimType.Moderator, GameType.CallOfDuty4.ToString()), |
| 71 | + new Claim(UserProfileClaimType.XtremeIdiotsId, ownerId)); |
| 72 | + |
| 73 | + var context = new AuthorizationHandlerContext( |
| 74 | + [requirement], |
| 75 | + user, |
| 76 | + (GameType.CallOfDuty4x, AdminActionType.Observation, ownerId)); |
| 77 | + |
| 78 | + var sut = new AdminActionsAuthHandler(); |
| 79 | + await sut.HandleAsync(context); |
| 80 | + |
| 81 | + Assert.True(context.HasSucceeded); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public async Task HandleAsync_EditObservation_DoesNotSucceedForCod4ModeratorOnCod4xWhenNotOwner() |
| 86 | + { |
| 87 | + var requirement = new AdminActionsEdit(); |
| 88 | + var ownerId = "owner-1"; |
| 89 | + var user = CreateUser( |
| 90 | + new Claim(UserProfileClaimType.Moderator, GameType.CallOfDuty4.ToString()), |
| 91 | + new Claim(UserProfileClaimType.XtremeIdiotsId, "different-owner")); |
| 92 | + |
| 93 | + var context = new AuthorizationHandlerContext( |
| 94 | + [requirement], |
| 95 | + user, |
| 96 | + (GameType.CallOfDuty4x, AdminActionType.Observation, ownerId)); |
| 97 | + |
| 98 | + var sut = new AdminActionsAuthHandler(); |
| 99 | + await sut.HandleAsync(context); |
| 100 | + |
| 101 | + Assert.False(context.HasSucceeded); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public async Task HandleAsync_Lift_SucceedsForCod4GameAdminOnCod4xWhenOwner() |
| 106 | + { |
| 107 | + var requirement = new AdminActionsLift(); |
| 108 | + var ownerId = "owner-2"; |
| 109 | + var user = CreateUser( |
| 110 | + new Claim(UserProfileClaimType.GameAdmin, GameType.CallOfDuty4.ToString()), |
| 111 | + new Claim(UserProfileClaimType.XtremeIdiotsId, ownerId)); |
| 112 | + |
| 113 | + var context = new AuthorizationHandlerContext( |
| 114 | + [requirement], |
| 115 | + user, |
| 116 | + (GameType.CallOfDuty4x, ownerId)); |
| 117 | + |
| 118 | + var sut = new AdminActionsAuthHandler(); |
| 119 | + await sut.HandleAsync(context); |
| 120 | + |
| 121 | + Assert.True(context.HasSucceeded); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public async Task HandleAsync_Lift_DoesNotSucceedForCod4GameAdminOnCod4xWhenNotOwner() |
| 126 | + { |
| 127 | + var requirement = new AdminActionsLift(); |
| 128 | + var ownerId = "owner-2"; |
| 129 | + var user = CreateUser( |
| 130 | + new Claim(UserProfileClaimType.GameAdmin, GameType.CallOfDuty4.ToString()), |
| 131 | + new Claim(UserProfileClaimType.XtremeIdiotsId, "different-owner")); |
| 132 | + |
| 133 | + var context = new AuthorizationHandlerContext( |
| 134 | + [requirement], |
| 135 | + user, |
| 136 | + new Tuple<GameType, string>(GameType.CallOfDuty4x, ownerId)); |
| 137 | + |
| 138 | + var sut = new AdminActionsAuthHandler(); |
| 139 | + await sut.HandleAsync(context); |
| 140 | + |
| 141 | + Assert.False(context.HasSucceeded); |
| 142 | + } |
| 143 | + |
| 144 | + private static ClaimsPrincipal CreateUser(params Claim[] claims) |
| 145 | + { |
| 146 | + var identity = new ClaimsIdentity(claims, authenticationType: "TestAuthType"); |
| 147 | + return new ClaimsPrincipal(identity); |
| 148 | + } |
| 149 | +} |
0 commit comments