public async Task OrganisationByIdHandler_HappyPath_ReturnsOrganisationFromId()
        {
            var authorization = AuthorizationBuilder.CreateUserAllowedToAccessOrganisation();

            var organisationId = Guid.NewGuid();
            var context = A.Fake<WeeeContext>();
            A.CallTo(() => context.Organisations).Returns(dbHelper.GetAsyncEnabledDbSet(new List<Organisation>
            {
                GetOrganisationWithId(organisationId)
            }));

            OrganisationData expectedReturnValue = new OrganisationData();
            Organisation mappedOrganisation = null;
            var organisationMap = A.Fake<IMap<Organisation, OrganisationData>>();
            A.CallTo(() => organisationMap.Map(A<Organisation>._))
                .Invokes((Organisation o) => mappedOrganisation = o)
                .Returns(expectedReturnValue);

            var handler = new OrganisationByIdHandler(authorization, context, organisationMap);
            var message = new GetOrganisationInfo(organisationId);

            var result = await handler.HandleAsync(message);

            Assert.NotNull(mappedOrganisation);
            Assert.Equal(organisationId, mappedOrganisation.Id);
            Assert.Same(expectedReturnValue, result);
        }
        public async Task OrganisationByIdHandler_NotOrganisationUser_ThrowsSecurityException()
        {
            var authorization = AuthorizationBuilder.CreateUserDeniedFromAccessingOrganisation();

            var handler = new OrganisationByIdHandler(authorization, context, map);
            var message = new GetOrganisationInfo(Guid.NewGuid());

            await Assert.ThrowsAsync <SecurityException>(async() => await handler.HandleAsync(message));
        }
        public async Task OrganisationByIdHandler_NotOrganisationUser_ThrowsSecurityException()
        {
            var authorization = AuthorizationBuilder.CreateUserDeniedFromAccessingOrganisation();

            var handler = new OrganisationByIdHandler(authorization, A.Dummy<WeeeContext>(), A.Dummy<OrganisationMap>());
            var message = new GetOrganisationInfo(Guid.NewGuid());

            await Assert.ThrowsAsync<SecurityException>(async () => await handler.HandleAsync(message));
        }
        public async Task OrganisationByIdHandler_ReturnsFalseForCanEditOrganisation_WhenCurrentUserIsNotInternalAdmin()
        {
            var weeeAuthorization = new AuthorizationBuilder()
                                    .AllowInternalAreaAccess()
                                    .DenyRole(Roles.InternalAdmin)
                                    .Build();

            var handler = new OrganisationByIdHandler(weeeAuthorization, context, map);

            var message = new GetOrganisationInfo(organisationId);

            var result = await handler.HandleAsync(message);

            result.CanEditOrganisation.Should().BeFalse();
        }
        public async Task OrganisationByIdHandler_HappyPath_ReturnsOrganisationFromId()
        {
            A.CallTo(() => context.Organisations).Returns(dbHelper.GetAsyncEnabledDbSet(new List <Organisation>
            {
                GetOrganisationWithId(organisationId)
            }));

            var expectedReturnValue = new OrganisationData();

            A.CallTo(() => map.Map(A <Organisation> ._)).Returns(expectedReturnValue);

            var message = new GetOrganisationInfo(organisationId);

            var result = await handler.HandleAsync(message);

            Assert.NotNull(expectedReturnValue);
            Assert.Same(expectedReturnValue, result);
        }
        public async Task OrganisationByIdHandler_NoSuchOrganisation_ThrowsArgumentException()
        {
            var authorization = AuthorizationBuilder.CreateUserAllowedToAccessOrganisation();

            var organisationId = Guid.NewGuid();
            var context = A.Fake<WeeeContext>();
            A.CallTo(() => context.Organisations).Returns(dbHelper.GetAsyncEnabledDbSet(new List<Organisation>()));

            var handler = new OrganisationByIdHandler(authorization, context, A.Dummy<OrganisationMap>());
            var message = new GetOrganisationInfo(organisationId);

            var exception = await Assert.ThrowsAsync<ArgumentException>(async () => await handler.HandleAsync(message));

            Assert.True(exception.Message.Contains(organisationId.ToString()));
            Assert.True(exception.Message.ToUpperInvariant().Contains("COULD NOT FIND"));
            Assert.True(exception.Message.ToUpperInvariant().Contains("ORGANISATION"));
        }
        public async Task OrganisationByIdHandler_NoSuchOrganisation_ThrowsArgumentException()
        {
            var authorization = AuthorizationBuilder.CreateUserAllowedToAccessOrganisation();

            A.CallTo(() => context.Organisations).Returns(dbHelper.GetAsyncEnabledDbSet(new List <Organisation>()));

            var handler = new OrganisationByIdHandler(authorization, context, map);
            var message = new GetOrganisationInfo(organisationId);

            var exception = await Assert.ThrowsAsync <ArgumentException>(async() => await handler.HandleAsync(message));

            Assert.True(exception.Message.Contains(organisationId.ToString()));
            Assert.True(exception.Message.ToUpperInvariant().Contains("COULD NOT FIND"));
            Assert.True(exception.Message.ToUpperInvariant().Contains("ORGANISATION"));
        }