Skip to content

Commit 5afd18f

Browse files
committed
feat: Added tests
Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent 673bc01 commit 5afd18f

File tree

8 files changed

+97
-6
lines changed

8 files changed

+97
-6
lines changed

bindings/python/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ set(SOURCES
1818
py_enforcer.cpp
1919
py_abac_data.cpp
2020
py_model.cpp
21-
)
21+
py_config.cpp)
2222

2323
set(HEADERS
2424
py_casbin.h

bindings/python/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ PYBIND11_MODULE(pycasbin, m) {
3838
bindPyCachedEnforcer(m);
3939
bindABACData(m);
4040
bindPyModel(m);
41+
bindPyConfig(m);
4142

4243
m.attr("__version__") = PY_CASBIN_VERSION;
4344
}

bindings/python/py_casbin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ void bindPyEnforcer(py::module &m);
2222
void bindPyCachedEnforcer(py::module &m);
2323
void bindABACData(py::module &m);
2424
void bindPyModel(py::module &m);
25+
void bindPyConfig(py::module &m);

bindings/python/py_config.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2021 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <pybind11/pybind11.h>
18+
#include <pybind11/stl.h>
19+
#include <casbin/casbin.h>
20+
21+
namespace py = pybind11;
22+
23+
void bindPyConfig(py::module &m) {
24+
py::class_<casbin::Config>(m, "Config")
25+
.def(py::init<>())
26+
.def(py::init<const std::string&>())
27+
.def_static("NewConfig", &casbin::Config::NewConfig, R"doc(
28+
/**
29+
* NewConfig create an empty configuration representation from file.
30+
*
31+
* @param confName the path of the model file.
32+
* @return the constructor of Config.
33+
*/
34+
)doc")
35+
.def_static("NewConfigFromText", &casbin::Config::NewConfigFromText, R"doc(
36+
/**
37+
* newConfigFromText create an empty configuration representation from text.
38+
*
39+
* @param text the model text.
40+
* @return the constructor of Config.
41+
*/
42+
)doc")
43+
.def("GetBool", &casbin::Config::GetBool)
44+
.def("GetInt", &casbin::Config::GetInt)
45+
.def("GetFloat", &casbin::Config::GetFloat)
46+
.def("GetString", &casbin::Config::GetString)
47+
.def("GetStrings", &casbin::Config::GetStrings)
48+
.def("Set", &casbin::Config::Set)
49+
.def("Get", &casbin::Config::Get);
50+
}

bindings/python/py_model.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
#include "py_casbin.h"
2222

23+
void PyLoadModelFromConfig(casbin::Model* model, std::shared_ptr<casbin::Config> config) {
24+
model->LoadModelFromConfig(config);
25+
}
26+
2327
void bindPyModel(py::module &m) {
2428
py::class_<casbin::Model, std::shared_ptr<casbin::Model>>(m, "Model")
2529
.def(py::init<>())
@@ -52,4 +56,6 @@ void bindPyModel(py::module &m) {
5256
.def_static("NewModel", &casbin::Model::NewModel, "NewModel creates an empty model.")
5357
.def_static("NewModelFromFile", &casbin::Model::NewModelFromFile, "NewModelFromFile creates a model from a .CONF file.")
5458
.def_static("NewModelFromString", &casbin::Model::NewModelFromString, "NewModel creates a model from a std::string which contains model text.");
59+
60+
m.def("LoadModelFromConfig", &PyLoadModelFromConfig);
5561
}

casbin/config/config.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ std::shared_ptr<Config> Config::NewConfigFromText(const std::string& text) {
114114
return c;
115115
}
116116

117+
Config::Config(const std::string& conf_name) {
118+
this->Parse(conf_name);
119+
}
120+
117121
bool Config::GetBool(std::string_view key) {
118122
return Get(key).compare("true")==0;
119123
}

casbin/config/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class Config : public ConfigInterface {
6464

6565
bool GetBool(std::string_view key);
6666

67+
Config(const std::string& conf_name);
68+
6769
int GetInt(std::string_view key);
6870

6971
float GetFloat(std::string_view key);

tests/python/test_model.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
import unittest
1818

1919
class TestModel(unittest.TestCase):
20+
21+
# def setUp(self):
22+
# self.basic_config = casbin.Config.NewConfig(basic_model_path)
23+
2024
def test_NewModel(self):
2125
model = casbin.Model.NewModel()
2226
self.assertIsNotNone(model)
@@ -25,8 +29,31 @@ def test_NewModelFromFile(self):
2529
model = casbin.Model.NewModelFromFile(basic_model_path)
2630
self.assertIsNotNone(model)
2731

28-
# def test_NewModelFromString(self):
29-
# with open(basic_model_path, 'r') as model_file:
30-
# model_string = model_file.read().replace('\n', '')
31-
# model = casbin.Model.NewModelFromString(model_string)
32-
# self.assertIsNotNone(model)
32+
def test_NewModelFromString(self):
33+
model_string = """[request_definition]
34+
r = sub, obj, act
35+
36+
[policy_definition]
37+
p = sub, obj, act
38+
39+
[policy_effect]
40+
e = some(where (p.eft == allow))
41+
42+
[matchers]
43+
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act"""
44+
model = casbin.Model.NewModelFromString(model_string)
45+
self.assertIsNotNone(model)
46+
47+
def test_LoadModelFromConfig(self):
48+
# config = casbin.Config(basic_model_path)
49+
# model = casbin.Model.NewModel()
50+
# casbin.LoadModelFromConfig(model, basic_config)
51+
# model = casbin.Model.NewModel()
52+
# config = casbin.Config.NewConfigFromText("")
53+
# model.LoadModelFromConfig(config)
54+
55+
def test_HasSection(self):
56+
# config = casbin.Config.NewConfig(basic_model_path)
57+
# model = casbin.Model.NewModel()
58+
# casbin.LoadModelFromConfig(model, basic_config)
59+

0 commit comments

Comments
 (0)