-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathContentSecurityPolicyGeneratorTests.cs
More file actions
81 lines (71 loc) · 2.72 KB
/
ContentSecurityPolicyGeneratorTests.cs
File metadata and controls
81 lines (71 loc) · 2.72 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
using FluentAssertions;
using Microsoft.CodeAnalysis;
using TUnit.Assertions;
using TUnit.Core;
using VerifyTests;
using VerifyTUnit;
namespace SourceGenerator.Test;
public class ContentSecurityPolicyGeneratorTests
{
private readonly IIncrementalGenerator _generator = new ContentSecurityPolicyGenerator();
private VerifySettings Settings()
{
var settings = new VerifySettings();
settings.UseDirectory("Snapshots");
settings.DisableRequireUniquePrefix();
return settings;
}
[Test]
public Task CanGenerateCspMixinsInGlobalNamespace()
{
var input = $$"""
using System;
using System.Collections.Generic;
#nullable enable
namespace NetEscapades.AspNetCore.SecurityHeaders.Headers.ContentSecurityPolicy
{
[CspMixin(MixinTypes.All | MixinTypes.ScriptHashTagHelper)]
public partial class TestBuilder : CspDirectiveBuilder
{
}
}
{{Included.CspDirectiveBuilder}}
""";
var (diagnostics, output) = TestHelpers.GetGeneratedOutput(_generator,
new(TestHelpers.EmbeddedAttribute, TestHelpers.EmbeddedEnum, input));
diagnostics.Should().BeEmpty();
return Verifier.Verify(output, Settings());
}
private class Included
{
public static readonly string CspDirectiveBuilder =
$$"""
namespace NetEscapades.AspNetCore.SecurityHeaders.Headers.ContentSecurityPolicy
{
using Microsoft.AspNetCore.Http;
public class CspDirectiveBuilder
{
public bool BlockResources { get; set; }
public bool MustReportSample { get; set; }
public List<string> Sources { get; } = new();
public SourceBuilderCollection SourceBuilders { get; } = new();
}
public class SourceBuilderCollection
{
public void Add(Func<HttpContext, string> a, string b)
{
}
}
}
namespace Microsoft.AspNetCore.Http
{
public class HttpContext
{
public string GetNonce() => string.Empty;
public string[] GetScriptCSPHashes() => new string[] { };
}
}
{{TestHelpers.InsecureApiAttribute}}
""";
}
}