Exemple #1
0
        public async Task ItShouldCallServices()
        {
            this.timestampCreator.Setup(v => v.Now()).Returns(Now);

            this.deleteTestUserAccounts.Setup(v => v.ExecuteAsync(ExpectedEndTime)).Returns(Task.FromResult(0)).Verifiable();

            var files = new List <OrphanedFileData>
            {
                new OrphanedFileData(FileId.Random(), ChannelId.Random(), "p1"),
                new OrphanedFileData(FileId.Random(), ChannelId.Random(), "p2"),
            };

            this.getFilesEligibleForGarbageCollection.Setup(v => v.ExecuteAsync(ExpectedEndTime)).ReturnsAsync(files);

            this.deleteBlobsForFile.Setup(v => v.ExecuteAsync(files[0])).Returns(Task.FromResult(0)).Verifiable();
            this.deleteFileDbStatement.Setup(v => v.ExecuteAsync(files[0].FileId)).Returns(Task.FromResult(0)).Verifiable();

            this.deleteBlobsForFile.Setup(v => v.ExecuteAsync(files[1])).Returns(Task.FromResult(0)).Verifiable();
            this.deleteFileDbStatement.Setup(v => v.ExecuteAsync(files[1].FileId)).Returns(Task.FromResult(0)).Verifiable();

            this.deleteOrphanedBlobContainers.Setup(v => v.ExecuteAsync(this.logger.Object, this.keepAliveHandler.Object, ExpectedEndTime, CancellationToken.None))
            .Returns(Task.FromResult(0))
            .Verifiable();

            this.keepAliveHandler.Setup(v => v.KeepAliveAsync()).Returns(Task.FromResult(0));

            await this.target.ExecuteAsync(this.logger.Object, this.keepAliveHandler.Object, CancellationToken.None);

            this.deleteTestUserAccounts.Verify();
            this.deleteBlobsForFile.Verify();
            this.deleteFileDbStatement.Verify();
            this.deleteOrphanedBlobContainers.Verify();
            this.keepAliveHandler.Verify(v => v.KeepAliveAsync(), Times.Exactly(2));
        }
Exemple #2
0
        public async Task WhenCancelled_ItShouldStopProcessing()
        {
            this.timestampCreator.Setup(v => v.Now()).Returns(Now);

            this.deleteTestUserAccounts.Setup(v => v.ExecuteAsync(ExpectedEndTime)).Returns(Task.FromResult(0)).Verifiable();

            var files = new List <OrphanedFileData>
            {
                new OrphanedFileData(FileId.Random(), ChannelId.Random(), "p1"),
                new OrphanedFileData(FileId.Random(), ChannelId.Random(), "p2"),
            };

            this.getFilesEligibleForGarbageCollection.Setup(v => v.ExecuteAsync(ExpectedEndTime)).ReturnsAsync(files);

            var cts = new CancellationTokenSource();

            this.deleteBlobsForFile.Setup(v => v.ExecuteAsync(files[0])).Returns(Task.FromResult(0)).Callback <OrphanedFileData>(a => cts.Cancel());
            this.deleteFileDbStatement.Setup(v => v.ExecuteAsync(files[0].FileId)).Returns(Task.FromResult(0)).Verifiable();

            this.keepAliveHandler.Setup(v => v.KeepAliveAsync()).Returns(Task.FromResult(0));

            await this.target.ExecuteAsync(this.logger.Object, this.keepAliveHandler.Object, cts.Token);

            this.keepAliveHandler.Verify(v => v.KeepAliveAsync(), Times.Exactly(1));
        }
Exemple #3
0
 private PaymentProcessingData CreateData()
 {
     return(new PaymentProcessingData(
                SubscriberId,
                CreatorId,
                Now,
                Now.AddMonths(1),
                new CommittedAccountBalance(100m),
                new List <SubscriberChannelsSnapshot>
     {
         new SubscriberChannelsSnapshot(
             Now.AddDays(5),
             UserId.Random(),
             new List <SubscriberChannelsSnapshotItem>
         {
             new SubscriberChannelsSnapshotItem(ChannelId.Random(), 100, Now.AddDays(4)),
             new SubscriberChannelsSnapshotItem(ChannelId.Random(), 110, Now.AddDays(8)),
         }),
     },
                new List <SubscriberSnapshot>
     {
         new SubscriberSnapshot(Now.AddDays(7), UserId.Random(), "*****@*****.**"),
         new SubscriberSnapshot(Now.AddDays(2), UserId.Random(), "*****@*****.**"),
     },
                new List <CalculatedAccountBalanceSnapshot>
     {
         new CalculatedAccountBalanceSnapshot(
             Now.AddDays(9),
             UserId.Random(),
             LedgerAccountType.FifthweekCredit,
             10.1m),
     },
                new List <CreatorChannelsSnapshot>
     {
         new CreatorChannelsSnapshot(
             Now.AddDays(1),
             UserId.Random(),
             new List <CreatorChannelsSnapshotItem>
         {
             new CreatorChannelsSnapshotItem(ChannelId.Random(), 200),
             new CreatorChannelsSnapshotItem(ChannelId.Random(), 300),
         }),
     },
                new List <CreatorFreeAccessUsersSnapshot>
     {
         new CreatorFreeAccessUsersSnapshot(
             Now.AddDays(3),
             UserId.Random(),
             new List <string> {
             "*****@*****.**", "*****@*****.**"
         }),
     },
                new List <CreatorPost>
     {
         new CreatorPost(ChannelId.Random(), Now.AddDays(2)),
         new CreatorPost(ChannelId.Random(), Now.AddDays(6)),
     },
                new CreatorPercentageOverrideData(0.9m, null)));
 }
 private PaymentProcessingData CreateData(DateTime startTimeInclusive, DateTime endTimeExclusive, CreatorPercentageOverrideData creatorPercentageOverride = null)
 {
     return(new PaymentProcessingData(
                SubscriberId,
                CreatorId,
                startTimeInclusive,
                endTimeExclusive,
                InitialCommittedAccountBalance,
                new List <SubscriberChannelsSnapshot>
     {
         new SubscriberChannelsSnapshot(
             DateTime.UtcNow,
             UserId.Random(),
             new List <SubscriberChannelsSnapshotItem>
         {
             new SubscriberChannelsSnapshotItem(ChannelId.Random(), 100, DateTime.UtcNow),
             new SubscriberChannelsSnapshotItem(ChannelId.Random(), 110, DateTime.UtcNow),
         }),
     },
                new List <SubscriberSnapshot>
     {
         new SubscriberSnapshot(DateTime.UtcNow, UserId.Random(), "*****@*****.**"),
         new SubscriberSnapshot(DateTime.UtcNow, UserId.Random(), "*****@*****.**"),
     },
                new List <CalculatedAccountBalanceSnapshot>
     {
         new CalculatedAccountBalanceSnapshot(DateTime.UtcNow, UserId.Random(), LedgerAccountType.FifthweekCredit, 10),
     },
                new List <CreatorChannelsSnapshot>
     {
         new CreatorChannelsSnapshot(
             DateTime.UtcNow,
             UserId.Random(),
             new List <CreatorChannelsSnapshotItem>
         {
             new CreatorChannelsSnapshotItem(ChannelId.Random(), 200),
             new CreatorChannelsSnapshotItem(ChannelId.Random(), 300),
         }),
     },
                new List <CreatorFreeAccessUsersSnapshot>
     {
         new CreatorFreeAccessUsersSnapshot(
             DateTime.UtcNow,
             UserId.Random(),
             new List <string> {
             "*****@*****.**", "*****@*****.**"
         }),
     },
                new List <CreatorPost>
     {
         new CreatorPost(ChannelId.Random(), DateTime.UtcNow),
         new CreatorPost(ChannelId.Random(), DateTime.UtcNow),
     },
                creatorPercentageOverride));
 }
        public async Task ItShouldStopProcessingWhenCancelled()
        {
            var channelIds = Enumerable.Repeat(0, 5).Select(v => ChannelId.Random()).ToList();

            this.getAllChannelIds.Setup(v => v.ExecuteAsync()).ReturnsAsync(channelIds);

            var blobClient = new Mock <ICloudBlobClient>(MockBehavior.Strict);

            this.cloudStorageAccount.Setup(v => v.CreateCloudBlobClient())
            .Returns(blobClient.Object);

            var segment1          = new Mock <IContainerResultSegment>(MockBehavior.Strict);
            var continuationToken = new BlobContinuationToken();

            segment1.Setup(v => v.ContinuationToken).Returns(continuationToken);

            var container1 = new Mock <ICloudBlobContainer>(MockBehavior.Strict);
            var container2 = new Mock <ICloudBlobContainer>(MockBehavior.Strict);
            var container3 = new Mock <ICloudBlobContainer>(MockBehavior.Strict);

            segment1.Setup(v => v.Results).Returns(
                new List <ICloudBlobContainer>
            {
                container1.Object,
                container2.Object,
                container3.Object,
            });

            var cts = new CancellationTokenSource();

            // Fails delete condition.
            container1.Setup(v => v.Name).Returns("NotAGuid").Callback(cts.Cancel);

            blobClient.Setup(v => v.ListContainersSegmentedAsync(null)).ReturnsAsync(segment1.Object);

            this.keepAliveHandler.Setup(v => v.KeepAliveAsync()).Returns(Task.FromResult(0));

            await this.target.ExecuteAsync(new Mock <ILogger>().Object, this.keepAliveHandler.Object, EndTimeExclusive, cts.Token);

            this.keepAliveHandler.Verify(v => v.KeepAliveAsync(), Times.Exactly(1));
        }
Exemple #6
0
        public async Task ItShouldStoreTheDataInABlob()
        {
            var data = new PersistedPaymentProcessingData(
                Guid.NewGuid(),
                new PaymentProcessingData(
                    UserId.Random(),
                    UserId.Random(),
                    DateTime.UtcNow,
                    DateTime.UtcNow,
                    new CommittedAccountBalance(100m),
                    new List <SubscriberChannelsSnapshot>
            {
                new SubscriberChannelsSnapshot(
                    DateTime.UtcNow,
                    UserId.Random(),
                    new List <SubscriberChannelsSnapshotItem>
                {
                    new SubscriberChannelsSnapshotItem(ChannelId.Random(), 100, DateTime.UtcNow),
                    new SubscriberChannelsSnapshotItem(ChannelId.Random(), 110, DateTime.UtcNow),
                }),
            },
                    new List <SubscriberSnapshot>
            {
                new SubscriberSnapshot(DateTime.UtcNow, UserId.Random(), "*****@*****.**"),
                new SubscriberSnapshot(DateTime.UtcNow, UserId.Random(), "*****@*****.**"),
            },
                    new List <CalculatedAccountBalanceSnapshot>
            {
                new CalculatedAccountBalanceSnapshot(DateTime.UtcNow, UserId.Random(), LedgerAccountType.FifthweekCredit, 10),
            },
                    new List <CreatorChannelsSnapshot>
            {
                new CreatorChannelsSnapshot(
                    DateTime.UtcNow,
                    UserId.Random(),
                    new List <CreatorChannelsSnapshotItem>
                {
                    new CreatorChannelsSnapshotItem(ChannelId.Random(), 200),
                    new CreatorChannelsSnapshotItem(ChannelId.Random(), 300),
                }),
            },
                    new List <CreatorFreeAccessUsersSnapshot>
            {
                new CreatorFreeAccessUsersSnapshot(
                    DateTime.UtcNow,
                    UserId.Random(),
                    new List <string> {
                    "*****@*****.**", "*****@*****.**"
                }),
            },
                    new List <CreatorPost>
            {
                new CreatorPost(ChannelId.Random(), DateTime.UtcNow),
                new CreatorPost(ChannelId.Random(), DateTime.UtcNow),
            },
                    new CreatorPercentageOverrideData(0.3m, DateTime.UtcNow)),
                new PaymentProcessingResults(
                    new CommittedAccountBalance(80m),
                    new List <PaymentProcessingResult>
            {
                new PaymentProcessingResult(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), new AggregateCostSummary(24), null, true),
                new PaymentProcessingResult(DateTime.UtcNow, DateTime.UtcNow.AddDays(2), new AggregateCostSummary(44), new CreatorPercentageOverrideData(0.5m, DateTime.UtcNow), false),
            }));

            var client = new Mock <ICloudBlobClient>();

            this.cloudStorageAccount.Setup(v => v.CreateCloudBlobClient()).Returns(client.Object);
            var container = new Mock <ICloudBlobContainer>();

            client.Setup(v => v.GetContainerReference(Shared.Constants.PaymentProcessingDataContainerName)).Returns(container.Object);
            var blob = new Mock <ICloudBlockBlob>();

            container.Setup(v => v.GetBlockBlobReference(data.Id.ToString())).Returns(blob.Object);

            string blobString = null;

            blob.Setup(v => v.UploadTextAsync(It.IsAny <string>())).Callback <string>(s => blobString = s).Returns(Task.FromResult(0));

            await this.target.ExecuteAsync(data);

            Assert.IsNotNull(blobString);
            Assert.AreEqual(data, JsonConvert.DeserializeObject <PersistedPaymentProcessingData>(blobString));
        }
        public async Task ItShouldDeleteAllOrphanedBlobContainers()
        {
            var channelIds = Enumerable.Repeat(0, 5).Select(v => ChannelId.Random()).ToList();

            this.getAllChannelIds.Setup(v => v.ExecuteAsync()).ReturnsAsync(channelIds);

            var blobClient = new Mock <ICloudBlobClient>(MockBehavior.Strict);

            this.cloudStorageAccount.Setup(v => v.CreateCloudBlobClient())
            .Returns(blobClient.Object);

            var segment1          = new Mock <IContainerResultSegment>(MockBehavior.Strict);
            var continuationToken = new BlobContinuationToken();

            segment1.Setup(v => v.ContinuationToken).Returns(continuationToken);

            var segment2 = new Mock <IContainerResultSegment>(MockBehavior.Strict);

            segment2.Setup(v => v.ContinuationToken).Returns((BlobContinuationToken)null);

            var container1 = new Mock <ICloudBlobContainer>(MockBehavior.Strict);
            var container2 = new Mock <ICloudBlobContainer>(MockBehavior.Strict);
            var container3 = new Mock <ICloudBlobContainer>(MockBehavior.Strict);
            var container4 = new Mock <ICloudBlobContainer>(MockBehavior.Strict);
            var container5 = new Mock <ICloudBlobContainer>(MockBehavior.Strict);

            segment1.Setup(v => v.Results).Returns(
                new List <ICloudBlobContainer>
            {
                container1.Object,
                container2.Object,
                container3.Object,
            });

            segment2.Setup(v => v.Results).Returns(
                new List <ICloudBlobContainer>
            {
                container4.Object,
                container5.Object,
            });

            // Fails delete condition.
            container1.Setup(v => v.Name).Returns("NotAGuid");

            // Fails delete condition.
            container2.Setup(v => v.Name).Returns(channelIds[2].Value.ToString("N"));

            container3.Setup(v => v.Name).Returns(Guid.NewGuid().ToString("N"));
            container4.Setup(v => v.Name).Returns(Guid.NewGuid().ToString("N"));
            container5.Setup(v => v.Name).Returns(Guid.NewGuid().ToString("N"));

            // Fails delete condition.
            container3.Setup(v => v.Properties).Returns(this.GetBlobContainerProperties(null));

            // Fails delete condition.
            container4.Setup(v => v.Properties).Returns(this.GetBlobContainerProperties(EndTimeExclusive));

            container5.Setup(v => v.Properties).Returns(this.GetBlobContainerProperties(EndTimeExclusive.AddTicks(-1)));

            blobClient.Setup(v => v.ListContainersSegmentedAsync(null)).ReturnsAsync(segment1.Object);
            blobClient.Setup(v => v.ListContainersSegmentedAsync(continuationToken)).ReturnsAsync(segment2.Object);

            container5.Setup(v => v.DeleteAsync()).Returns(Task.FromResult(0)).Verifiable();

            this.keepAliveHandler.Setup(v => v.KeepAliveAsync()).Returns(Task.FromResult(0));

            await this.target.ExecuteAsync(new Mock <ILogger>().Object, this.keepAliveHandler.Object, EndTimeExclusive, CancellationToken.None);

            container5.Verify();
            this.keepAliveHandler.Verify(v => v.KeepAliveAsync(), Times.Exactly(5));
        }
        private async Task CreateEntitiesAsync(TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                // Create header file
                var blogId            = BlogId.Random();
                var headerImageFileId = FileId.Random();
                await databaseContext.CreateTestBlogAsync(UserId.Value, blogId.Value, headerImageFileId.Value);

                var headerImageFile = databaseContext.Files.First(v => v.Id == headerImageFileId.Value);
                headerImageFile.UploadStartedDate = IncludedDate;

                var channelId = ChannelId.Random();
                var channel   = ChannelTests.UniqueEntity(Random);
                channel.BlogId = blogId.Value;
                channel.Blog   = databaseContext.Blogs.First(v => v.Id == blogId.Value);
                channel.Id     = channelId.Value;
                databaseContext.Channels.Add(channel);

                // Create profile image file
                var user = databaseContext.Users.First(v => v.Id == UserId.Value);
                var profileImageFileId = FileId.Random();
                var profileImageFile   = this.CreateTestFileWithExistingUserAsync(UserId.Value, profileImageFileId.Value, IncludedDate);
                databaseContext.Files.Add(profileImageFile);
                user.ProfileImageFileId = profileImageFileId.Value;

                // Create image post file.
                var post1       = PostTests.UniqueFileOrImage(Random);
                var imageFileId = FileId.Random();
                var image       = this.CreateTestFileWithExistingUserAsync(UserId.Value, imageFileId.Value, IncludedDate);
                databaseContext.Files.Add(image);
                post1.PreviewImageId = imageFileId.Value;
                post1.PreviewImage   = image;
                post1.ChannelId      = channelId.Value;
                post1.Channel        = channel;
                databaseContext.Posts.Add(post1);

                var post1File = new PostFile(post1.Id, post1, imageFileId.Value, image);
                databaseContext.PostFiles.Add(post1File);

                // Create file post file.
                var post2      = PostTests.UniqueFileOrImage(Random);
                var fileFileId = FileId.Random();
                var file       = this.CreateTestFileWithExistingUserAsync(UserId.Value, fileFileId.Value, IncludedDate);
                databaseContext.Files.Add(file);
                post2.ChannelId = channelId.Value;
                post2.Channel   = channel;
                databaseContext.Posts.Add(post2);

                var post2File = new PostFile(post2.Id, post2, fileFileId.Value, file);
                databaseContext.PostFiles.Add(post2File);

                // Create files excluded because of date.
                var lateFile1 = this.CreateTestFileWithExistingUserAsync(UserId.Value, FileId.Random().Value, EndDate);
                databaseContext.Files.Add(lateFile1);
                var lateFile2 = this.CreateTestFileWithExistingUserAsync(UserId.Value, FileId.Random().Value, Now);
                databaseContext.Files.Add(lateFile2);

                // Create orphaned files.
                var orphanedFile1 = this.CreateTestFileWithExistingUserAsync(UserId.Value, OrphanedFileId1.Value, IncludedDate);
                databaseContext.Files.Add(orphanedFile1);
                var orphanedFile2 = this.CreateTestFileWithExistingUserAsync(UserId.Value, OrphanedFileId2.Value, IncludedDate);
                databaseContext.Files.Add(orphanedFile2);

                await databaseContext.SaveChangesAsync();
            }
        }
        public async Task WhenSubscribers_ItShouldReturnSubscribersWithFileInformation()
        {
            this.blogSecurity.Setup(v => v.AssertWriteAllowedAsync(UserId, BlogId)).Returns(Task.FromResult(0));

            this.getCreatorRevenue.Setup(v => v.ExecuteAsync(UserId, ReleasableRevenueDate))
            .ReturnsAsync(new GetCreatorRevenueDbStatement.GetCreatorRevenueDbStatementResult(10, 20, 30));

            var subscriber1 = new GetBlogSubscriberInformationDbStatement.GetBlogSubscriberInformationDbStatementResult.Subscriber(
                new Username(Guid.NewGuid().ToString()),
                UserId.Random(),
                new FileId(Guid.NewGuid()),
                ChannelId.Random(),
                DateTime.UtcNow.AddDays(-1),
                10,
                new Email("*****@*****.**"),
                PaymentStatus.Retry2,
                true);

            var subscriber2 = new GetBlogSubscriberInformationDbStatement.GetBlogSubscriberInformationDbStatementResult.Subscriber(
                new Username(Guid.NewGuid().ToString()),
                UserId.Random(),
                new FileId(Guid.NewGuid()),
                ChannelId.Random(),
                DateTime.UtcNow.AddDays(-2),
                20,
                new Email("*****@*****.**"),
                PaymentStatus.None,
                false);

            var subscriber3 = new GetBlogSubscriberInformationDbStatement.GetBlogSubscriberInformationDbStatementResult.Subscriber(
                subscriber1.Username,
                subscriber1.UserId,
                subscriber1.ProfileImageFileId,
                ChannelId.Random(),
                DateTime.UtcNow.AddDays(-3),
                30,
                new Email("*****@*****.**"),
                PaymentStatus.Failed,
                true);

            var subscriber4 = new GetBlogSubscriberInformationDbStatement.GetBlogSubscriberInformationDbStatementResult.Subscriber(
                new Username(Guid.NewGuid().ToString()),
                UserId.Random(),
                null,
                ChannelId.Random(),
                DateTime.UtcNow.AddDays(-4),
                40,
                null,
                PaymentStatus.None,
                true);

            this.getBlogSubscriberInformation.Setup(v => v.ExecuteAsync(Query.BlogId)).ReturnsAsync(
                new GetBlogSubscriberInformationDbStatement.GetBlogSubscriberInformationDbStatementResult(
                    new List <GetBlogSubscriberInformationDbStatement.GetBlogSubscriberInformationDbStatementResult.Subscriber>
            {
                subscriber1,
                subscriber2,
                subscriber3,
                subscriber4
            }));

            var fileInformation1 = new FileInformation(subscriber1.ProfileImageFileId, "container1");
            var fileInformation2 = new FileInformation(subscriber2.ProfileImageFileId, "container2");

            this.fileInformationAggregator.Setup(v => v.GetFileInformationAsync(null, subscriber1.ProfileImageFileId, FilePurposes.ProfileImage))
            .ReturnsAsync(fileInformation1);
            this.fileInformationAggregator.Setup(v => v.GetFileInformationAsync(null, subscriber2.ProfileImageFileId, FilePurposes.ProfileImage))
            .ReturnsAsync(fileInformation2);

            var result = await this.target.HandleAsync(Query);

            Assert.AreEqual(10, result.UnreleasedRevenue);
            Assert.AreEqual(20, result.ReleasedRevenue);
            Assert.AreEqual(30, result.ReleasableRevenue);
            CollectionAssert.AreEqual(
                new List <BlogSubscriberInformation.Subscriber>
            {
                new BlogSubscriberInformation.Subscriber(
                    subscriber1.Username,
                    subscriber1.UserId,
                    fileInformation1,
                    subscriber1.FreeAccessEmail,
                    PaymentStatus.Retry2,
                    true,
                    new List <BlogSubscriberInformation.SubscriberChannel>
                {
                    new BlogSubscriberInformation.SubscriberChannel(
                        subscriber1.ChannelId,
                        subscriber1.SubscriptionStartDate,
                        subscriber1.AcceptedPrice),
                    new BlogSubscriberInformation.SubscriberChannel(
                        subscriber3.ChannelId,
                        subscriber3.SubscriptionStartDate,
                        subscriber3.AcceptedPrice),
                }),
                new BlogSubscriberInformation.Subscriber(
                    subscriber2.Username,
                    subscriber2.UserId,
                    fileInformation2,
                    subscriber2.FreeAccessEmail,
                    PaymentStatus.None,
                    false,
                    new List <BlogSubscriberInformation.SubscriberChannel>
                {
                    new BlogSubscriberInformation.SubscriberChannel(
                        subscriber2.ChannelId,
                        subscriber2.SubscriptionStartDate,
                        subscriber2.AcceptedPrice),
                }),
                new BlogSubscriberInformation.Subscriber(
                    subscriber4.Username,
                    subscriber4.UserId,
                    null,
                    subscriber4.FreeAccessEmail,
                    PaymentStatus.None,
                    true,
                    new List <BlogSubscriberInformation.SubscriberChannel>
                {
                    new BlogSubscriberInformation.SubscriberChannel(
                        subscriber4.ChannelId,
                        subscriber4.SubscriptionStartDate,
                        subscriber4.AcceptedPrice),
                }),
            },
                result.Subscribers.ToList());
        }