コード例 #1
0
        public async Task POST_dockerhub_handler_should_parse_and_log_callback_url()
        {
            // Arrange
            var secret       = "my_secret";
            var callback_url = "https://registry.hub.docker.com/u/dopplerdock/doppler-cd-helper/hook/2jiedged52hbhfi1acgdggdi1ih123456/";
            var tag          = "tag";
            var repo_name    = "repoName";

            var loggerMock = new Mock <ILogger <HooksController> >();

            using var customFactory = _factory.WithWebHostBuilder(c =>
            {
                c.ConfigureServices(s => s
                                    .AddSingleton(loggerMock.Object)
                                    .AddSingleton(Mock.Of <ISwarmClient>())
                                    .AddSingleton(Options.Create(new DockerHubHookSettings()
                {
                    Secret = secret
                })));
            });

            var client = customFactory.CreateClient(new WebApplicationFactoryClientOptions()
            {
                AllowAutoRedirect = false
            });

            // Act
            var response = await client.PostAsync(
                $"/hooks/{secret}/",
                JsonContent.Create(new
            {
                callback_url,
                push_data  = new { tag },
                repository = new { repo_name }
            }));

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            // It is a FormattedLogValues object
            // See: https://github.com/dotnet/runtime/blob/01b7e73cd378145264a7cb7a09365b41ed42b240/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/FormattedLogValues.cs#L16
            IReadOnlyList <KeyValuePair <string, object> > logParameters = null;

            loggerMock.Verify(
                x => x.Log(
                    LogLevel.Debug,
                    It.IsAny <EventId>(),
                    It.Is <It.IsAnyType>((v, t) => v.ToString().StartsWith("Hook event") && AssertHelper.IsTypeAndGetValue(v, out logParameters)),
                    It.IsAny <Exception>(),
                    It.IsAny <Func <It.IsAnyType, Exception, string> >()),
                Times.Once);

            Assert.Contains(logParameters, x =>
                            x.Key == "@data" &&
                            x.Value is DockerHubHookData data &&
                            data.callback_url == callback_url &&
                            data.push_data.tag == tag &&
                            data.repository.repo_name == repo_name);
        }
コード例 #2
0
        public async Task POST_dockerhub_with_a_wrong_secret_should_return_error_log_data_and_do_nothing_more()
        {
            // Arrange
            var secret       = "my_secret";
            var wrongSecret  = "wrongSecret";
            var callback_url = "callback_url";
            var tag          = "tag";
            var repo_name    = "repoName";

            var loggerMock      = new Mock <ILogger <HooksController> >();
            var swarmClientMock = new Mock <ISwarmClient>();

            using var customFactory = _factory.WithWebHostBuilder(c =>
            {
                c.ConfigureServices(s => s
                                    .AddSingleton(loggerMock.Object)
                                    .AddSingleton(swarmClientMock.Object)
                                    .AddSingleton(Options.Create(new DockerHubHookSettings()
                {
                    Secret = secret
                })));
            });

            var client = customFactory.CreateClient(new WebApplicationFactoryClientOptions()
            {
                AllowAutoRedirect = false
            });

            // Act
            var response = await client.PostAsync(
                $"/hooks/{wrongSecret}/",
                JsonContent.Create(new
            {
                callback_url,
                push_data  = new { tag },
                repository = new { repo_name }
            }));

            // Assert
            Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);

            swarmClientMock.VerifyNoOtherCalls();

            // It is a FormattedLogValues object
            // See: https://github.com/dotnet/runtime/blob/01b7e73cd378145264a7cb7a09365b41ed42b240/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/FormattedLogValues.cs#L16
            IReadOnlyList <KeyValuePair <string, object> > logParameters = null;

            loggerMock.Verify(
                x => x.Log(
                    LogLevel.Warning,
                    It.IsAny <EventId>(),
                    It.Is <It.IsAnyType>((v, t) => v.ToString().StartsWith("Hook event with a wrong secret!") && AssertHelper.IsTypeAndGetValue(v, out logParameters)),
                    It.IsAny <Exception>(),
                    It.IsAny <Func <It.IsAnyType, Exception, string> >()),
                Times.Once);

            Assert.Contains($"secret: {wrongSecret}", logParameters.ToString());

            Assert.Contains(logParameters, x =>
                            x.Key == "@data" &&
                            x.Value is DockerHubHookData data &&
                            data.callback_url == callback_url &&
                            data.push_data.tag == tag &&
                            data.repository.repo_name == repo_name);
        }