@@ -21,7 +21,7 @@ def tearDown(self):
2121 del self .myclient
2222
2323 def test_client (self ):
24- """Set up the test for OpaClient object"""
24+ """Set up the test for OpaClient object"""
2525
2626 client = OpaClient ('localhost' , 8181 , 'v1' )
2727 self .assertEqual ('http://localhost:8181/v1' , client ._root_url )
@@ -35,59 +35,74 @@ def test_client(self):
3535 self .assertEqual ('localhost' , self .myclient ._host )
3636 self .assertEqual (8181 , self .myclient ._port )
3737
38- def test_functions (self ):
39-
38+ def test_connection_to_opa (self ):
4039 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
4944
50- # self.assertEqual(_dict, self.myclient.get_policies_info())
45+ import data.test.acl
46+ import input
5147
52- new_policy = """
53- package play
48+ default allow = false
5449
55- default hello = false
50+ allow {
51+ access := acl[input.user]
52+ access[_] == input.access
53+ }
5654
57- hello {
58- m := input.message
59- m == "world"
55+ authorized_users[user] {
56+ access := acl[user]
57+ access[_] == input.access
6058 }
6159 """
62- self .assertEqual (True , self .myclient .update_opa_policy_fromstring (new_policy , 'test' ))
6360
64- self .assertEqual (['test' ], self .myclient .get_policies_list ())
6561 _dict = {
6662 '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+ ],
6968 }
7069 }
7170
72- self .assertEqual (_dict , self .myclient .get_policies_info ())
71+ my_policy_list = {
72+ "alice" : ["read" ,"write" ],
73+ "bob" : ["read" ]
74+ }
7375
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' ])
7885
7986 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 ' )
8188 )
82- value = {'result' : {'hello' : False }}
8389
8490 self .assertEqual (True , self .myclient .opa_policy_to_file ('test' ))
8591
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" ))
87101
88102 self .assertTrue (True , self .myclient .delete_opa_policy ('test' ))
89103 with self .assertRaises (DeletePolicyError ):
90104 self .myclient .delete_opa_policy ('test' )
91105
106+ self .assertTrue (True , self .myclient .delete_opa_data ('test/acl' ))
92107 with self .assertRaises (DeleteDataError ):
93- self .myclient .delete_opa_data ('play ' )
108+ self .myclient .delete_opa_data ('test/acl ' )
0 commit comments