-
Notifications
You must be signed in to change notification settings - Fork 659
Expand file tree
/
Copy pathPagesControllerFacts.cs
More file actions
172 lines (140 loc) · 6.48 KB
/
PagesControllerFacts.cs
File metadata and controls
172 lines (140 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 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.Framework;
using NuGetGallery.Infrastructure.Mail.Messages;
using NuGetGallery.ViewModels;
using Xunit;
namespace NuGetGallery
{
public class PagesControllerFacts
{
public class TheContactAction : TestContainer
{
[Fact]
public async Task HtmlEncodesTheSupportContactEmail()
{
// arrage: the contact form, the expected encoding, and setup a user
var contactForm = new ContactSupportViewModel
{
Message = "<strong>Something with HTML in it</strong>",
SubjectLine = "<script>alert('malicious javascript perhaps')</script>"
};
var expectedMessage = HttpUtility.HtmlEncode(contactForm.Message);
var expectedSubjectLine = HttpUtility.HtmlEncode(contactForm.SubjectLine);
var controller = GetController<PagesController>();
// Have to set this up first because it needs current user
controller.SetCurrentUser(new User
{
Username = "aUsername",
UnconfirmedEmailAddress = "[email protected]",
EmailConfirmationToken = "aToken",
});
// act: run the controller action
await controller.Contact(contactForm);
// assert: the HTML encoded message was passed to the service
GetMock<IMessageService>()
.Verify(svc => svc.SendMessageAsync(
It.Is<ContactSupportMessage>(
msg =>
msg.Message == expectedMessage
&& msg.Reason == expectedSubjectLine),
false,
false));
}
[Fact]
public async Task HtmlEncodesTheSupportRequest()
{
// arrage: the contact form, the expected encoding, and setup a user
var contactForm = new ContactSupportViewModel
{
Message = "<b>some html</b>",
SubjectLine = "maybe some malicious javascript: <script>alert('teh XSS hax')</script>"
};
var expectedMessage = HttpUtility.HtmlEncode(contactForm.Message);
var controller = GetController<PagesController>();
// Have to set this up first because it needs current user
controller.SetCurrentUser(new User
{
Username = "aUsername",
UnconfirmedEmailAddress = "[email protected]",
EmailConfirmationToken = "aToken",
});
// act: run the controller action
await controller.Contact(contactForm);
// assert: the HTML encoded message was passed to the service
GetMock<ISupportRequestService>()
.Verify(m => m.AddNewSupportRequestAsync(
It.IsAny<string>(),
expectedMessage,
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<User>(),
It.IsAny<Package>()));
}
[Fact]
public async Task WithExternalPrivacyUrlConfigured()
{
var externalPrivacyUrl = "https://privacy.microsoft.com";
var configuration = GetConfigurationService();
var pagesController = GetController<PagesController>();
configuration.Current.ExternalPrivacyPolicyUrl = externalPrivacyUrl;
var result = await pagesController.Privacy();
GetMock<IContentService>()
.Verify(m => m.GetContentItemAsync(
It.IsAny<string>(),
It.IsAny<TimeSpan>()), Times.Never);
}
[Fact]
public async Task WithoutExternalPrivacyUrlConfigured()
{
var externalPrivacyUrl = "";
var configuration = GetConfigurationService();
var pagesController = GetController<PagesController>();
configuration.Current.ExternalPrivacyPolicyUrl = externalPrivacyUrl;
var result = await pagesController.Privacy();
GetMock<IContentService>()
.Verify(m => m.GetContentItemAsync(
It.IsAny<string>(),
It.IsAny<TimeSpan>()), Times.Once);
}
}
public class TheYourPrivacyChoicesUrl : TestContainer
{
[Fact]
public void WithExternalYourPrivacyChoicesUrlConfigured()
{
var expectedUrl = "https://aka.ms/yourcaliforniaprivacychoices";
var configuration = GetConfigurationService();
configuration.Current.ExternalYourPrivacyChoicesUrl = expectedUrl;
var urlHelper = TestUtility.MockUrlHelper();
var result = UrlHelperExtensions.YourPrivacyChoices(urlHelper);
Assert.Equal(expectedUrl, result);
}
[Fact]
public void WithoutExternalYourPrivacyChoicesUrlConfigured()
{
var configuration = GetConfigurationService();
configuration.Current.ExternalYourPrivacyChoicesUrl = "";
var urlHelper = TestUtility.MockUrlHelper();
var result = UrlHelperExtensions.YourPrivacyChoices(urlHelper);
Assert.NotEqual("https://aka.ms/yourcaliforniaprivacychoices", result);
}
[Fact]
public void WithNullExternalYourPrivacyChoicesUrlConfigured()
{
var configuration = GetConfigurationService();
configuration.Current.ExternalYourPrivacyChoicesUrl = null;
var urlHelper = TestUtility.MockUrlHelper();
var result = UrlHelperExtensions.YourPrivacyChoices(urlHelper);
Assert.NotEqual("https://aka.ms/yourcaliforniaprivacychoices", result);
}
}
}
}