@@ -21,7 +21,7 @@ def tearDown(self):
21
21
del self .myclient
22
22
23
23
def test_client (self ):
24
- """Set up the test for OpaClient object"""
24
+ """Set up the test for OpaClient object"""
25
25
26
26
client = OpaClient ('localhost' , 8181 , 'v1' )
27
27
self .assertEqual ('http://localhost:8181/v1' , client ._root_url )
@@ -35,59 +35,74 @@ def test_client(self):
35
35
self .assertEqual ('localhost' , self .myclient ._host )
36
36
self .assertEqual (8181 , self .myclient ._port )
37
37
38
- def test_functions (self ):
39
-
38
+ def test_connection_to_opa (self ):
40
39
self .assertEqual ("Yes I'm here :)" , self .myclient .check_connection ())
41
- self .assertEqual (list (), self .myclient .get_policies_list ())
42
-
43
- self .assertEqual (dict (), self .myclient .get_policies_info ())
44
-
45
- # _dict = {'test': {'path': [
46
- # 'http://localhost:8181/v1/data/play'],
47
- # 'rules': ['http://localhost:8181/v1/data/play/hello']}
48
- # }
40
+
41
+ def test_functions (self ):
42
+ new_policy = """
43
+ package test.policy
49
44
50
- # self.assertEqual(_dict, self.myclient.get_policies_info())
45
+ import data.test.acl
46
+ import input
51
47
52
- new_policy = """
53
- package play
48
+ default allow = false
54
49
55
- default hello = false
50
+ allow {
51
+ access := acl[input.user]
52
+ access[_] == input.access
53
+ }
56
54
57
- hello {
58
- m := input.message
59
- m == "world"
55
+ authorized_users[user] {
56
+ access := acl[user]
57
+ access[_] == input.access
60
58
}
61
59
"""
62
- self .assertEqual (True , self .myclient .update_opa_policy_fromstring (new_policy , 'test' ))
63
60
64
- self .assertEqual (['test' ], self .myclient .get_policies_list ())
65
61
_dict = {
66
62
'test' : {
67
- 'path' : ['http://localhost:8181/v1/data/play' ],
68
- 'rules' : ['http://localhost:8181/v1/data/play/hello' ],
63
+ 'path' : ['http://localhost:8181/v1/data/test/policy' ],
64
+ 'rules' : [
65
+ 'http://localhost:8181/v1/data/test/policy/allow' ,
66
+ 'http://localhost:8181/v1/data/test/policy/authorized_users'
67
+ ],
69
68
}
70
69
}
71
70
72
- self .assertEqual (_dict , self .myclient .get_policies_info ())
71
+ my_policy_list = {
72
+ "alice" : ["read" ,"write" ],
73
+ "bob" : ["read" ]
74
+ }
73
75
74
- my_policy_list = [
75
- {'resource' : '/api/someapi' , 'identity' : 'your_identity' , 'method' : 'PUT' },
76
- {'resource' : '/api/someapi' , 'identity' : 'your_identity' , 'method' : 'GET' },
77
- ]
76
+ self .assertEqual (list (), self .myclient .get_policies_list ())
77
+ self .assertEqual (dict (), self .myclient .get_policies_info ())
78
+ self .assertEqual (True , self .myclient .update_opa_policy_fromstring (new_policy , 'test' ))
79
+ self .assertEqual (['test' ], self .myclient .get_policies_list ())
80
+
81
+ policy_info = self .myclient .get_policies_info ()
82
+ self .assertEqual (_dict ['test' ]['path' ], policy_info ['test' ]['path' ])
83
+ for rule in _dict ['test' ]['rules' ]:
84
+ self .assertIn (rule , policy_info ['test' ]['rules' ])
78
85
79
86
self .assertTrue (
80
- True , self .myclient .update_or_create_opa_data (my_policy_list , 'exampledata/accesses ' )
87
+ True , self .myclient .update_or_create_opa_data (my_policy_list , 'test/acl ' )
81
88
)
82
- value = {'result' : {'hello' : False }}
83
89
84
90
self .assertEqual (True , self .myclient .opa_policy_to_file ('test' ))
85
91
86
- self .assertEqual (value , self .myclient .get_opa_raw_data ('play' ))
92
+ value = {'result' : {'acl' : {'alice' : ['read' , 'write' ], 'bob' : ['read' ]}, 'policy' : {'allow' : False , 'authorized_users' : []}}}
93
+ self .assertEqual (value , self .myclient .get_opa_raw_data ('test' ))
94
+
95
+ _input_a = {"input" : {"user" : "alice" , "access" : "write" }}
96
+ _input_b = {"input" : {"access" : "read" }}
97
+ value_a = {"result" : True }
98
+ value_b = {"result" : ["alice" , "bob" ]}
99
+ self .assertEqual (value_a , self .myclient .check_permission (input_data = _input_a , policy_name = "test" , rule_name = "allow" ))
100
+ self .assertEqual (value_b , self .myclient .check_permission (input_data = _input_b , policy_name = "test" , rule_name = "authorized_users" ))
87
101
88
102
self .assertTrue (True , self .myclient .delete_opa_policy ('test' ))
89
103
with self .assertRaises (DeletePolicyError ):
90
104
self .myclient .delete_opa_policy ('test' )
91
105
106
+ self .assertTrue (True , self .myclient .delete_opa_data ('test/acl' ))
92
107
with self .assertRaises (DeleteDataError ):
93
- self .myclient .delete_opa_data ('play ' )
108
+ self .myclient .delete_opa_data ('test/acl ' )
0 commit comments