-
Notifications
You must be signed in to change notification settings - Fork 11
/
MultiEntryPointTests.cs
115 lines (97 loc) · 3.38 KB
/
MultiEntryPointTests.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
using NUnit.Framework;
using System;
namespace Opa.Wasm.UnitTests
{
public class MultiEntryPointTests
{
[Test]
public void DumpEntryPointsForMultiTest()
{
using var module = OpaPolicyModule.Load(WasmFiles.MultiEntrypointExample);
using var opaPolicy = module.CreatePolicyInstance();
var entrypoints = opaPolicy.Entrypoints;
Assert.That(entrypoints.Count, Is.EqualTo(3));
Assert.That(entrypoints["example/one"], Is.EqualTo(1));
Assert.That(entrypoints["example/one/myCompositeRule"], Is.EqualTo(2));
Assert.That(entrypoints["example"], Is.EqualTo(0));
}
[Test]
public void DefaultEntrypointMultiTest()
{
using var module = OpaPolicyModule.Load(WasmFiles.MultiEntrypointExample);
using var opaPolicy = module.CreatePolicyInstance();
string input = new
{
someProp = "thisValue",
anotherProp = "thatValue"
}.ToJson();
string outputJson = opaPolicy.EvaluateJson(input);
// [{"result":{"one":{"myOtherRule":true,"myRule":true,"myCompositeRule":true}}}]
dynamic output = outputJson.ToDynamic();
Assert.That(output[0].result.one.myOtherRule, Is.True);
Assert.That(output[0].result.one.myRule, Is.True);
Assert.That(output[0].result.one.myCompositeRule, Is.True);
}
[Test]
public void ExampleOneEntrypointByNumberMultiTest()
{
using var module = OpaPolicyModule.Load(WasmFiles.MultiEntrypointExample);
using var opaPolicy = module.CreatePolicyInstance();
string input = new
{
someProp = "thisValue",
anotherProp = "thatValue"
}.ToJson();
string outputJson = opaPolicy.EvaluateJson(input, 1);
// [{"result":{"myOtherRule":true,"myRule":true,"myCompositeRule":true}}]
dynamic output = outputJson.ToDynamic();
Assert.That(output[0].result.myOtherRule, Is.True);
Assert.That(output[0].result.myRule, Is.True);
Assert.That(output[0].result.myCompositeRule, Is.True);
}
[Test]
public void ExampleOneEntrypointByNameMultiTest()
{
using var module = OpaPolicyModule.Load(WasmFiles.MultiEntrypointExample);
using var opaPolicy = module.CreatePolicyInstance();
string input = new
{
someProp = "thisValue",
anotherProp = "thatValue"
}.ToJson();
string outputJson = opaPolicy.EvaluateJson(input, "example/one");
// [{"result":{"myOtherRule":true,"myRule":true,"myCompositeRule":true}}]
dynamic output = outputJson.ToDynamic();
Assert.That(output[0].result.myOtherRule, Is.True);
Assert.That(output[0].result.myRule, Is.True);
Assert.That(output[0].result.myCompositeRule, Is.True);
}
[Test]
public void InvalidNumericalEntrypointMultiTest()
{
using var module = OpaPolicyModule.Load(WasmFiles.MultiEntrypointExample);
using var opaPolicy = module.CreatePolicyInstance();
string input = new
{
someProp = "thisValue",
anotherProp = "thatValue"
}.ToJson();
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => opaPolicy.EvaluateJson(input, 5));
}
[Test]
public void ExampleOneRuleEntrypointMultiTest()
{
using var module = OpaPolicyModule.Load(WasmFiles.MultiEntrypointExample);
using var opaPolicy = module.CreatePolicyInstance();
string input = new
{
someProp = "thisValue",
anotherProp = "thatValue"
}.ToJson();
string outputJson = opaPolicy.EvaluateJson(input, "example/one/myCompositeRule");
// [{"result":true}]
dynamic output = outputJson.ToDynamic();
Assert.That(output[0].result, Is.True);
}
}
}