Ejemplo n.º 1
0
        public async Task ShouldBeHttpStatusCodeOk()
        {
            // Arrange
            var userId   = new UserId();
            var playerId = new PlayerId();

            var authentication = new LeagueOfLegendsGameAuthentication(
                playerId,
                new LeagueOfLegendsGameAuthenticationFactor(
                    1,
                    string.Empty,
                    2,
                    string.Empty));

            var summoner = new Summoner
            {
                AccountId     = playerId,
                ProfileIconId = authentication.Factor.ExpectedSummonerProfileIconId
            };

            var factory = TestHost.WithClaimsFromDefaultAuthentication(new Claim(JwtClaimTypes.Subject, userId.ToString()))
                          .WithWebHostBuilder(
                builder => builder.ConfigureTestContainer <ContainerBuilder>(
                    container =>
            {
                var mockLeagueOfLegendsService = new Mock <ILeagueOfLegendsService>();

                mockLeagueOfLegendsService
                .Setup(leagueOfLegendsService => leagueOfLegendsService.Summoner.GetSummonerByAccountIdAsync(It.IsAny <Region>(), playerId))
                .ReturnsAsync(summoner)
                .Verifiable();

                container.RegisterInstance(mockLeagueOfLegendsService.Object).As <ILeagueOfLegendsService>().SingleInstance();
            }));

            _httpClient = factory.CreateClient();

            factory.Server.CleanupDbContext();

            await factory.Server.UsingScopeAsync(
                async scope =>
            {
                var credentialRepository = scope.GetRequiredService <IGameAuthenticationRepository>();

                await credentialRepository.AddAuthenticationAsync(userId, Game.LeagueOfLegends, authentication);
            });

            // Act
            using var response = await this.ExecuteAsync(Game.LeagueOfLegends);

            // Assert
            response.EnsureSuccessStatusCode();

            response.StatusCode.Should().Be(HttpStatusCode.OK);
        }
        public async Task GenerateAuthenticationAsync_ShouldBeOfTypeOkObjectResult()
        {
            // Arrange
            var gameAuthentication = new LeagueOfLegendsGameAuthentication(
                PlayerId.Parse("playerId"),
                new LeagueOfLegendsGameAuthenticationFactor(
                    1,
                    string.Empty,
                    2,
                    string.Empty));

            TestMock.GameAuthenticationService
            .Setup(authFactorService => authFactorService.GenerateAuthenticationAsync(It.IsAny <UserId>(), It.IsAny <Game>(), It.IsAny <object>()))
            .ReturnsAsync(DomainValidationResult <GameAuthentication> .Succeeded(gameAuthentication))
            .Verifiable();

            var authFactorController = new GameAuthenticationsController(
                TestMock.GameAuthenticationService.Object,
                TestMock.GameCredentialService.Object,
                TestMapper)
            {
                ControllerContext =
                {
                    HttpContext = MockHttpContextAccessor.GetInstance()
                }
            };

            // Act
            var result = await authFactorController.GenerateAuthenticationAsync(Game.LeagueOfLegends, "playerId");

            // Assert
            result.Should().BeOfType <OkObjectResult>();

            TestMock.GameAuthenticationService.Verify(
                authFactorService => authFactorService.GenerateAuthenticationAsync(It.IsAny <UserId>(), It.IsAny <Game>(), It.IsAny <object>()),
                Times.Once);
        }