9using System.Collections.Generic;
11using System.IO.IsolatedStorage;
12using System.Reflection;
19using System.Diagnostics;
20using System.Security.Claims;
26using TestClass = NUnit.Framework.TestFixtureAttribute;
27using TestInitialize = NUnit.Framework.SetUpAttribute;
28using TestCleanup = NUnit.Framework.TearDownAttribute;
29using TestMethod = NUnit.Framework.TestAttribute;
31using Microsoft.VisualStudio.TestTools.UnitTesting;
46 private static ClaimsPrincipal GetPrincipal(params
string[] roles)
48 var identity =
new ClaimsIdentity();
49 foreach (var item
in roles)
50 identity.AddClaim(
new Claim(ClaimTypes.Role, item));
51 return new ClaimsPrincipal(identity);
57 _anonymousDIContext = TestDIContextFactory.CreateContext(
new ClaimsPrincipal());
58 _adminDIContext = TestDIContextFactory.CreateContext(GetPrincipal(
"Admin"));
65 ApplicationContext applicationContext = _adminDIContext.CreateTestApplicationContext();
71 Assert.AreEqual(
true, applicationContext.
User.IsInRole(
"Admin"));
73 #region "Pre Cloning Tests"
76 Assert.AreEqual(
"[DenyReadOnProperty] Can't read property", root.DenyReadOnProperty,
77 "Read should have been denied 1");
80 root.DenyWriteOnProperty =
"DenyWriteOnProperty";
82 Assert.AreEqual(
"[DenyWriteOnProperty] Can't write variable", root.Auth,
83 "Write should have been denied 2");
86 Assert.AreEqual(
"[DenyReadWriteOnProperty] Can't read property", root.DenyReadWriteOnProperty,
87 "Read should have been denied 3");
89 root.DenyReadWriteOnProperty =
"DenyReadWriteONproperty";
91 Assert.AreEqual(
"[DenyReadWriteOnProperty] Can't write variable", root.Auth,
92 "Write should have been denied 4");
95 Assert.AreEqual(root.AllowReadWriteOnProperty, root.Auth,
96 "Read should have been allowed 5");
98 root.AllowReadWriteOnProperty =
"No value";
99 Assert.AreEqual(
"No value", root.Auth,
100 "Write should have been allowed 6");
104 #region "After Cloning Tests"
112 Assert.AreEqual(
"[DenyReadOnProperty] Can't read property", newRoot.DenyReadOnProperty,
113 "Read should have been denied 7");
116 newRoot.DenyWriteOnProperty =
"DenyWriteOnProperty";
118 Assert.AreEqual(
"[DenyWriteOnProperty] Can't write variable", newRoot.Auth,
119 "Write should have been denied 8");
122 Assert.AreEqual(
"[DenyReadWriteOnProperty] Can't read property", newRoot.DenyReadWriteOnProperty,
123 "Read should have been denied 9");
125 newRoot.DenyReadWriteOnProperty =
"DenyReadWriteONproperty";
127 Assert.AreEqual(
"[DenyReadWriteOnProperty] Can't write variable", newRoot.Auth,
128 "Write should have been denied 10");
131 Assert.AreEqual(newRoot.AllowReadWriteOnProperty, newRoot.Auth,
132 "Read should have been allowed 11");
134 newRoot.AllowReadWriteOnProperty =
"AllowReadWriteOnProperty";
135 Assert.AreEqual(
"AllowReadWriteOnProperty", newRoot.Auth,
136 "Write should have been allowed 12");
145 Guid managerInstanceId;
146 TestDIContext customDIContext = TestDIContextFactory.CreateContext(GetPrincipal(
"Admin"));
148 ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext();
154 Assert.AreEqual(
true, applicationContext.
Principal.IsInRole(
"Admin"));
156 root.Data =
"Something new";
160 #region "Pre-Testing"
162 root.Data =
"Something new 1";
166 Debug.WriteLine(managerInstanceId);
167 string result = root.DenyReadOnProperty;
169 Assert.AreEqual(
"[DenyReadOnProperty] Can't read property", root.DenyReadOnProperty,
170 "Read should have been denied");
173 root.DenyWriteOnProperty =
"DenyWriteOnProperty";
175 Assert.AreEqual(
"[DenyWriteOnProperty] Can't write variable", root.Auth,
176 "Write should have been denied");
179 Assert.AreEqual(
"[DenyReadWriteOnProperty] Can't read property", root.DenyReadWriteOnProperty,
180 "Read should have been denied");
182 root.DenyReadWriteOnProperty =
"DenyReadWriteONproperty";
184 Assert.AreEqual(
"[DenyReadWriteOnProperty] Can't write variable", root.Auth,
185 "Write should have been denied");
188 Assert.AreEqual(root.AllowReadWriteOnProperty, root.Auth,
189 "Read should have been allowed");
191 root.AllowReadWriteOnProperty =
"No value";
192 Assert.AreEqual(
"No value", root.Auth,
193 "Write should have been allowed");
197 #region "Cancel Edit"
203 Assert.AreEqual(
"[DenyReadOnProperty] Can't read property", root.DenyReadOnProperty,
204 "Read should have been denied");
207 root.DenyWriteOnProperty =
"DenyWriteOnProperty";
209 Assert.AreEqual(
"[DenyWriteOnProperty] Can't write variable", root.Auth,
210 "Write should have been denied");
213 Assert.AreEqual(
"[DenyReadWriteOnProperty] Can't read property", root.DenyReadWriteOnProperty,
214 "Read should have been denied");
216 root.DenyReadWriteOnProperty =
"DenyReadWriteONproperty";
218 Assert.AreEqual(
"[DenyReadWriteOnProperty] Can't write variable", root.Auth,
219 "Write should have been denied");
222 Assert.AreEqual(root.AllowReadWriteOnProperty, root.Auth,
223 "Read should have been allowed");
225 root.AllowReadWriteOnProperty =
"No value";
226 Assert.AreEqual(
"No value", root.Auth,
227 "Write should have been allowed");
235 Assert.AreEqual(
"[DenyReadOnProperty] Can't read property", root.DenyReadOnProperty,
236 "Read should have been denied");
239 root.DenyWriteOnProperty =
"DenyWriteOnProperty";
241 Assert.AreEqual(
"[DenyWriteOnProperty] Can't write variable", root.Auth,
242 "Write should have been denied");
245 Assert.AreEqual(
"[DenyReadWriteOnProperty] Can't read property", root.DenyReadWriteOnProperty,
246 "Read should have been denied");
248 root.DenyReadWriteOnProperty =
"DenyReadWriteONproperty";
250 Assert.AreEqual(
"[DenyReadWriteOnProperty] Can't write variable", root.Auth,
251 "Write should have been denied");
254 Assert.AreEqual(root.AllowReadWriteOnProperty, root.Auth,
255 "Read should have been allowed");
257 root.AllowReadWriteOnProperty =
"No value";
258 Assert.AreEqual(
"No value", root.Auth,
259 "Write should have been allowed");
269 TestDIContext customDIContext = TestDIContextFactory.CreateContext(GetPrincipal(
"Admin"));
271 ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext();
283 applicationContext.User =
new ClaimsPrincipal();
286 applicationContext.User = GetPrincipal(
"Admin");
313 pr.FirstName =
"test";
319 TestDIContext customDIContext = TestDIContextFactory.CreateContext(GetPrincipal(
"Admin"));
321 ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext();
331 Assert.AreEqual(
true, applicationContext.Principal.IsInRole(
"Admin"));
334 applicationContext.User =
new ClaimsPrincipal();
336 Assert.AreEqual(
false, applicationContext.Principal.IsInRole(
"Admin"));
342 TestDIContext customDIContext = TestDIContextFactory.CreateContext(GetPrincipal(
"Admin"));
344 ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext();
352 Assert.AreEqual(
true, applicationContext.Principal.IsInRole(
"Admin"));
355 applicationContext.User =
new ClaimsPrincipal();
357 Assert.AreEqual(
false, applicationContext.Principal.IsInRole(
"Admin"));
365 ApplicationContext applicationContext = _anonymousDIContext.CreateTestApplicationContext();
369 Assert.AreEqual(
false, applicationContext.User.IsInRole(
"Admin"));
381 ApplicationContext applicationContext = _adminDIContext.CreateTestApplicationContext();
385 var root = dataPortal.
Create();
387 Assert.IsTrue(applicationContext.Principal.IsInRole(
"Admin"));
388 Assert.IsFalse(applicationContext.Principal.IsInRole(
"User"));
393 applicationContext.RuleSet =
"custom1";
395 applicationContext.RuleSet =
"custom2";
410 ApplicationContext applicationContext = _adminDIContext.CreateTestApplicationContext();
414 var root = dataPortal.
Create();
416 Assert.IsTrue(applicationContext.Principal.IsInRole(
"Admin"));
417 Assert.IsFalse(applicationContext.Principal.IsInRole(
"User"));
426 applicationContext.RuleSet =
"custom1";
428 applicationContext.RuleSet =
"custom2";
444 ApplicationContext applicationContext = _anonymousDIContext.CreateTestApplicationContext();
454 Assert.IsInstanceOfType(ex, typeof(TargetInvocationException));
455 Assert.IsInstanceOfType(ex.InnerException, typeof(ArgumentException));
466 Assert.IsInstanceOfType(ex, typeof(TargetInvocationException));
467 Assert.IsInstanceOfType(ex.InnerException, typeof(ArgumentException));
478 var root = dataPortal.
Create();
485 TestDIContext testDIContext = TestDIContextFactory.CreateContext(
486 options => options.DataPortal(
487 dp => dp.AddServerSideDataPortal(
498 TestDIContext customDIContext = TestDIContextFactory.CreateContext(
499 options => options.DataPortal(
502 ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext();
510 ApplicationContext applicationContext = _anonymousDIContext.CreateTestApplicationContext();
524 new[] {
new object() }));
538 ApplicationContext applicationContext = _anonymousDIContext.CreateTestApplicationContext();
552 new[] {
new object() }));
566 ApplicationContext applicationContext = _anonymousDIContext.CreateTestApplicationContext();
580 new[] {
new object() }));
596 return Activator.CreateInstance(
ResolveType(requestedType));
612 return requestedType;
622 [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
641 using (SuppressListChangedEvents)
643 Add(childDataPortal.CreateChild());
653 base.AddBusinessRules();
Provides consistent context information between the client and server DataPortal objects.
IContextManager ContextManager
Gets the context manager responsible for storing user and context information for the application.
string? RuleSet
Gets or sets the RuleSet name to use for static HasPermission calls.
IPrincipal User
Get or set the current IPrincipal object representing the user's identity.
ClaimsPrincipal Principal
Get or set the current ClaimsPrincipal object representing the user's identity.
const string DefaultRuleSet
The default RuleSet name
This is the base class from which most business objects will be derived.
T Clone()
Creates a clone of the object.
This is the base class from which most business collections or lists will be derived.
Client side data portal used for making asynchronous data portal calls in .NET.
T Create(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
T Fetch(params object[] criteria)
Called by a factory method in a business class to Fetch a new object, which is loaded with default va...
Base class providing basic authorization rule implementation.
Tracks the business rules for a business object.
static bool HasPermission(ApplicationContext applicationContext, AuthorizationActions action, Type objectType)
Checks per-type authorization rules.
void AddRule(IBusinessRuleBase rule)
Associates a business rule with the business object.
IsInRole authorization rule.
void PerTypeAuthDeleteWithCriteria()
void TestUnauthorizedAccessToGet()
void AuthorizeRemoveFromList()
void PerTypeAuthFetchWithCriteria()
void TestAuthRuleSetsOnStaticHasPermissionMethodsWhenAddingAuthzRuleSetUsingApplicationContextRuleSet()
void PerTypeAuthEditObject()
void TestAuthBeginEditRules()
void PerTypeAuthCreateWithCriteria()
void TestUnauthorizedAccessToSet()
void TestAuthCloneRules()
void TestAuthorizedAccess()
void TestAuthRuleSetsOnStaticHasPermissionMethodsWhenAddingAuthzRuleSetExplicitly()
static void ClassInitialize(TestContext context)
void PerTypeAuthEditObjectViaInterface()
void TestAuthRulesCleanupAndAddAgainWhenExceptionIsThrownInAddObjectBusinessRules()
void TestAuthorizationAfterEditCycle()
override void AddBusinessRules()
Type ResolveType(Type requestedType)
Gets the actual business domain class type based on the requested type (which might be an interface).
void FinalizeInstance(object obj)
Finalizes an existing business object instance.
void InitializeInstance(object obj)
Initializes an existing business object instance.
object CreateInstance(Type requestedType)
Gets a new instance of the requested type.
static void AddObjectAuthorizationRules()
Static dictionary-like class that offers similar functionality to GlobalContext This is used in tests...
static void Reinitialise()
Reinitialise the dictionary, clearing any existing results, ready for the next test
Type to carry context information for DI in unit tests
Interface defining the members of the child data portal type.
Interface defining the members of the data portal type.
object Create(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
Implemented by objects which provide context information to an authorization rule when it is invoked.
bool HasPermission
Gets or sets a value indicating whether the current user has permission to perform the requested acti...
AuthorizationActions
Authorization actions.
@ Serializable
Prevents updating or inserting until the transaction is complete.
@ Execute
Execute operation.