Ejemplo n.º 1
0
        public void Attempt_to_refresh_token_multiple_times_despite_exception()
        {
            // Arrange
            var clientId          = "abc123";
            var clientSecret      = "xyz789";
            var authorizationCode = "INVALID_AUTH_CODE";
            var connectionInfo    = new OAuthConnectionInfo(clientId, clientSecret, authorizationCode, null);
            var apiResponse       = "{ \"reason\":\"Invalid authorization code " + authorizationCode + "\",\"error\":\"invalid_request\"}";

            var mockHttp = new MockHttpMessageHandler();

            mockHttp
            .When(HttpMethod.Post, $"https://api.zoom.us/oauth/token?grant_type=authorization_code&code={authorizationCode}")
            .Respond(HttpStatusCode.BadRequest, "application/json", apiResponse);

            var handler = new OAuthTokenHandler(connectionInfo, mockHttp.ToHttpClient(), null);

            // Act
            Should.Throw <ZoomException>(() => handler.RefreshTokenIfNecessary(true));
            Should.Throw <ZoomException>(() => handler.RefreshTokenIfNecessary(true));

            // Assert
            mockHttp.VerifyNoOutstandingExpectation();
            mockHttp.VerifyNoOutstandingRequest();
        }
Ejemplo n.º 2
0
        public OAuthTokenHandler(OAuthConnectionInfo connectionInfo, HttpClient httpClient, TimeSpan?clockSkew = null)
        {
            if (string.IsNullOrEmpty(connectionInfo.ClientId))
            {
                throw new ArgumentNullException(nameof(connectionInfo.ClientId));
            }
            if (string.IsNullOrEmpty(connectionInfo.ClientSecret))
            {
                throw new ArgumentNullException(nameof(connectionInfo.ClientSecret));
            }

            _connectionInfo = connectionInfo;
            _httpClient     = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
            _clockSkew      = clockSkew.GetValueOrDefault(TimeSpan.FromMinutes(5));
        }
Ejemplo n.º 3
0
        public async Task <int> RunAsync()
        {
            // -----------------------------------------------------------------------------
            // Do you want to proxy requests through Fiddler? Can be useful for debugging.
            var useFiddler  = true;
            var fiddlerPort = 8866;             // By default Fiddler4 uses port 8888 and Fiddler Everywhere uses port 8866

            // Do you want to use JWT or OAuth?
            var connectionMethod = ConnectionMethods.OAuth;
            // -----------------------------------;------------------------------------------

            // Configure ZoomNet client
            IConnectionInfo connectionInfo;

            if (connectionMethod == ConnectionMethods.Jwt)
            {
                var apiKey    = Environment.GetEnvironmentVariable("ZOOM_JWT_APIKEY", EnvironmentVariableTarget.User);
                var apiSecret = Environment.GetEnvironmentVariable("ZOOM_JWT_APISECRET", EnvironmentVariableTarget.User);
                connectionInfo = new JwtConnectionInfo(apiKey, apiSecret);
            }
            else
            {
                var clientId     = Environment.GetEnvironmentVariable("ZOOM_OAUTH_CLIENTID", EnvironmentVariableTarget.User);
                var clientSecret = Environment.GetEnvironmentVariable("ZOOM_OAUTH_CLIENTSECRET", EnvironmentVariableTarget.User);
                var refreshToken = Environment.GetEnvironmentVariable("ZOOM_OAUTH_REFRESHTOKEN", EnvironmentVariableTarget.User);
                var accessToken  = Environment.GetEnvironmentVariable("ZOOM_OAUTH_ACCESSTOKEN", EnvironmentVariableTarget.User);
                connectionInfo = new OAuthConnectionInfo(clientId, clientSecret, refreshToken, accessToken,
                                                         (newRefreshToken, newAccessToken) =>
                {
                    Environment.SetEnvironmentVariable("ZOOM_OAUTH_REFRESHTOKEN", newRefreshToken, EnvironmentVariableTarget.User);
                    Environment.SetEnvironmentVariable("ZOOM_OAUTH_ACCESSTOKEN", newAccessToken, EnvironmentVariableTarget.User);
                });

                //var authorizationCode = "<-- the code generated by Zoom when the app is authorized by the user -->";
                //connectionInfo = new OAuthConnectionInfo(clientId, clientSecret, authorizationCode,
                //	(newRefreshToken, newAccessToken) =>
                //	{
                //		Environment.SetEnvironmentVariable("ZOOM_OAUTH_REFRESHTOKEN", newRefreshToken, EnvironmentVariableTarget.User);
                //		Environment.SetEnvironmentVariable("ZOOM_OAUTH_ACCESSTOKEN", newAccessToken, EnvironmentVariableTarget.User);
                //	});
            }

            var userId = Environment.GetEnvironmentVariable("ZOOM_USERID");
            var proxy  = useFiddler ? new WebProxy($"http://localhost:{fiddlerPort}") : null;
            var client = new ZoomClient(connectionInfo, proxy, null, _loggerFactory.CreateLogger <ZoomClient>());

            // Configure Console
            var source = new CancellationTokenSource();

            Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                source.Cancel();
            };

            // Ensure the Console is tall enough and centered on the screen
            Console.WindowHeight = Math.Min(60, Console.LargestWindowHeight);
            Utils.CenterConsole();

            // These are the integration tests that we will execute
            var integrationTests = new Type[]
            {
                typeof(Meetings),
                typeof(Users),
                typeof(Webinars),
            };

            // Execute the async tests in parallel (with max degree of parallelism)
            var results = await integrationTests.ForEachAsync(
                async testType =>
            {
                var log = new StringWriter();

                try
                {
                    var integrationTest = (IIntegrationTest)Activator.CreateInstance(testType);
                    await integrationTest.RunAsync(userId, client, log, source.Token).ConfigureAwait(false);
                    return(TestName : testType.Name, ResultCode : ResultCodes.Success, Message : SUCCESSFUL_TEST_MESSAGE);
                }
                catch (OperationCanceledException)
                {
                    await log.WriteLineAsync($"-----> TASK CANCELLED").ConfigureAwait(false);
                    return(TestName : testType.Name, ResultCode : ResultCodes.Cancelled, Message : "Task cancelled");
                }
                catch (Exception e)
                {
                    var exceptionMessage = e.GetBaseException().Message;
                    await log.WriteLineAsync($"-----> AN EXCEPTION OCCURRED: {exceptionMessage}").ConfigureAwait(false);
                    return(TestName : testType.Name, ResultCode : ResultCodes.Exception, Message : exceptionMessage);
                }
                finally
                {
                    lock (Console.Out)
                    {
                        Console.Out.WriteLine(log.ToString());
                    }
                }
            }, MAX_ZOOM_API_CONCURRENCY)
                          .ConfigureAwait(false);

            // Display summary
            var summary = new StringWriter();
            await summary.WriteLineAsync("\n\n**************************************************").ConfigureAwait(false);

            await summary.WriteLineAsync("******************** SUMMARY *********************").ConfigureAwait(false);

            await summary.WriteLineAsync("**************************************************").ConfigureAwait(false);

            foreach (var(TestName, ResultCode, Message) in results.OrderBy(r => r.TestName).ToArray())
            {
                var name = TestName.Length <= TEST_NAME_MAX_LENGTH ? TestName : TestName.Substring(0, TEST_NAME_MAX_LENGTH - 3) + "...";
                await summary.WriteLineAsync($"{name.PadRight(TEST_NAME_MAX_LENGTH, ' ')} : {Message}").ConfigureAwait(false);
            }

            await summary.WriteLineAsync("**************************************************").ConfigureAwait(false);

            await Console.Out.WriteLineAsync(summary.ToString()).ConfigureAwait(false);

            // Prompt user to press a key in order to allow reading the log in the console
            var promptLog = new StringWriter();
            await promptLog.WriteLineAsync("\n\n**************************************************").ConfigureAwait(false);

            await promptLog.WriteLineAsync("Press any key to exit").ConfigureAwait(false);

            Utils.Prompt(promptLog.ToString());

            // Return code indicating success/failure
            var resultCode = (int)ResultCodes.Success;

            if (results.Any(result => result.ResultCode != ResultCodes.Success))
            {
                if (results.Any(result => result.ResultCode == ResultCodes.Exception))
                {
                    resultCode = (int)ResultCodes.Exception;
                }
                else if (results.Any(result => result.ResultCode == ResultCodes.Cancelled))
                {
                    resultCode = (int)ResultCodes.Cancelled;
                }
                else
                {
                    resultCode = (int)results.First(result => result.ResultCode != ResultCodes.Success).ResultCode;
                }
            }

            return(await Task.FromResult(resultCode));
        }