public async Task SynchronizeChallenge_ShouldThrowFailedPreconditionRpcException()
        {
            // Arrange
            var          userId      = new UserId();
            const string email       = "*****@*****.**";
            var          challengeId = new ChallengeId();

            var claims = new[] { new Claim(JwtClaimTypes.Subject, userId.ToString()), new Claim(JwtClaimTypes.Email, email) };
            var host   = TestHost.WithClaimsFromBearerAuthentication(claims);

            host.Server.CleanupDbContext();

            await host.Server.UsingScopeAsync(
                async scope =>
            {
                var challengeRepository = scope.GetRequiredService <IChallengeRepository>();

                var challenge = new Challenge(
                    challengeId,
                    new ChallengeName("test"),
                    Game.LeagueOfLegends,
                    BestOf.Five,
                    Entries.Eight,
                    new ChallengeTimeline(new UtcNowDateTimeProvider(), ChallengeDuration.FiveDays),
                    new Scoring());

                challengeRepository.Create(challenge);
                await challengeRepository.CommitAsync(false);
            });

            var request = new SynchronizeChallengeRequest
            {
                ChallengeId = challengeId
            };

            var client = new ChallengeService.ChallengeServiceClient(host.CreateChannel());

            // Act Assert
            var func = new Func <Task>(async() => await client.SynchronizeChallengeAsync(request));

            func.Should().Throw <RpcException>();
        }
        public void CreateChallenge_ShouldThrowFailedPreconditionRpcException()
        {
            // Arrange
            var          userId = new UserId();
            const string email  = "*****@*****.**";

            var claims = new[] { new Claim(JwtClaimTypes.Subject, userId.ToString()), new Claim(JwtClaimTypes.Email, email) };
            var host   = TestHost.WithClaimsFromBearerAuthentication(claims);

            host.Server.CleanupDbContext();

            var request = new CreateChallengeRequest();

            var client = new ChallengeService.ChallengeServiceClient(host.CreateChannel());

            // Act Assert
            var func = new Func <Task>(async() => await client.CreateChallengeAsync(request));

            func.Should().Throw <RpcException>();
        }
        public async Task FetchChallenges_ShouldBeOfTypeFetchChallengesResponse()
        {
            // Arrange
            var          userId = new UserId();
            const string email  = "*****@*****.**";

            var claims = new[] { new Claim(JwtClaimTypes.Subject, userId.ToString()), new Claim(JwtClaimTypes.Email, email) };
            var host   = TestHost.WithClaimsFromBearerAuthentication(claims);

            host.Server.CleanupDbContext();

            var request = new FetchChallengesRequest();

            var client = new ChallengeService.ChallengeServiceClient(host.CreateChannel());

            // Act
            var response = await client.FetchChallengesAsync(request);

            //Assert
            response.Should().BeOfType <FetchChallengesResponse>();
        }
        public async Task CreateChallenge_ShouldBeOfTypeCreateChallengeResponse()
        {
            // Arrange
            var          userId = new UserId();
            const string email  = "*****@*****.**";

            var claims = new[] { new Claim(JwtClaimTypes.Subject, userId.ToString()), new Claim(JwtClaimTypes.Email, email) };
            var host   = TestHost.WithClaimsFromBearerAuthentication(claims);

            host.Server.CleanupDbContext();

            var request = new CreateChallengeRequest
            {
                BestOf   = BestOf.Five,
                Duration = 5,
                Entries  = Entries.Four,
                Game     = EnumGame.LeagueOfLegends,
                Name     = "Test",
                Scoring  = new ChallengeScoringDto
                {
                    Items =
                    {
                        new ChallengeScoringDto.Types.Item
                        {
                            StatName      = "StatName",
                            StatWeighting = 5F,
                            Order         = 0
                        }
                    }
                }
            };

            var client = new ChallengeService.ChallengeServiceClient(host.CreateChannel());

            // Act
            var response = await client.CreateChallengeAsync(request);

            //Assert
            response.Should().BeOfType <CreateChallengeResponse>();
        }
        public async Task Success()
        {
            // Arrange
            var mockServiceBusPubliser = new Mock <IServiceBusPublisher>();

            mockServiceBusPubliser.Setup(serviceBusPubliser => serviceBusPubliser.PublishAsync(It.IsAny <ChallengeParticipantRegisteredIntegrationEvent>()))
            .Verifiable();

            var user = new User
            {
                Id             = Guid.NewGuid(),
                Email          = "*****@*****.**",
                EmailConfirmed = true,
                UserName       = "******",
                Country        = Country.Canada
            };

            var account = new Account(user.Id.ConvertTo <UserId>());

            var moneyAccount = new MoneyAccountDecorator(account);

            moneyAccount.Deposit(Money.TwoHundred).MarkAsSucceeded();

            var doxatag = new Doxatag(
                account.Id,
                "TestUser",
                1000,
                new UtcNowDateTimeProvider());

            var gamePlayerId = new PlayerId();

            var challenge = new Challenge(
                new ChallengeId(),
                new ChallengeName("TestChallenge"),
                Game.LeagueOfLegends,
                BestOf.One,
                Entries.Two,
                new ChallengeTimeline(new UtcNowDateTimeProvider(), ChallengeDuration.OneDay),
                Scoring);

            var payout = new ChallengePayoutFactory();

            var challengePayout = new Cashier.Domain.AggregateModels.ChallengeAggregate.Challenge(
                challenge.Id,
                payout.CreateInstance().GetChallengePayout(ChallengePayoutEntries.One, MoneyEntryFee.OneHundred));

            using var gamesHost = new GamesHostFactory().WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, account.Id));

            gamesHost.Server.CleanupDbContext();

            await gamesHost.Server.UsingScopeAsync(
                async scope =>
            {
                var gameCredentialRepository = scope.GetRequiredService <IGameCredentialRepository>();

                gameCredentialRepository.CreateCredential(
                    new Credential(
                        account.Id,
                        challenge.Game,
                        gamePlayerId,
                        new UtcNowDateTimeProvider()));

                await gameCredentialRepository.UnitOfWork.CommitAsync(false);
            });

            var gameServiceClient = new GameService.GameServiceClient(gamesHost.CreateChannel());

            using var challengesHost = new ChallengesHostFactory().WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, account.Id))
                                       .WithWebHostBuilder(
                      builder =>
            {
                builder.ConfigureTestContainer <ContainerBuilder>(
                    container =>
                {
                    container.RegisterInstance(mockServiceBusPubliser.Object).As <IServiceBusPublisher>().SingleInstance();
                });
            });

            challengesHost.Server.CleanupDbContext();

            await challengesHost.Server.UsingScopeAsync(
                async scope =>
            {
                var challengeRepository = scope.GetRequiredService <IChallengeRepository>();

                challengeRepository.Create(challenge);

                await challengeRepository.CommitAsync(false);
            });

            var challengeServiceClient = new ChallengeService.ChallengeServiceClient(challengesHost.CreateChannel());

            using var cashierHost = new CashierHostFactory().WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, account.Id));

            cashierHost.Server.CleanupDbContext();

            await cashierHost.Server.UsingScopeAsync(
                async scope =>
            {
                var accountRepository = scope.GetRequiredService <IAccountRepository>();

                accountRepository.Create(account);

                await accountRepository.CommitAsync();
            });

            await cashierHost.Server.UsingScopeAsync(
                async scope =>
            {
                var challengeRepository = scope.GetRequiredService <IChallengePayoutRepository>();

                challengeRepository.Create(challengePayout);

                await challengeRepository.CommitAsync();
            });

            var cashierServiceClient = new CashierService.CashierServiceClient(cashierHost.CreateChannel());

            using var identityHost = new IdentityHostFactory();

            identityHost.Server.CleanupDbContext();

            await identityHost.Server.UsingScopeAsync(
                async scope =>
            {
                var doxatagRepository = scope.GetRequiredService <IUserService>();

                await doxatagRepository.CreateAsync(user, "Pass@word1");
            });

            await identityHost.Server.UsingScopeAsync(
                async scope =>
            {
                var doxatagRepository = scope.GetRequiredService <IDoxatagRepository>();

                doxatagRepository.Create(doxatag);

                await doxatagRepository.UnitOfWork.CommitAsync(false);
            });

            var identityServiceClient = new IdentityService.IdentityServiceClient(identityHost.CreateChannel());

            using var challengesAggregatorHost = new ChallengesWebAggregatorHostFactory()
                                                 .WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, account.Id))
                                                 .WithWebHostBuilder(
                      builder =>
            {
                builder.ConfigureTestContainer <ContainerBuilder>(
                    container =>
                {
                    container.RegisterInstance(challengeServiceClient).SingleInstance();

                    container.RegisterInstance(cashierServiceClient).SingleInstance();

                    container.RegisterInstance(identityServiceClient).SingleInstance();

                    container.RegisterInstance(gameServiceClient).SingleInstance();
                });
            });

            var client = challengesAggregatorHost.CreateClient();

            // Act
            var response = await client.PostAsJsonAsync(
                $"api/challenges/{challenge.Id}/participants",
                new
            {
            });

            // Assert
            response.EnsureSuccessStatusCode();

            mockServiceBusPubliser.Verify(
                serviceBusPubliser => serviceBusPubliser.PublishAsync(It.IsAny <ChallengeParticipantRegisteredIntegrationEvent>()),
                Times.Once);
        }
 public ChallengeRecurringJob(ChallengeService.ChallengeServiceClient challengeServiceClient, GameService.GameServiceClient gameServiceClient)
 {
     _challengeServiceClient = challengeServiceClient;
     _gameServiceClient      = gameServiceClient;
 }