private static async Task ClearLedger(TestDatabaseContext testDatabase)
 {
     using (var connection = testDatabase.CreateConnection())
     {
         await connection.ExecuteAsync(string.Format("DELETE FROM {0}", AppendOnlyLedgerRecord.Table));
     }
 }
        private async Task CreateUserAsync(TestDatabaseContext testDatabase)
        {
            var random = new Random();
            var user   = UserTests.UniqueEntity(random);

            user.Id       = CreatorId.Value;
            user.UserName = Username.Value;

            var file = FileTests.UniqueEntity(random);

            file.Id     = ProfileFileId.Value;
            file.UserId = CreatorId.Value;

            using (var databaseContext = testDatabase.CreateContext())
            {
                databaseContext.Users.Add(user);
                await databaseContext.SaveChangesAsync();
            }

            using (var connection = testDatabase.CreateConnection())
            {
                await connection.InsertAsync(file);
            }

            using (var databaseContext = testDatabase.CreateContext())
            {
                file = await databaseContext.Files.FirstAsync(v => v.Id == ProfileFileId.Value);

                user = await databaseContext.Users.FirstAsync(v => v.Id == CreatorId.Value);

                user.ProfileImageFile   = file;
                user.ProfileImageFileId = file.Id;
                await databaseContext.SaveChangesAsync();
            }
        }
Exemple #3
0
        private async Task <Tuple <UserId, UserId> > CreateDataAsync(TestDatabaseContext testDatabase, int creatorChannelsSnapshotCount)
        {
            var random       = new Random();
            var testUserId   = Guid.NewGuid();
            var normalUserId = Guid.NewGuid();

            using (var databaseContext = testDatabase.CreateContext())
            {
                var testUser = UserTests.UniqueEntity(random);
                testUser.Id = testUserId;
                databaseContext.Users.Add(testUser);

                var normalUser = UserTests.UniqueEntity(random);
                normalUser.Id = normalUserId;
                databaseContext.Users.Add(normalUser);

                await databaseContext.SaveChangesAsync();
            }

            using (var connection = testDatabase.CreateConnection())
            {
                var userRoles = new List <FifthweekUserRole>();
                userRoles.Add(new FifthweekUserRole(FifthweekRole.CreatorId, normalUserId));
                userRoles.Add(new FifthweekUserRole(FifthweekRole.CreatorId, testUserId));
                userRoles.Add(new FifthweekUserRole(FifthweekRole.TestUserId, testUserId));

                await connection.InsertAsync(userRoles, false);

                return(new Tuple <UserId, UserId>(
                           new UserId(testUserId),
                           new UserId(normalUserId)));
            }
        }
Exemple #4
0
        private async Task <IReadOnlyList <UserId> > CreateDataAsync(TestDatabaseContext testDatabase)
        {
            using (var connection = testDatabase.CreateConnection())
            {
                var creatorChannelsSnapshots     = new List <CreatorChannelsSnapshot>();
                var creatorChannelsSnapshotItems = new List <CreatorChannelsSnapshotItem>();

                for (int i = 0; i < Days; i++)
                {
                    creatorChannelsSnapshots.Add(new CreatorChannelsSnapshot(Guid.NewGuid(), Now.AddDays(i), CreatorId1.Value));
                    creatorChannelsSnapshotItems.Add(new CreatorChannelsSnapshotItem(creatorChannelsSnapshots.Last().Id, null, Guid.NewGuid(), 100 + i));
                    creatorChannelsSnapshotItems.Add(new CreatorChannelsSnapshotItem(creatorChannelsSnapshots.Last().Id, null, Guid.NewGuid(), 100 + i));

                    creatorChannelsSnapshots.Add(new CreatorChannelsSnapshot(Guid.NewGuid(), Now.AddDays(i).AddHours(12), CreatorId2.Value));
                    creatorChannelsSnapshotItems.Add(new CreatorChannelsSnapshotItem(creatorChannelsSnapshots.Last().Id, null, Guid.NewGuid(), 200 + i));
                    creatorChannelsSnapshotItems.Add(new CreatorChannelsSnapshotItem(creatorChannelsSnapshots.Last().Id, null, Guid.NewGuid(), 200 + i));
                }

                await connection.InsertAsync(creatorChannelsSnapshots, false);

                await connection.InsertAsync(creatorChannelsSnapshotItems, false);

                return(creatorChannelsSnapshots.Select(v => new UserId(v.CreatorId)).ToList());
            }
        }
        private async Task CreateDataAsync(TestDatabaseContext testDatabase, bool addEmailAddress)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                await databaseContext.CreateTestChannelsAsync(
                    CreatorId1.Value,
                    BlogId1.Value,
                    ChannelId1.Value,
                    ChannelId2.Value);

                await databaseContext.CreateTestChannelsAsync(
                    CreatorId2.Value,
                    BlogId2.Value,
                    ChannelId3.Value,
                    ChannelId4.Value);
            }

            await this.CreateUsersAsync(testDatabase);

            if (addEmailAddress)
            {
                using (var connection = testDatabase.CreateConnection())
                {
                    foreach (var item in InitialFreeAccessUsers)
                    {
                        await connection.InsertAsync(item);
                    }
                }
            }
        }
        private async Task <IReadOnlyList <UserId> > CreateDataAsync(TestDatabaseContext testDatabase, int normalUserCount)
        {
            var random        = new Random();
            var testUserIds   = new List <Guid>();
            var normalUserIds = new List <Guid>();

            using (var databaseContext = testDatabase.CreateContext())
            {
                // Create some test users which should be ignored.
                for (int i = 0; i < 3; i++)
                {
                    var user = UserTests.UniqueEntity(random);
                    testUserIds.Add(user.Id);
                    databaseContext.Users.Add(user);
                }

                // Create some normal users which shouldn't be ignored.
                for (int i = 0; i < normalUserCount; i++)
                {
                    var user = UserTests.UniqueEntity(random);
                    normalUserIds.Add(user.Id);
                    databaseContext.Users.Add(user);
                }

                await databaseContext.SaveChangesAsync();
            }

            using (var connection = testDatabase.CreateConnection())
            {
                var userRoles = new List <FifthweekUserRole>();

                foreach (var userId in normalUserIds)
                {
                    // Add some normal users to creator roles.
                    if (random.NextDouble() > 0.5)
                    {
                        userRoles.Add(new FifthweekUserRole(FifthweekRole.CreatorId, userId));
                    }
                }

                foreach (var userId in testUserIds)
                {
                    // Add some test users.
                    userRoles.Add(new FifthweekUserRole(FifthweekRole.TestUserId, userId));

                    if (random.NextDouble() > 0.5)
                    {
                        userRoles.Add(new FifthweekUserRole(FifthweekRole.CreatorId, userId));
                    }
                }

                await connection.InsertAsync(userRoles, false);

                return(normalUserIds.Select(v => new UserId(v)).ToList());
            }
        }
        private async Task <IReadOnlyList <UserId> > CreateDataAsync(TestDatabaseContext testDatabase)
        {
            using (var connection = testDatabase.CreateConnection())
            {
                var snapshots     = new List <CalculatedAccountBalance>();
                var ledgerRecords = new List <AppendOnlyLedgerRecord>();

                for (int i = 0; i < Days; i++)
                {
                    snapshots.Add(new CalculatedAccountBalance(UserId1.Value, LedgerAccountType.FifthweekRevenue, Now.AddDays(i), (i + 1) * 2));
                    snapshots.Add(new CalculatedAccountBalance(UserId1.Value, LedgerAccountType.FifthweekCredit, Now.AddDays(i), i));
                    snapshots.Add(new CalculatedAccountBalance(UserId1.Value, LedgerAccountType.Stripe, Now.AddDays(i), i + 0.25m));
                    snapshots.Add(new CalculatedAccountBalance(UserId1.Value, LedgerAccountType.ReleasedRevenue, Now.AddDays(i), (i + 1) * 4));
                    ledgerRecords.Add(
                        new AppendOnlyLedgerRecord(
                            Guid.NewGuid(),
                            UserId1.Value,
                            null,
                            Now.AddDays(i),
                            1,
                            LedgerAccountType.FifthweekRevenue,
                            LedgerTransactionType.SubscriptionPayment,
                            Guid.NewGuid(),
                            null,
                            null,
                            null,
                            null));

                    snapshots.Add(new CalculatedAccountBalance(UserId2.Value, LedgerAccountType.FifthweekRevenue, Now.AddDays(i), (i + 1) * 3));
                    snapshots.Add(new CalculatedAccountBalance(UserId2.Value, LedgerAccountType.FifthweekCredit, Now.AddDays(i).AddHours(12), i + 0.5m));
                    snapshots.Add(new CalculatedAccountBalance(UserId2.Value, LedgerAccountType.Stripe, Now.AddDays(i).AddHours(12), i + 0.75m));
                    snapshots.Add(new CalculatedAccountBalance(UserId2.Value, LedgerAccountType.ReleasedRevenue, Now.AddDays(i), (i + 1) * 5));
                    ledgerRecords.Add(
                        new AppendOnlyLedgerRecord(
                            Guid.NewGuid(),
                            UserId2.Value,
                            null,
                            Now.AddDays(i),
                            2,
                            LedgerAccountType.FifthweekRevenue,
                            LedgerTransactionType.SubscriptionPayment,
                            Guid.NewGuid(),
                            null,
                            null,
                            null,
                            null));
                }

                await connection.InsertAsync(snapshots, false);

                await connection.InsertAsync(ledgerRecords, false);

                return(snapshots.Select(v => new UserId(v.UserId)).ToList());
            }
        }
        private async Task CreateHeaderFileAsync(TestDatabaseContext testDatabase)
        {
            var random = new Random();
            var file   = FileTests.UniqueEntity(random);

            file.Id     = HeaderFileId.Value;
            file.UserId = CreatorId.Value;

            using (var connection = testDatabase.CreateConnection())
            {
                await connection.InsertAsync(file);
            }
        }
Exemple #9
0
        private async Task CreateBlogAsync(TestDatabaseContext testDatabase)
        {
            var random = new Random();
            var blog   = BlogTests.UniqueEntity(random);

            blog.Id        = BlogId.Value;
            blog.CreatorId = CreatorId.Value;

            using (var connection = testDatabase.CreateConnection())
            {
                await connection.InsertAsync(blog);
            }
        }
        private async Task CreateDataAsync(TestDatabaseContext testDatabase, bool setTestUser)
        {
            using (var context = testDatabase.CreateContext())
            {
                await context.CreateTestChannelAsync(CreatorId.Value, BlogId.Value, ChannelId1.Value);
            }

            if (setTestUser)
            {
                using (var connection = testDatabase.CreateConnection())
                {
                    var userRoles = new List <FifthweekUserRole>();
                    userRoles.Add(new FifthweekUserRole(FifthweekRole.TestUserId, CreatorId.Value));
                    await connection.InsertAsync(userRoles, false);
                }
            }
        }
Exemple #11
0
        private async Task CreateDataAsync(TestDatabaseContext testDatabase, bool addEmailAddress)
        {
            await this.CreateUserAsync(testDatabase);

            await this.CreateBlogAsync(testDatabase);

            if (addEmailAddress)
            {
                using (var connection = testDatabase.CreateConnection())
                {
                    foreach (var item in InitialFreeAccessUsers)
                    {
                        await connection.InsertAsync(item);
                    }
                }
            }
        }
        private async Task CreateDataAsync(TestDatabaseContext testDatabase)
        {
            IReadOnlyList <Channel> blog1Channels;
            IReadOnlyList <Channel> blog2Channels;
            IReadOnlyList <Channel> blog3Channels;

            using (var databaseContext = testDatabase.CreateContext())
            {
                blog1Channels = await databaseContext.CreateTestChannelsAsync(Creator1Id.Value, Blog1Id.Value, Blog1ChannelIds.Select(v => v.Value).ToArray());

                blog2Channels = await databaseContext.CreateTestChannelsAsync(Creator2Id.Value, Blog2Id.Value, Blog2ChannelIds.Select(v => v.Value).ToArray());

                blog3Channels = await databaseContext.CreateTestChannelsAsync(Creator3Id.Value, Blog3Id.Value, Blog3ChannelIds.Select(v => v.Value).ToArray());

                blog2Channels[0].Price                     = Blog2Channel1CurrentPrice;
                blog2Channels[0].PriceLastSetDate          = PriceLastSetDate;
                blog2Channels[0].IsVisibleToNonSubscribers = true;
                blog3Channels[0].Price                     = Blog3Channel1CurrentPrice;
                blog3Channels[0].PriceLastSetDate          = PriceLastSetDate;
                blog3Channels[0].IsVisibleToNonSubscribers = false;
                blog3Channels[2].Price                     = Blog3Channel3CurrentPrice;
                blog3Channels[2].PriceLastSetDate          = PriceLastSetDate;
                blog3Channels[2].IsVisibleToNonSubscribers = true;

                await databaseContext.SaveChangesAsync();
            }

            await this.CreateUserAsync(testDatabase);

            await this.CreateProfileImagesAsync(testDatabase);

            using (var connection = testDatabase.CreateConnection())
            {
                foreach (var item in InitialFreeAccessUsers)
                {
                    await connection.InsertAsync(item);
                }

                await connection.InsertAsync(new ChannelSubscription(blog2Channels[0].Id, null, UserId.Value, null, Blog2Channel1AcceptedPrice, PriceLastAcceptedDate, SubscriptionStartDate));

                await connection.InsertAsync(new ChannelSubscription(blog3Channels[0].Id, null, UserId.Value, null, Blog3Channel1AcceptedPrice, PriceLastAcceptedDate, SubscriptionStartDate));

                await connection.InsertAsync(new ChannelSubscription(blog3Channels[2].Id, null, UserId.Value, null, Blog3Channel3AcceptedPrice, PriceLastAcceptedDate, SubscriptionStartDate));
            }
        }
Exemple #13
0
 private async Task CreateSubscriptionsAsync(TestDatabaseContext testDatabase)
 {
     using (var connection = testDatabase.CreateConnection())
     {
         foreach (var item in Subscriptions)
         {
             await connection.InsertAsync(
                 new ChannelSubscription(
                     item.ChannelId.Value,
                     null,
                     UserId.Value,
                     null,
                     item.AcceptedPrice.Value,
                     PriceLastAcceptedDate,
                     SubscriptionStartDate));
         }
     }
 }
Exemple #14
0
        private async Task <IReadOnlyList <UserId> > CreateDataAsync(TestDatabaseContext testDatabase)
        {
            using (var connection = testDatabase.CreateConnection())
            {
                var creatorChannelsSnapshots = new List <SubscriberSnapshot>();

                for (int i = 0; i < Days; i++)
                {
                    creatorChannelsSnapshots.Add(new SubscriberSnapshot(Now.AddDays(i), SubscriberId1.Value, i + "@test.com"));

                    creatorChannelsSnapshots.Add(new SubscriberSnapshot(Now.AddDays(i).AddHours(12), SubscriberId2.Value, i + "@test2.com"));
                }

                await connection.InsertAsync(creatorChannelsSnapshots, false);

                return(creatorChannelsSnapshots.Select(v => new UserId(v.SubscriberId)).ToList());
            }
        }
        private async Task CreateBlogAsync(TestDatabaseContext testDatabase)
        {
            var random = new Random();
            var blog   = BlogTests.UniqueEntity(random);

            blog.Id                = BlogId.Value;
            blog.CreatorId         = CreatorId.Value;
            blog.HeaderImageFileId = HeaderFileId.Value;
            blog.CreationDate      = CreationDate;
            blog.Description       = Description;
            blog.ExternalVideoUrl  = ExternalVideoUrl;
            blog.Introduction      = Introduction;
            blog.Name              = Name;

            using (var connection = testDatabase.CreateConnection())
            {
                await connection.InsertAsync(blog);
            }
        }
Exemple #16
0
        private async Task <IReadOnlyList <UserId> > CreateDataAsync(TestDatabaseContext testDatabase)
        {
            using (var connection = testDatabase.CreateConnection())
            {
                var snapshots = new List <CalculatedAccountBalance>();

                for (int i = 0; i < Days; i++)
                {
                    snapshots.Add(new CalculatedAccountBalance(SubscriberId1.Value, LedgerAccountType.FifthweekCredit, Now.AddDays(i), i));
                    snapshots.Add(new CalculatedAccountBalance(SubscriberId1.Value, LedgerAccountType.Stripe, Now.AddDays(i), i + 0.25m));

                    snapshots.Add(new CalculatedAccountBalance(SubscriberId2.Value, LedgerAccountType.FifthweekCredit, Now.AddDays(i).AddHours(12), i + 0.5m));
                    snapshots.Add(new CalculatedAccountBalance(SubscriberId2.Value, LedgerAccountType.Stripe, Now.AddDays(i).AddHours(12), i + 0.75m));
                }

                await connection.InsertAsync(snapshots, false);

                return(snapshots.Select(v => new UserId(v.UserId)).ToList());
            }
        }
        private async Task <FifthweekUser> CreateUserAsync(TestDatabaseContext testDatabase)
        {
            var random = new Random();
            var user   = UserTests.UniqueEntity(random);

            user.Id = UserId.Value;

            using (var databaseContext = testDatabase.CreateContext())
            {
                databaseContext.Users.Add(user);
                await databaseContext.SaveChangesAsync();
            }

            using (var connection = testDatabase.CreateConnection())
            {
                var users = await connection.QueryAsync <FifthweekUser>("SELECT * FROM AspNetUsers WHERE Id=@Id", new { Id = UserId.Value });

                return(users.Single());
            }
        }
        private async Task CreateDataAsync(TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                await databaseContext.CreateTestChannelsAsync(
                    CreatorId.Value,
                    BlogId.Value,
                    ChannelId1.Value,
                    ChannelId2.Value);

                await databaseContext.SaveChangesAsync();
            }

            using (var databaseConnection = testDatabase.CreateConnection())
            {
                await databaseConnection.InsertAsync(new FreeAccessUser(BlogId.Value, Email1));

                await databaseConnection.InsertAsync(new FreeAccessUser(BlogId.Value, Email2));
            }
        }
        private async Task <int> CreateDataAsync(TestDatabaseContext testDatabase)
        {
            IReadOnlyList <Channel> channels;

            using (var context = testDatabase.CreateContext())
            {
                channels = await context.CreateTestChannelsAsync(CreatorId.Value, Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid());

                await context.CreateTestUserAsync(SubscriberId.Value);
            }

            using (var connection = testDatabase.CreateConnection())
            {
                foreach (var channel in channels)
                {
                    await connection.InsertAsync(new ChannelSubscription(channel.Id, null, SubscriberId.Value, null, channel.Price / 2, DateTime.UtcNow, DateTime.UtcNow));
                }
            }

            return(channels.Sum(v => v.Price / 2));
        }
Exemple #20
0
        private async Task <List <IIdentityEquatable> > CreateEntitiesAsync(TestDatabaseContext testDatabase)
        {
            var random = new Random();

            using (var connection = testDatabase.CreateConnection())
            {
                var expectedDeletions = new List <IIdentityEquatable>();
                await this.AddUser(connection, random, UserId.Random(), IncludedDate, false);

                expectedDeletions.AddRange(await this.AddUser(connection, random, TestUserId1, IncludedDate, true));
                var channelId = new ChannelId(expectedDeletions.OfType <Channel>().First().Id);
                expectedDeletions.AddRange(await this.AddUser(connection, random, TestUserId2, IncludedDate, true, TestUserId1, channelId));

                var nonDeletedUserEntities = await this.AddUser(connection, random, TestUserId3, EndDate, true, TestUserId1, channelId);

                expectedDeletions.AddRange(nonDeletedUserEntities.OfType <ChannelSubscription>());

                // Files shouldn't be deleted.
                return(expectedDeletions.Where(v => !(v is File)).ToList());
            }
        }
        private static async Task <decimal> GetExistingFifthweekBalance(TestDatabaseContext testDatabase)
        {
            decimal existingFifthweekBalance;

            using (var connection = testDatabase.CreateConnection())
            {
                existingFifthweekBalance =
                    await
                    connection.ExecuteScalarAsync <decimal>(
                        string.Format(
                            "SELECT TOP 1 {0} FROM {1} WHERE {2}='{5}' AND {3}={6} ORDER BY {4} DESC",
                            CalculatedAccountBalance.Fields.Amount,
                            CalculatedAccountBalance.Table,
                            CalculatedAccountBalance.Fields.UserId,
                            CalculatedAccountBalance.Fields.AccountType,
                            CalculatedAccountBalance.Fields.Timestamp,
                            Guid.Empty.ToString(),
                            (int)LedgerAccountType.FifthweekRevenue));
            }
            return(existingFifthweekBalance);
        }
        private async Task CreateDataAsync(TestDatabaseContext testDatabase)
        {
            using (var connection = testDatabase.CreateConnection())
            {
                var ledgerEntry = new AppendOnlyLedgerRecord(
                    Guid.NewGuid(),
                    SubscriberId.Value,
                    CreatorId.Value,
                    Now,
                    10,
                    LedgerAccountType.FifthweekCredit,
                    LedgerTransactionType.SubscriptionPayment,
                    Guid.NewGuid(),
                    Guid.NewGuid(),
                    null,
                    null,
                    null);

                var ledgerEntry2 = new AppendOnlyLedgerRecord(
                    Guid.NewGuid(),
                    SubscriberId.Value,
                    CreatorId.Value,
                    Now.AddDays(1),
                    10,
                    LedgerAccountType.FifthweekCredit,
                    LedgerTransactionType.SubscriptionPayment,
                    Guid.NewGuid(),
                    Guid.NewGuid(),
                    null,
                    null,
                    null);

                await connection.InsertAsync(ledgerEntry, false);

                await connection.InsertAsync(ledgerEntry2, false);
            }
        }
        private async Task CreateChannelsAndCollectionsAsync(TestDatabaseContext testDatabase)
        {
            var random = new Random();

            var channel1 = ChannelTests.UniqueEntity(random);

            channel1.Id = ChannelId1.Value;
            ConfigureChannel(channel1);

            var channel2 = ChannelTests.UniqueEntity(random);

            channel2.Id = ChannelId2.Value;
            ConfigureChannel(channel2);

            var channel3 = ChannelTests.UniqueEntity(random);

            channel3.Id = ChannelId3.Value;
            ConfigureChannel(channel3);

            // Forth channel is not visible.
            var channel4 = ChannelTests.UniqueEntity(random);

            channel4.Id = ChannelId4.Value;
            ConfigureChannel(channel4);
            channel4.IsVisibleToNonSubscribers = false;

            using (var connection = testDatabase.CreateConnection())
            {
                await connection.InsertAsync(channel1);

                await connection.InsertAsync(channel2);

                await connection.InsertAsync(channel3);

                await connection.InsertAsync(channel4);
            }
        }
        private async Task CreateDataAsync(TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var random = new Random();
                this.CreateUser(random, databaseContext, UserId1);
                this.CreateUser(random, databaseContext, UserId2);
                this.CreateUser(random, databaseContext, UserId3);
                this.CreateUser(random, databaseContext, UserId4);
                this.CreateUser(random, databaseContext, UserId5);
                this.CreateUser(random, databaseContext, UserId6);
                await databaseContext.SaveChangesAsync();
            }

            using (var connection = testDatabase.CreateConnection())
            {
                // Not in list of user ids to update.
                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId1.Value,
                                                 null,
                                                 PaymentOriginKey,
                                                 PaymentOriginKeyType,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.Retry1));

                // Should be updated to Retry1.
                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId2.Value,
                                                 null,
                                                 PaymentOriginKey,
                                                 PaymentOriginKeyType,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.None));

                // Should be updated to Failed.
                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId3.Value,
                                                 null,
                                                 PaymentOriginKey,
                                                 PaymentOriginKeyType,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.Retry3));

                // Should not be updated because it is failed.
                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId4.Value,
                                                 null,
                                                 PaymentOriginKey,
                                                 PaymentOriginKeyType,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.Failed));

                // Should not be updated because it doesn't have a payment origin key.
                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId5.Value,
                                                 null,
                                                 null,
                                                 PaymentOriginKeyType.Stripe,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.None));

                // Should not be updated because it doesn't have a payment origin key type.
                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId5.Value,
                                                 null,
                                                 PaymentOriginKey,
                                                 PaymentOriginKeyType.None,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.None));
            }
        }
Exemple #25
0
        private async Task <IReadOnlyList <UserId> > CreateDataAsync(TestDatabaseContext testDatabase, int creatorChannelsSnapshotCount)
        {
            var random        = new Random();
            var testUserIds   = new List <Guid>();
            var normalUserIds = new List <Guid>();

            using (var databaseContext = testDatabase.CreateContext())
            {
                // Create some test users which should be ignored.
                for (int i = 0; i < 3; i++)
                {
                    var user = UserTests.UniqueEntity(random);
                    testUserIds.Add(user.Id);
                    databaseContext.Users.Add(user);
                }

                // Create some normal users which shouldn't be ignored.
                for (int i = 0; i < creatorChannelsSnapshotCount / 2; i++)
                {
                    var user = UserTests.UniqueEntity(random);
                    normalUserIds.Add(user.Id);
                    databaseContext.Users.Add(user);
                }

                await databaseContext.SaveChangesAsync();
            }

            using (var connection = testDatabase.CreateConnection())
            {
                var creatorChannelsSnapshots     = new List <SubscriberChannelsSnapshot>();
                var creatorChannelsSnapshotItems = new List <SubscriberChannelsSnapshotItem>();
                var userRoles = new List <FifthweekUserRole>();

                foreach (var userId in normalUserIds)
                {
                    // Add some normal users to creator roles.
                    if (random.NextDouble() > 0.5)
                    {
                        userRoles.Add(new FifthweekUserRole(FifthweekRole.CreatorId, userId));
                    }

                    creatorChannelsSnapshots.Add(new SubscriberChannelsSnapshot(Guid.NewGuid(), DateTime.UtcNow, Guid.NewGuid()));
                    creatorChannelsSnapshotItems.Add(new SubscriberChannelsSnapshotItem(creatorChannelsSnapshots.Last().Id, null, Guid.NewGuid(), Guid.NewGuid(), 100, DateTime.UtcNow));
                }

                for (int i = 0; i < creatorChannelsSnapshotCount - normalUserIds.Count; i++)
                {
                    // Add some deleted users (not in AspNetUsers table).
                    creatorChannelsSnapshots.Add(new SubscriberChannelsSnapshot(Guid.NewGuid(), DateTime.UtcNow, Guid.NewGuid()));
                    creatorChannelsSnapshotItems.Add(new SubscriberChannelsSnapshotItem(creatorChannelsSnapshots.Last().Id, null, Guid.NewGuid(), Guid.NewGuid(), 100, DateTime.UtcNow));
                }

                foreach (var userId in testUserIds)
                {
                    // Add some test users.
                    userRoles.Add(new FifthweekUserRole(FifthweekRole.TestUserId, userId));

                    if (random.NextDouble() > 0.5)
                    {
                        userRoles.Add(new FifthweekUserRole(FifthweekRole.CreatorId, userId));
                    }

                    creatorChannelsSnapshots.Add(new SubscriberChannelsSnapshot(Guid.NewGuid(), DateTime.UtcNow, userId));
                    creatorChannelsSnapshotItems.Add(new SubscriberChannelsSnapshotItem(creatorChannelsSnapshots.Last().Id, null, Guid.NewGuid(), Guid.NewGuid(), 100, DateTime.UtcNow));
                }

                await connection.InsertAsync(creatorChannelsSnapshots, false);

                await connection.InsertAsync(creatorChannelsSnapshotItems, false);

                await connection.InsertAsync(userRoles, false);

                return(creatorChannelsSnapshots.Where(v => !testUserIds.Contains(v.SubscriberId)).Select(v => new UserId(v.SubscriberId)).ToList());
            }
        }
Exemple #26
0
        private async Task CreateDataAsync(TestDatabaseContext testDatabase)
        {
            IReadOnlyList <Channel> blog1Channels;
            IReadOnlyList <Channel> blog2Channels;
            IReadOnlyList <Channel> blog3Channels;

            using (var databaseContext = testDatabase.CreateContext())
            {
                blog1Channels = await databaseContext.CreateTestChannelsAsync(Creator1Id.Value, Blog1Id.Value, Blog1ChannelIds.Select(v => v.Value).ToArray());

                blog2Channels = await databaseContext.CreateTestChannelsAsync(Creator2Id.Value, Blog2Id.Value, Blog2ChannelIds.Select(v => v.Value).ToArray());

                blog3Channels = await databaseContext.CreateTestChannelsAsync(Creator3Id.Value, Blog3Id.Value, Blog3ChannelIds.Select(v => v.Value).ToArray());

                blog2Channels[0].Price                     = Blog2Channel1CurrentPrice;
                blog2Channels[0].PriceLastSetDate          = PriceLastSetDate;
                blog2Channels[0].IsVisibleToNonSubscribers = true;
                blog3Channels[0].Price                     = Blog3Channel1CurrentPrice;
                blog3Channels[0].PriceLastSetDate          = PriceLastSetDate;
                blog3Channels[0].IsVisibleToNonSubscribers = false;
                blog3Channels[2].Price                     = Blog3Channel3CurrentPrice;
                blog3Channels[2].PriceLastSetDate          = PriceLastSetDate;
                blog3Channels[2].IsVisibleToNonSubscribers = true;

                await databaseContext.SaveChangesAsync();
            }

            await this.CreateUserAsync(testDatabase, UserId1, UserEmail1, Username1);

            await this.CreateUserAsync(testDatabase, UserId2, UserEmail2, Username2);

            await this.CreateUserAsync(testDatabase, UserId3, UserEmail3, Username3);

            await this.CreateProfileImagesAsync(testDatabase, ProfileImageFileId1, UserId1);

            using (var connection = testDatabase.CreateConnection())
            {
                foreach (var item in InitialFreeAccessUsers)
                {
                    await connection.InsertAsync(item);
                }

                await connection.InsertAsync(new ChannelSubscription(blog1Channels[0].Id, null, UserId1.Value, null, 10, PriceLastAcceptedDate, SubscriptionStartDate));

                await connection.InsertAsync(new ChannelSubscription(blog1Channels[1].Id, null, UserId1.Value, null, 10, PriceLastAcceptedDate, SubscriptionStartDate));

                await connection.InsertAsync(new ChannelSubscription(blog2Channels[0].Id, null, UserId1.Value, null, 10, PriceLastAcceptedDate, SubscriptionStartDate));

                await connection.InsertAsync(new ChannelSubscription(blog3Channels[0].Id, null, UserId1.Value, null, 10, PriceLastAcceptedDate, SubscriptionStartDate));

                await connection.InsertAsync(new ChannelSubscription(blog3Channels[2].Id, null, UserId1.Value, null, 10, PriceLastAcceptedDate, SubscriptionStartDate));

                await connection.InsertAsync(new ChannelSubscription(blog1Channels[0].Id, null, UserId2.Value, null, 20, PriceLastAcceptedDate, SubscriptionStartDate));

                await connection.InsertAsync(new ChannelSubscription(blog1Channels[1].Id, null, UserId2.Value, null, 20, PriceLastAcceptedDate, SubscriptionStartDate));

                await connection.InsertAsync(new ChannelSubscription(blog1Channels[1].Id, null, UserId3.Value, null, 30, PriceLastAcceptedDate, SubscriptionStartDate));


                await connection.InsertAsync(
                    new UserPaymentOrigin(
                        UserId1.Value,
                        null,
                        "blah",
                        PaymentOriginKeyType.Stripe,
                        null,
                        null,
                        null,
                        null,
                        PaymentStatus.Retry2));

                await connection.InsertAsync(
                    new UserPaymentOrigin(
                        UserId2.Value,
                        null,
                        "blah",
                        PaymentOriginKeyType.None,
                        null,
                        null,
                        null,
                        null,
                        PaymentStatus.Failed));
            }
        }
        private async Task CreateDataAsync(TestDatabaseContext testDatabase)
        {
            var random = new Random();

            using (var databaseContext = testDatabase.CreateContext())
            {
                // Create some test users which should be ignored.
                var testUser = UserTests.UniqueEntity(random);
                testUser.Id = TestUserId.Value;
                databaseContext.Users.Add(testUser);

                // Add one creator to database
                var creator = UserTests.UniqueEntity(random);
                creator.Id = CreatorId2.Value;
                databaseContext.Users.Add(creator);

                await databaseContext.SaveChangesAsync();
            }

            using (var connection = testDatabase.CreateConnection())
            {
                var subscriberChannelsSnapshots     = new List <SubscriberChannelsSnapshot>();
                var subscriberChannelsSnapshotItems = new List <SubscriberChannelsSnapshotItem>();
                var userRoles = new List <FifthweekUserRole>();

                subscriberChannelsSnapshots.Add(new SubscriberChannelsSnapshot(Guid.NewGuid(), Now, SubscriberId.Value));
                subscriberChannelsSnapshotItems.Add(new SubscriberChannelsSnapshotItem(subscriberChannelsSnapshots.Last().Id, null, ChannelId1.Value, CreatorId1.Value, 100, Now));
                subscriberChannelsSnapshotItems.Add(new SubscriberChannelsSnapshotItem(subscriberChannelsSnapshots.Last().Id, null, ChannelId2.Value, CreatorId1.Value, 100, Now));

                subscriberChannelsSnapshots.Add(new SubscriberChannelsSnapshot(Guid.NewGuid(), Now.AddDays(-1), SubscriberId.Value));
                subscriberChannelsSnapshotItems.Add(new SubscriberChannelsSnapshotItem(subscriberChannelsSnapshots.Last().Id, null, ChannelId1.Value, CreatorId1.Value, 100, Now));

                subscriberChannelsSnapshots.Add(new SubscriberChannelsSnapshot(Guid.NewGuid(), Now.AddDays(1), SubscriberId.Value));
                subscriberChannelsSnapshotItems.Add(new SubscriberChannelsSnapshotItem(subscriberChannelsSnapshots.Last().Id, null, ChannelId3.Value, CreatorId2.Value, 100, DateTime.UtcNow));
                subscriberChannelsSnapshotItems.Add(new SubscriberChannelsSnapshotItem(subscriberChannelsSnapshots.Last().Id, null, TestUserChannelId.Value, TestUserId.Value, 100, DateTime.UtcNow));

                await connection.InsertAsync(subscriberChannelsSnapshots, false);

                await connection.InsertAsync(subscriberChannelsSnapshotItems, false);

                var creatorChannelsSnapshots     = new List <CreatorChannelsSnapshot>();
                var creatorChannelsSnapshotItems = new List <CreatorChannelsSnapshotItem>();

                creatorChannelsSnapshots.Add(new CreatorChannelsSnapshot(Guid.NewGuid(), DateTime.UtcNow, CreatorId1.Value));
                creatorChannelsSnapshotItems.Add(new CreatorChannelsSnapshotItem(creatorChannelsSnapshots.Last().Id, null, ChannelId1.Value, 100));

                creatorChannelsSnapshots.Add(new CreatorChannelsSnapshot(Guid.NewGuid(), DateTime.UtcNow, CreatorId1.Value));
                creatorChannelsSnapshotItems.Add(new CreatorChannelsSnapshotItem(creatorChannelsSnapshots.Last().Id, null, ChannelId2.Value, 100));
                creatorChannelsSnapshots.Add(new CreatorChannelsSnapshot(Guid.NewGuid(), DateTime.UtcNow, CreatorId1.Value));
                creatorChannelsSnapshotItems.Add(new CreatorChannelsSnapshotItem(creatorChannelsSnapshots.Last().Id, null, ChannelId2.Value, 100));

                userRoles.Add(new FifthweekUserRole(FifthweekRole.CreatorId, CreatorId2.Value));
                creatorChannelsSnapshots.Add(new CreatorChannelsSnapshot(Guid.NewGuid(), DateTime.UtcNow, CreatorId2.Value));
                creatorChannelsSnapshotItems.Add(new CreatorChannelsSnapshotItem(creatorChannelsSnapshots.Last().Id, null, ChannelId3.Value, 100));

                userRoles.Add(new FifthweekUserRole(FifthweekRole.TestUserId, TestUserId.Value));
                creatorChannelsSnapshots.Add(new CreatorChannelsSnapshot(Guid.NewGuid(), DateTime.UtcNow, TestUserId.Value));
                creatorChannelsSnapshotItems.Add(new CreatorChannelsSnapshotItem(creatorChannelsSnapshots.Last().Id, null, TestUserChannelId.Value, 100));

                await connection.InsertAsync(creatorChannelsSnapshots, false);

                await connection.InsertAsync(creatorChannelsSnapshotItems, false);

                await connection.InsertAsync(userRoles, false);
            }
        }
        private async Task CreateDataAsync(TestDatabaseContext testDatabase)
        {
            using (var databaseContext = testDatabase.CreateContext())
            {
                var random = new Random();
                this.CreateUser(random, databaseContext, UserId1);
                this.CreateUser(random, databaseContext, UserId2);
                this.CreateUser(random, databaseContext, UserId3);
                this.CreateUser(random, databaseContext, UserId4);
                this.CreateUser(random, databaseContext, UserId5);
                this.CreateUser(random, databaseContext, UserId6);
                this.CreateUser(random, databaseContext, UserId7);
                await databaseContext.SaveChangesAsync();
            }

            using (var connection = testDatabase.CreateConnection())
            {
                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId1.Value,
                                                 null,
                                                 PaymentOriginKey,
                                                 PaymentOriginKeyType,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.None));

                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId2.Value,
                                                 null,
                                                 PaymentOriginKey,
                                                 PaymentOriginKeyType,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.Retry1));

                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId3.Value,
                                                 null,
                                                 PaymentOriginKey,
                                                 PaymentOriginKeyType,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.Retry2));

                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId4.Value,
                                                 null,
                                                 PaymentOriginKey,
                                                 PaymentOriginKeyType,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.Retry3));

                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId5.Value,
                                                 null,
                                                 PaymentOriginKey,
                                                 PaymentOriginKeyType,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.Failed));

                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId6.Value,
                                                 null,
                                                 null,
                                                 PaymentOriginKeyType,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.Retry2));

                await connection.InsertAsync(new UserPaymentOrigin(
                                                 UserId6.Value,
                                                 null,
                                                 PaymentOriginKey,
                                                 PaymentOriginKeyType.None,
                                                 CountryCode,
                                                 CreditCardPrefix,
                                                 IpAddress,
                                                 TaxamoTransactionKey,
                                                 PaymentStatus.Retry2));
            }
        }
Exemple #29
0
        private async Task CreateUser(TestDatabaseContext testDatabase, bool addUser1Roles = true)
        {
            using (var connection = testDatabase.CreateConnection())
            {
                var random = new Random();
                var user1  = UserTests.UniqueEntity(random);
                user1.Id           = UserId.Value;
                user1.UserName     = Username;
                user1.PasswordHash = user1.UserName;

                var user2 = UserTests.UniqueEntity(random);
                user2.Id           = Guid.NewGuid();
                user2.PasswordHash = user1.UserName;

                await connection.InsertAsync(user1);

                await connection.InsertAsync(user2);

                var role1 = new FifthweekRole(Guid.NewGuid());
                role1.Name = "One";

                var role2 = new FifthweekRole(Guid.NewGuid());
                role2.Name = "Two";

                var role3 = new FifthweekRole(Guid.NewGuid());
                role3.Name = "Three";

                await connection.InsertAsync(role1);

                await connection.InsertAsync(role2);

                await connection.InsertAsync(role3);

                if (addUser1Roles)
                {
                    var userRole1 = new FifthweekUserRole();
                    userRole1.UserId = user1.Id;
                    userRole1.RoleId = role1.Id;

                    var userRole2 = new FifthweekUserRole();
                    userRole2.UserId = user1.Id;
                    userRole2.RoleId = role2.Id;

                    await connection.InsertAsync(userRole1);

                    await connection.InsertAsync(userRole2);
                }

                var userRole3 = new FifthweekUserRole();
                userRole3.UserId = user2.Id;
                userRole3.RoleId = role2.Id;

                var userRole4 = new FifthweekUserRole();
                userRole4.UserId = user2.Id;
                userRole4.RoleId = role3.Id;

                await connection.InsertAsync(userRole3);

                await connection.InsertAsync(userRole4);
            }
        }
        private async Task <IReadOnlyList <UserId> > CreateDataAsync(TestDatabaseContext testDatabase)
        {
            using (var context = testDatabase.CreateContext())
            {
                var random = new Random();
                var user   = UserTests.UniqueEntity(random);
                user.Id             = UserId1.Value;
                user.UserName       = Username1.Value;
                user.Email          = Email1.Value;
                user.EmailConfirmed = EmailConfirmed1;
                context.Users.Add(user);
                await context.SaveChangesAsync();
            }

            using (var connection = testDatabase.CreateConnection())
            {
                var snapshots     = new List <CalculatedAccountBalance>();
                var ledgerRecords = new List <AppendOnlyLedgerRecord>();

                for (int i = 0; i < Days; i++)
                {
                    snapshots.Add(new CalculatedAccountBalance(UserId1.Value, LedgerAccountType.FifthweekRevenue, Now.AddDays(i), (i + 1) * 2));
                    snapshots.Add(new CalculatedAccountBalance(UserId1.Value, LedgerAccountType.FifthweekCredit, Now.AddDays(i), i));
                    snapshots.Add(new CalculatedAccountBalance(UserId1.Value, LedgerAccountType.Stripe, Now.AddDays(i), i + 0.25m));
                    snapshots.Add(new CalculatedAccountBalance(UserId1.Value, LedgerAccountType.ReleasedRevenue, Now.AddDays(i), (i + 1) * 4));
                    ledgerRecords.Add(
                        new AppendOnlyLedgerRecord(
                            Guid.NewGuid(),
                            UserId1.Value,
                            null,
                            Now.AddDays(i),
                            1,
                            LedgerAccountType.FifthweekRevenue,
                            LedgerTransactionType.SubscriptionPayment,
                            Guid.NewGuid(),
                            null,
                            null,
                            null,
                            null));

                    snapshots.Add(new CalculatedAccountBalance(UserId2.Value, LedgerAccountType.FifthweekRevenue, Now.AddDays(i), (i + 1) * 3));
                    snapshots.Add(new CalculatedAccountBalance(UserId2.Value, LedgerAccountType.FifthweekCredit, Now.AddDays(i).AddHours(12), i + 0.5m));
                    snapshots.Add(new CalculatedAccountBalance(UserId2.Value, LedgerAccountType.Stripe, Now.AddDays(i).AddHours(12), i + 0.75m));
                    snapshots.Add(new CalculatedAccountBalance(UserId2.Value, LedgerAccountType.ReleasedRevenue, Now.AddDays(i), (i + 1) * 5));
                    ledgerRecords.Add(
                        new AppendOnlyLedgerRecord(
                            Guid.NewGuid(),
                            UserId2.Value,
                            null,
                            Now.AddDays(i),
                            2,
                            LedgerAccountType.FifthweekRevenue,
                            LedgerTransactionType.SubscriptionPayment,
                            Guid.NewGuid(),
                            null,
                            null,
                            null,
                            null));
                }

                snapshots.Add(new CalculatedAccountBalance(UserId3.Value, LedgerAccountType.FifthweekRevenue, Now, 100));

                await connection.InsertAsync(snapshots, false);

                await connection.InsertAsync(ledgerRecords, false);

                return(snapshots.Select(v => new UserId(v.UserId)).ToList());
            }
        }