def test_loginout(self):
     con = Connection('http://0.0.0.0:3333')
     con.login('*****@*****.**', 'test')
     self.assertIsNotNone(con.token)
     con.logout()
     self.assertIsNone(con.token)
     self.assertRaisesRegex(BaseError, 'KNORA-ERROR: status code=400*', con.login, 'invalid', 'invalid')
Exemple #2
0
    def test_Group_update(self):
        """
        Here we test if we can update an existing group
        :return: None
        """
        con = Connection('http://0.0.0.0:3333')
        con.login('*****@*****.**', 'test')

        group = Group(con=con,
                      name="KNORA-PY UPDATE",
                      description="Test project for knora-py",
                      project="http://rdfh.ch/projects/0001",
                      status=True,
                      selfjoin=False).create()
        self.iri = group.id
        group.name = "KNORA-PY UPDATE - modified"
        group.description = "gaga gaga gaga gaga gaga gaga gaga"
        group.selfjoin = True
        group.status = False
        ngroup = group.update()
        self.assertEqual(group.name, 'KNORA-PY UPDATE - modified')
        self.assertEqual(group.description,
                         'gaga gaga gaga gaga gaga gaga gaga')
        self.assertEqual(group.project, 'http://rdfh.ch/projects/0001')
        self.assertFalse(group.status)
        self.assertTrue(group.selfjoin)
 def test_ListNode_getAllLists(self):
     """
     gett all root nodes, that is all lists
     :return: None
     """
     con = Connection('http://0.0.0.0:3333')
     con.login('*****@*****.**', 'test')
     lists = ListNode.getAllLists(con, 'http://rdfh.ch/projects/0001')
     tmp = list(map(lambda a: a.id, lists))
     self.assertIn('http://rdfh.ch/lists/0001/otherTreeList', tmp)
     self.assertIn('http://rdfh.ch/lists/0001/treeList', tmp)
Exemple #4
0
 def test_getAllGroups(self):
     """
     Test if we can retrieve all groups
     :return: None
     """
     con = Connection('http://0.0.0.0:3333')
     con.login('*****@*****.**', 'test')
     groups = Group.getAllGroups(con)
     self.assertIsNotNone(groups)
     for group in groups:
         group.print()
 def test_ListNode_read(self):
     """
     Read an existing node
     :return: None
     """
     con = Connection('http://0.0.0.0:3333')
     con.login('*****@*****.**', 'test')
     node = ListNode(con=con,
                     id='http://rdfh.ch/lists/0001/otherTreeList').read()
     self.assertEqual(node.id, 'http://rdfh.ch/lists/0001/otherTreeList')
     self.assertEqual(node.project, 'http://rdfh.ch/projects/0001')
     self.assertEqual(node.label['en'], 'Tree list root')
     self.assertTrue(node.isRootNode)
 def test_ListNode_create(self):
     con = Connection('http://0.0.0.0:3333')
     con.login('*****@*****.**', 'test')
     node = ListNode(con=con,
                     project=self.project,
                     label=LangString({Languages.DE: "root node 1"}),
                     comment=LangString({Languages.DE: "first root node"}),
                     name="test_node_1").create()
     self.assertIsNotNone(node.id)
     self.assertEqual(node.project, self.project)
     self.assertEqual(node.label['de'], "root node 1")
     self.assertEqual(node.comment['de'], "first root node")
     self.assertEqual(node.name, "test_node_1")
     self.assertTrue(node.isRootNode)
 def test_ListNode_read2(self):
     """
     read another existing node
     :return:
     """
     con = Connection('http://0.0.0.0:3333')
     con.login('*****@*****.**', 'test')
     node = ListNode(con=con,
                     id='http://rdfh.ch/lists/0001/otherTreeList03').read()
     self.assertEqual(node.id, 'http://rdfh.ch/lists/0001/otherTreeList03')
     self.assertEqual(node.label['en'], 'Other Tree list node 03')
     self.assertFalse(node.isRootNode)
     self.assertEqual(node.rootNodeIri,
                      'http://rdfh.ch/lists/0001/otherTreeList')
     self.assertIsNone(node.children)
Exemple #8
0
    def test_Group_read(self):
        """
        Test if we can read an existing group and retrieve information about it
        :return: None
        """
        con = Connection('http://0.0.0.0:3333')
        con.login('*****@*****.**', 'test')

        group = Group(con=con,
                      id='http://rdfh.ch/groups/0001/thing-searcher').read()
        self.assertEqual(group.name, 'Thing searcher')
        self.assertEqual(group.description, 'A group for thing searchers.')
        self.assertEqual(group.project, 'http://rdfh.ch/projects/0001')
        self.assertTrue(group.status)
        self.assertTrue(group.selfjoin)
Exemple #9
0
    def test_ResourceClass_update(self):
        #
        # Connect to Knora
        #
        con = Connection('http://0.0.0.0:3333')
        con.login('*****@*****.**', 'test')

        #
        # Create a test ontology
        #
        onto = Ontology(
            con=con,
            project=self.project,
            name='resclass-test-onto-2',
            label='resclass test ontology 2',
        ).create()
        last_modification_date = onto.lastModificationDate
        self.assertIsNotNone(onto.id)
        #
        # create test resource class
        #
        last_modification_date, resclass = ResourceClass(
            con=con,
            context=onto.context,
            name=self.name,
            ontology_id=onto.id,
            label=self.label,
            comment=self.comment).create(last_modification_date)
        onto.lastModificationDate = last_modification_date
        self.assertIsNotNone(resclass.id)

        #
        # Modify the resource class
        #
        resclass.addLabel('en', "This is english gaga")
        resclass.rmLabel('de')
        resclass.addComment('it', "Commentario italiano")
        last_modification_date, resclass = resclass.update(
            last_modification_date)
        onto.lastModificationDate = last_modification_date
        self.assertEqual(resclass.label['en'], "This is english gaga")
        self.assertEqual(resclass.comment['it'], "Commentario italiano")

        #
        # Now delete the resource class to clean up
        #
        last_modification_date = resclass.delete(last_modification_date)
        onto.lastModificationDate = last_modification_date
Exemple #10
0
    def test_ListNode_getAllNodes(self):
        """
        Get all node of a list
        :return: None
        """
        con = Connection('http://0.0.0.0:3333')
        con.login('*****@*****.**', 'test')
        root = ListNode(
            con=con,
            id='http://rdfh.ch/lists/0001/otherTreeList').getAllNodes()
        self.assertTrue(root.isRootNode)
        self.assertEqual(root.project, 'http://rdfh.ch/projects/0001')
        self.assertEqual(root.label['en'], 'Tree list root')
        self.assertIsNotNone(root.children)

        self.assertEqual(root.children[0].id,
                         'http://rdfh.ch/lists/0001/otherTreeList01')
        self.assertEqual(root.children[0].name, 'Other Tree list node 01')
        self.assertEqual(root.children[0].label['en'],
                         'Other Tree list node 01')

        self.assertEqual(root.children[1].id,
                         'http://rdfh.ch/lists/0001/otherTreeList02')
        self.assertEqual(root.children[1].name, 'Other Tree list node 02')
        self.assertEqual(root.children[1].label['en'],
                         'Other Tree list node 02')

        self.assertEqual(root.children[2].id,
                         'http://rdfh.ch/lists/0001/otherTreeList03')
        self.assertEqual(root.children[2].name, 'Other Tree list node 03')
        self.assertEqual(root.children[2].label['en'],
                         'Other Tree list node 03')

        self.assertIsNotNone(root.children[2].children)
        self.assertEqual(root.children[2].children[0].id,
                         'http://rdfh.ch/lists/0001/otherTreeList10')
        self.assertEqual(root.children[2].children[0].name,
                         'Other Tree list node 10')
        self.assertEqual(root.children[2].children[0].label['en'],
                         'Other Tree list node 10')

        self.assertEqual(root.children[2].children[1].id,
                         'http://rdfh.ch/lists/0001/otherTreeList11')
        self.assertEqual(root.children[2].children[1].name,
                         'Other Tree list node 11')
        self.assertEqual(root.children[2].children[1].label['en'],
                         'Other Tree list node 11')
    def test_post(self):
        resinfo = """{
            "@type" : "anything:Thing",
            "knora-api:attachedToProject" : {
                "@id" : "http://rdfh.ch/projects/0001"
            },
            "anything:hasBoolean" : {
                "@type" : "knora-api:BooleanValue",
                "knora-api:booleanValueAsBoolean" : true
            },
            "rdfs:label" : "knora-py thing",
            "knora-api:hasPermissions" : "CR knora-admin:Creator|V http://rdfh.ch/groups/0001/thing-searcher",
            "@context" : {
                "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
                "knora-api" : "http://api.knora.org/ontology/knora-api/v2#",
                "rdfs" : "http://www.w3.org/2000/01/rdf-schema#",
                "xsd" : "http://www.w3.org/2001/XMLSchema#",
                "anything" : "http://0.0.0.0:3333/ontology/0001/anything/v2#"
            }
        }
        """
        con = Connection('http://0.0.0.0:3333')
        con.login('*****@*****.**', 'test')
        res = con.post('/v2/resources', resinfo)
        self.assertIsNotNone(res['@id'])
        id = res['@id']
        self.assertEqual(res['@type'], 'anything:Thing')
        self.assertEqual(res['rdfs:label'], 'knora-py thing')

        eraseinfo = """{
            "@id" : "%s",
            "@type" : "anything:Thing",
            "@context" : {
                "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
                "knora-api" : "http://api.knora.org/ontology/knora-api/v2#",
                "rdfs" : "http://www.w3.org/2000/01/rdf-schema#",
                "xsd" : "http://www.w3.org/2001/XMLSchema#",
                "anything" : "http://0.0.0.0:3333/ontology/0001/anything/v2#"
            }
        }
        """ % id
        res = con.post('/v2/resources/erase', eraseinfo)
        self.assertIsNotNone(res['knora-api:result'])
        self.assertEqual(res['knora-api:result'], 'Resource erased')
Exemple #12
0
    def test_Group(self):
        """
        Test the creation of the group instance (without interaction with the triple store...)
        :return: None
        """
        con = Connection('http://0.0.0.0:3333')
        con.login('*****@*****.**', 'test')

        group = Group(con=con,
                      name="KNORA-PY-TEST",
                      description="Test project for knora-py",
                      project="http://rdfh.ch/projects/0001",
                      status=True,
                      selfjoin=False)
        self.assertEqual(group.name, 'KNORA-PY-TEST')
        self.assertEqual(group.description, 'Test project for knora-py')
        self.assertEqual(group.project, 'http://rdfh.ch/projects/0001')
        self.assertTrue(group.status)
        self.assertFalse(group.selfjoin)
Exemple #13
0
 def test_ListNode_update(self):
     """
     Update the data of a node...
     :return:
     """
     con = Connection('http://0.0.0.0:3333')
     con.login('*****@*****.**', 'test')
     node = ListNode(con=con,
                     project=self.project,
                     label=LangString({Languages.DE: "root node 3"}),
                     comment=LangString({Languages.DE: "Third root node"}),
                     name="test_node_3").create()
     node.addLabel('fr', 'Une racine d\' une liste')
     node.rmLabel('de')
     node.addComment('fr', 'un commentaire en français')
     node.rmComment('de')
     node.name = 'GAGAGA'
     node.update()
     self.assertEqual(node.label['fr'], 'Une racine d\' une liste')
     self.assertEqual(node.comment['fr'], 'un commentaire en français')
     self.assertEqual(node.name, 'GAGAGA')
Exemple #14
0
    def test_ResourceClass_create(self):
        #
        # Connect to Knora
        #
        con = Connection('http://0.0.0.0:3333')
        con.login('*****@*****.**', 'test')

        #
        # Create a test ontology
        #
        onto = Ontology(
            con=con,
            project=self.project,
            name='resclass-test-onto-1',
            label='resclass test ontology 1',
        ).create()
        last_modification_date = onto.lastModificationDate
        self.assertIsNotNone(onto.id)
        #
        # Create new resource class
        #
        last_modification_date, resclass = ResourceClass(
            con=con,
            context=onto.context,
            name=self.name,
            ontology_id=onto.id,
            label=self.label,
            comment=self.comment).create(last_modification_date)
        onto.lastModificationDate = last_modification_date
        self.assertIsNotNone(resclass.id)

        self.assertEqual(resclass.name, self.name)
        self.assertEqual(resclass.label['de'], self.label['de'])
        self.assertEqual(resclass.comment['de'], self.comment['de'])

        #
        # Delete the new resource class
        #
        last_modification_date = resclass.delete(last_modification_date)
        onto.lastModificationDate = last_modification_date
Exemple #15
0
    def test_Group_create(self):
        """
        Test if we can create a new group in the triple store
        :return: None
        """
        global __iri
        con = Connection('http://0.0.0.0:3333')
        con.login('*****@*****.**', 'test')

        group = Group(con=con,
                      name="KNORA-PY CREATE",
                      description="Test project for knora-py",
                      project="http://rdfh.ch/projects/0001",
                      status=True,
                      selfjoin=False).create()
        self.iri = group.id

        self.assertEqual(group.name, 'KNORA-PY CREATE')
        self.assertEqual(group.description, 'Test project for knora-py')
        self.assertEqual(group.project, 'http://rdfh.ch/projects/0001')
        self.assertTrue(group.status)
        self.assertFalse(group.selfjoin)
Exemple #16
0
    def test_Group_delete(self):
        """
        Here we test if we can mark an existing group as deleted (it will not be deleted completely
        from the triplestore, but marked!!)
        :return: None
        """
        con = Connection('http://0.0.0.0:3333')
        con.login('*****@*****.**', 'test')

        group = Group(con=con,
                      name="KNORA-PY DELETE",
                      description="Test project for knora-py",
                      project="http://rdfh.ch/projects/0001",
                      status=True,
                      selfjoin=False).create()
        self.iri = group.id
        ngroup = group.delete()
        self.assertEqual(ngroup.name, 'KNORA-PY DELETE')
        self.assertEqual(ngroup.description, 'Test project for knora-py')
        self.assertEqual(ngroup.project, 'http://rdfh.ch/projects/0001')
        self.assertFalse(ngroup.status)
        self.assertFalse(ngroup.selfjoin)
Exemple #17
0
 def test_ListNode_hierarchy(self):
     """
     Create a node and a sub-node
     :return: None
     """
     con = Connection('http://0.0.0.0:3333')
     con.login('*****@*****.**', 'test')
     node = ListNode(con=con,
                     project=self.project,
                     label=LangString({Languages.DE: "root node 2"}),
                     comment=LangString({Languages.DE: "second root node"}),
                     name="test_node_2").create()
     node2 = ListNode(
         con=con,
         project=self.project,
         label=LangString({Languages.DE: 'Eine Knoten der Liste'}),
         comment=LangString({Languages.DE: "So ein Kommentar"}),
         name="NODE2",
         parent=node).create()
     self.assertEqual(node2.label['de'], 'Eine Knoten der Liste')
     self.assertEqual(node2.comment['de'], "So ein Kommentar")
     self.assertEqual(node2.name, "NODE2")
     self.assertFalse(node2.isRootNode)
Exemple #18
0
 def test_getAllUsers(self):
     con = Connection('http://0.0.0.0:3333')
     con.login('*****@*****.**', 'test')
     all_users = User.getAllUsers(con)
     for u in all_users:
         self.assertIsNotNone(u.id)
Exemple #19
0
def xml_upload(input_file: str,
               server: str,
               user: str,
               password: str,
               imgdir: str,
               sipi: str,
               verbose: bool) -> bool:
    current_dir = os.path.dirname(os.path.realpath(__file__))

    xmlschema_doc = etree.parse(os.path.join(current_dir, 'knora-data-schema.xsd'))
    xmlschema = etree.XMLSchema(xmlschema_doc)
    doc = etree.parse(input_file)
    xmlschema.assertValid(doc)

    del xmlschema
    del doc
    del xmlschema_doc

    print("The input data file is syntactically correct and passed validation!")

    #
    # Connect to the DaSCH Service Platform API
    #
    con = Connection(server)
    con.login(user, password)

    proj_context = ProjectContext(con=con)

    #
    # read the XML file containing the data, including project shortcode
    #
    context: etree.iterparse = etree.iterparse(input_file, events=("start", "end"))
    resources: List[KnoraResource] = []
    permissions: Dict[str, XmlPermission] = {}
    shortcode: Union[str, None] = None
    default_ontology = None
    while True:
        event, node = next(context)
        if event == 'start':
            if node.tag == 'knora':
                default_ontology = node.attrib['default-ontology']
                shortcode = node.attrib['shortcode']
                proj_context.shortcode = shortcode
            elif event == 'start' and node.tag == 'resource':
                resources.append(KnoraResource(context, node, default_ontology))
            elif event == 'start' and node.tag == 'permissions':
                permission = XmlPermission(context, node, proj_context)
                permissions[permission.id] = permission
        elif event == 'end':
            if node.tag == 'knora':
                break

    #
    # sort the resources so that resources which do not link to others come first
    #
    resources = do_sortorder(resources)

    sipi = Sipi(sipi, con.get_token())

    factory = ResourceInstanceFactory(con, shortcode)

    permissions_lookup: Dict[str, Permissions] = {}
    for key, perm in permissions.items():
        permissions_lookup[key] = perm.get_permission_instance()

    resclassnames = factory.get_resclass_names()
    resclasses: Dict[str, type] = {}
    for resclassname in resclassnames:
        resclasses[resclassname] = factory.get_resclass(resclassname)
    resiri_lookup: StrDict = {}

    for resource in resources:
        if resource.image:
            img = sipi.upload_image(os.path.join(imgdir, resource.image))
            stillimage = img['uploadedFiles'][0]['internalFilename']
        else:
            stillimage = None
        instance = resclasses[resource.restype](con=con,
                                                label=resource.label,
                                                permissions=permissions_lookup.get(resource.permissions),
                                                stillimage=stillimage,
                                                values=resource.get_propvals(resiri_lookup, permissions_lookup)).create()
        resiri_lookup[resource.id] = instance.iri
        print("Created:", instance.iri)
class TestPropertyClass(unittest.TestCase):
    project = "http://rdfh.ch/projects/0001"
    onto_name = 'propclass-test'
    onto_label = 'propclass_test_ontology'

    onto: Ontology
    last_modification_date: LastModificationDate
    con: Connection

    name = 'MyPropClassName'
    object = 'TextValue'
    label = LangString({Languages.DE: 'MyPropClassLabel'})
    comment = LangString(
        {Languages.DE: 'This is a property class for testing'})

    def setUp(self) -> None:
        #
        # Connect to Knora
        #
        self.con = Connection('http://0.0.0.0:3333')
        self.con.login('*****@*****.**', 'test')

        #
        # Create a test ontology
        #
        self.onto = Ontology(
            con=self.con,
            project=self.project,
            name=self.onto_name,
            label=self.onto_label,
        ).create()
        self.assertIsNotNone(self.onto.id)
        self.last_modification_date = self.onto.lastModificationDate

    def tearDown(self):
        #
        # remove test ontology
        #
        result = self.onto.delete()
        self.assertIsNotNone(result)

    def test_PropertyClass_create(self):
        #
        # Create new property class
        #
        self.last_modification_date, propclass = PropertyClass(
            con=self.con,
            context=self.onto.context,
            name=self.name,
            ontology_id=self.onto.id,
            object=self.object,
            label=self.label,
            comment=self.comment).create(self.last_modification_date)
        self.onto.lastModificationDate = self.last_modification_date
        self.assertIsNotNone(propclass.id)

        self.assertEqual(propclass.name, self.name)
        self.assertEqual(propclass.label['de'], self.label['de'])
        self.assertEqual(propclass.comment['de'], self.comment['de'])

        #
        # Again get ontology data
        #
        self.onto = self.onto.read()
        self.last_modification_date = self.onto.lastModificationDate
        self.last_modification_date = propclass.delete(
            self.last_modification_date)

        #
        # Again get ontology data
        #
        self.onto = self.onto.read()

    def test_PropertyClass_update(self):
        self.onto = self.onto.read()

        #
        # create test resource class
        #
        self.last_modification_date, propclass = PropertyClass(
            con=self.con,
            context=self.onto.context,
            name=self.name,
            ontology_id=self.onto.id,
            object=self.object,
            label=self.label,
            comment=self.comment).create(self.last_modification_date)
        self.onto.lastModificationDate = self.last_modification_date
        self.assertIsNotNone(propclass.id)

        #
        # Modify the property class
        #
        propclass.addLabel('en', "This is english gaga")
        propclass.rmLabel('de')
        propclass.addComment('it', "Commentario italiano")
        self.last_modification_date, propclass = propclass.update(
            self.last_modification_date)
        self.onto.lastModificationDate = self.last_modification_date
        self.assertEqual(propclass.label['en'], "This is english gaga")
        self.assertEqual(propclass.comment['it'], "Commentario italiano")

        #
        # Now delete the resource class to clean up
        #
        self.last_modification_date = propclass.delete(
            self.last_modification_date)
        self.onto.lastModificationDate = self.last_modification_date
Exemple #21
0
sys.path.append(os.path.dirname(os.path.realpath(__file__)))

from dsplib.models.connection import Connection
from dsplib.models.resource import ResourceInstanceFactory
from dsplib.models.value import BooleanValue, ColorValue, DateValue, DecimalValue, IntValue, IntervalValue, TextValue, \
    UriValue, KnoraStandoffXml, make_value
from dsplib.models.permission import PermissionValue, Permissions, PermissionsIterator
from dsplib.models.sipi import Sipi
from pprint import pprint

#
# Connect to server and make a login
#
con = Connection('http://0.0.0.0:3333')
con.login('*****@*****.**', 'test')

#
# Make class factory for project 'anything. The factory creates classes that implement the CRUD methods
# for the given resource classes, that is to create, read, update and delete instances (=resources) of the given classesd
#
factory = ResourceInstanceFactory(con, 'anything')
resclassnames = factory.get_resclass_names()

#
# Get an python class of a BlueThing resource class
#
BlueThing = factory.get_resclass('anything:BlueThing')

print("====================================================")
an_old_thing = BlueThing(con=con, iri="http://rdfh.ch/0001/H6gBWUuJSuuO-CilHV8kQw").read()
Exemple #22
0
class TestOntology(unittest.TestCase):
    project = "http://rdfh.ch/projects/0001"
    label = 'test_ontology'
    last_modification_date: LastModificationDate

    def setUp(self) -> None:
        self.con = Connection('http://0.0.0.0:3333')
        self.con.login('*****@*****.**', 'test')

    def test_ontology(self):
        onto = Ontology(con=self.con,
                        project=self.project,
                        name="test_ontology_0",
                        label="test ontology 0",
                        lastModificationDate="2017-12-19T15:23:42.166Z")
        self.assertEqual(onto.project, self.project)
        self.assertEqual(onto.label, "test ontology 0")
        self.assertEqual(onto.lastModificationDate,
                         LastModificationDate("2017-12-19T15:23:42.166Z"))

    def test_read(self):
        onto = Ontology(
            con=self.con,
            id='http://0.0.0.0:3333/ontology/0001/anything/v2').read()
        self.assertEqual(onto.id,
                         "http://0.0.0.0:3333/ontology/0001/anything/v2")
        self.assertEqual(onto.project, "http://rdfh.ch/projects/0001")
        self.assertEqual(onto.label, "The anything ontology")
        self.assertIsNotNone(onto.lastModificationDate)

    def test_create(self):
        onto = Ontology(
            con=self.con,
            project=self.project,
            name="testonto_1",
            label="test ontology 1",
        ).create()
        self.assertIsNotNone(onto.id)
        self.assertEqual(
            onto.id,
            'http://0.0.0.0:3333/ontology/0001/' + "testonto_1" + '/v2')
        self.assertEqual(onto.label, "test ontology 1")
        self.assertEqual(onto.project, self.project)

    def test_update(self):
        onto = Ontology(
            con=self.con,
            project=self.project,
            name="testonto_2",
            label="test ontology 2",
        ).create()
        self.assertIsNotNone(onto.id)
        onto.label = 'This is a modified label!'
        onto = onto.update()
        self.assertEqual(onto.label, 'This is a modified label!')

    def test_delete(self):
        onto = Ontology(
            con=self.con,
            project=self.project,
            name="testonto_3",
            label="test ontology 3",
        ).create()
        self.assertIsNotNone(onto.id)
        res = onto.delete()
        self.assertIsNotNone(res)

    def test_get_ontologies_of_project(self):
        ontolist = Ontology.getProjectOntologies(self.con, self.project)
        ontolist_ids = [x.id for x in ontolist]
        self.assertIn('http://0.0.0.0:3333/ontology/0001/anything/v2',
                      ontolist_ids)
        self.assertIn('http://0.0.0.0:3333/ontology/0001/minimal/v2',
                      ontolist_ids)
        self.assertIn('http://0.0.0.0:3333/ontology/0001/something/v2',
                      ontolist_ids)
Exemple #23
0
class TestAllClass(unittest.TestCase):
    project = "http://rdfh.ch/projects/0001"
    onto_name = 'all-test'
    onto_label = 'all_test_ontology'

    resclass_name = 'MyResClassName'
    resclass_label = LangString({Languages.DE: 'MyResClassLabel'})
    resclass_comment = LangString(
        {Languages.DE: 'This is a resource class for testing'})

    propclass_name = 'MyPropClassName'
    propclass_object = 'TextValue'
    propclass_label = LangString({Languages.DE: 'MyPropClassLabel'})
    propclass_comment = LangString(
        {Languages.DE: 'This is a property class for testing'})

    con: Connection
    onto: Ontology
    last_modification_date: LastModificationDate

    def setUp(self) -> None:
        #
        # Connect to Knora
        #
        self.con = Connection('http://0.0.0.0:3333')
        self.con.login('*****@*****.**', 'test')

        #
        # Create a test ontology
        #
        self.onto = Ontology(
            con=self.con,
            project=self.project,
            name=self.onto_name,
            label=self.onto_label,
        ).create()
        self.last_modification_date = self.onto.lastModificationDate
        self.assertIsNotNone(self.onto.id)

    def tearDown(self):
        #
        # remove test ontology
        #
        result = self.onto.delete()
        self.assertIsNotNone(result)

    def test_AllThings_create(self):
        #
        # Create new resource class
        #
        self.last_modification_date, resclass = ResourceClass(
            con=self.con,
            context=self.onto.context,
            name=self.resclass_name,
            ontology_id=self.onto.id,
            label=self.resclass_label,
            comment=self.resclass_comment).create(self.last_modification_date)
        self.onto.lastModificationDate = self.last_modification_date
        self.assertIsNotNone(resclass.id)

        self.assertEqual(resclass.name, self.resclass_name)
        self.assertEqual(resclass.label['de'], self.resclass_label['de'])
        self.assertEqual(resclass.comment['de'], self.resclass_comment['de'])

        #
        # Create new property class
        #
        self.last_modification_date, propclass = PropertyClass(
            con=self.con,
            context=self.onto.context,
            name=self.propclass_name,
            ontology_id=self.onto.id,
            object=self.propclass_object,
            label=self.propclass_label,
            comment=self.propclass_comment).create(self.last_modification_date)
        self.onto.lastModificationDate = self.last_modification_date
        self.assertIsNotNone(propclass.id)

        self.assertEqual(propclass.name, self.propclass_name)
        self.assertEqual(propclass.label['de'], self.propclass_label['de'])
        self.assertEqual(propclass.comment['de'], self.propclass_comment['de'])

        #
        # Create HasProperty (cardinality)
        #
        self.last_modification_date = resclass.addProperty(
            propclass.id, Cardinality.C_1, self.last_modification_date)
        self.onto.lastModificationDate = self.last_modification_date
        self.assertEqual(
            resclass.getProperty(propclass.id).cardinality, Cardinality.C_1)
        self.assertIsNotNone(self.last_modification_date)

        #
        # Modify HasProperty (cardinality)
        #
        self.last_modification_date = resclass.updateProperty(
            propclass.id, Cardinality.C_1_n, self.last_modification_date)
        self.onto.lastModificationDate = self.last_modification_date
        self.assertEqual(
            resclass.getProperty(propclass.id).cardinality, Cardinality.C_1_n)
        self.assertIsNotNone(self.last_modification_date)
Exemple #24
0
class OpenConnectionDialog(wx.Dialog):
    """
    This open a dialog which allows the user to select a server and to
    give the username and password
    """
    def __init__(self, *args, **kw):
        super(OpenConnectionDialog,
              self).__init__(*args,
                             **kw,
                             title="Open connection...",
                             style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)

        topsizer = wx.BoxSizer(wx.VERTICAL)

        panel1 = wx.Panel(self)
        l0 = wx.StaticText(panel1, label="Server: ")
        server = wx.TextCtrl(panel1,
                             name="server",
                             value="http://0.0.0.0:3333",
                             size=wx.Size(200, -1))

        l1 = wx.StaticText(panel1, label="Username: "******"username",
                               value="*****@*****.**",
                               size=wx.Size(200, -1))
        l2 = wx.StaticText(panel1, label="Password: "******"password",
                               value="test",
                               size=wx.Size(200, -1),
                               style=wx.TE_PASSWORD)
        gsizer = wx.GridSizer(cols=2)
        gsizer.Add(l0,
                   flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL,
                   border=3)
        gsizer.Add(server,
                   wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL,
                   border=3)
        gsizer.Add(l1,
                   flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL,
                   border=3)
        gsizer.Add(username,
                   wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL,
                   border=3)
        gsizer.Add(l2,
                   flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL,
                   border=3)
        gsizer.Add(password,
                   flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL,
                   border=3)
        gsizer.SetSizeHints(panel1)
        panel1.SetSizer(gsizer)
        panel1.SetAutoLayout(1)
        gsizer.Fit(panel1)

        topsizer.Add(panel1, flag=wx.EXPAND | wx.ALL, border=5)

        bsizer = self.CreateStdDialogButtonSizer(wx.OK | wx.CANCEL)
        topsizer.Add(bsizer, flag=wx.EXPAND | wx.ALL, border=5)

        self.SetSizerAndFit(topsizer)

        self.ShowModal()
        if self.GetReturnCode() == wx.ID_OK:
            server_str = server.GetLineText(0)
            username_str = username.GetLineText(0)
            password_str = password.GetLineText(0)
            self.con = Connection(server_str)
            self.con.login(username_str, password_str)

        else:
            print("CANCEL PRESSED")

    def get_res(self):
        return self.con
Exemple #25
0
class TestUser(unittest.TestCase):
    def setUp(self) -> None:
        self.con = Connection('http://0.0.0.0:3333')
        self.con.login('*****@*****.**', 'test')

    def test_user(self):
        user = User(con=self.con,
                    username='******',
                    email='*****@*****.**',
                    givenName='Wile E.',
                    familyName='Coyote',
                    password='******',
                    lang=Languages.EN,
                    status=True,
                    sysadmin=True,
                    in_projects={"http://rdfh.ch/projects/0001": True},
                    in_groups={"http://rdfh.ch/groups/00FF/images-reviewer"})
        self.assertEqual(user.username, 'wilee')
        self.assertEqual(user.email, '*****@*****.**')
        self.assertEqual(user.givenName, 'Wile E.')
        self.assertEqual(user.familyName, 'Coyote')
        self.assertTrue(user.status)
        self.assertEqual(user.lang, Languages.EN)
        self.assertTrue(user.sysadmin)
        self.assertEqual(user.in_projects,
                         {"http://rdfh.ch/projects/0001": True})
        self.assertEqual(user.in_groups,
                         {"http://rdfh.ch/groups/00FF/images-reviewer"})

    def test_User_read(self):
        user = User(con=self.con, id='http://rdfh.ch/users/91e19f1e01').read()
        self.assertEqual(user.id, 'http://rdfh.ch/users/91e19f1e01')
        self.assertEqual(user.username, 'root-alt')
        self.assertEqual(user.familyName, 'Admin-alt')
        self.assertEqual(user.givenName, 'Administrator-alt')
        self.assertEqual(user.lang, Languages.DE)
        self.assertTrue(user.status)
        self.assertFalse(user.sysadmin)
        self.assertEqual(user.in_projects,
                         {"http://rdfh.ch/projects/0803": False})

    def test_User_create(self):
        user = User(con=self.con,
                    username='******',
                    email='*****@*****.**',
                    givenName='Wile E.',
                    familyName='Coyote',
                    password='******',
                    lang=Languages.EN,
                    status=True,
                    sysadmin=True,
                    in_projects={
                        "http://rdfh.ch/projects/0001": True
                    },
                    in_groups={"http://rdfh.ch/groups/00FF/images-reviewer"
                               }).create()
        self.assertEqual(user.username, 'wilee')
        self.assertEqual(user.email, '*****@*****.**')
        self.assertEqual(user.givenName, 'Wile E.')
        self.assertEqual(user.familyName, 'Coyote')
        self.assertTrue(user.status)
        self.assertEqual(user.lang, Languages.EN)
        self.assertTrue(user.sysadmin)
        self.assertEqual(user.in_projects,
                         {"http://rdfh.ch/projects/0001": True})
        self.assertEqual(user.in_groups,
                         {"http://rdfh.ch/groups/00FF/images-reviewer"})

    def test_User_read2(self):
        user = User(con=self.con,
                    username='******',
                    email='*****@*****.**',
                    givenName='Wile E.2',
                    familyName='Coyote2',
                    password='******',
                    lang=Languages.EN,
                    status=True,
                    sysadmin=True,
                    in_projects={
                        "http://rdfh.ch/projects/0001": True
                    },
                    in_groups={"http://rdfh.ch/groups/00FF/images-reviewer"
                               }).create()
        user = User(con=self.con, email='*****@*****.**').read()
        self.assertEqual(user.username, 'wilee2')
        self.assertEqual(user.familyName, 'Coyote2')
        self.assertEqual(user.givenName, 'Wile E.2')
        self.assertEqual(user.lang, Languages.EN)
        self.assertTrue(user.status)
        self.assertTrue(user.sysadmin)
        self.assertEqual(user.in_projects,
                         {"http://rdfh.ch/projects/0001": True})
        self.assertEqual(user.in_groups,
                         {"http://rdfh.ch/groups/00FF/images-reviewer"})

    def test_User_delete(self):
        user = User(con=self.con,
                    username='******',
                    email='*****@*****.**',
                    givenName='Wile E.3',
                    familyName='Coyote3',
                    password='******',
                    lang=Languages.EN,
                    status=True,
                    sysadmin=True,
                    in_projects={
                        "http://rdfh.ch/projects/0001": True
                    },
                    in_groups={"http://rdfh.ch/groups/00FF/images-reviewer"
                               }).create()
        nuser = user.delete()
        self.assertIsNotNone(nuser)
        self.assertFalse(nuser.status)

    def test_User_update(self):
        user = User(con=self.con,
                    username='******',
                    email='*****@*****.**',
                    givenName='Wile E.4',
                    familyName='Coyote4',
                    password='******',
                    lang=Languages.EN,
                    status=True,
                    sysadmin=True,
                    in_projects={
                        "http://rdfh.ch/projects/0001": True
                    },
                    in_groups={"http://rdfh.ch/groups/00FF/images-reviewer"
                               }).create()
        user.email = '*****@*****.**'
        user.username = '******'
        user.givenName = 'roadrunner'
        user.familyName = 'Geococcyx'
        user.lang = 'fr'
        user.status = False
        user.sysadmin = False
        nuser = user.update()
        self.assertEqual(nuser.email, '*****@*****.**')
        self.assertEqual(nuser.username, 'roadrunner')
        self.assertEqual(nuser.givenName, 'roadrunner')
        self.assertEqual(nuser.familyName, 'Geococcyx')
        self.assertEqual(nuser.lang, Languages.FR)
        self.assertFalse(nuser.status)
        self.assertFalse(nuser.sysadmin)

    def test_User_update2(self):
        user = User(con=self.con,
                    username='******',
                    email='*****@*****.**',
                    givenName='Wile E.5',
                    familyName='Coyote5',
                    password='******',
                    lang=Languages.EN,
                    status=True,
                    sysadmin=True,
                    in_projects={
                        "http://rdfh.ch/projects/0001": True
                    },
                    in_groups={"http://rdfh.ch/groups/00FF/images-reviewer"
                               }).create()
        user.password = '******'
        nuser = user.update('test')
        self.assertIsNotNone(nuser)

    def test_User_addToGroup(self):
        return  #  TODO: Check why this test failes with error: Message:{"error":"org.knora.webapi.UpdateNotPerformedException: User's 'group' memberships where not updated. Please report this as a possible bug."}
        user = self.createTestUser()
        user.addToGroup('http://rdfh.ch/groups/0001/thing-searcher')
        nuser = user.update()
        self.assertIsNotNone(nuser)
        self.assertEqual(
            nuser.in_groups, {
                "http://rdfh.ch/groups/00FF/images-reviewer",
                'http://rdfh.ch/groups/0001/thing-searcher'
            })

    def test_User_rmFromGroup(self):
        user = User(con=self.con,
                    username='******',
                    email='*****@*****.**',
                    givenName='Wile E.6',
                    familyName='Coyote6',
                    password='******',
                    lang=Languages.EN,
                    status=True,
                    sysadmin=True,
                    in_projects={
                        "http://rdfh.ch/projects/0001": True
                    },
                    in_groups={"http://rdfh.ch/groups/00FF/images-reviewer"
                               }).create()
        user.rmFromGroup('http://rdfh.ch/groups/00FF/images-reviewer')
        nuser = user.update()
        self.assertIsNotNone(nuser)

    def test_User_addToProject(self):
        user = User(con=self.con,
                    username='******',
                    email='*****@*****.**',
                    givenName='Wile E.7',
                    familyName='Coyote7',
                    password='******',
                    lang=Languages.EN,
                    status=True,
                    sysadmin=True,
                    in_projects={
                        "http://rdfh.ch/projects/0001": True
                    },
                    in_groups={"http://rdfh.ch/groups/00FF/images-reviewer"
                               }).create()
        user.addToProject('http://rdfh.ch/projects/00FF', False)
        nuser = user.update()
        self.assertIsNotNone(nuser)
        self.assertEqual(
            nuser.in_projects, {
                "http://rdfh.ch/projects/0001": True,
                'http://rdfh.ch/projects/00FF': False
            })

    def test_User_rmFromProject(self):
        user = User(con=self.con,
                    username='******',
                    email='*****@*****.**',
                    givenName='Wile E.8',
                    familyName='Coyote8',
                    password='******',
                    lang=Languages.EN,
                    status=True,
                    sysadmin=True,
                    in_projects={
                        "http://rdfh.ch/projects/0001": True
                    },
                    in_groups={"http://rdfh.ch/groups/00FF/images-reviewer"
                               }).create()
        user.rmFromProject('http://rdfh.ch/projects/0001')
        nuser = user.update()
        self.assertIsNotNone(nuser)
        self.assertEqual(nuser.in_projects, {})

    def test_User_unmakeProjectAdmin(self):
        user = User(con=self.con,
                    username='******',
                    email='*****@*****.**',
                    givenName='Wile E.9',
                    familyName='Coyote9',
                    password='******',
                    lang=Languages.EN,
                    status=True,
                    sysadmin=True,
                    in_projects={
                        "http://rdfh.ch/projects/0001": True
                    },
                    in_groups={"http://rdfh.ch/groups/00FF/images-reviewer"
                               }).create()
        user.unmakeProjectAdmin('http://rdfh.ch/projects/0001')
        nuser = user.update()
        self.assertIsNotNone(nuser)
        self.assertEqual(nuser.in_projects,
                         {'http://rdfh.ch/projects/0001': False})

    def test_User_makeProjectAdmin(self):
        user = User(con=self.con,
                    username='******',
                    email='*****@*****.**',
                    givenName='Wile E.A',
                    familyName='CoyoteA',
                    password='******',
                    lang=Languages.EN,
                    status=True,
                    sysadmin=True,
                    in_projects={
                        "http://rdfh.ch/projects/0001": True
                    },
                    in_groups={"http://rdfh.ch/groups/00FF/images-reviewer"
                               }).create()
        user.addToProject('http://rdfh.ch/projects/00FF', False)
        user = user.update()
        user.makeProjectAdmin('http://rdfh.ch/projects/00FF')
        nuser = user.update()
        self.assertIsNotNone(nuser)
        self.assertEqual(
            nuser.in_projects, {
                'http://rdfh.ch/projects/0001': True,
                'http://rdfh.ch/projects/00FF': True
            })

    def test_getAllUsers(self):
        con = Connection('http://0.0.0.0:3333')
        con.login('*****@*****.**', 'test')
        all_users = User.getAllUsers(con)
        for u in all_users:
            self.assertIsNotNone(u.id)