public void GetProperties_63plus_Forbidden()
        {
            const string serverUrl  = "http://localhost";
            const string projectKey = "my-project";

            var responseMock = new Mock <HttpWebResponse>();

            responseMock.SetupGet(x => x.StatusCode).Returns(HttpStatusCode.Forbidden);

            var downloaderMock = new Mock <IDownloader>();

            downloaderMock
            .Setup(x => x.Download($"{serverUrl}/api/server/version"))
            .Returns("6.3.0.0");

            var content = string.Empty;

            downloaderMock
            .Setup(x => x.TryDownloadIfExists($"{serverUrl}/api/settings/values?component={projectKey}", out content))
            .Throws(new WebException("Forbidden", new Exception(), WebExceptionStatus.ConnectionClosed, responseMock.Object));

            var service = new SonarWebService(downloaderMock.Object, serverUrl, logger);

            Action action = () => service.GetProperties(projectKey);

            action.ShouldThrow <WebException>();

            logger.Errors.Should().HaveCount(1);
            logger.Warnings.Should().HaveCount(1);
            logger.Warnings[0].Should().Be("To analyze private projects make sure the scanner user has 'Browse' permission.");
        }
        public void GetProperties_Old_Forbidden()
        {
            const string serverUrl  = "http://localhost";
            const string projectKey = "my-project";

            var responseMock = new Mock <HttpWebResponse>();

            responseMock.SetupGet(x => x.StatusCode).Returns(HttpStatusCode.Forbidden);

            var downloaderMock = new Mock <IDownloader>();

            downloaderMock
            .Setup(x => x.Download($"{serverUrl}/api/server/version", false))
            .Returns(Task.FromResult("1.2.3.4"));
            downloaderMock
            .Setup(x => x.Download($"{serverUrl}/api/properties?resource={projectKey}", true))
            .Throws(new HttpRequestException("Forbidden"));

            var service = new SonarWebService(downloaderMock.Object, serverUrl, this.logger);

            Func <Task> action = async() => await service.GetProperties(projectKey, null);

            action.Should().Throw <HttpRequestException>();

            this.logger.Errors.Should().HaveCount(1);
        }
        public void GetProperties_Old_Forbidden()
        {
            const string serverUrl  = "http://localhost";
            const string projectKey = "my-project";

            var responseMock = new Mock <HttpWebResponse>();

            responseMock.SetupGet(x => x.StatusCode).Returns(HttpStatusCode.Forbidden);

            var downloaderMock = new Mock <IDownloader>();

            downloaderMock
            .Setup(x => x.Download($"{serverUrl}/api/server/version"))
            .Returns("1.2.3.4");
            downloaderMock
            .Setup(x => x.Download($"{serverUrl}/api/properties?resource={projectKey}"))
            .Throws(new WebException("Forbidden", new Exception(), WebExceptionStatus.ConnectionClosed, responseMock.Object));

            var service = new SonarWebService(downloaderMock.Object, serverUrl, this.logger);

            Action action = () => service.GetProperties(projectKey, null);

            action.Should().Throw <WebException>();

            this.logger.Errors.Should().HaveCount(1);
            this.logger.Warnings.Should().HaveCount(1);
            this.logger.Warnings[0].Should().Be("To analyze private projects make sure the scanner user has 'Browse' permission.");
        }
        public void ServerUrlWithTrailingSlash()
        {
            ws = new SonarWebService(downloader, "http://localhost:9000/", "cs", "fxcop");

            downloader.Pages["http://localhost:9000/api/profiles/list?language=cs&project=foo%20bar"] = "[{\"name\":\"profile1\",\"language\":\"cs\",\"default\":true}]";
            Assert.AreEqual("profile1", ws.GetQualityProfile("foo bar"));

            downloader.Pages["http://localhost:9000/api/profiles/index?language=cs&name=Sonar%20way"] = "[{\"name\":\"Sonar way\",\"language\":\"cs\",\"default\":true}]";
            var expected1 = new List <string>();
            var actual1   = new List <string>(ws.GetActiveRuleKeys("Sonar way"));

            Assert.AreEqual(true, expected1.SequenceEqual(actual1));

            downloader.Pages["http://localhost:9000/api/rules/search?f=internalKey&ps=" + int.MaxValue + "&repositories=fxcop"] = "{\"total\":2,\"p\":1,\"ps\":10,\"rules\":[{\"key\":\"fxcop:My_Own_FxCop_Rule\"},{\"key\":\"fxcop:UriParametersShouldNotBeStrings\",\"internalKey\":\"CA1054\"}]}";
            var expected2 = new Dictionary <string, string>();

            expected2["fxcop:My_Own_FxCop_Rule"] = null;
            expected2["fxcop:UriParametersShouldNotBeStrings"] = "CA1054";
            var actual2 = ws.GetInternalKeys();

            Assert.AreEqual(true, expected2.Count == actual2.Count && !expected2.Except(actual2).Any());

            downloader.Pages["http://localhost:9000/api/properties?resource=foo%20bar"] = "[{\"key\": \"sonar.property1\",\"value\": \"value1\"},{\"key\": \"sonar.property2\",\"value\": \"value2\"}]";
            var expected3 = new Dictionary <string, string>();

            expected3["sonar.property1"] = "value1";
            expected3["sonar.property2"] = "value2";
            expected3["sonar.cs.msbuild.testProjectPattern"] = ".*test.*";
            var actual3 = ws.GetProperties("foo bar");

            Assert.AreEqual(true, expected3.Count == actual3.Count && !expected3.Except(actual3).Any());
        }
        public void GetProperties_NullProjectKey_Throws()
        {
            // Arrange
            var    testSubject = new SonarWebService(new TestDownloader(), "http://myserver", new TestLogger());
            Action act         = () => _ = testSubject.GetProperties(null, null).Result;

            // Act & Assert
            act.Should().Throw <ArgumentNullException>().And.ParamName.Should().Be("projectKey");
        }
예제 #6
0
        public void ServerUrlWithTrailingSlash()
        {
            ws = new SonarWebService(downloader, "http://myhost:222/");

            downloader.Pages["http://myhost:222/api/profiles/list?language=cs&project=foo+bar"] = "[{\"name\":\"profile1\",\"language\":\"cs\",\"default\":true}]";
            string qualityProfile;
            bool   result = ws.TryGetQualityProfile("foo bar", "cs", out qualityProfile);

            Assert.IsTrue(result);
            Assert.AreEqual("profile1", qualityProfile);

            downloader.Pages["http://myhost:222/api/profiles/index?language=cs&name=Sonar+way"] = "[{\"name\":\"Sonar way\",\"language\":\"cs\",\"default\":true}]";
            var expected1 = new List <string>();
            var actual1   = new List <string>(ws.GetActiveRuleKeys("Sonar way", "cs", "foo"));

            Assert.AreEqual(true, expected1.SequenceEqual(actual1));

            downloader.Pages["http://myhost:222/api/rules/search?f=internalKey&ps=" + int.MaxValue + "&repositories=fxcop"] = "{\"total\":2,\"p\":1,\"ps\":10,\"rules\":[{\"key\":\"fxcop:My_Own_FxCop_Rule\"},{\"key\":\"fxcop:UriParametersShouldNotBeStrings\",\"internalKey\":\"CA1054\"}]}";
            var expected2 = new Dictionary <string, string>();

            expected2["fxcop:My_Own_FxCop_Rule"] = null;
            expected2["fxcop:UriParametersShouldNotBeStrings"] = "CA1054";
            var actual2 = ws.GetInternalKeys("fxcop");

            Assert.AreEqual(true, expected2.Count == actual2.Count && !expected2.Except(actual2).Any());

            downloader.Pages["http://myhost:222/api/properties?resource=foo+bar"] = "[{\"key\": \"sonar.property1\",\"value\": \"value1\"},{\"key\": \"sonar.property2\",\"value\": \"value2\"}]";
            var expected3 = new Dictionary <string, string>();

            expected3["sonar.property1"] = "value1";
            expected3["sonar.property2"] = "value2";
            expected3["sonar.cs.msbuild.testProjectPattern"] = SonarProperties.DefaultTestProjectPattern;
            var actual3 = ws.GetProperties("foo bar", new TestLogger());

            Assert.AreEqual(true, expected3.Count == actual3.Count && !expected3.Except(actual3).Any());

            downloader.Pages["http://myhost:222/api/updatecenter/installed_plugins"] = "[{\"key\":\"visualstudio\",\"name\":\"...\",\"version\":\"1.2\"},{\"key\":\"csharp\",\"name\":\"C#\",\"version\":\"4.0\"}]";
            var expected4 = new List <string>();

            expected4.Add("visualstudio");
            expected4.Add("csharp");
            var actual4 = new List <string>(ws.GetInstalledPlugins());

            Assert.AreEqual(true, expected4.SequenceEqual(actual4));
        }
        public void GetProperties_63plus_Forbidden()
        {
            const string serverUrl  = "http://localhost";
            const string projectKey = "my-project";

            var downloaderMock = new Mock <IDownloader>();

            downloaderMock
            .Setup(x => x.Download($"{serverUrl}/api/server/version", false))
            .Returns(Task.FromResult("6.3.0.0"));

            downloaderMock
            .Setup(x => x.TryDownloadIfExists($"{serverUrl}/api/settings/values?component={projectKey}", true))
            .Throws(new HttpRequestException("Forbidden"));

            var service = new SonarWebService(downloaderMock.Object, serverUrl, this.logger);

            Action action = () => _ = service.GetProperties(projectKey, null).Result;

            action.Should().Throw <HttpRequestException>();

            this.logger.Errors.Should().HaveCount(1);
        }
        public void ServerUrlWithTrailingSlash()
        {
            ws = new SonarWebService(downloader, "http://myhost:222/");

            downloader.Pages["http://myhost:222/api/profiles/list?language=cs&project=foo+bar"] = "[{\"name\":\"profile1\",\"language\":\"cs\",\"default\":true}]";
            string qualityProfile;
            bool result = ws.TryGetQualityProfile("foo bar", "cs", out qualityProfile);
            Assert.IsTrue(result);
            Assert.AreEqual("profile1", qualityProfile);

            downloader.Pages["http://myhost:222/api/profiles/index?language=cs&name=Sonar+way"] = "[{\"name\":\"Sonar way\",\"language\":\"cs\",\"default\":true}]";
            var expected1 = new List<string>();
            var actual1 = new List<string>(ws.GetActiveRuleKeys("Sonar way", "cs", "foo"));
            Assert.AreEqual(true, expected1.SequenceEqual(actual1));

            downloader.Pages["http://myhost:222/api/rules/search?f=internalKey&ps=" + int.MaxValue + "&repositories=fxcop"] = "{\"total\":2,\"p\":1,\"ps\":10,\"rules\":[{\"key\":\"fxcop:My_Own_FxCop_Rule\"},{\"key\":\"fxcop:UriParametersShouldNotBeStrings\",\"internalKey\":\"CA1054\"}]}";
            var expected2 = new Dictionary<string, string>();
            expected2["fxcop:My_Own_FxCop_Rule"] = null;
            expected2["fxcop:UriParametersShouldNotBeStrings"] = "CA1054";
            var actual2 = ws.GetInternalKeys("fxcop");

            Assert.AreEqual(true, expected2.Count == actual2.Count && !expected2.Except(actual2).Any());

            downloader.Pages["http://myhost:222/api/properties?resource=foo+bar"] = "[{\"key\": \"sonar.property1\",\"value\": \"value1\"},{\"key\": \"sonar.property2\",\"value\": \"value2\"}]";
            var expected3 = new Dictionary<string, string>();
            expected3["sonar.property1"] = "value1";
            expected3["sonar.property2"] = "value2";
            expected3["sonar.cs.msbuild.testProjectPattern"] = SonarProperties.DefaultTestProjectPattern;
            var actual3 = ws.GetProperties("foo bar", new TestLogger());

            Assert.AreEqual(true, expected3.Count == actual3.Count && !expected3.Except(actual3).Any());

            downloader.Pages["http://myhost:222/api/updatecenter/installed_plugins"] = "[{\"key\":\"visualstudio\",\"name\":\"...\",\"version\":\"1.2\"},{\"key\":\"csharp\",\"name\":\"C#\",\"version\":\"4.0\"}]";
            var expected4 = new List<string>();
            expected4.Add("visualstudio");
            expected4.Add("csharp");
            var actual4 = new List<string>(ws.GetInstalledPlugins());

            Assert.AreEqual(true, expected4.SequenceEqual(actual4));
        }
예제 #9
0
        public void ServerUrlWithTrailingSlash()
        {
            ws = new SonarWebService(downloader, "http://myhost:222/", new TestLogger());

            // Check that profiles are correctly defaulted as well as branch-specific
            // This test includes a regression scenario for SONARMSBRU-187:
            // Requesting properties for project:branch should return branch-specific data
            downloader.Pages["http://myhost:222/api/profiles/list?language=cs&project=foo+bar"]                 = "[{\"name\":\"profile1\",\"language\":\"cs\",\"default\":true}]";
            downloader.Pages["http://myhost:222/api/profiles/list?language=cs&project=foo+bar%3AaBranch"]       = "[{\"name\":\"profile2\",\"language\":\"cs\",\"default\":false}]";
            downloader.Pages["http://myhost:222/api/profiles/list?language=cs&project=foo+bar%3AanotherBranch"] = "[{\"name\":\"profile3\",\"language\":\"cs\",\"default\":false}]";
            string qualityProfile1;
            string qualityProfile2;
            string qualityProfile3;
            bool   result1 = ws.TryGetQualityProfile("foo bar", null, "cs", out qualityProfile1);
            bool   result2 = ws.TryGetQualityProfile("foo bar", "aBranch", "cs", out qualityProfile2);
            bool   result3 = ws.TryGetQualityProfile("foo bar", "anotherBranch", "cs", out qualityProfile3);

            Assert.IsTrue(result1);
            Assert.IsTrue(result2);
            Assert.IsTrue(result3);
            Assert.AreEqual("profile1", qualityProfile1);
            Assert.AreEqual("profile2", qualityProfile2);
            Assert.AreEqual("profile3", qualityProfile3);

            Assert.IsTrue(result1);
            Assert.IsTrue(result2);
            Assert.IsTrue(result3);
            Assert.AreEqual("profile1", qualityProfile1);
            Assert.AreEqual("profile2", qualityProfile2);
            Assert.AreEqual("profile3", qualityProfile3);

            downloader.Pages["http://myhost:222/api/profiles/index?language=cs&name=Sonar+way"] = "[{\"name\":\"Sonar way\",\"language\":\"cs\",\"default\":true}]";
            var expected1 = new List <string>();
            var actual1   = new List <string>(ws.GetActiveRuleKeys("Sonar way", "cs", "foo"));

            Assert.AreEqual(true, expected1.SequenceEqual(actual1));

            downloader.Pages["http://myhost:222/api/rules/search?f=internalKey&ps=" + int.MaxValue + "&repositories=fxcop"] = "{\"total\":2,\"p\":1,\"ps\":10,\"rules\":[{\"key\":\"fxcop:My_Own_FxCop_Rule\"},{\"key\":\"fxcop:UriParametersShouldNotBeStrings\",\"internalKey\":\"CA1054\"}]}";
            var expected2 = new Dictionary <string, string>();

            expected2["fxcop:UriParametersShouldNotBeStrings"] = "CA1054";
            var actual2 = ws.GetInternalKeys("fxcop");

            Assert.AreEqual(true, expected2.Count == actual2.Count && !expected2.Except(actual2).Any());

            downloader.Pages["http://myhost:222/api/properties?resource=foo+bar"]           = "[{\"key\": \"sonar.property1\",\"value\": \"value1\"},{\"key\": \"sonar.property2\",\"value\": \"value2\"}]";
            downloader.Pages["http://myhost:222/api/properties?resource=foo+bar%3AaBranch"] = "[{\"key\": \"sonar.property1\",\"value\": \"anotherValue1\"},{\"key\": \"sonar.property2\",\"value\": \"anotherValue2\"}]";

            var expected3_1 = new Dictionary <string, string>();

            expected3_1["sonar.property1"] = "value1";
            expected3_1["sonar.property2"] = "value2";
            expected3_1["sonar.cs.msbuild.testProjectPattern"] = SonarProperties.DefaultTestProjectPattern;
            var actual3_1 = ws.GetProperties("foo bar");

            var expected3_2 = new Dictionary <string, string>();

            expected3_2["sonar.property1"] = "anotherValue1";
            expected3_2["sonar.property2"] = "anotherValue2";
            expected3_2["sonar.cs.msbuild.testProjectPattern"] = SonarProperties.DefaultTestProjectPattern;
            var actual3_2 = ws.GetProperties("foo bar", "aBranch");

            Assert.AreEqual(true, expected3_1.Count == actual3_1.Count && !expected3_1.Except(actual3_1).Any());
            Assert.AreEqual(true, expected3_2.Count == actual3_2.Count && !expected3_2.Except(actual3_2).Any());

            downloader.Pages["http://myhost:222/api/updatecenter/installed_plugins"] = "[{\"key\":\"visualstudio\",\"name\":\"...\",\"version\":\"1.2\"},{\"key\":\"csharp\",\"name\":\"C#\",\"version\":\"4.0\"}]";
            var expected4 = new List <string>();

            expected4.Add("visualstudio");
            expected4.Add("csharp");
            var actual4 = new List <string>(ws.GetInstalledPlugins());

            Assert.AreEqual(true, expected4.SequenceEqual(actual4));
        }
        public void ServerUrlWithTrailingSlash()
        {
            ws = new SonarWebService(downloader, "http://myhost:222/", new TestLogger());

            // Check that profiles are correctly defaulted as well as branch-specific
            // This test includes a regression scenario for SONARMSBRU-187:
            // Requesting properties for project:branch should return branch-specific data
            downloader.Pages["http://myhost:222/api/profiles/list?language=cs&project=foo+bar"] = "[{\"name\":\"profile1\",\"language\":\"cs\",\"default\":true}]";
            downloader.Pages["http://myhost:222/api/profiles/list?language=cs&project=foo+bar%3AaBranch"] = "[{\"name\":\"profile2\",\"language\":\"cs\",\"default\":false}]";
            downloader.Pages["http://myhost:222/api/profiles/list?language=cs&project=foo+bar%3AanotherBranch"] = "[{\"name\":\"profile3\",\"language\":\"cs\",\"default\":false}]";
            string qualityProfile1;
            string qualityProfile2;
            string qualityProfile3;
            bool result1 = ws.TryGetQualityProfile("foo bar", null, "cs", out qualityProfile1);
            bool result2 = ws.TryGetQualityProfile("foo bar", "aBranch", "cs", out qualityProfile2);
            bool result3 = ws.TryGetQualityProfile("foo bar", "anotherBranch", "cs", out qualityProfile3);

            Assert.IsTrue(result1);
            Assert.IsTrue(result2);
            Assert.IsTrue(result3);
            Assert.AreEqual("profile1", qualityProfile1);
            Assert.AreEqual("profile2", qualityProfile2);
            Assert.AreEqual("profile3", qualityProfile3);

            Assert.IsTrue(result1);
            Assert.IsTrue(result2);
            Assert.IsTrue(result3);
            Assert.AreEqual("profile1", qualityProfile1);
            Assert.AreEqual("profile2", qualityProfile2);
            Assert.AreEqual("profile3", qualityProfile3);

            downloader.Pages["http://myhost:222/api/profiles/index?language=cs&name=Sonar+way"] = "[{\"name\":\"Sonar way\",\"language\":\"cs\",\"default\":true}]";
            var expected1 = new List<string>();
            var actual1 = new List<string>(ws.GetActiveRuleKeys("Sonar way", "cs", "foo"));
            Assert.AreEqual(true, expected1.SequenceEqual(actual1));

            downloader.Pages["http://myhost:222/api/rules/search?f=internalKey&ps=" + int.MaxValue + "&repositories=fxcop"] = "{\"total\":2,\"p\":1,\"ps\":10,\"rules\":[{\"key\":\"fxcop:My_Own_FxCop_Rule\"},{\"key\":\"fxcop:UriParametersShouldNotBeStrings\",\"internalKey\":\"CA1054\"}]}";
            var expected2 = new Dictionary<string, string>();
            expected2["fxcop:UriParametersShouldNotBeStrings"] = "CA1054";
            var actual2 = ws.GetInternalKeys("fxcop");

            Assert.AreEqual(true, expected2.Count == actual2.Count && !expected2.Except(actual2).Any());

            downloader.Pages["http://myhost:222/api/properties?resource=foo+bar"] = "[{\"key\": \"sonar.property1\",\"value\": \"value1\"},{\"key\": \"sonar.property2\",\"value\": \"value2\"}]";
            downloader.Pages["http://myhost:222/api/properties?resource=foo+bar%3AaBranch"] = "[{\"key\": \"sonar.property1\",\"value\": \"anotherValue1\"},{\"key\": \"sonar.property2\",\"value\": \"anotherValue2\"}]";

            var expected3_1 = new Dictionary<string, string>();
            expected3_1["sonar.property1"] = "value1";
            expected3_1["sonar.property2"] = "value2";
            expected3_1["sonar.cs.msbuild.testProjectPattern"] = SonarProperties.DefaultTestProjectPattern;
            var actual3_1 = ws.GetProperties("foo bar");

            var expected3_2 = new Dictionary<string, string>();
            expected3_2["sonar.property1"] = "anotherValue1";
            expected3_2["sonar.property2"] = "anotherValue2";
            expected3_2["sonar.cs.msbuild.testProjectPattern"] = SonarProperties.DefaultTestProjectPattern;
            var actual3_2 = ws.GetProperties("foo bar", "aBranch");

            Assert.AreEqual(true, expected3_1.Count == actual3_1.Count && !expected3_1.Except(actual3_1).Any());
            Assert.AreEqual(true, expected3_2.Count == actual3_2.Count && !expected3_2.Except(actual3_2).Any());

            downloader.Pages["http://myhost:222/api/updatecenter/installed_plugins"] = "[{\"key\":\"visualstudio\",\"name\":\"...\",\"version\":\"1.2\"},{\"key\":\"csharp\",\"name\":\"C#\",\"version\":\"4.0\"}]";
            var expected4 = new List<string>();
            expected4.Add("visualstudio");
            expected4.Add("csharp");
            var actual4 = new List<string>(ws.GetInstalledPlugins());

            Assert.AreEqual(true, expected4.SequenceEqual(actual4));
        }