public async Task HandleAsync_WithNonInternalAccess_ThrowsSecurityException(AuthorizationBuilder.UserType userType)
        {
            // Arrange
            var authorization = AuthorizationBuilder.CreateFromUserType(userType);
            var dataAccess    = A.Fake <IOrganisationDetailsDataAccess>();

            var handler = new UpdateOrganisationDetailsHandler(dataAccess, authorization);

            // Act
            Func <Task> action = async() => await handler.HandleAsync(A.Dummy <UpdateOrganisationDetails>());

            // Assert
            await Assert.ThrowsAsync <SecurityException>(action);
        }
        public async Task HandleAsync_WithNonInternalAccess_ThrowsSecurityException(AuthorizationBuilder.UserType userType)
        {
            // Arrange
            var authorization = AuthorizationBuilder.CreateFromUserType(userType);
            var dataAccess = A.Fake<IOrganisationDetailsDataAccess>();

            var handler = new UpdateOrganisationDetailsHandler(dataAccess, authorization);
            
            // Act
            Func<Task> action = async () => await handler.HandleAsync(A.Dummy<UpdateOrganisationDetails>());

            // Assert
            await Assert.ThrowsAsync<SecurityException>(action);
        }
        public async Task HandleAsync_WithNonInternalAdminRole_ThrowsSecurityException()
        {
            // Arrange
            IWeeeAuthorization authorization = new AuthorizationBuilder()
                                               .AllowInternalAreaAccess()
                                               .DenyRole(Roles.InternalAdmin)
                                               .Build();

            var dataAccess = A.Fake <IOrganisationDetailsDataAccess>();

            var handler = new UpdateOrganisationDetailsHandler(dataAccess, authorization);

            // Act
            Func <Task> action = async() => await handler.HandleAsync(A.Dummy <UpdateOrganisationDetails>());

            // Assert
            await Assert.ThrowsAsync <SecurityException>(action);
        }
        public async Task HandleAsync_WithNonInternalAdminRole_ThrowsSecurityException()
        {
            // Arrange
            IWeeeAuthorization authorization = new AuthorizationBuilder()
                .AllowInternalAreaAccess()
                .DenyRole(Roles.InternalAdmin)
                .Build();
            
            var dataAccess = A.Fake<IOrganisationDetailsDataAccess>();

            var handler = new UpdateOrganisationDetailsHandler(dataAccess, authorization);

            // Act
            Func<Task> action = async () => await handler.HandleAsync(A.Dummy<UpdateOrganisationDetails>());

            // Assert
            await Assert.ThrowsAsync<SecurityException>(action);
        }
        public UpdateOrganisationDetailsHandlerTests()
        {
            userContext = A.Fake<IUserContext>();
            A.CallTo(() => userContext.UserId).Returns(userId);

            context = new TestIwsContext(userContext);

            var country = CountryFactory.Create(countryId);
            context.Countries.Add(country);

            address = new Address(address1, address2, town, null, postcode, country.Name);

            context.Organisations.Add(GetOrganisation());

            context.Users.Add(GetUser());

            handler = new UpdateOrganisationDetailsHandler(context, userContext);
        }
        public UpdateOrganisationDetailsHandlerTests()
        {
            userContext = A.Fake <IUserContext>();
            A.CallTo(() => userContext.UserId).Returns(userId);

            context = new TestIwsContext(userContext);

            var country = CountryFactory.Create(countryId);

            context.Countries.Add(country);

            address = new Address(address1, address2, town, null, postcode, country.Name);

            context.Organisations.Add(GetOrganisation());

            context.Users.Add(GetUser());

            handler = new UpdateOrganisationDetailsHandler(context, userContext);
        }
        public async Task UpdateOrganisationDetailsHandler_WithValidData_FetchesOrganisationAndUpdatesAndSaves()
        {
            // Arrange
            var organisationData = new OrganisationData
            {
                Id = new Guid("9a310218-311b-460d-bd50-9d246c237dcc"),
                OrganisationType          = OrganisationType.RegisteredCompany,
                Name                      = "CompanyName",
                CompanyRegistrationNumber = "123456789",
                OrganisationName          = "CompanyName",
                BusinessAddress           = new Core.Shared.AddressData
                {
                    Address1       = "Address1",
                    Address2       = "Address2",
                    TownOrCity     = "Town",
                    CountyOrRegion = "County",
                    Postcode       = "Postcode",
                    CountryId      = new Guid("79b70dfb-bbfd-4801-9849-880f66ee48e4"),
                    Telephone      = "012345678",
                    Email          = "*****@*****.**"
                }
            };

            var request = new UpdateOrganisationDetails(organisationData);

            var dataAccess        = A.Fake <IOrganisationDetailsDataAccess>();
            var weeeAuthorization = A.Fake <IWeeeAuthorization>();

            var organisation = A.Dummy <Organisation>();

            A.CallTo(() => dataAccess.FetchOrganisationAsync(new Guid("9a310218-311b-460d-bd50-9d246c237dcc")))
            .Returns(organisation);

            var country = new Country(
                new Guid("79b70dfb-bbfd-4801-9849-880f66ee48e4"),
                "Name");

            A.CallTo(() => dataAccess.FetchCountryAsync(new Guid("79b70dfb-bbfd-4801-9849-880f66ee48e4")))
            .Returns(country);

            var handler = new UpdateOrganisationDetailsHandler(dataAccess, weeeAuthorization);

            // Act
            var result = await handler.HandleAsync(request);

            // Assert
            A.CallTo(() => dataAccess.FetchOrganisationAsync(new Guid("9a310218-311b-460d-bd50-9d246c237dcc")))
            .MustHaveHappened(Repeated.Exactly.Once);

            Assert.Equal("CompanyName", organisation.Name);
            Assert.Equal("123456789", organisation.CompanyRegistrationNumber);
            Assert.Equal("Address1", organisation.BusinessAddress.Address1);
            Assert.Equal("Address2", organisation.BusinessAddress.Address2);
            Assert.Equal("Town", organisation.BusinessAddress.TownOrCity);
            Assert.Equal("County", organisation.BusinessAddress.CountyOrRegion);
            Assert.Equal("Postcode", organisation.BusinessAddress.Postcode);
            Assert.Equal(new Guid("79b70dfb-bbfd-4801-9849-880f66ee48e4"), organisation.BusinessAddress.Country.Id);
            Assert.Equal("012345678", organisation.BusinessAddress.Telephone);
            Assert.Equal("*****@*****.**", organisation.BusinessAddress.Email);

            A.CallTo(() => dataAccess.SaveAsync())
            .MustHaveHappened(Repeated.Exactly.Once);

            Assert.Equal(result, true);
        }
        public async Task UpdateOrganisationDetailsHandler_WithValidData_FetchesOrganisationAndUpdatesAndSaves()
        {
            // Arrange
            OrganisationData organisationData = new OrganisationData();
            organisationData.Id = new Guid("9a310218-311b-460d-bd50-9d246c237dcc");
            organisationData.OrganisationType = OrganisationType.RegisteredCompany;
            organisationData.Name = "CompanyName";
            organisationData.CompanyRegistrationNumber = "123456789";
            organisationData.OrganisationName = "CompanyName";
            organisationData.BusinessAddress = new Core.Shared.AddressData();
            organisationData.BusinessAddress.Address1 = "Address1";
            organisationData.BusinessAddress.Address2 = "Address2";
            organisationData.BusinessAddress.TownOrCity = "Town";
            organisationData.BusinessAddress.CountyOrRegion = "County";
            organisationData.BusinessAddress.Postcode = "Postcode";
            organisationData.BusinessAddress.CountryId = new Guid("79b70dfb-bbfd-4801-9849-880f66ee48e4");
            organisationData.BusinessAddress.Telephone = "012345678";
            organisationData.BusinessAddress.Email = "*****@*****.**";

            UpdateOrganisationDetails request = new UpdateOrganisationDetails(organisationData);

            IOrganisationDetailsDataAccess dataAccess = A.Fake<IOrganisationDetailsDataAccess>();
            IWeeeAuthorization weeeAuthorization = A.Fake<IWeeeAuthorization>();

            Organisation organisation = A.Dummy<Organisation>();
            A.CallTo(() => dataAccess.FetchOrganisationAsync(new Guid("9a310218-311b-460d-bd50-9d246c237dcc")))
                .Returns(organisation);

            Country country = new Country(
                new Guid("79b70dfb-bbfd-4801-9849-880f66ee48e4"),
                "Name");
            A.CallTo(() => dataAccess.FetchCountryAsync(new Guid("79b70dfb-bbfd-4801-9849-880f66ee48e4")))
                .Returns(country);

            UpdateOrganisationDetailsHandler handler = new UpdateOrganisationDetailsHandler(dataAccess, weeeAuthorization);

            // Act
            bool result = await handler.HandleAsync(request);

            // Assert
            A.CallTo(() => dataAccess.FetchOrganisationAsync(new Guid("9a310218-311b-460d-bd50-9d246c237dcc")))
                .MustHaveHappened(Repeated.Exactly.Once);

            Assert.Equal("CompanyName", organisation.Name);
            Assert.Equal("123456789", organisation.CompanyRegistrationNumber);
            Assert.Equal("Address1", organisation.BusinessAddress.Address1);
            Assert.Equal("Address2", organisation.BusinessAddress.Address2);
            Assert.Equal("Town", organisation.BusinessAddress.TownOrCity);
            Assert.Equal("County", organisation.BusinessAddress.CountyOrRegion);
            Assert.Equal("Postcode", organisation.BusinessAddress.Postcode);
            Assert.Equal(new Guid("79b70dfb-bbfd-4801-9849-880f66ee48e4"), organisation.BusinessAddress.Country.Id);
            Assert.Equal("012345678", organisation.BusinessAddress.Telephone);
            Assert.Equal("*****@*****.**", organisation.BusinessAddress.Email);

            A.CallTo(() => dataAccess.SaveAsync())
                .MustHaveHappened(Repeated.Exactly.Once);

            Assert.Equal(result, true);
        }