diff --git a/src/NuGetGallery/Controllers/PagesController.cs b/src/NuGetGallery/Controllers/PagesController.cs index c0c2271da9..a0c7d7f0df 100644 --- a/src/NuGetGallery/Controllers/PagesController.cs +++ b/src/NuGetGallery/Controllers/PagesController.cs @@ -152,6 +152,11 @@ public virtual async Task Terms() [HttpGet] public virtual async Task Privacy() { + if (!String.IsNullOrEmpty(Url.ExternalPrivacyUrl())) + { + return Redirect(Url.ExternalPrivacyUrl()); + } + if (_contentService != null) { ViewBag.Content = await _contentService.GetContentItemAsync( diff --git a/src/NuGetGallery/UrlHelperExtensions.cs b/src/NuGetGallery/UrlHelperExtensions.cs index ad74ec59da..e94fe7236a 100644 --- a/src/NuGetGallery/UrlHelperExtensions.cs +++ b/src/NuGetGallery/UrlHelperExtensions.cs @@ -1412,6 +1412,11 @@ public static string Privacy(this UrlHelper url, bool relativeUrl = true) return GetActionLink(url, "Privacy", "Pages", relativeUrl); } + public static string ExternalPrivacyUrl(this UrlHelper url) + { + return _configuration.Current.ExternalPrivacyPolicyUrl; + } + public static string About(this UrlHelper url, bool relativeUrl = true) { if (!String.IsNullOrEmpty(_configuration.Current.ExternalAboutUrl)) diff --git a/tests/NuGetGallery.Facts/Controllers/PagesControllerFacts.cs b/tests/NuGetGallery.Facts/Controllers/PagesControllerFacts.cs index 61ca37c8d0..46466fe2e7 100644 --- a/tests/NuGetGallery.Facts/Controllers/PagesControllerFacts.cs +++ b/tests/NuGetGallery.Facts/Controllers/PagesControllerFacts.cs @@ -1,12 +1,14 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Threading.Tasks; using System.Web; using Moq; using NuGet.Services.Entities; using NuGet.Services.Messaging.Email; using NuGetGallery.Areas.Admin; +using NuGetGallery.Configuration; using NuGetGallery.Framework; using NuGetGallery.Infrastructure.Mail.Messages; using NuGetGallery.ViewModels; @@ -88,6 +90,41 @@ public async Task HtmlEncodesTheSupportRequest() It.IsAny(), It.IsAny())); } + + [Fact] + public async Task WithExternalPrivacyUrlConfigured() + { + var externalPrivacyUrl = "https://privacy.microsoft.com"; + var configuration = GetConfigurationService(); + var pagesController = GetController(); + + configuration.Current.ExternalPrivacyPolicyUrl = externalPrivacyUrl; + + var result = await pagesController.Privacy(); + + GetMock() + .Verify(m => m.GetContentItemAsync( + It.IsAny(), + It.IsAny()), Times.Never); + + } + + [Fact] + public async Task WithoutExternalPrivacyUrlConfigured() + { + var externalPrivacyUrl = ""; + var configuration = GetConfigurationService(); + var pagesController = GetController(); + + configuration.Current.ExternalPrivacyPolicyUrl = externalPrivacyUrl; + + var result = await pagesController.Privacy(); + + GetMock() + .Verify(m => m.GetContentItemAsync( + It.IsAny(), + It.IsAny()), Times.Once); + } } } } \ No newline at end of file diff --git a/tests/NuGetGallery.Facts/UrlHelperExtensionsFacts.cs b/tests/NuGetGallery.Facts/UrlHelperExtensionsFacts.cs index b240ca538f..88c1745b21 100644 --- a/tests/NuGetGallery.Facts/UrlHelperExtensionsFacts.cs +++ b/tests/NuGetGallery.Facts/UrlHelperExtensionsFacts.cs @@ -276,6 +276,23 @@ public void UsesConfiguredSiteRootInAbsoluteUri( } } + public class GetExternalPrivacyUrlMethod : TestContainer + { + [Theory] + [InlineData("")] + [InlineData("https://privacy.microsoft.com")] + public void VerifyExternalPrivacyUrlMethod(string expectedUrl) { + var configurationService = GetConfigurationService(); + configurationService.Current.ExternalPrivacyPolicyUrl = expectedUrl; + + var urlHelper = TestUtility.MockUrlHelper(expectedUrl); + + var result = UrlHelperExtensions.ExternalPrivacyUrl(TestUtility.MockUrlHelper()); + + Assert.Equal(expectedUrl, result); + } + } + public class TheGetActionLinkMethod : TestContainer { public static IEnumerable GeneratesTheCorrectActionLink_Data