Esempio n. 1
0
        /// <summary>
        /// Authenticate against the F1 login page.
        /// </summary>
        /// <param name="page">Page to login through.</param>
        /// <param name="login">Username/Email to authenticate with</param>
        /// <param name="password">Password to authenticate with</param>
        /// <param name="cancellationToken">Possible cancellation request token</param>
        /// <returns>Authentication response values</returns>
        /// <exception cref="AuthenticationException">Authentication failure exception</exception>
        public async Task <AuthenticationResponse> AuthenticateAsync(IPage page, string login, string password, CancellationToken cancellationToken = default)
        {
            _logger.LogDebug("Navigating to {Endpoint} to authenticate {User}", Endpoints.LoginPage, login);
            await page.GoToAsync(Endpoints.LoginPage, LifecycleEvent.DOMContentLoaded)
            .ConfigureAwait(false);

#if DEBUG
            _logger.LogTrace("Navigated to page {Endpoint} to authenticate {User}", Endpoints.LoginPage, login);
#endif
            await TaskUtils.RandomDelay(_random, 200, 1500, cancellationToken);

            await PageUtils.ClickAsync(page, ConsentButtonSelector, _random);

            await TaskUtils.RandomDelay(_random, cancellationToken);

            await PageUtils.TypeAsync(page, LoginSelector, login, 50, 300, _random, cancellationToken)
            .ConfigureAwait(false);

            await TaskUtils.RandomDelay(_random, cancellationToken);

            await PageUtils.TypeAsync(page, PasswordSelector, password, 50, 300, _random, cancellationToken)
            .ConfigureAwait(false);

            await TaskUtils.RandomDelay(_random, cancellationToken);

            Task <IResponse> responseTask = page.WaitForResponseAsync(Endpoints.AuthenticationByPassword);
            Task             clickTask    = PageUtils.ClickAsync(page, LoginButtonSelector, _random);
#if DEBUG
            _logger.LogTrace("Awaiting click & response");
#endif

            Task.WaitAll(responseTask, clickTask);
#if DEBUG
            _logger.LogDebug("Response received for {Endpoint}", Endpoints.AuthenticationByPassword);
#endif

            switch (responseTask.Result.Status)
            {
            case HttpStatusCode.OK:
                AuthenticationResponse response = await responseTask.Result.GetJsonAsync <AuthenticationResponse>()
                                                  .ConfigureAwait(false);

                if (response != null && response.Session != null && !string.IsNullOrWhiteSpace(response.Session.Token))
                {
                    _logger.LogInformation("Authenticated {User} successfully.", login);
                    return(response);
                }
                throw new AuthenticationException("Authentication failed as an invalid/incomprehensive response was retrieved.", HttpStatusCode.Forbidden);

            case HttpStatusCode.Unauthorized:
                throw new AuthenticationException("Invalid credentials provided.", responseTask.Result.Status);
            }
            throw new ApiException("Unhandled response (this shouldn't happen!)");
        }
        public async Task ClickAsync_ShouldNotClickSelector()
        {
            // Arrange
            var clicked = false;

            var pageMoq = new Mock <IPage>();

            pageMoq.Setup(x => x.ClickAsync("specificSelector",
                                            It.IsAny <int>(),
                                            It.IsAny <MouseButton>(), It.IsAny <int>(),
                                            It.IsAny <Modifier[]>(),
                                            It.IsAny <System.Drawing.Point?>(),
                                            It.IsAny <int?>(),
                                            It.IsAny <bool>(),
                                            It.IsAny <bool?>()))
            .Callback(() => clicked = true);

            // Act
            await PageUtils.ClickAsync(pageMoq.Object, "randomSelector");

            // Assert
            clicked.Should()
            .BeFalse();
        }