public async Task VerifyOrganisationExistsHandler_NotOrganisationOrInternalUser_ThrowsSecurityException()
        {
            var deniedAuthorization = AuthorizationBuilder.CreateUserDeniedFromAccessingOrganisation();

            var handler = new VerifyOrganisationExistsHandler(deniedAuthorization, A.Dummy <WeeeContext>());
            var message = new VerifyOrganisationExists(Guid.NewGuid());

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

            var handler = new VerifyOrganisationExistsHandler(deniedAuthorization, A.Dummy<WeeeContext>());
            var message = new VerifyOrganisationExists(Guid.NewGuid());

            await Assert.ThrowsAsync<SecurityException>(async () => await handler.HandleAsync(message));
        }
        public async Task VerifyOrganisationExistsHandler_OrgDoesntExist_ReturnsFalse()
        {
            var organisations = MakeOrganisation();

            var context = A.Fake <WeeeContext>();

            A.CallTo(() => context.Organisations).Returns(organisations);

            var handler = new VerifyOrganisationExistsHandler(permissiveAuthorization, context);

            var exists = await handler.HandleAsync(new VerifyOrganisationExists(Guid.NewGuid()));

            Assert.False(exists);
        }
        public async Task VerifyOrganisationExistsHandler_OrgExists_ReturnsTrue()
        {
            var organisations = MakeOrganisation();

            var context = A.Fake <WeeeContext>();

            A.CallTo(() => context.Organisations).Returns(organisations);

            var handler = new VerifyOrganisationExistsHandler(permissiveAuthorization, context);

            var exists = await handler.HandleAsync(new VerifyOrganisationExists(organisations.FirstOrDefault().Id));

            Assert.True(exists);
        }
        public async Task VerifyOrganisationExistsHandler_OrgExists_ReturnsTrue()
        {
            var organisations = MakeOrganisation();

            var context = A.Fake<WeeeContext>();

            A.CallTo(() => context.Organisations).Returns(organisations);

            var handler = new VerifyOrganisationExistsHandler(permissiveAuthorization, context);

            var exists = await handler.HandleAsync(new VerifyOrganisationExists(organisations.FirstOrDefault().Id));

            Assert.True(exists);
        }
        public async Task VerifyOrganisationExistsHandler_OrganisationUser_PassesSecurityCheck()
        {
            var organisationAuthorization = AuthorizationBuilder.CreateUserAllowedToAccessOrganisation();

            var organisations = MakeOrganisation();
            var context = A.Fake<WeeeContext>();
            A.CallTo(() => context.Organisations).Returns(organisations);

            var handler = new VerifyOrganisationExistsHandler(organisationAuthorization, context);
            var message = new VerifyOrganisationExists(Guid.NewGuid());

            await handler.HandleAsync(message);

            A.CallTo(() => context.Organisations).MustHaveHappened();
        }
        public async Task VerifyOrganisationExistsHandler_InternalUser_PassesSecurityCheck()
        {
            var internalAuthorization = AuthorizationBuilder.CreateFromUserType(AuthorizationBuilder.UserType.Internal);

            var organisations = MakeOrganisation();
            var context = A.Fake<WeeeContext>();
            A.CallTo(() => context.Organisations).Returns(organisations);

            var handler = new VerifyOrganisationExistsHandler(internalAuthorization, context);
            var message = new VerifyOrganisationExists(Guid.NewGuid());

            await handler.HandleAsync(message);

            A.CallTo(() => context.Organisations).MustHaveHappened();
        }
        public async Task VerifyOrganisationExistsHandler_OrganisationUser_PassesSecurityCheck()
        {
            var organisationAuthorization = AuthorizationBuilder.CreateUserAllowedToAccessOrganisation();

            var organisations = MakeOrganisation();
            var context       = A.Fake <WeeeContext>();

            A.CallTo(() => context.Organisations).Returns(organisations);

            var handler = new VerifyOrganisationExistsHandler(organisationAuthorization, context);
            var message = new VerifyOrganisationExists(Guid.NewGuid());

            await handler.HandleAsync(message);

            A.CallTo(() => context.Organisations).MustHaveHappened();
        }
        public async Task VerifyOrganisationExistsHandler_InternalUser_PassesSecurityCheck()
        {
            var internalAuthorization = AuthorizationBuilder.CreateFromUserType(AuthorizationBuilder.UserType.Internal);

            var organisations = MakeOrganisation();
            var context       = A.Fake <WeeeContext>();

            A.CallTo(() => context.Organisations).Returns(organisations);

            var handler = new VerifyOrganisationExistsHandler(internalAuthorization, context);
            var message = new VerifyOrganisationExists(Guid.NewGuid());

            await handler.HandleAsync(message);

            A.CallTo(() => context.Organisations).MustHaveHappened();
        }
        public async Task VerifyOrganisationExistsHandler_OrgDoesntExist_ReturnsFalse()
        {
            var organisations = MakeOrganisation();

            var context = A.Fake<WeeeContext>();

            A.CallTo(() => context.Organisations).Returns(organisations);

            var handler = new VerifyOrganisationExistsHandler(permissiveAuthorization, context);

            var exists = await handler.HandleAsync(new VerifyOrganisationExists(Guid.NewGuid()));

            Assert.False(exists);
        }