Skip to content
Snippets Groups Projects
Commit b78fc431 authored by Alexander Kreft's avatar Alexander Kreft
Browse files

test commit

parent d536f89c
No related branches found
No related tags found
2 merge requests!8F set permissions,!6F set permissions
......@@ -25,15 +25,14 @@
As a result, only a specific user or group may access it.
This script assumes that data similar to the demo server of IndiScale (at
demo.indiscale.com) exists on the server specified in the pycaosdb.ini
configuration.
This script assumes that the user specified in the
pycaosdb.ini configuration can create new entities.
"""
import caosdb as db
from caosdb import administration as admin
import lxml
from caosdb.utils.caosdb_admin import do_activate_user, do_retrieve_user, do_retrieve_role, do_create_role, do_add_user_roles, do_retrieve_user_roles
def assert_user_and_role():
......@@ -49,53 +48,35 @@ out : tuple
"""
try:
human_user = admin._retrieve_user("jane")
_activate_user("jane")
except db.ResourceNotFoundError:
human_user = do_retrieve_user(user_name="jane")
do_activate_user(user_name="jane")
except db.HTTPResourceNotFoundError:
human_user = admin._insert_user(
"jane", password="Human_Rememberable_Password_1234", status="ACTIVE")
try:
alien_user = admin._retrieve_user("xaxys")
_activate_user("xaxys")
except db.ResourceNotFoundError:
alien_user = do_retrieve_user(user_name="xaxys")
do_activate_user(user_name="jane")
except db.HTTPResourceNotFoundError:
alien_user = admin._insert_user("xaxys", password="4321_Syxax",
status="ACTIVE")
# At the moment, the return value is only "ok" for successful insertions.
try:
human_role = admin._retrieve_role("human")
except db.ResourceNotFoundError:
human_role = admin._insert_role("human", "An Earthling.")
human_role = do_retrieve_role(role_name="human")
except db.HTTPResourceNotFoundError:
human_role = do_create_role(role_name="human", role_description="An Earthling.")
try:
alien_role = admin._retrieve_role("alien")
except db.ResourceNotFoundError:
alien_role = admin._insert_role("alien", "An Extra-terrestrial.")
alien_role = do_retrieve_role(role_name="alien")
except db.HTTPResourceNotFoundError:
alien_role = do_create_role(role_name="alien", role_description="An Extra-terrestrial.")
admin._set_roles("jane", ["human"])
admin._set_roles("xaxys", ["alien"])
do_add_user_roles(user_name="jane", user_roles=["human"])
do_add_user_roles(user_name="xaxys", user_roles=["alien"])
return (("jane", list(admin._get_roles("jane"))),
("xaxys", list(admin._get_roles("xaxys"))))
def _activate_user(user):
"""Set the user state to "ACTIVE" if necessary.
Parameters
----------
user : str
The user to activate.
Returns
-------
None
"""
user_xml = lxml.etree.fromstring(admin._retrieve_user(user))
if user_xml.xpath("User")[0].attrib["status"] != "ACTIVE":
admin._update_user(user, status="ACTIVE")
return (("jane", list(do_retrieve_user_roles(user_name="jane"))),
("xaxys", list(do_retrieve_user_roles(user_name="xaxys"))))
def get_entities(count=1):
......@@ -111,7 +92,7 @@ Returns
out : Container
A container of retrieved entities, the length is given by the parameter count.
"""
cont = db.execute_query("FIND RECORD Guitar", flags={
cont = db.execute_query("FIND RECORD 'Human Food'", flags={
"P": "0L{n}".format(n=count)})
if len(cont) != count:
raise db.CaosDBException(
......@@ -221,20 +202,37 @@ None
print("Retrieval of all entities was successfully denied.")
def create_entities():
"""Create some test entities.
After calling this function, there will be an RecordType "Human Food" with the corresponding Records
"Bread", "Tomatoes", and "Twinkies".
"""
rt = db.RecordType(name="Human Food", description="Food that can be eaten only by humans").insert()
food = ("Bread", "Tomatoes", "Twinkies")
cont = db.Container()
for i in range(len(food)):
rec = db.Record(food[i])
rec.add_parent(name="Human Food")
cont.append(rec)
cont.insert()
def main():
"""The main function of this script."""
db.connection.connection.get_connection()._login()
"""Create new users"""
human, alien = assert_user_and_role()
# public, private, undefined entities
"""Load the newly created entities."""
entities = get_entities(count=3)
"""Set permission for the entities (only humans are allowed to eat human food)"""
set_permission(human[1][0], alien[1][0], entities)
"""Test the permissions"""
test_permission((human[0], "Human_Rememberable_Password_1234"),
(alien[0], "4321_Syxax"), entities)
if __name__ == "__main__":
main()
# create_entities()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment