public async Task CompleteRegistrationHandler_HappyPath_OrganisationCompleteAndSchemeAdded()
        {
            var context = A.Fake<WeeeContext>();

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

            Scheme addedScheme = null;
            A.CallTo(() => context.Schemes).Returns(dbHelper.GetAsyncEnabledDbSet(new List<Scheme>()));
            A.CallTo(() => context.Schemes.Add(A<Scheme>._))
                .Invokes((Scheme s) => addedScheme = s);

            var handler = new CompleteRegistrationHandler(permissiveAuthorization, context);
            var message = new CompleteRegistration(organisationId);

            var result = await handler.HandleAsync(message);

            Assert.Equal(organisationId, result);
            Assert.Equal(OrganisationStatus.Complete, organisation.OrganisationStatus);

            A.CallTo(() => context.Schemes.Add(A<Scheme>._)).MustHaveHappened();
            Assert.NotNull(addedScheme);
            Assert.Equal(organisationId, addedScheme.OrganisationId);
        }
        public async Task CompleteRegistrationHandler_HappyPath_OrganisationCompleteAndSchemeAdded()
        {
            var context = A.Fake <WeeeContext>();

            var organisationId = Guid.NewGuid();
            var addressId      = Guid.NewGuid();
            var contactId      = Guid.NewGuid();

            var organisation = GetOrganisationWithId(organisationId);

            organisation.OrganisationStatus = OrganisationStatus.Incomplete;
            A.CallTo(() => context.Organisations).Returns(dbHelper.GetAsyncEnabledDbSet(new List <Organisation> {
                organisation
            }));

            Scheme addedScheme = null;

            A.CallTo(() => context.Schemes).Returns(dbHelper.GetAsyncEnabledDbSet(new List <Scheme>()));
            A.CallTo(() => context.Schemes.Add(A <Scheme> ._))
            .Invokes((Scheme s) => addedScheme = s);

            var handler = new CompleteRegistrationHandler(permissiveAuthorization, context);
            var message = new CompleteRegistration(organisationId, addressId, contactId);

            var result = await handler.HandleAsync(message);

            Assert.Equal(organisationId, result);
            Assert.Equal(OrganisationStatus.Complete, organisation.OrganisationStatus);

            A.CallTo(() => context.Schemes.Add(A <Scheme> .That.Matches(s => s.AddressId.Equals(addressId) && s.ContactId.Equals(contactId)))).MustHaveHappened();
            Assert.NotNull(addedScheme);
            Assert.Equal(organisationId, addedScheme.OrganisationId);
        }
        public async Task CompleteRegistrationHandler_NotOrganisationUser_ThrowsSecurityException()
        {
            var handler = new CompleteRegistrationHandler(denyingAuthorization, A.Dummy<WeeeContext>());
            var message = new CompleteRegistration(Guid.NewGuid());

            await
                Assert.ThrowsAsync<SecurityException>(async () => await handler.HandleAsync(message));
        }
        public async Task CompleteRegistrationHandler_NotOrganisationUser_ThrowsSecurityException()
        {
            var handler = new CompleteRegistrationHandler(denyingAuthorization, A.Dummy <WeeeContext>());
            var message = new CompleteRegistration(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid());

            await
            Assert.ThrowsAsync <SecurityException>(async() => await handler.HandleAsync(message));
        }
        public async Task CompleteRegistrationHandler_NoSuchOrganisation_ThrowsArgumentException()
        {
            var context = A.Fake<WeeeContext>();
            A.CallTo(() => context.Organisations).Returns(dbHelper.GetAsyncEnabledDbSet(new List<Organisation>()));

            var handler = new CompleteRegistrationHandler(permissiveAuthorization, context);
            var message = new CompleteRegistration(Guid.NewGuid());

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

            Assert.True(exception.Message.ToUpperInvariant().Contains("COULD NOT FIND"));
            Assert.True(exception.Message.ToUpperInvariant().Contains("ORGANISATION"));
            Assert.True(exception.Message.Contains(message.OrganisationId.ToString()));
        }
        public async Task CompleteRegistrationHandler_NoSuchOrganisation_ThrowsArgumentException()
        {
            var context = A.Fake <WeeeContext>();

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

            var handler = new CompleteRegistrationHandler(permissiveAuthorization, context);
            var message = new CompleteRegistration(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid());

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

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