Beispiel #1
0
    def test_container_tags_update(self, user1, dtype):
        """
        Test updating a Object without losing linked Tags.

        If we load a Object without loading Annotations, then update
        and save the Object, we don't want to lose Annotation links
        """
        conn = get_connection(user1)
        user_name = conn.getUser().getName()
        django_client = self.new_django_client(user_name, user_name)

        container = dtype[1]()
        container.name = rstring('test_container_tags_update')
        tag = TagAnnotationI()
        tag.textValue = rstring('tag')
        container.linkAnnotation(tag)
        container = get_update_service(user1).saveAndReturnObject(container)

        version = api_settings.API_VERSIONS[-1]
        object_url = reverse('api_%s' % dtype[0],
                             kwargs={
                                 'api_version': version,
                                 'object_id': container.id.val
                             })
        save_url = reverse('api_save', kwargs={'api_version': version})
        # Get container, update and save back
        rsp = get_json(django_client, object_url)
        object_json = rsp['data']
        object_json['Name'] = 'renamed container'
        put_json(django_client, save_url, object_json)

        # Check container has been updated and still has annotation links
        proj = conn.getObject(dtype[0], container.id.val)
        assert proj.getName() == 'renamed container'
        assert len(list(proj.listAnnotations())) == 1
Beispiel #2
0
    def test_project_update(self, user1):
        conn = get_connection(user1)
        group = conn.getEventContext().groupId
        user_name = conn.getUser().getName()
        django_client = self.new_django_client(user_name, user_name)

        project = ProjectI()
        project.name = rstring('test_project_update')
        project.description = rstring('Test update')
        project = get_update_service(user1).saveAndReturnObject(project)

        # Update Project in 2 ways...
        version = api_settings.API_VERSIONS[-1]
        project_url = reverse('api_project',
                              kwargs={
                                  'api_version': version,
                                  'object_id': project.id.val
                              })
        save_url = reverse('api_save', kwargs={'api_version': version})
        # 1) Get Project, update and save back
        rsp = get_json(django_client, project_url)
        project_json = rsp['data']
        assert project_json['Name'] == 'test_project_update'
        project_json['Name'] = 'new name'
        rsp = put_json(django_client, save_url, project_json)
        project_json = rsp['data']
        assert project_json['@id'] == project.id.val
        assert project_json['Name'] == 'new name'  # Name has changed
        assert project_json['Description'] == 'Test update'  # No change

        # 2) Put from scratch (will delete empty fields, E.g. Description)
        save_url += '?group=' + str(group)
        payload = {'Name': 'updated name', '@id': project.id.val}
        # Test error message if we don't pass @type:
        rsp = put_json(django_client, save_url, payload, status_code=400)
        assert rsp['message'] == 'Need to specify @type attribute'
        # Add @type and try again
        payload['@type'] = project_json['@type']
        rsp = put_json(django_client, save_url, payload)
        project_json = rsp['data']
        assert project_json['@id'] == project.id.val
        assert project_json['Name'] == 'updated name'
        assert 'Description' not in project_json
        # Get project again to check update
        rsp = get_json(django_client, project_url)
        pr_json = rsp['data']
        assert pr_json['Name'] == 'updated name'
        assert 'Description' not in pr_json
        # Now add description and save again
        pr_json['Description'] = 'New test description update'
        put_json(django_client, save_url, pr_json)
        # Read to check
        rsp = get_json(django_client, project_url)
        assert rsp['data']['Description'] == 'New test description update'
    def test_project_delete(self, user1):
        conn = get_connection(user1)
        user_name = conn.getUser().getName()
        django_client = self.new_django_client(user_name, user_name)

        project = ProjectI()
        project.name = rstring('test_project_delete')
        project.description = rstring('Test update')
        project = get_update_service(user1).saveAndReturnObject(project)
        version = api_settings.API_VERSIONS[-1]
        project_url = reverse('api_project',
                              kwargs={'api_version': version,
                                      'object_id': project.id.val})
        # Before delete, we can read
        pr_json = get_json(django_client, project_url)
        assert pr_json['data']['Name'] == 'test_project_delete'
        # Delete
        delete_json(django_client, project_url, {})
        # Get should now return 404
        rsp = get_json(django_client, project_url, status_code=404)
        assert rsp['message'] == 'Project %s not found' % project.id.val
        # Delete (again) should return 404
        rsp = delete_json(django_client, project_url, {}, status_code=404)
        assert rsp['message'] == 'Project %s not found' % project.id.val
        save_url = reverse('api_save', kwargs={'api_version': version})
        # TODO: Try to save deleted object - should return ApiException
        # see https://trello.com/c/qWNt9vLN/178-save-deleted-object
        with pytest.raises(AssertionError):
            rsp = put_json(django_client, save_url, pr_json, status_code=400)
            assert rsp['message'] == 'Project %s not found' % project.id.val
Beispiel #4
0
 def test_security_violation(self, group_B, user_A):
     """Test saving to incorrect group."""
     conn = get_connection(user_A)
     group_A_id = conn.getEventContext().groupId
     userName = conn.getUser().getName()
     django_client = self.new_django_client(userName, userName)
     version = api_settings.API_VERSIONS[-1]
     group_B_id = group_B.id.val
     save_url = reverse('api_save', kwargs={'api_version': version})
     # Create project in group_A (default group)
     payload = {'Name': 'test_security_violation',
                '@type': OME_SCHEMA_URL + '#Project'}
     save_url_grp_A = save_url + '?group=' + native_str(group_A_id)
     rsp = post_json(django_client, save_url_grp_A, payload,
                     status_code=201)
     pr_json = rsp['data']
     projectId = pr_json['@id']
     # Try to save again into group B
     save_url_grp_B = save_url + '?group=' + native_str(group_B_id)
     rsp = put_json(django_client, save_url_grp_B, pr_json, status_code=403)
     assert 'message' in rsp
     msg = "Cannot read ome.model.containers.Project:Id_%s" % projectId
     assert msg in rsp['message']
     assert rsp['stacktrace'].startswith(
         'Traceback (most recent call last):')
Beispiel #5
0
 def test_save_put_id(self):
     """If PUT to /save/ to update, data must contain @id."""
     django_client = self.django_root_client
     version = api_settings.API_VERSIONS[-1]
     save_url = reverse('api_save', kwargs={'api_version': version})
     payload = {'Name': 'test_save_put_id',
                '@type': '%s#Project' % OME_SCHEMA_URL}
     rsp = put_json(django_client, save_url, payload, status_code=400)
     assert (rsp['message'] ==
             "No '@id' attribute. Use POST to create new objects")
    def test_screen_plates_update(self, user1, screen_plates):
        """Test update of Screen doesn't break links to Plate."""
        conn = get_connection(user1)
        user_name = conn.getUser().getName()
        django_client = self.new_django_client(user_name, user_name)
        version = api_settings.API_VERSIONS[-1]
        screen = screen_plates[0]
        plate_count = len(screen.linkedPlateList())
        screen_url = reverse('api_screen',
                             kwargs={'api_version': version,
                                     'object_id': screen.id.val})
        save_url = reverse('api_save', kwargs={'api_version': version})
        # Get Screen, update and save back
        rsp = get_json(django_client, screen_url)
        screen_json = rsp['data']
        screen_json['Name'] = 'renamed Screen'
        put_json(django_client, save_url, screen_json)

        # Check Screen has been updated and still has child Plates
        scr = conn.getObject('Screen', screen.id.val)
        assert scr.getName() == 'renamed Screen'
        assert len(list(scr.listChildren())) == plate_count
Beispiel #7
0
    def test_project_datasets_update(self, user1, dtype,
                                     project_hierarchy_user1_group1):
        """
        Test updating a Project without losing child Datasets.

        If we load a Project without loading Datasets, then update
        and save the Project, we don't want to lose Dataset links
        """
        conn = get_connection(user1)
        user_name = conn.getUser().getName()
        django_client = self.new_django_client(user_name, user_name)

        if dtype == 'Project':
            parent = project_hierarchy_user1_group1[0]
            child_count = len(parent.linkedDatasetList())
            url_name = 'api_project'
        else:
            parent = project_hierarchy_user1_group1[0].linkedDatasetList()[0]
            child_count = len(parent.linkedImageList())
            url_name = 'api_dataset'

        version = api_settings.API_VERSIONS[-1]
        parent_url = reverse(url_name,
                             kwargs={
                                 'api_version': version,
                                 'object_id': parent.id.val
                             })
        save_url = reverse('api_save', kwargs={'api_version': version})
        # Get Parent, update and save back
        rsp = get_json(django_client, parent_url)
        parent_json = rsp['data']
        parent_json['Name'] = 'renamed %s' % dtype
        put_json(django_client, save_url, parent_json)

        # Check Parent has been updated and still has children linked
        parentWrapper = conn.getObject(dtype, parent.id.val)
        assert parentWrapper.getName() == 'renamed %s' % dtype
        assert len(list(parentWrapper.listChildren())) == child_count
Beispiel #8
0
    def test_container_crud(self, dtype):
        """
        Test create, read, update and delete of Containers.

        Create with POST to /save
        Read with GET of /m/dtype/:id/
        Update with PUT to /m/dtype/:id/
        Delete with DELETE to /m/dtype/:id/
        """
        django_client = self.django_root_client
        group = self.ctx.groupId
        version = api_settings.API_VERSIONS[-1]
        # Need to get the Schema url to create @type
        base_url = reverse('api_base', kwargs={'api_version': version})
        rsp = get_json(django_client, base_url)
        schema_url = rsp['url:schema']
        # specify group via query params
        save_url = "%s?group=%s" % (rsp['url:save'], group)
        project_name = 'test_container_create_read'
        payload = {
            'Name': project_name,
            '@type': '%s#%s' % (schema_url, dtype)
        }
        rsp = post_json(django_client, save_url, payload, status_code=201)
        new_obj = rsp['data']
        # We get the complete new Object returned
        assert new_obj['Name'] == project_name
        object_id = new_obj['@id']

        # Read Object
        object_url = "%sm/%ss/%s/" % (base_url, dtype.lower(), object_id)
        rsp = get_json(django_client, object_url)
        object_json = rsp['data']
        assert object_json['@id'] == object_id
        conn = BlitzGateway(client_obj=self.root)
        assert_objects(conn, [object_json], [object_id], dtype=dtype)

        # Update Object...
        object_json['Name'] = 'new name'
        rsp = put_json(django_client, save_url, object_json)
        # ...and read again to check
        rsp = get_json(django_client, object_url)
        updated_json = rsp['data']
        assert updated_json['Name'] == 'new name'

        # Delete
        delete_json(django_client, object_url, {})
        # Get should now return 404
        rsp = get_json(django_client, object_url, status_code=404)