diff --git a/src/EventHub.Application/Organizations/OrganizationAppService.cs b/src/EventHub.Application/Organizations/OrganizationAppService.cs index 5ca0472..100bba2 100644 --- a/src/EventHub.Application/Organizations/OrganizationAppService.cs +++ b/src/EventHub.Application/Organizations/OrganizationAppService.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using EventHub.Permissions; using Microsoft.AspNetCore.Authorization; using Volo.Abp.Application.Dtos; +using Volo.Abp.Authorization; using Volo.Abp.Domain.Repositories; using Volo.Abp.Users; @@ -81,7 +82,7 @@ namespace EventHub.Organizations if (organization.OwnerUserId != CurrentUser.GetId()) { - return; + throw new AbpAuthorizationException(); } organization.SetDisplayName(input.DisplayName); diff --git a/src/EventHub.Web/Pages/Organizations/Edit.cshtml.cs b/src/EventHub.Web/Pages/Organizations/Edit.cshtml.cs index 79a210e..0bb0b9c 100644 --- a/src/EventHub.Web/Pages/Organizations/Edit.cshtml.cs +++ b/src/EventHub.Web/Pages/Organizations/Edit.cshtml.cs @@ -51,6 +51,7 @@ namespace EventHub.Web.Pages.Organizations } catch (Exception exception) { + ShowAlert(exception); return Page(); } } diff --git a/test/EventHub.Application.Tests/Organizations/OrganizationAppServiceTests.cs b/test/EventHub.Application.Tests/Organizations/OrganizationAppServiceTests.cs index 3e2f5f4..7b90118 100644 --- a/test/EventHub.Application.Tests/Organizations/OrganizationAppServiceTests.cs +++ b/test/EventHub.Application.Tests/Organizations/OrganizationAppServiceTests.cs @@ -1,7 +1,13 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; using Shouldly; using Volo.Abp; using Volo.Abp.Application.Dtos; +using Volo.Abp.Authorization; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Users; using Xunit; namespace EventHub.Organizations @@ -10,16 +16,25 @@ namespace EventHub.Organizations { private readonly IOrganizationAppService _organizationAppService; private readonly EventHubTestData _testData; - + private ICurrentUser _currentUser; + public OrganizationAppServiceTests() { _organizationAppService = GetRequiredService(); _testData = GetRequiredService(); } + + protected override void AfterAddApplication(IServiceCollection services) + { + _currentUser = Substitute.For(); + services.AddSingleton(_currentUser); + } [Fact] public async Task Should_Create_A_Valid_Organization() { + Login(_testData.UserAdminId); + await _organizationAppService.CreateAsync( new CreateOrganizationDto { @@ -38,6 +53,8 @@ namespace EventHub.Organizations [Fact] public async Task Should_Not_Create_Organization_With_Existing_Name() { + Login(_testData.UserAdminId); + var exception = await Assert.ThrowsAsync(() => _organizationAppService.CreateAsync( new CreateOrganizationDto @@ -68,5 +85,46 @@ namespace EventHub.Organizations result.Id.ShouldBe(_testData.OrganizationVolosoftId); result.Name.ShouldBe(_testData.OrganizationVolosoftName); } + + [Fact] + public async Task Should_Update_Organization_If_User_Is_Owner() + { + Login(_testData.UserAdminId); + + await _organizationAppService.UpdateAsync( + _testData.OrganizationVolosoftId, + new UpdateOrganizationDto + { + DisplayName = "VOLOSOFT", + Description = "Test description text that is valid and long enough for updating!", + Website = "https://volosoft.com/", + MediumUsername = "volosoft" + }); + } + + [Fact] + public async Task Should_Not_Update_Organization_If_User_Not_Owner() + { + Login(_testData.UserAdminId); + + var exception = await Assert.ThrowsAsync(() => + _organizationAppService.UpdateAsync( + _testData.OrganizationDotnetEuropeId, + new UpdateOrganizationDto + { + DisplayName = "Dotnet Europe (DE)", + Description = "Test description text that is valid and long enough for updating!", + Website = "https://dotnet-europe.com/" + }) + ); + + exception.ShouldNotBeNull(); + } + + private void Login(Guid userId) + { + _currentUser.Id.Returns(userId); + _currentUser.IsAuthenticated.Returns(true); + } } }