forked from kristofferahl/FluentSecurity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecurityConfigurationSpec.cs
188 lines (157 loc) · 5.46 KB
/
SecurityConfigurationSpec.cs
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Configuration;
using System.Linq;
using System.Reflection;
using FluentSecurity.Specification.Helpers;
using FluentSecurity.Specification.TestData;
using NUnit.Framework;
namespace FluentSecurity.Specification
{
[TestFixture]
[Category("SecurityConfigurationSpec")]
public class When_creating_a_new_SecurityConfiguration
{
private static SecurityConfiguration _securityConfiguration;
private static void Because()
{
_securityConfiguration = new SecurityConfiguration(policy => { });
}
[Test]
public void Should_not_contain_any_policycontainers()
{
// Act
Because();
// Assert
var containers = _securityConfiguration.PolicyContainers.Count();
Assert.That(containers, Is.EqualTo(0));
}
[Test]
public void Should_not_have_ServiceLocator()
{
// Act
Because();
// Assert
Assert.That(_securityConfiguration.Runtime.ExternalServiceLocator, Is.Null);
}
[Test]
public void Should_have_advanced_configuration()
{
// Act
Because();
// Assert
Assert.That(_securityConfiguration.Runtime, Is.Not.Null);
}
}
[TestFixture]
[Category("SecurityConfigurationSpec")]
public class When_creating_a_new_SecurityConfiguration_passing_null_as_the_argument
{
[Test]
public void Should_throw()
{
// Act & assert
var exception = Assert.Throws<ArgumentNullException>(() => new SecurityConfiguration(null));
// Assert
Assert.That(exception.ParamName, Is.EqualTo("configurationExpression"));
}
}
[TestFixture]
[Category("SecurityConfigurationSpec")]
public class When_I_check_what_I_have
{
[Test]
public void Should_return_the_current_configuration()
{
const string expectedOutput = @"Ignore missing configuration: True
------------------------------------------------------------------------------------
FluentSecurity.Specification.TestData.BlogController > DeletePost
FluentSecurity.Policy.RequireAnyRolePolicy (Owner or Publisher)
FluentSecurity.Specification.TestData.BlogController > Index
FluentSecurity.Policy.DenyAnonymousAccessPolicy
------------------------------------------------------------------------------------";
var securityConfiguration = new SecurityConfiguration(configuration =>
{
configuration.GetAuthenticationStatusFrom(StaticHelper.IsAuthenticatedReturnsFalse);
configuration.Advanced.IgnoreMissingConfiguration();
configuration.For<BlogController>(x => x.DeletePost(0)).DenyAnonymousAccess().RequireAnyRole(UserRole.Owner, UserRole.Publisher);
configuration.For<BlogController>(x => x.Index()).DenyAnonymousAccess();
});
SecurityConfigurator.SetConfiguration(securityConfiguration);
// Act
var whatIHave = securityConfiguration.WhatDoIHave();
// Assert
Assert.That(whatIHave, Is.EqualTo(expectedOutput));
}
}
[TestFixture]
[Category("SecurityConfigurationSpec")]
public class When_setting_the_configuration_to_null
{
[Test]
public void Should_throw()
{
// Arrange
var setConfigurationMethod = typeof(SecurityConfiguration).GetMethod("SetConfiguration", BindingFlags.Static | BindingFlags.NonPublic);
var parameters = new System.Collections.Generic.List<object> { null }.ToArray();
// Act
var exception = Assert.Catch(() => setConfigurationMethod.Invoke(null, parameters));
// Assert
Assert.That(exception.InnerException, Is.TypeOf(typeof(ArgumentNullException)));
}
}
[TestFixture]
[Category("SecurityConfigurationSpec")]
public class When_calling_assert_all_actions_on_configuration
{
[Test]
public void Should_throw_exception_when_all_actions_are_not_configured()
{
//Arrange & Act
var securityConfiguration = new SecurityConfiguration(policy => {});
//Assert
Assert.Throws<ConfigurationErrorsException>(() => securityConfiguration.AssertAllActionsAreConfigured());
}
[Test]
public void Should_not_throw_exception_when_all_actions_are_configured()
{
//Arrange & Act
var assemblies = (
from Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()
where !(assembly is System.Reflection.Emit.AssemblyBuilder) &&
assembly.GetType().FullName != "System.Reflection.Emit.InternalAssemblyBuilder" &&
!assembly.GlobalAssemblyCache
select assembly
).ToArray();
var securityConfiguration = new SecurityConfiguration(policy => policy
.ForAllControllersInAssembly(assemblies)
.DenyAuthenticatedAccess()
);
//Assert
Assert.DoesNotThrow(() => securityConfiguration.AssertAllActionsAreConfigured());
}
}
[TestFixture]
[Category("SecurityConfigurationSpec")]
public class When_calling_assert_all_actions_on_configuration_passing_assemblies_as_argument
{
[Test]
public void Should_throw_exception_when_all_actions_are_not_configured()
{
//Arrange & Act
var securityConfiguration = new SecurityConfiguration(policy => {});
//Assert
Assert.Throws<ConfigurationErrorsException>(() => securityConfiguration.AssertAllActionsAreConfigured(new [] { GetType().Assembly }));
}
[Test]
public void Should_not_throw_exception_when_all_actions_are_configured()
{
//Arrange & Act
var securityConfiguration = new SecurityConfiguration(policy => policy
.ForAllControllers()
.DenyAuthenticatedAccess()
);
//Assert
Assert.DoesNotThrow(() => securityConfiguration.AssertAllActionsAreConfigured(new [] { GetType().Assembly }));
}
}
}