Esempio n. 1
0
        public async Task TryToRunJobOnJenkinsWithCSRFProtectionWithoutSavedPassword_AskUserForPassword()
        {
            var basicAuthHeader = new BasicAuthenticationHeader(UserName, Password);
            var tokenAuthHeader = new BearerAuthenticationHeader(Token);

            var configuration = new JenoConfiguration
            {
                JenkinsUrl = JenkinsUrl,
                UserName   = UserName,
                Token      = Token,
                Password   = string.Empty,
                Repository = new Dictionary <string, string>()
                {
                    { "firstExampleRepoUrl", "firstExampleJob" },
                    { "secondExampleRepoUrl", "secondExampleJob" },
                    { DefaultKey, DefaultJob },
                }
            };

            var client = new MockHttpMessageHandler();

            client.Expect($"{JenkinsUrl}/{DefaultJob}/job/{Branch}/buildWithParameters")
            .WithHeaders("Authorization", $"{tokenAuthHeader.Scheme} {tokenAuthHeader.Parameter}")
            .Respond(request => new HttpResponseMessage
            {
                StatusCode   = HttpStatusCode.Forbidden,
                ReasonPhrase = "No valid crumb was included in the request"
            });
            client.Expect($"{JenkinsUrl}/crumbIssuer/api/json")
            .WithHeaders("Authorization", $"{basicAuthHeader.Scheme} {basicAuthHeader.Parameter}")
            .Respond(CrumbContentType, JsonConvert.SerializeObject(_crumbHeader));
            client.Expect($"{JenkinsUrl}/{DefaultJob}/job/{Branch}/buildWithParameters")
            .WithHeaders("Authorization", $"{basicAuthHeader.Scheme} {basicAuthHeader.Parameter}")
            .WithHeaders(_crumbHeader.CrumbRequestField, _crumbHeader.Crumb)
            .Respond(HttpStatusCode.OK);

            var httpClientFactory = new Mock <IHttpClientFactory>();

            httpClientFactory.Setup(s => s.CreateClient(It.IsAny <string>()))
            .Returns(client.ToHttpClient());

            var passwordProvider = GetUserConsoleMock();

            var command = new RunJob(GetDefaultGitMock().Object,
                                     GetEncryptorMock().Object,
                                     passwordProvider.Object,
                                     httpClientFactory.Object,
                                     GetOptionsMock(configuration).Object);

            var app = new CommandLineApplication();

            app.Command(command.Name, command.Command);

            var result = await app.ExecuteAsync(new string[] { Command });

            Assert.That(result, Is.EqualTo(JenoCodes.Ok));
            passwordProvider.Verify(s => s.ReadInput("password", true), Times.AtLeastOnce);
        }
Esempio n. 2
0
        protected CommandBase(HttpClient httpClient, BasicAuthenticationHeader authHeader, T requestModel, string endPoint)
        {
            _httpClient   = httpClient;
            _authHeader   = authHeader;
            _requestModel = requestModel;
            _endPoint     = endPoint;
            _validator    = new V();

            Result = new U();
        }
Esempio n. 3
0
        public async Task TryRunJobOnJenkinsWithCSRFProtection_UseBasicCredentialsAndAskForCrumb()
        {
            var basicAuthHeader = new BasicAuthenticationHeader(UserName, Password);
            var tokenAuthHeader = new BearerAuthenticationHeader(Token);

            var client = new MockHttpMessageHandler();

            client.Expect($"{JenkinsUrl}/{DefaultJob}/job/{Branch}/buildWithParameters")
            .WithHeaders("Authorization", $"{tokenAuthHeader.Scheme} {tokenAuthHeader.Parameter}")
            .Respond(request => new HttpResponseMessage
            {
                StatusCode   = HttpStatusCode.Forbidden,
                ReasonPhrase = "No valid crumb was included in the request"
            });
            client.Expect($"{JenkinsUrl}/crumbIssuer/api/json")
            .WithHeaders("Authorization", $"{basicAuthHeader.Scheme} {basicAuthHeader.Parameter}")
            .Respond(CrumbContentType, JsonConvert.SerializeObject(_crumbHeader));
            client.Expect($"{JenkinsUrl}/{DefaultJob}/job/{Branch}/buildWithParameters")
            .WithHeaders("Authorization", $"{basicAuthHeader.Scheme} {basicAuthHeader.Parameter}")
            .WithHeaders(_crumbHeader.CrumbRequestField, _crumbHeader.Crumb)
            .Respond(HttpStatusCode.OK);

            var httpClientFactory = new Mock <IHttpClientFactory>();

            httpClientFactory.Setup(s => s.CreateClient(It.IsAny <string>()))
            .Returns(client.ToHttpClient());

            var command = new RunJob(GetDefaultGitMock().Object,
                                     GetEncryptorMock().Object,
                                     GetUserConsoleMock().Object,
                                     httpClientFactory.Object,
                                     GetOptionsMock().Object);

            var app = new CommandLineApplication();

            app.Command(command.Name, command.Command);

            var result = await app.ExecuteAsync(new string[] { Command });

            Assert.That(result, Is.EqualTo(JenoCodes.Ok));
        }
 public FlightsInTimeIntervalCommand(HttpClient httpClient, BasicAuthenticationHeader authHeader, FlightsInTimeIntervalRequestModel requestModel) : base(httpClient, authHeader, requestModel, OpenSkyApiConstants.ALL_FLIGHTS_IN_INTERVAL)
 {
 }
 public ArrivalsByAirportCommand(HttpClient httpClient, BasicAuthenticationHeader authHeader, ArrivalsByAirportRequestModel requestModel) : base(httpClient, authHeader, requestModel, OpenSkyApiConstants.AIRPORT_ARRIVALS)
 {
 }
 public DeparturesByAirportCommand(HttpClient httpClient, BasicAuthenticationHeader authHeader, DeparturesByAirportRequestModel requestModel) : base(httpClient, authHeader, requestModel, OpenSkyApiConstants.AIRPORT_DEPARTURES)
 {
 }
 public AllStateVectorsCommand(HttpClient httpClient, BasicAuthenticationHeader authHeader, AllStateVectorsRequestModel requestModel) : base(httpClient, authHeader, requestModel, OpenSkyApiConstants.AVAILABLE_STATES)
 {
 }
 public OwnStateVectorsCommand(HttpClient httpClient, BasicAuthenticationHeader authHeader, OwnStateVectorsRequestModel requestModel) : base(httpClient, authHeader, requestModel, OpenSkyApiConstants.SPECIFIED_USER_STATES)
 {
 }
Esempio n. 9
0
 public FlightsByAircraftCommand(HttpClient httpClient, BasicAuthenticationHeader authHeader, FlightsByAircraftRequestModel requestModel) : base(httpClient, authHeader, requestModel, OpenSkyApiConstants.SPECIFIED_AIRCRAFT_FLIGHTS_IN_INTERVAL)
 {
 }
 public TrackByAircraftCommand(HttpClient httpClient, BasicAuthenticationHeader authHeader, TrackByAircraftRequestModel requestModel) : base(httpClient, authHeader, requestModel, OpenSkyApiConstants.SPECIFIED_AIRCRAFT_TRACK)
 {
 }