public void RegisterChallengeParticipant_ShouldThrowNotFoundRpcException()
        {
            // 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 RegisterChallengeParticipantRequest
            {
                ChallengeId   = new ChallengeId(),
                GamePlayerId  = new PlayerId(),
                ParticipantId = new ParticipantId()
            };

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

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

            func.Should().Throw <RpcException>();
        }
        public async Task RegisterChallengeParticipant_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.Two,
                    new ChallengeTimeline(new UtcNowDateTimeProvider(), ChallengeDuration.FiveDays),
                    new Scoring());

                challenge.Register(
                    new Participant(
                        new ParticipantId(),
                        new UserId(),
                        new PlayerId(),
                        new UtcNowDateTimeProvider()));

                challenge.Register(
                    new Participant(
                        new ParticipantId(),
                        new UserId(),
                        new PlayerId(),
                        new UtcNowDateTimeProvider()));

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

            var request = new RegisterChallengeParticipantRequest
            {
                ChallengeId   = challengeId,
                GamePlayerId  = new PlayerId(),
                ParticipantId = new ParticipantId()
            };

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

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

            func.Should().Throw <RpcException>();
        }
        public async Task RegisterChallengeParticipant_ShouldBeOfTypeRegisterChallengeParticipantResponse()
        {
            // Arrange
            var userId = new UserId();

            var challengeId = new ChallengeId();

            var host = TestHost.WithClaimsFromBearerAuthentication(new Claim(JwtClaimTypes.Subject, userId.ToString()));

            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.Two,
                    new ChallengeTimeline(new UtcNowDateTimeProvider(), ChallengeDuration.FiveDays),
                    new Scoring());

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

            var request = new RegisterChallengeParticipantRequest
            {
                ChallengeId   = challengeId,
                GamePlayerId  = new PlayerId(),
                ParticipantId = new ParticipantId()
            };

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

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

            //Assert
            response.Should().BeOfType <RegisterChallengeParticipantResponse>();
        }
        public override async Task <RegisterChallengeParticipantResponse> RegisterChallengeParticipant(
            RegisterChallengeParticipantRequest request,
            ServerCallContext context
            )
        {
            var httpContext = context.GetHttpContext();

            var userId = httpContext.GetUserId();

            var challengeId = request.ChallengeId.ParseEntityId <ChallengeId>();

            if (!await _challengeService.ChallengeExistsAsync(challengeId))
            {
                throw context.NotFoundRpcException("Challenge not found.");
            }

            var challenge = await _challengeService.FindChallengeAsync(challengeId);

            var result = await _challengeService.RegisterChallengeParticipantAsync(
                challenge,
                userId,
                request.ParticipantId.ParseEntityId <ParticipantId>(),
                request.GamePlayerId.ParseStringId <PlayerId>(),
                new UtcNowDateTimeProvider());

            if (result.IsValid)
            {
                var response = new RegisterChallengeParticipantResponse
                {
                    Participant = ChallengeProfile.Map(challenge, result.Response)
                };

                return(context.Ok(response));
            }

            throw context.FailedPreconditionRpcException(result);
        }
Exemple #5
0
        public async Task <IActionResult> RegisterChallengeParticipantAsync(string challengeId)
        {
            var participantId = new ParticipantId();

            var findChallengeRequest = new FindChallengeRequest
            {
                ChallengeId = challengeId
            };

            var findChallengeResponse = await _challengesServiceClient.FindChallengeAsync(findChallengeRequest);

            var findPlayerGameCredentialRequest = new FindPlayerGameCredentialRequest
            {
                Game = findChallengeResponse.Challenge.Game
            };

            var findPlayerGameCredentialResponse = await _gameServiceClient.FindPlayerGameCredentialAsync(findPlayerGameCredentialRequest);

            var fetchDoxatagsRequest = new FetchDoxatagsRequest();

            var fetchDoxatagsResponse = await _identityServiceClient.FetchDoxatagsAsync(fetchDoxatagsRequest);

            var findChallengePayoutRequest = new FindChallengePayoutRequest
            {
                ChallengeId = challengeId
            };

            var challengePayoutResponse = await _cashierServiceClient.FindChallengePayoutAsync(findChallengePayoutRequest);

            var createTransactionRequest = new CreateTransactionRequest
            {
                Custom = new CustomTransaction
                {
                    Type     = EnumTransactionType.Charge,
                    Currency = challengePayoutResponse.Payout.EntryFee
                },
                Metadata =
                {
                    new Dictionary <string, string>
                    {
                        [nameof(ChallengeId)]   = challengeId,
                        [nameof(ParticipantId)] = participantId
                    }
                }
            };

            var createTransactionResponse = await _cashierServiceClient.CreateTransactionAsync(createTransactionRequest);

            try
            {
                var registerChallengeParticipantRequest = new RegisterChallengeParticipantRequest
                {
                    ChallengeId   = challengeId,
                    GamePlayerId  = findPlayerGameCredentialResponse.Credential.PlayerId,
                    ParticipantId = participantId
                };

                var participant = await _challengesServiceClient.RegisterChallengeParticipantAsync(registerChallengeParticipantRequest);

                return(this.Ok(ChallengeMapper.Map(challengePayoutResponse.Payout.ChallengeId, participant.Participant, fetchDoxatagsResponse.Doxatags)));
            }
            catch (RpcException exception)
            {
                var deleteTransactionRequest = new DeleteTransactionRequest
                {
                    TransactionId = createTransactionResponse.Transaction.Id
                };

                await _cashierServiceClient.DeleteTransactionAsync(deleteTransactionRequest);

                throw exception.Capture();
            }
        }