-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathJudgeAndersonTests.cs
81 lines (69 loc) · 2.42 KB
/
JudgeAndersonTests.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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
namespace MegaCityOne.Tests
{
/// <summary>
/// Test if JudgeAnderson features works properly.
/// </summary>
[TestClass]
public class JudgeAndersonTests
{
/// <summary>
/// Test to see if a simple law works well.
/// </summary>
[TestMethod]
public void SimpleJavaScriptRuleSuccess()
{
JudgeAnderson judge = new JudgeAnderson();
judge.Load(@"function CanSeeBankAccount(principal) {
return principal.IsInRole('BankingConsultant');
}");
judge.Principal = new GenericPrincipal(
WindowsIdentity.GetCurrent(),
new string[] { "BankingConsultant" });
judge.Enforce("CanSeeBankAccount");
}
/// <summary>
/// Insure that an Advise failure throws an LawgiverException.
/// </summary>
[TestMethod]
[ExpectedException(typeof(LawgiverException))]
public void SimpleJavaScriptRuleFail()
{
JudgeAnderson judge = new JudgeAnderson();
judge.Load(@"function CanSeeBankAccount(principal) {
return principal.IsInRole('BankingConsultant');
}");
judge.Principal = new GenericPrincipal(
WindowsIdentity.GetCurrent(), new string[0]);
judge.Enforce("CanSeeBankAccount");
}
/// <summary>
/// Loads a set of law from a Javascript Law definition file.
/// This method shall not thow any LawgiverException.
/// </summary>
[TestMethod]
public void LoadLawsFromJavaScriptFile()
{
JudgeAnderson judge = new JudgeAnderson();
judge.Load(new FileInfo("JusticeDepartment.js"));
judge.Principal = new GenericPrincipal(
new GenericIdentity("rico"),
new string[] { "BankUser" });
object account = new
{
Owner = "rico",
Balance = decimal.Zero
};
judge.Enforce("CanWithdrawFromAccount", account);
judge.Enforce("CanDepositToAccount", account);
judge.Enforce("CanDisplayBalance", account);
}
}
}