Beispiel #1
0
        public async Task UpdateProfileUpdatesProfileInResultsCacheTest(ProfileStatus status)
        {
            var expected     = Model.Create <UpdatableProfile>().Set(x => x.Status = status);
            var profile      = Model.Create <Profile>().Set(x => x.BannedAt = null);
            var changeResult = Model.Create <ProfileChangeResult>().Set(x => x.ProfileChanged = true);
            var cacheResults = new List <ProfileResult>
            {
                Model.Create <ProfileResult>().Set(x => x.Id = profile.Id)
            };

            var store      = Substitute.For <IProfileStore>();
            var calculator = Substitute.For <IProfileChangeCalculator>();
            var processor  = Substitute.For <IProfileChangeProcessor>();
            var cache      = Substitute.For <IProfileCache>();

            var sut = new ProfileCommand(store, calculator, processor, cache);

            using (var tokenSource = new CancellationTokenSource())
            {
                cache.GetProfileResults().Returns(cacheResults);
                store.GetProfile(profile.Id, tokenSource.Token).Returns(profile);
                calculator.CalculateChanges(profile, expected).Returns(changeResult);

                await sut.UpdateProfile(profile.Id, expected, tokenSource.Token).ConfigureAwait(false);

                // This is a mouthful. We want to make sure that the profile results being put into
                // the cache contains only a single item matching our updated profile and that has
                // all the same data as the updated profile
                cache.Received().StoreProfileResults(
                    Verify.That <ICollection <ProfileResult> >(
                        x => x.Single(y => y.Id == profile.Id)
                        .Should().BeEquivalentTo(expected, opt => opt.ExcludingMissingMembers())));
            }
        }
Beispiel #2
0
        public async Task UpdateProfileStoresProfileWithOriginalProfileBannedAtValueTest()
        {
            var expected     = Model.Create <UpdatableProfile>();
            var profile      = Model.Create <Profile>().Set(x => x.BannedAt = DateTimeOffset.UtcNow);
            var changeResult = Model.Create <ProfileChangeResult>().Set(x => x.ProfileChanged = true);

            var store      = Substitute.For <IProfileStore>();
            var calculator = Substitute.For <IProfileChangeCalculator>();
            var processor  = Substitute.For <IProfileChangeProcessor>();
            var cache      = Substitute.For <IProfileCache>();

            var sut = new ProfileCommand(store, calculator, processor, cache);

            using (var tokenSource = new CancellationTokenSource())
            {
                store.GetProfile(profile.Id, tokenSource.Token).Returns(profile);
                calculator.CalculateChanges(profile, expected).Returns(changeResult);

                await sut.UpdateProfile(profile.Id, expected, tokenSource.Token).ConfigureAwait(false);

                await processor.Received().Execute(
                    Arg.Is <Profile>(x => x.BannedAt == profile.BannedAt),
                    Arg.Any <ProfileChangeResult>(),
                    tokenSource.Token).ConfigureAwait(false);

                cache.DidNotReceive().StoreProfile(Arg.Any <Profile>());
            }
        }
Beispiel #3
0
        public async Task UpdateProfileRemovesHiddenProfileFromResultsCacheTest()
        {
            var expected     = Model.Create <UpdatableProfile>().Set(x => x.Status = ProfileStatus.Hidden);
            var profile      = Model.Create <Profile>().Set(x => x.BannedAt = null);
            var changeResult = Model.Create <ProfileChangeResult>().Set(x => x.ProfileChanged = true);
            var cacheResults = new List <ProfileResult>
            {
                Model.Create <ProfileResult>().Set(x => x.Id = profile.Id)
            };

            var store      = Substitute.For <IProfileStore>();
            var calculator = Substitute.For <IProfileChangeCalculator>();
            var processor  = Substitute.For <IProfileChangeProcessor>();
            var cache      = Substitute.For <IProfileCache>();

            var sut = new ProfileCommand(store, calculator, processor, cache);

            using (var tokenSource = new CancellationTokenSource())
            {
                cache.GetProfileResults().Returns(cacheResults);
                store.GetProfile(profile.Id, tokenSource.Token).Returns(profile);
                calculator.CalculateChanges(profile, expected).Returns(changeResult);

                await sut.UpdateProfile(profile.Id, expected, tokenSource.Token).ConfigureAwait(false);

                cache.Received().StoreProfileResults(
                    Verify.That <ICollection <ProfileResult> >(x => x.Should().NotContain(y => y.Id == profile.Id)));
            }
        }
Beispiel #4
0
        public async Task UpdateProfileDoesNotProcessProfileChangesWhenNoChangeFoundTest()
        {
            var expected     = Model.Create <UpdatableProfile>();
            var profile      = Model.Create <Profile>();
            var changeResult = Model.Create <ProfileChangeResult>().Set(x => x.ProfileChanged = false);

            var store      = Substitute.For <IProfileStore>();
            var calculator = Substitute.For <IProfileChangeCalculator>();
            var processor  = Substitute.For <IProfileChangeProcessor>();
            var cache      = Substitute.For <IProfileCache>();

            var sut = new ProfileCommand(store, calculator, processor, cache);

            using (var tokenSource = new CancellationTokenSource())
            {
                store.GetProfile(profile.Id, tokenSource.Token).Returns(profile);
                calculator.CalculateChanges(profile, expected).Returns(changeResult);

                await sut.UpdateProfile(profile.Id, expected, tokenSource.Token).ConfigureAwait(false);

                await processor.DidNotReceive().Execute(
                    Arg.Any <Profile>(),
                    Arg.Any <ProfileChangeResult>(),
                    Arg.Any <CancellationToken>()).ConfigureAwait(false);
            }
        }
Beispiel #5
0
        public async Task UpdateProfileDoesNotAddHiddenProfileToResultsCacheWhenNotPreviouslyCachedTest()
        {
            var expected     = Model.Create <UpdatableProfile>().Set(x => x.Status = ProfileStatus.Hidden);
            var profile      = Model.Create <Profile>().Set(x => x.BannedAt = null);
            var changeResult = Model.Create <ProfileChangeResult>().Set(x => x.ProfileChanged = true);
            var cacheResults = new List <ProfileResult>();

            var store      = Substitute.For <IProfileStore>();
            var calculator = Substitute.For <IProfileChangeCalculator>();
            var processor  = Substitute.For <IProfileChangeProcessor>();
            var cache      = Substitute.For <IProfileCache>();

            var sut = new ProfileCommand(store, calculator, processor, cache);

            using (var tokenSource = new CancellationTokenSource())
            {
                cache.GetProfileResults().Returns(cacheResults);
                store.GetProfile(profile.Id, tokenSource.Token).Returns(profile);
                calculator.CalculateChanges(profile, expected).Returns(changeResult);

                await sut.UpdateProfile(profile.Id, expected, tokenSource.Token).ConfigureAwait(false);

                cache.DidNotReceive().StoreProfileResults(Arg.Any <ICollection <ProfileResult> >());
            }
        }
Beispiel #6
0
        public async Task UpdateProfileCalculatesAndProcessesChangesForUpdatedProfileTest()
        {
            var expected = Model.Create <UpdatableProfile>();
            var profile  = Model.Create <Profile>().Set(x =>
            {
                x.AcceptCoC = true;
                x.AcceptToS = true;
            });
            var changeResult = Model.Create <ProfileChangeResult>().Set(x => x.ProfileChanged = true);

            var store      = Substitute.For <IProfileStore>();
            var calculator = Substitute.For <IProfileChangeCalculator>();
            var processor  = Substitute.For <IProfileChangeProcessor>();
            var cache      = Substitute.For <IProfileCache>();

            var sut = new ProfileCommand(store, calculator, processor, cache);

            using (var tokenSource = new CancellationTokenSource())
            {
                store.GetProfile(profile.Id, tokenSource.Token).Returns(profile);
                calculator.CalculateChanges(profile, expected).Returns(changeResult);

                await sut.UpdateProfile(profile.Id, expected, tokenSource.Token).ConfigureAwait(false);

                await processor.Received().Execute(
                    Verify.That <Profile>(x =>
                                          x.Should().BeEquivalentTo(expected, opt => opt.ExcludingMissingMembers())),
                    changeResult,
                    tokenSource.Token).ConfigureAwait(false);
            }
        }
Beispiel #7
0
        public void UpdateProfileThrowsExceptionWithNullProfileTest()
        {
            var profileId  = Guid.NewGuid();
            var store      = Substitute.For <IProfileStore>();
            var calculator = Substitute.For <IProfileChangeCalculator>();
            var processor  = Substitute.For <IProfileChangeProcessor>();
            var cache      = Substitute.For <IProfileCache>();

            var sut = new ProfileCommand(store, calculator, processor, cache);

            Func <Task> action = async() => await sut.UpdateProfile(profileId, null, CancellationToken.None)
                                 .ConfigureAwait(false);

            action.Should().Throw <ArgumentNullException>();
        }
Beispiel #8
0
        public async Task UpdateProfileStoresProfileWithAcceptedToSAtDependingOnConsentTest(bool originalConsent,
                                                                                            bool updatedConsent, bool timeSet)
        {
            var expected = Model.Create <UpdatableProfile>().Set(x => x.AcceptToS = updatedConsent);
            var profile  = Model.Create <Profile>().Set(x =>
            {
                x.AcceptToS     = originalConsent;
                x.AcceptedToSAt = DateTimeOffset.UtcNow.AddYears(-1);
            });
            var changeResult = Model.Create <ProfileChangeResult>().Set(x => x.ProfileChanged = true);

            var store      = Substitute.For <IProfileStore>();
            var calculator = Substitute.For <IProfileChangeCalculator>();
            var processor  = Substitute.For <IProfileChangeProcessor>();
            var cache      = Substitute.For <IProfileCache>();

            var sut = new ProfileCommand(store, calculator, processor, cache);

            using (var tokenSource = new CancellationTokenSource())
            {
                store.GetProfile(profile.Id, tokenSource.Token).Returns(profile);
                calculator.CalculateChanges(profile, expected).Returns(changeResult);

                await sut.UpdateProfile(profile.Id, expected, tokenSource.Token).ConfigureAwait(false);

                if (timeSet)
                {
                    await processor.Received().Execute(
                        Verify.That <Profile>(x => x.AcceptedToSAt.Should().BeCloseTo(DateTimeOffset.UtcNow, 1000)),
                        Arg.Any <ProfileChangeResult>(),
                        tokenSource.Token).ConfigureAwait(false);
                }
                else
                {
                    await processor.Received().Execute(
                        Arg.Is <Profile>(x => x.AcceptedToSAt == profile.AcceptedToSAt),
                        Arg.Any <ProfileChangeResult>(),
                        tokenSource.Token).ConfigureAwait(false);
                }
            }
        }