Skip to content

Commit a47cb0e

Browse files
authored
Convert to TUnit (#255)
* Add TUnit package * TEMP remove implicit usings * Convert to TUnit by running dotnet format analyzers --severity info --diagnostics TUXU0001 * Revert "TEMP remove implicit usings" This reverts commit 0fd8c1e. * Remove xunit * Fix warnings * Fix test writing to console (doesn't happen in xunit) * Switch to Verify.TUnit * Update TRX * Fix trx report * Fix --report-trx
1 parent dac30af commit a47cb0e

22 files changed

+1053
-2044
lines changed

build/Build.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ class Build : NukeBuild
7575
.SetConfiguration(Configuration)
7676
.SetProperty("Version", Version)
7777
.When(IsServerBuild, x => x
78-
.SetLoggers("trx")
79-
.SetResultsDirectory(TestResultsDirectory))
78+
.SetProcessArgumentConfigurator(x=>x
79+
.Add("--")
80+
.Add("--report-trx")
81+
.Add("--results-directory")
82+
.Add(TestResultsDirectory)))
8083
.EnableNoBuild()
8184
.EnableNoRestore());
8285
});

test/NetEscapades.AspNetCore.SecurityHeaders.TagHelpers.Test/AttributeHashTagHelperTests.cs

Lines changed: 25 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using Microsoft.AspNetCore.Routing;
1515
using Moq;
1616
using NetEscapades.AspNetCore.SecurityHeaders.Infrastructure;
17-
using Xunit;
1817

1918
namespace NetEscapades.AspNetCore.SecurityHeaders.TagHelpers.Test
2019
{
@@ -25,10 +24,8 @@ public class AttributeHashTagHelperTests
2524
color: red;
2625
background: blue;
2726
";
28-
2927
const string inlineScriptSnippet = "myScript()";
30-
31-
[Fact]
28+
[Test]
3229
public async Task ProcessAsync_StyleAttribute_GeneratesExpectedOutput()
3330
{
3431
// Arrange
@@ -41,17 +38,15 @@ public async Task ProcessAsync_StyleAttribute_GeneratesExpectedOutput()
4138
{
4239
ViewContext = GetViewContext(),
4340
};
44-
4541
// Act
4642
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
47-
4843
// Assert
4944
fixture.Output.TagName.Should().Be(tagName);
5045
fixture.Output.Attributes.Should().BeEquivalentTo([styleAttribute]);
5146
fixture.Output.Content.GetContent().Should().BeEmpty();
5247
}
5348

54-
[Fact]
49+
[Test]
5550
public async Task ProcessAsync_StyleAttribute_AddsHashToHttpContext()
5651
{
5752
// Arrange
@@ -64,17 +59,14 @@ public async Task ProcessAsync_StyleAttribute_AddsHashToHttpContext()
6459
{
6560
ViewContext = GetViewContext(),
6661
};
67-
6862
// Act
6963
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
70-
7164
// Assert
7265
var expected = "'sha256-NerDAUWfwD31YdZHveMrq0GLjsNFMwxLpZl0dPUeCcw='";
73-
var hash = tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle()
74-
.Which.Should().Be(expected);
66+
var hash = tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle().Which.Should().Be(expected);
7567
}
7668

77-
[Fact]
69+
[Test]
7870
public async Task ProcessAsync_StyleAttributeWithExplicitHashType_AddsHashToHttpContext()
7971
{
8072
// Arrange
@@ -87,17 +79,14 @@ public async Task ProcessAsync_StyleAttributeWithExplicitHashType_AddsHashToHttp
8779
{
8880
ViewContext = GetViewContext(),
8981
};
90-
9182
// Act
9283
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
93-
9484
// Assert
9585
var expected = "'sha384-YoSV9pxydVBLyyDpluNe9tQWgtUWlnzHS/zCvuNc30tEu0YwLQPRgNAXk+h06DXU'";
96-
tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle()
97-
.Which.Should().Be(expected);
86+
tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle().Which.Should().Be(expected);
9887
}
9988

100-
[Fact]
89+
[Test]
10190
public async Task ProcessAsync_StyleAttributeWithMultiLine_AddsHashToHttpContext()
10291
{
10392
// Arrange
@@ -110,17 +99,14 @@ public async Task ProcessAsync_StyleAttributeWithMultiLine_AddsHashToHttpContext
11099
{
111100
ViewContext = GetViewContext(),
112101
};
113-
114102
// Act
115103
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
116-
117104
// Assert
118105
var expected = "'sha256-ly/Q8sGjROqYelSQCwIsD00L09JdMcVcMFTDyK7N7GM='";
119-
tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle()
120-
.Which.Should().Be(expected);
106+
tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle().Which.Should().Be(expected);
121107
}
122108

123-
[Fact]
109+
[Test]
124110
public async Task ProcessAsync_StyleAttributeTargetingNonExistingAttribute_DoesntAddAndCleansUp()
125111
{
126112
// Arrange
@@ -132,17 +118,15 @@ public async Task ProcessAsync_StyleAttributeTargetingNonExistingAttribute_Doesn
132118
{
133119
ViewContext = GetViewContext(),
134120
};
135-
136121
// Act
137122
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
138-
139123
// Assert
140124
fixture.Output.TagName.Should().Be(tagName);
141125
fixture.Output.Attributes.Should().BeEmpty();
142126
fixture.Output.Content.GetContent().Should().BeEmpty();
143127
}
144128

145-
[Fact]
129+
[Test]
146130
public async Task ProcessAsync_InlineScriptAttribute_GeneratesExpectedOutput()
147131
{
148132
// Arrange
@@ -155,17 +139,15 @@ public async Task ProcessAsync_InlineScriptAttribute_GeneratesExpectedOutput()
155139
{
156140
ViewContext = GetViewContext(),
157141
};
158-
159142
// Act
160143
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
161-
162144
// Assert
163145
fixture.Output.TagName.Should().Be(tagName);
164146
fixture.Output.Attributes.Should().BeEquivalentTo([inlineScriptAttribute]);
165147
fixture.Output.Content.GetContent().Should().BeEmpty();
166148
}
167149

168-
[Fact]
150+
[Test]
169151
public async Task ProcessAsync_InlineScriptAttribute_AddsHashToHttpContext()
170152
{
171153
// Arrange
@@ -178,50 +160,35 @@ public async Task ProcessAsync_InlineScriptAttribute_AddsHashToHttpContext()
178160
{
179161
ViewContext = GetViewContext(),
180162
};
181-
182163
// Act
183164
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
184-
185165
// Assert
186166
var expected = "'sha256-1lzfyKjJuCLGsHTaOB3al0SElf3ats68l7XOAdrWd+E='";
187-
tagHelper.ViewContext.HttpContext.GetScriptCSPHashes().Should().ContainSingle()
188-
.Which.Should().Be(expected);
167+
tagHelper.ViewContext.HttpContext.GetScriptCSPHashes().Should().ContainSingle().Which.Should().Be(expected);
189168
}
190169

191-
[Fact]
170+
[Test]
192171
public async Task ProcessAsync_MultipleAttributes_AddsAllHashesToHttpContext()
193172
{
194173
// Arrange
195174
var id = Guid.NewGuid().ToString();
196175
var tagName = "div";
197-
198176
var styleAttribute = new TagHelperAttribute("style", inlineStyleSnippet);
199177
var inlineScriptAttribute = new TagHelperAttribute("onclick", inlineScriptSnippet);
200178
var cspStyleAttribute = new TagHelperAttribute("asp-add-csp-for-style");
201179
var cspScriptAttribute = new TagHelperAttribute("asp-add-csp-for-onclick");
202-
203-
var fixture = CreateFixture(id, tagName,
204-
styleAttribute,
205-
inlineScriptAttribute,
206-
cspStyleAttribute,
207-
cspScriptAttribute
208-
);
209-
180+
var fixture = CreateFixture(id, tagName, styleAttribute, inlineScriptAttribute, cspStyleAttribute, cspScriptAttribute);
210181
var tagHelper = new AttributeHashTagHelper()
211182
{
212183
ViewContext = GetViewContext(),
213184
};
214-
215185
// Act
216186
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
217-
218187
// Assert
219188
var expectedStyleHash = "'sha256-NerDAUWfwD31YdZHveMrq0GLjsNFMwxLpZl0dPUeCcw='";
220189
var expectedScriptHash = "'sha256-1lzfyKjJuCLGsHTaOB3al0SElf3ats68l7XOAdrWd+E='";
221-
tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle()
222-
.Which.Should().Be(expectedStyleHash);
223-
tagHelper.ViewContext.HttpContext.GetScriptCSPHashes().Should().ContainSingle()
224-
.Which.Should().Be(expectedScriptHash);
190+
tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle().Which.Should().Be(expectedStyleHash);
191+
tagHelper.ViewContext.HttpContext.GetScriptCSPHashes().Should().ContainSingle().Which.Should().Be(expectedScriptHash);
225192
}
226193

227194
private static Fixture CreateFixture(string id, string tagName, params TagHelperAttribute[] attributes)
@@ -236,39 +203,27 @@ private static Fixture CreateFixture(string id, string tagName, params TagHelper
236203
private static ViewContext GetViewContext()
237204
{
238205
var actionContext = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
239-
return new ViewContext(actionContext,
240-
Mock.Of<IView>(),
241-
new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()),
242-
Mock.Of<ITempDataDictionary>(),
243-
TextWriter.Null,
244-
new HtmlHelperOptions());
206+
return new ViewContext(actionContext, Mock.Of<IView>(), new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()), Mock.Of<ITempDataDictionary>(), TextWriter.Null, new HtmlHelperOptions());
245207
}
246208

247209
private static TagHelperContext GetTagHelperContext(string id, string tagName, TagHelperAttributeList attributes)
248210
{
249-
return new TagHelperContext(
250-
tagName: tagName,
251-
allAttributes: attributes,
252-
items: new Dictionary<object, object>(),
253-
uniqueId: id);
211+
return new TagHelperContext(tagName: tagName, allAttributes: attributes, items: new Dictionary<object, object>(), uniqueId: id);
254212
}
255213

256214
private static TagHelperOutput GetTagHelperOutput(string id, string tagName, TagHelperAttributeList attributes)
257215
{
258-
return new TagHelperOutput(
259-
tagName,
260-
attributes: attributes,
261-
getChildContentAsync: (useCachedResult, encoder) =>
262-
{
263-
var tagHelperContent = new DefaultTagHelperContent();
264-
return Task.FromResult<TagHelperContent>(tagHelperContent);
265-
});
216+
return new TagHelperOutput(tagName, attributes: attributes, getChildContentAsync: (useCachedResult, encoder) =>
217+
{
218+
var tagHelperContent = new DefaultTagHelperContent();
219+
return Task.FromResult<TagHelperContent>(tagHelperContent);
220+
});
266221
}
267222

268223
private class Fixture
269224
{
270-
public TagHelperContext Context { get; set; } = default!;
271-
public TagHelperOutput Output { get; set; } = default!;
225+
public TagHelperContext Context { get; set; } = default !;
226+
public TagHelperOutput Output { get; set; } = default !;
272227
}
273228
}
274-
}
229+
}

0 commit comments

Comments
 (0)