|
| 1 | +using System.Reflection; |
| 2 | +using Bit.Api.AdminConsole.Attributes; |
| 3 | +using Bit.Core.Entities; |
| 4 | +using Bit.Core.Exceptions; |
| 5 | +using Bit.Core.Repositories; |
| 6 | +using Microsoft.AspNetCore.Http; |
| 7 | +using Microsoft.AspNetCore.Mvc; |
| 8 | +using Microsoft.AspNetCore.Mvc.ModelBinding; |
| 9 | +using Microsoft.AspNetCore.Routing; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using NSubstitute; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace Bit.Api.Test.AdminConsole.Attributes; |
| 15 | + |
| 16 | +public class OrganizationUserModelBinderTests |
| 17 | +{ |
| 18 | + private readonly IOrganizationUserRepository _organizationUserRepository; |
| 19 | + private readonly OrganizationUser _organizationUser; |
| 20 | + private readonly Guid _orgId; |
| 21 | + private readonly Guid _orgUserId; |
| 22 | + |
| 23 | + public OrganizationUserModelBinderTests() |
| 24 | + { |
| 25 | + _organizationUserRepository = Substitute.For<IOrganizationUserRepository>(); |
| 26 | + _orgId = Guid.NewGuid(); |
| 27 | + _orgUserId = Guid.NewGuid(); |
| 28 | + _organizationUser = new OrganizationUser { Id = _orgUserId, OrganizationId = _orgId }; |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public async Task BindModelAsync_OrgUserExistsAndBelongsToOrg_BindsSuccessfully() |
| 33 | + { |
| 34 | + var binder = new OrganizationUserModelBinder(); |
| 35 | + _organizationUserRepository.GetByIdAsync(_orgUserId).Returns(_organizationUser); |
| 36 | + |
| 37 | + var context = CreateBindingContext(); |
| 38 | + |
| 39 | + await binder.BindModelAsync(context); |
| 40 | + |
| 41 | + Assert.True(context.Result.IsModelSet); |
| 42 | + Assert.Equal(_organizationUser, context.Result.Model); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public async Task BindModelAsync_OrgUserNotFound_ThrowsNotFoundException() |
| 47 | + { |
| 48 | + var binder = new OrganizationUserModelBinder(); |
| 49 | + _organizationUserRepository.GetByIdAsync(_orgUserId).Returns((OrganizationUser)null); |
| 50 | + |
| 51 | + var context = CreateBindingContext(); |
| 52 | + |
| 53 | + await Assert.ThrowsAsync<NotFoundException>(() => binder.BindModelAsync(context)); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public async Task BindModelAsync_OrgUserBelongsToDifferentOrg_ThrowsNotFoundException() |
| 58 | + { |
| 59 | + var binder = new OrganizationUserModelBinder(); |
| 60 | + var wrongOrgUser = new OrganizationUser { Id = _orgUserId, OrganizationId = Guid.NewGuid() }; |
| 61 | + _organizationUserRepository.GetByIdAsync(_orgUserId).Returns(wrongOrgUser); |
| 62 | + |
| 63 | + var context = CreateBindingContext(); |
| 64 | + |
| 65 | + await Assert.ThrowsAsync<NotFoundException>(() => binder.BindModelAsync(context)); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public async Task BindModelAsync_InvalidOrgId_ThrowsBadRequestException() |
| 70 | + { |
| 71 | + var binder = new OrganizationUserModelBinder(); |
| 72 | + var context = CreateBindingContext(orgIdRouteValue: "not-a-guid"); |
| 73 | + |
| 74 | + var exception = await Assert.ThrowsAsync<BadRequestException>(() => binder.BindModelAsync(context)); |
| 75 | + Assert.Equal("Route parameter 'orgId' or 'organizationId' is missing or invalid.", exception.Message); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public async Task BindModelAsync_MissingOrgId_ThrowsBadRequestException() |
| 80 | + { |
| 81 | + var binder = new OrganizationUserModelBinder(); |
| 82 | + var context = CreateBindingContext(includeOrgId: false); |
| 83 | + |
| 84 | + var exception = await Assert.ThrowsAsync<BadRequestException>(() => binder.BindModelAsync(context)); |
| 85 | + Assert.Equal("Route parameter 'orgId' or 'organizationId' is missing or invalid.", exception.Message); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public async Task BindModelAsync_InvalidOrgUserId_ThrowsBadRequestException() |
| 90 | + { |
| 91 | + var binder = new OrganizationUserModelBinder(); |
| 92 | + var context = CreateBindingContext(orgUserIdRouteValue: "not-a-guid"); |
| 93 | + |
| 94 | + var exception = await Assert.ThrowsAsync<BadRequestException>(() => binder.BindModelAsync(context)); |
| 95 | + Assert.Equal("Route parameter 'id' is missing or invalid.", exception.Message); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public async Task BindModelAsync_MissingOrgUserId_ThrowsBadRequestException() |
| 100 | + { |
| 101 | + var binder = new OrganizationUserModelBinder(); |
| 102 | + var context = CreateBindingContext(includeOrgUserId: false); |
| 103 | + |
| 104 | + var exception = await Assert.ThrowsAsync<BadRequestException>(() => binder.BindModelAsync(context)); |
| 105 | + Assert.Equal("Route parameter 'id' is missing or invalid.", exception.Message); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public async Task BindModelAsync_OrganizationIdRouteParam_ResolvesOrgId() |
| 110 | + { |
| 111 | + var binder = new OrganizationUserModelBinder(); |
| 112 | + _organizationUserRepository.GetByIdAsync(_orgUserId).Returns(_organizationUser); |
| 113 | + |
| 114 | + var context = CreateBindingContext(useOrganizationIdRoute: true); |
| 115 | + |
| 116 | + await binder.BindModelAsync(context); |
| 117 | + |
| 118 | + Assert.True(context.Result.IsModelSet); |
| 119 | + Assert.Equal(_organizationUser, context.Result.Model); |
| 120 | + } |
| 121 | + |
| 122 | + [Fact] |
| 123 | + public async Task BindModelAsync_CustomRouteParamName_ReadsCorrectRouteValue() |
| 124 | + { |
| 125 | + var binder = new OrganizationUserModelBinder(); |
| 126 | + _organizationUserRepository.GetByIdAsync(_orgUserId).Returns(_organizationUser); |
| 127 | + |
| 128 | + var parameterInfo = typeof(OrganizationUserModelBinderTests) |
| 129 | + .GetMethod(nameof(DummyMethodWithCustomRouteParam), BindingFlags.NonPublic | BindingFlags.Static)! |
| 130 | + .GetParameters()[0]; |
| 131 | + |
| 132 | + var context = CreateBindingContext( |
| 133 | + orgUserIdRouteKey: "organizationUserId", |
| 134 | + parameterInfo: parameterInfo); |
| 135 | + |
| 136 | + await binder.BindModelAsync(context); |
| 137 | + |
| 138 | + Assert.True(context.Result.IsModelSet); |
| 139 | + Assert.Equal(_organizationUser, context.Result.Model); |
| 140 | + } |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// Dummy method used to produce a <see cref="ParameterInfo"/> carrying a custom |
| 144 | + /// <see cref="InjectOrganizationUserAttribute"/> for the custom route param test. |
| 145 | + /// </summary> |
| 146 | + private static void DummyMethodWithCustomRouteParam( |
| 147 | + [InjectOrganizationUser("organizationUserId")] OrganizationUser user) |
| 148 | + { } |
| 149 | + |
| 150 | + private DefaultModelBindingContext CreateBindingContext( |
| 151 | + string orgIdRouteValue = null, |
| 152 | + string orgUserIdRouteValue = null, |
| 153 | + string orgUserIdRouteKey = "id", |
| 154 | + bool includeOrgId = true, |
| 155 | + bool includeOrgUserId = true, |
| 156 | + bool useOrganizationIdRoute = false, |
| 157 | + ParameterInfo parameterInfo = null) |
| 158 | + { |
| 159 | + var httpContext = new DefaultHttpContext(); |
| 160 | + var services = new ServiceCollection(); |
| 161 | + services.AddScoped(_ => _organizationUserRepository); |
| 162 | + httpContext.RequestServices = services.BuildServiceProvider(); |
| 163 | + |
| 164 | + var routeData = new RouteData(); |
| 165 | + if (includeOrgId) |
| 166 | + { |
| 167 | + var key = useOrganizationIdRoute ? "organizationId" : "orgId"; |
| 168 | + routeData.Values[key] = orgIdRouteValue ?? _orgId.ToString(); |
| 169 | + } |
| 170 | + if (includeOrgUserId) |
| 171 | + { |
| 172 | + routeData.Values[orgUserIdRouteKey] = orgUserIdRouteValue ?? _orgUserId.ToString(); |
| 173 | + } |
| 174 | + |
| 175 | + httpContext.Request.RouteValues = routeData.Values; |
| 176 | + |
| 177 | + var actionContext = new ActionContext( |
| 178 | + httpContext, |
| 179 | + routeData, |
| 180 | + new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor(), |
| 181 | + new ModelStateDictionary()); |
| 182 | + |
| 183 | + var metadataProvider = new EmptyModelMetadataProvider(); |
| 184 | + ModelMetadata metadata; |
| 185 | + |
| 186 | + if (parameterInfo != null) |
| 187 | + { |
| 188 | + metadata = metadataProvider.GetMetadataForParameter(parameterInfo); |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + metadata = metadataProvider.GetMetadataForType(typeof(OrganizationUser)); |
| 193 | + } |
| 194 | + |
| 195 | + return new DefaultModelBindingContext |
| 196 | + { |
| 197 | + ActionContext = actionContext, |
| 198 | + ModelMetadata = metadata, |
| 199 | + ModelName = "organizationUser" |
| 200 | + }; |
| 201 | + } |
| 202 | +} |
0 commit comments