Exemplo n.º 1
0
def test_clear_update(app):
    """Test Community.update(clear_fields=True)."""
    with app.app_context():
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        db.session.commit()

    with app.app_context():
        retrieved = Community.get(id=community_id)
        retrieved.update(community_update, clear_fields=True)
        # test invalid update
        with pytest.raises(InvalidCommunityError):
            retrieved.update({}, clear_fields=True)
        db.session.commit()

    with app.app_context():
        updated = Community.get(id=community_id)
        assert community_id == updated.id
        assert updated.updated
        for field, value in updated_community_metadata.items():
            if field not in community_update:
                if field == 'publication_workflow':
                    assert updated.publication_workflow == \
                        'review_and_publish'
                elif field == 'restricted_submission':
                    assert updated.restricted_submission == False
                else:
                    assert getattr(updated, field) is None
            else:
                assert getattr(updated, field) == value
Exemplo n.º 2
0
def test_valid_delete(app, login_user, communities_permissions):
    """Test VALID community delete request (DELETE .../communities/<id>)."""
    with app.app_context():
        # create community
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        # create allowed user
        allowed_user = create_user('allowed')
        communities_permissions(allowed_user.id).delete_permission(
            True, community_id)
        db.session.commit()

    with app.app_context():
        with app.test_client() as client:
            login_user(allowed_user, client)
            headers = [('Content-Type', 'application/json'),
                       ('Accept', 'application/json')]
            res = client.delete(url_for('b2share_communities.communities_item',
                                        community_id=community_id),
                                headers=headers)
            assert res.status_code == 204

    with app.app_context():
        # check database state
        with pytest.raises(CommunityDeletedError):
            Community.get(community_id)
        community = Community.get(community_id, with_deleted=True)
        assert community.deleted
        # check that one community has been created
        assert len(CommunityModel.query.all()) == 1
Exemplo n.º 3
0
def test_block_schemas(app):
    """Test valid usage of the BlockSchema API."""
    with app.app_context():
        new_community = Community.create_community(**communities_metadata[0])
        new_community_id = new_community.id
        db.session.commit()

        block_schema_name = "schema 1"
        block_schema_1 = BlockSchema.create_block_schema(
            community_id=new_community.id, name=block_schema_name)
        block_schema_1_id = block_schema_1.id
        db.session.commit()

    with app.app_context():
        retrieved_block_schema = BlockSchema.get_block_schema(
            block_schema_1_id)
        assert retrieved_block_schema.name == block_schema_name
        assert retrieved_block_schema.community == new_community_id
        # test changing the community maintaining the block schema
        new_community_2 = Community.create_community(**communities_metadata[1])
        retrieved_block_schema.community = new_community_2.id
        assert retrieved_block_schema.community == new_community_2.id
        # test setting the schema name
        retrieved_block_schema.name = new_name = 'new name'
        assert retrieved_block_schema.name == new_name
Exemplo n.º 4
0
def test_update(app):
    """Test Community.update()."""
    with app.app_context():
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        db.session.commit()

    with app.app_context():
        retrieved = Community.get(id=community_id)
        retrieved.update(community_update)
        db.session.commit()

    with app.app_context():
        updated = Community.get(id=community_id)
        assert community_id == updated.id
        assert updated.updated
        for field, value in updated_community_metadata.items():
            assert getattr(updated, field) == value

        # test invalid update
        with pytest.raises(InvalidCommunityError):
            updated.update({'name': None})
        # check that the community was not modified
        for field, value in updated_community_metadata.items():
            assert getattr(updated, field) == value
Exemplo n.º 5
0
def test_clear_update(app):
    """Test Community.update(clear_fields=True)."""
    with app.app_context():
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        db.session.commit()

    with app.app_context():
        retrieved = Community.get(id=community_id)
        retrieved.update(community_update, clear_fields=True)
        # test invalid update
        with pytest.raises(InvalidCommunityError):
            retrieved.update({}, clear_fields=True)
        db.session.commit()

    with app.app_context():
        updated = Community.get(id=community_id)
        assert community_id == updated.id
        assert updated.updated
        for field, value in updated_community_metadata.items():
            if field not in community_update:
                if field == 'publication_workflow':
                    assert updated.publication_workflow == \
                        'review_and_publish'
                elif field == 'restricted_submission':
                    assert updated.restricted_submission == False
                else:
                    assert getattr(updated, field) is None
            else:
                assert getattr(updated, field) == value
Exemplo n.º 6
0
def test_update(app):
    """Test Community.update()."""
    with app.app_context():
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        db.session.commit()

    with app.app_context():
        retrieved = Community.get(id=community_id)
        retrieved.update(community_update)
        db.session.commit()

    with app.app_context():
        updated = Community.get(id=community_id)
        assert community_id == updated.id
        assert updated.updated
        for field, value in updated_community_metadata.items():
            assert getattr(updated, field) == value

        # test invalid update
        with pytest.raises(InvalidCommunityError):
            updated.update({'name': None})
        # check that the community was not modified
        for field, value in updated_community_metadata.items():
            assert getattr(updated, field) == value
Exemplo n.º 7
0
def _create_communities(path, verbose):
    """Create demo communities."""
    if verbose > 0:
        click.secho('Creating communities', fg='yellow', bold=True)
    with db.session.begin_nested():
        communities_dir = os.path.join(path, 'communities')
        communities = dict()
        nb_communities = 0
        for filename in sorted(os.listdir(communities_dir)):
            if os.path.splitext(filename)[1] == '.json':
                with open(os.path.join(communities_dir,
                                       filename)) as json_file:
                    json_config = json.loads(json_file.read())
                    workflow = json_config.get('publication_workflow',
                                               'review_and_publish')
                    is_restricted = json_config.get('restricted_submission',
                                                    False)
                    community = Community.create_community(
                        name=json_config['name'],
                        description=json_config['description'],
                        logo=json_config['logo'],
                        publication_workflow=workflow,
                        restricted_submission=is_restricted,
                        id_=UUID(json_config['id']),
                    )
                    if verbose > 1:
                        click.secho('Created community {0} with ID {1}'.format(
                            community.name, community.id))
                    communities[community.name] = DemoCommunity(
                        community, json_config)
                    nb_communities += 1
    if verbose > 0:
        click.secho('Created {} communities!'.format(nb_communities),
                    fg='green')
    return communities
def test_invalid_get(app, login_user,
                     communities_permissions):
    """Test INVALID community get request (GET .../communities/<id>)."""
    with app.app_context():
        # create user allowed to GET all records
        allowed_user = create_user('allowed')
        communities_permissions(
            allowed_user.id).read_permission(True, None)
        db.session.commit()

    with app.app_context():
        with app.test_client() as client:
            login_user(allowed_user, client)
            unknown_uuid = uuid.uuid4()
            # check that GET with non existing id will return 404
            headers = [('Content-Type', 'application/json'),
                       ('Accept', 'application/json')]
            res = client.get(url_for('b2share_communities.communities_item',
                                     community_id=str(unknown_uuid)),
                             headers=headers)
            assert res.status_code == 404

            # create community
            created_community = Community.create_community(
                **community_metadata)
            community_id = created_community.id
            db.session.commit()

            login_user(allowed_user, client)
            # check that GET with non accepted format will return 406
            headers = [('Accept', 'video/mp4')]
            res = client.get(url_for('b2share_communities.communities_item',
                                     community_id=community_id),
                             headers=headers)
            assert res.status_code == 406
Exemplo n.º 9
0
def test_invalid_get(app, login_user, communities_permissions):
    """Test INVALID community get request (GET .../communities/<id>)."""
    with app.app_context():
        # create user allowed to GET all records
        allowed_user = create_user('allowed')
        communities_permissions(allowed_user.id).read_permission(True, None)
        db.session.commit()

    with app.app_context():
        with app.test_client() as client:
            login_user(allowed_user, client)
            unknown_uuid = uuid.uuid4()
            # check that GET with non existing id will return 404
            headers = [('Content-Type', 'application/json'),
                       ('Accept', 'application/json')]
            res = client.get(url_for('b2share_communities.communities_item',
                                     community_id=str(unknown_uuid)),
                             headers=headers)
            assert res.status_code == 404

            # create community
            created_community = Community.create_community(
                **community_metadata)
            community_id = created_community.id
            db.session.commit()

            login_user(allowed_user, client)
            # check that GET with non accepted format will return 406
            headers = [('Accept', 'video/mp4')]
            res = client.get(url_for('b2share_communities.communities_item',
                                     community_id=community_id),
                             headers=headers)
            assert res.status_code == 406
Exemplo n.º 10
0
def test_patch(app):
    """Test BlockSchema.patch()."""
    with app.app_context():
        created_community = Community.create_community(**community_metadata)
        block_schema = BlockSchema.create_block_schema(created_community.id,
                                                       'abc')
        block_schema_id = block_schema.id
        db.session.commit()

        retrieved = BlockSchema.get_block_schema(block_schema_id)
        retrieved.patch([{
            'op': 'replace',
            'path': '/name',
            'value': 'patched'
        }])
        db.session.commit()

    with app.app_context():
        patched = BlockSchema.get_block_schema(block_schema_id)
        assert block_schema_id == patched.id
        assert getattr(patched, 'name') == 'patched'

        with pytest.raises(JsonPatchConflict):
            patched.patch([{
                'op': 'replace',
                'path': '/non_exist_name',
                'value': None
            }])
        assert getattr(patched, 'name') == 'patched'
Exemplo n.º 11
0
def test_block_schemas_versions_backward_compatibility(app):
    """Test non backward compatible root schemas."""
    with app.app_context():
        new_community = Community.create_community(**communities_metadata[0])
        db.session.commit()

        block_schema_name = "schema 1"
        block_schema_1 = BlockSchema.create_block_schema(
            community_id=new_community.id, name=block_schema_name)
        block_schema_id = block_schema_1.id

        # test the versions iterator with no versions created
        assert len(block_schema_1.versions) == 0
        for version in block_schema_1.versions:
            pytest.fail('No versions have been added yet.')

        # create the versions
        for json_schema in block_schemas_json_schemas[0]:
            block_schema_1.create_version(json_schema=json_schema, )
        block_schema_1_id = block_schema_1.id

        # try creating a new not backward compatbile version of the schema
        for json_schema in backward_incompatible_block_schemas_json_schemas:
            with pytest.raises(JSONSchemaCompatibilityError):
                block_schema_1.create_version(json_schema=json_schema, )
        db.session.commit()

    # check that no invalid schema was created
    with app.app_context():
        block_schema_1 = BlockSchema.get_block_schema(block_schema_id)
        assert len(block_schema_1.versions) == \
            len(block_schemas_json_schemas[0])
Exemplo n.º 12
0
def test_block_schema_version_errors(app):
    """Test invalid usage of the BlockSchemaVersion API."""
    with app.app_context():
        new_community = Community.create_community(**communities_metadata[0])
        block_schema = BlockSchema.create_block_schema(
            community_id=new_community.id, name='test')
        db.session.commit()

        # test create block schema version with json_schema == None
        with pytest.raises(InvalidJSONSchemaError):
            block_schema.create_version(None)
        # test invalid $schema URLs
        with pytest.raises(InvalidJSONSchemaError):
            block_schema.create_version({})
        with pytest.raises(InvalidJSONSchemaError):
            block_schema.create_version({'$schema': 'invalid-url'})
        with pytest.raises(InvalidJSONSchemaError):
            block_schema.create_version({'$schema': 'http://examples.com'})

        block_schema.deprecated = True
        # test adding a version to a deprecated schema
        with pytest.raises(BlockSchemaIsDeprecated):
            block_schema.create_version(block_schemas_json_schemas[0][0])

        # try accessing a non existing version
        with pytest.raises(IndexError):
            block_schema.versions[0]

        assert len(block_schema.versions) == 0
Exemplo n.º 13
0
def test_block_schemas_versions(app):
    """Test valid usage of the BlockSchemaVersion API."""
    with app.app_context():
        new_community = Community.create_community(**communities_metadata[0])
        db.session.commit()

        block_schema_name = "schema 1"
        block_schema_1 = BlockSchema.create_block_schema(
            community_id=new_community.id, name=block_schema_name)

        # test the versions iterator with no versions created
        assert len(block_schema_1.versions) == 0
        for version in block_schema_1.versions:
            pytest.fail('No versions have been added yet.')

        # create the versions
        for json_schema in block_schemas_json_schemas[0]:
            block_schema_1.create_version(json_schema=json_schema, )
        block_schema_1_id = block_schema_1.id
        db.session.commit()

    with app.app_context():
        retrieved_block_schema = BlockSchema.get_block_schema(
            block_schema_1_id)
        all_versions = retrieved_block_schema.versions
        assert len(all_versions) == 2

        expected_version = 0
        for version in all_versions:
            # retrieving by version number should return the same
            # BlockSchemaVersion
            retrieved_version = all_versions[expected_version]
            assert json.loads(version.json_schema) == \
                json.loads(retrieved_version.json_schema) == \
                block_schemas_json_schemas[0][version.version]
            # test sorting
            assert version.version == retrieved_version.version == \
                expected_version
            # test __contains__
            assert expected_version in all_versions
            expected_version += 1
        # check access for not existing version
        assert len(block_schemas_json_schemas[0]) not in all_versions
        with pytest.raises(IndexError):
            all_versions[len(block_schemas_json_schemas[0])]

    with app.app_context():
        retrieved_block_schema = BlockSchema.get_block_schema(
            block_schema_1_id)
        all_versions = retrieved_block_schema.versions
        assert len(all_versions) == 2

        retrieved_block_schema.create_version(block_schemas_json_schemas[0][0],
                                              len(all_versions))

        with pytest.raises(SchemaVersionExistsError):
            retrieved_block_schema.create_version(
                block_schemas_json_schemas[0][0], 1)
Exemplo n.º 14
0
    def community(self, community_id):
        """Set the ID of the community maintaining of this Block Schema.

        Raises:
            :class:`b2share.modules.communities.errors.CommunityDoesNotExistError`:
                if the community does not exist.
            :class:`b2share.modules.schemas.errors.InvalidBlockSchemaError`:
                if another conflict occured.
        """  # noqa
        try:
            with db.session.begin_nested():
                self.model.community = community_id
                db.session.merge(self.model)
        except sqlalchemy.exc.IntegrityError as e:
            # this will raise CommunityDoesNotExistError if necessary
            Community.get(community_id)
            # else raise a generic exception
            raise InvalidBlockSchemaError() from e
Exemplo n.º 15
0
    def community(self, community_id):
        """Set the ID of the community maintaining of this Block Schema.

        Raises:
            :class:`b2share.modules.communities.errors.CommunityDoesNotExistError`:
                if the community does not exist.
            :class:`b2share.modules.schemas.errors.InvalidBlockSchemaError`:
                if another conflict occured.
        """  # noqa
        try:
            with db.session.begin_nested():
                self.model.community = community_id
                db.session.merge(self.model)
        except sqlalchemy.exc.IntegrityError as e:
            # this will raise CommunityDoesNotExistError if necessary
            Community.get(community_id)
            # else raise a generic exception
            raise InvalidBlockSchemaError() from e
Exemplo n.º 16
0
def test_valid_put(app, login_user, communities_permissions):
    """Test VALID community put request (PUT .../communities/<id>)."""
    with app.app_context():
        # create community
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        # create allowed user
        allowed_user = create_user('allowed')
        communities_permissions(allowed_user.id).update_permission(
            True, community_id)
        communities_permissions(allowed_user.id).read_permission(
            True, community_id)
        db.session.commit()

    with app.app_context():
        with app.test_client() as client:
            login_user(allowed_user, client)

            headers = [('Content-Type', 'application/json'),
                       ('Accept', 'application/json')]
            res = client.put(url_for('b2share_communities.communities_item',
                                     community_id=community_id),
                             data=json.dumps(patched_community_metadata),
                             headers=headers)
            assert res.status_code == 200
            # check that the returned community matches the given data
            response_data = json.loads(res.get_data(as_text=True))
            for field, value in patched_community_metadata.items():
                assert response_data[field] == value

            # check that an internal community returned id and that it contains
            # the same data
            assert 'id' in response_data.keys()
            assert response_data['id'] == str(
                Community.get(name=patched_community_metadata['name']).id)
            headers = res.headers

    with app.app_context():
        with app.test_client() as client:
            login_user(allowed_user, client)
            # check that the returned self link returns the same data
            subtest_self_link(response_data, headers, client)
Exemplo n.º 17
0
def test_patch(app):
    """Test Community.patch()."""
    with app.app_context():
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        db.session.commit()

    with app.app_context():
        retrieved = Community.get(id=community_id)
        retrieved.patch(community_patch)
        db.session.commit()

    with app.app_context():
        patched = Community.get(id=community_id)
        assert community_id == patched.id
        assert patched.updated
        for field, value in patched_community_metadata.items():
            assert getattr(patched, field) == value

        # test invalid or conflicting patchs
        with pytest.raises(JsonPatchConflict):
            patched.patch([{
                'op': 'replace',
                'path': '/name',
                'value': 'this should not be applied'
            }, {
                'op': 'replace',
                'path': '/non-existing-field',
                'value': 'random value'
            }])
        with pytest.raises(InvalidJsonPatch):
            patched.patch({'whatever': 'key'})
        with pytest.raises(InvalidCommunityError):
            patched.patch([{
                'op': 'replace',
                'path': '/name',
                'value': None
            }])
        # check that the community was not modified
        for field, value in patched_community_metadata.items():
            assert getattr(patched, field) == value
Exemplo n.º 18
0
def test_valid_get(app, login_user,
                   communities_permissions):
    """Test VALID community get request (GET .../communities/<id>)."""
    with app.app_context():
        # create community
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        # create allowed user
        allowed_user = create_user('allowed')
        communities_permissions(
            allowed_user.id).read_permission(True, community_id)
        db.session.commit()

    with app.app_context():
        with app.test_client() as client:
            login_user(allowed_user, client)

            headers = [('Content-Type', 'application/json'),
                       ('Accept', 'application/json')]
            res = client.get(url_for('b2share_communities.communities_item',
                                     community_id=community_id),
                             headers=headers)
            assert res.status_code == 200
            # check that the returned community matches the given data
            response_data = json.loads(res.get_data(as_text=True))
            for field, value in community_metadata.items():
                assert response_data[field] == value

            # check that an internal community returned id and that it contains
            # the same data
            assert 'id' in response_data.keys()
            assert response_data['id'] == str(Community.get(
                name=community_metadata['name']).id)
            headers = res.headers

    with app.app_context():
        with app.test_client() as client:
            login_user(allowed_user, client)
            # check that the returned self link returns the same data
            subtest_self_link(response_data, headers, client)
Exemplo n.º 19
0
def test_invalid_put(app, login_user, communities_permissions):
    """Test INVALID community put request (PUT .../communities/<id>)."""
    with app.app_context():
        # create allowed user
        allowed_user = create_user('allowed')
        communities_permissions(allowed_user.id).update_permission(True, None)
        db.session.commit()

        with app.test_client() as client:
            login_user(allowed_user, client)
            unknown_uuid = uuid.uuid4()
            # check that PUT with non existing id will return 404
            headers = [('Content-Type', 'application/json'),
                       ('Accept', 'application/json')]
            res = client.put(url_for('b2share_communities.communities_item',
                                     community_id=unknown_uuid),
                             data=json.dumps(patched_community_metadata),
                             headers=headers)
            assert res.status_code == 404

            # create community
            created_community = Community.create_community(
                **community_metadata)
            community_id = created_community.id
            db.session.commit()

            # check that PUT with non accepted format will return 406
            headers = [('Content-Type', 'application/json'),
                       ('Accept', 'video/mp4')]
            res = client.put(url_for('b2share_communities.communities_item',
                                     community_id=community_id),
                             data=json.dumps(patched_community_metadata),
                             headers=headers)
            assert res.status_code == 406

            # check that PUT with non-json Content-Type will return 415
            headers = [('Content-Type', 'video/mp4'),
                       ('Accept', 'application/json')]
            res = client.put(url_for('b2share_communities.communities_item',
                                     community_id=community_id),
                             data=json.dumps(patched_community_metadata),
                             headers=headers)
            assert res.status_code == 415

            # check that PUT with invalid json will return 400
            headers = [('Content-Type', 'application/json'),
                       ('Accept', 'application/json')]
            res = client.put(url_for('b2share_communities.communities_item',
                                     community_id=community_id),
                             data='{invalid-json',
                             headers=headers)
            assert res.status_code == 400
Exemplo n.º 20
0
def test_delete(app):
    """Test Community.delete()."""
    with app.app_context():
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        db.session.commit()
    # test deleting a community
    with app.app_context():
        retrieved = Community.get(id=community_id)
        retrieved.delete()
        assert retrieved.deleted
        db.session.commit()
    # test getting a deleted community
    with app.app_context():
        with pytest.raises(CommunityDeletedError):
            Community.get(id=community_id)
    # test force getting a deleted community
    with app.app_context():
        deleted_community = Community.get(id=community_id, with_deleted=True)
        assert deleted_community.deleted
        for field, value in community_metadata.items():
            assert getattr(deleted_community, field) == value
Exemplo n.º 21
0
def test_delete(app):
    """Test Community.delete()."""
    with app.app_context():
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        db.session.commit()
    # test deleting a community
    with app.app_context():
        retrieved = Community.get(id=community_id)
        retrieved.delete()
        assert retrieved.deleted
        db.session.commit()
    # test getting a deleted community
    with app.app_context():
        with pytest.raises(CommunityDeletedError):
            Community.get(id=community_id)
    # test force getting a deleted community
    with app.app_context():
        deleted_community = Community.get(id=community_id, with_deleted=True)
        assert deleted_community.deleted
        for field, value in community_metadata.items():
            assert getattr(deleted_community, field) == value
Exemplo n.º 22
0
def test_create_community_and_get(app):
    """Test Community.create_community() and Community.get()."""
    with app.app_context():
        # check that getting a nonexistent community raises an exception
        with pytest.raises(CommunityDoesNotExistError):
            Community.get(id='e174feb1-5882-4c1e-bab6-6678f59b993f')
        # test misuse of community.get()
        with pytest.raises(ValueError):
            Community.get(id='e174feb1-5882-4c1e-bab6-6678f59b993f',
                          name='myname')
        with pytest.raises(ValueError):
            Community.get()
    # test invalid community creation
    with app.app_context():
        with pytest.raises(InvalidCommunityError):
            created_community = Community.create_community(
                name=None,
                description=None,
            )
    # create a community
    with app.app_context():
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        db.session.commit()
        for field, value in community_metadata.items():
            assert getattr(created_community, field) == value
        assert not created_community.deleted
        assert created_community.created
        assert created_community.updated

    # get the created community
    with app.app_context():
        retrieved_by_id = Community.get(id=community_id)
        retrieved_by_name = Community.get(name=community_metadata['name'])
        assert community_id == retrieved_by_id.id
        assert community_id == retrieved_by_name.id
        for field, value in community_metadata.items():
            assert getattr(retrieved_by_id, field) == value
            assert getattr(retrieved_by_name, field) == value
Exemplo n.º 23
0
def test_create_community_and_get(app):
    """Test Community.create_community() and Community.get()."""
    with app.app_context():
        # check that getting a nonexistent community raises an exception
        with pytest.raises(CommunityDoesNotExistError):
            Community.get(id='e174feb1-5882-4c1e-bab6-6678f59b993f')
        # test misuse of community.get()
        with pytest.raises(ValueError):
            Community.get(id='e174feb1-5882-4c1e-bab6-6678f59b993f',
                          name='myname')
        with pytest.raises(ValueError):
            Community.get()
    # test invalid community creation
    with app.app_context():
        with pytest.raises(InvalidCommunityError):
            created_community = Community.create_community(name=None,
                                                           description=None,)
    # create a community
    with app.app_context():
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        db.session.commit()
        for field, value in community_metadata.items():
            assert getattr(created_community, field) == value
        assert not created_community.deleted
        assert created_community.created
        assert created_community.updated

    # get the created community
    with app.app_context():
        retrieved_by_id = Community.get(id=community_id)
        retrieved_by_name = Community.get(name=community_metadata['name'])
        assert community_id == retrieved_by_id.id
        assert community_id == retrieved_by_name.id
        for field, value in community_metadata.items():
            assert getattr(retrieved_by_id, field) == value
            assert getattr(retrieved_by_name, field) == value
Exemplo n.º 24
0
def test_patch(app):
    """Test Community.patch()."""
    with app.app_context():
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        db.session.commit()

    with app.app_context():
        retrieved = Community.get(id=community_id)
        retrieved.patch(community_patch)
        db.session.commit()

    with app.app_context():
        patched = Community.get(id=community_id)
        assert community_id == patched.id
        assert patched.updated
        for field, value in patched_community_metadata.items():
            assert getattr(patched, field) == value

        # test invalid or conflicting patchs
        with pytest.raises(JsonPatchConflict):
            patched.patch([{
                'op': 'replace',
                'path': '/name',
                'value': 'this should not be applied'
            }, {
                'op': 'replace',
                'path': '/non-existing-field',
                'value': 'random value'
            }])
        with pytest.raises(InvalidJsonPatch):
            patched.patch({'whatever': 'key'})
        with pytest.raises(InvalidCommunityError):
            patched.patch([{'op': 'replace', 'path': '/name', 'value': None}])
        # check that the community was not modified
        for field, value in patched_community_metadata.items():
            assert getattr(patched, field) == value
Exemplo n.º 25
0
def test_valid_patch(app, login_user, communities_permissions,
                     patch_community_function):
    """Test VALID community patch request (PATCH .../communities/<id>)."""
    with app.app_context():
        # create community
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        # create allowed user
        allowed_user = create_user('allowed')
        communities_permissions(allowed_user.id).update_permission(
            True, community_id)
        communities_permissions(allowed_user.id).read_permission(
            True, community_id)
        db.session.commit()

    with app.app_context():
        with app.test_client() as client:
            login_user(allowed_user, client)
            res = patch_community_function(community_id, client)
            assert res.status_code == 200
            # check that the returned community matches the given data
            response_data = json.loads(res.get_data(as_text=True))
            for field, value in patched_community_metadata.items():
                assert response_data[field] == value

            # check that an internal community returned id and that it contains
            # the same data
            assert 'id' in response_data.keys()
            assert response_data['id'] == str(
                Community.get(name=patched_community_metadata['name']).id)
            headers = res.headers

    with app.app_context():
        with app.test_client() as client:
            login_user(allowed_user, client)
            # check that the returned self link returns the same data
            subtest_self_link(response_data, headers, client)
Exemplo n.º 26
0
def test_valid_create(app, login_user, communities_permissions):
    """Test VALID community creation request (POST .../communities/)."""
    with app.app_context():
        allowed_user = create_user('allowed')
        communities_permissions(allowed_user.id).create_permission(True)
        communities_permissions(allowed_user.id).read_permission(True)
        db.session.commit()

    with app.test_client() as client:
        with app.app_context():
            login_user(allowed_user, client)

            headers = [('Content-Type', 'application/json'),
                       ('Accept', 'application/json')]
            res = client.post(url_for('b2share_communities.communities_list',
                                      _external=False),
                              data=json.dumps(community_metadata),
                              headers=headers)
            assert res.status_code == 201
            # check that the returned community matches the given data
            response_data = json.loads(res.get_data(as_text=True))
            for field, value in community_metadata.items():
                assert response_data[field] == value

            # check that an internal community returned id and that it contains
            # the same data
            assert 'id' in response_data.keys()
            assert response_data['id'] == str(
                Community.get(name=community_metadata['name']).id)
            headers = res.headers
            community_id = response_data['id']

    with app.app_context():
        communities_permissions(allowed_user.id).read_permission(
            True, community_id)
        db.session.commit()

    with app.app_context():
        with app.test_client() as client:
            login_user(allowed_user, client)
            # check that the returned self link returns the same data
            subtest_self_link(response_data, headers, client)
            assert headers['Location'] == response_data['links']['self']
        # check that one community has been created
        assert len(CommunityModel.query.all()) == 1
Exemplo n.º 27
0
def test_block_schema_errors(app):
    """Test invalid usage of the BlockSchema API."""
    with app.app_context():
        unknown_uuid = uuid.uuid4()
        # test with an invalid community ID
        with pytest.raises(InvalidBlockSchemaError):
            BlockSchema.create_block_schema(community_id=unknown_uuid,
                                            name='test')
        new_community = Community.create_community(**communities_metadata[0])
        db.session.commit()
        # test with name == None
        with pytest.raises(InvalidBlockSchemaError):
            BlockSchema.create_block_schema(community_id=new_community.id,
                                            name=None)
        # test with len(name) too short
        with pytest.raises(InvalidBlockSchemaError):
            BlockSchema.create_block_schema(community_id=new_community.id,
                                            name='t')
        # test with len(name) too long
        with pytest.raises(InvalidBlockSchemaError):
            BlockSchema.create_block_schema(community_id=new_community.id,
                                            name='t'*500)
        # test getting non existing block schema
        with pytest.raises(BlockSchemaDoesNotExistError):
            BlockSchema.get_block_schema(unknown_uuid)

        block_schema = BlockSchema.create_block_schema(
            community_id=new_community.id, name='test'
        )
        # test setting the community to an unknown uuid
        with pytest.raises(CommunityDoesNotExistError):
            block_schema.community = unknown_uuid
        assert block_schema.community == new_community.id

        # test setting the name to None
        with pytest.raises(InvalidBlockSchemaError):
            block_schema.name = None
        # test setting the name to a too short string
        with pytest.raises(InvalidBlockSchemaError):
            block_schema.name = 't'
        # test setting the name to a too long string
        with pytest.raises(InvalidBlockSchemaError):
            block_schema.name = 't'*500
            db.session.commit()
Exemplo n.º 28
0
def test_update(app):
    """Test BlockSchema.update()."""
    with app.app_context():
        created_community = Community.create_community(**community_metadata)
        block_schema = BlockSchema.create_block_schema(created_community.id, 'abc')
        block_schema_id = block_schema.id
        db.session.commit()

    with app.app_context():
        retrieved = BlockSchema.get_block_schema(block_schema_id)
        retrieved.update({'name': 'updated'})
        db.session.commit()

    with app.app_context():
        updated = BlockSchema.get_block_schema(block_schema_id)
        assert block_schema_id == updated.id
        assert getattr(updated, 'name') == 'updated'

        with pytest.raises(InvalidBlockSchemaError):
            updated.update({'name': None})
        assert getattr(updated, 'name') == 'updated'
Exemplo n.º 29
0
def _create_communities(path, verbose):
    """Create demo communities."""
    if verbose > 0:
        click.secho('Creating communities', fg='yellow', bold=True)
    with db.session.begin_nested():
        communities_dir = os.path.join(path, 'communities')
        communities = dict()
        nb_communities = 0
        for filename in sorted(os.listdir(communities_dir)):
            if os.path.splitext(filename)[1] == '.json':
                with open(os.path.join(communities_dir,
                                       filename)) as json_file:
                    json_config = json.loads(json_file.read())
                    workflow = json_config.get('publication_workflow',
                                               'review_and_publish')
                    is_restricted = json_config.get('restricted_submission',
                                                    False)
                    community = Community.create_community(
                        name=json_config['name'],
                        description=json_config['description'],
                        logo=json_config['logo'],
                        publication_workflow=workflow,
                        restricted_submission=is_restricted,
                        id_=UUID(json_config['id']),
                    )
                    if verbose > 1:
                        click.secho('Created community {0} with ID {1}'.format(
                            community.name, community.id
                        ))
                    communities[community.name] = DemoCommunity(community,
                                                                json_config)
                    nb_communities += 1
    if verbose > 0:
        click.secho('Created {} communities!'.format(nb_communities),
                    fg='green')
    return communities
Exemplo n.º 30
0
def test_action_on_deleted(app, login_user,
                           communities_permissions):
    """Test getting, deleting and updating a perviously deleted community."""
    with app.app_context():
        # create community
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        # create allowed user
        allowed_user = create_user('allowed')
        communities_permissions(
            allowed_user.id).delete_permission(True, community_id)
        db.session.commit()

    def assert_410_gone_result(res):
        """Check that the result is a 410 error with JSON message."""
        assert res.status_code == 410
        # check that the returned record matches the given data
        response_data = json.loads(res.get_data(as_text=True))
        assert response_data['status'] == 410

    with app.app_context():
        # delete the record
        Community.get(community_id).delete()
        with app.test_client() as client:
            login_user(allowed_user, client)
#             # try DELETE
#             headers = [('Content-Type', 'application/json'),
#                        ('Accept', 'application/json')]
#             res = client.delete(url_for('b2share_communities.communities_item',
#                                         community_id=community_id),
#                                 headers=headers)
#             assert_410_gone_result(res)
            # try GET
            headers = [('Content-Type', 'application/json'),
                       ('Accept', 'application/json')]
            res = client.get(url_for('b2share_communities.communities_item',
                                     community_id=community_id),
                             headers=headers)
            assert_410_gone_result(res)
#             # try PUT
#             headers = [('Content-Type', 'application/json'),
#                        ('Accept', 'application/json')]
#             res = client.put(url_for('b2share_communities.communities_item',
#                                      community_id=community_id),
#                              data=json.dumps(patched_community_metadata),
#                              headers=headers)
#             assert_410_gone_result(res)
#             # try PATCH
#             headers = [('Content-Type', 'application/json-patch+json'),
#                        ('Accept', 'application/json')]
#             res = client.patch(url_for('b2share_communities.communities_item',
#                                        community_id=community_id),
#                                data=json.dumps(community_patch),
#                                headers=headers)
#             assert_410_gone_result(res)

    # check that the record exist and is marked as deleted
    with app.app_context():
        # check database state
        with pytest.raises(CommunityDeletedError):
            Community.get(community_id)
        community = Community.get(community_id, with_deleted=True)
        assert community.deleted
        # check that one community has been created
        assert len(CommunityModel.query.all()) == 1
Exemplo n.º 31
0
def _process_record(rec):
    #rec is dict representing 1 record
    #from json donwloaded from b2share_v1 API
    result = {}
    generic_keys = ['open_access', 'contact_email', 'publication_date']
    for k in generic_keys:
        result[k] = rec[k]
    result['license'] = {'license': rec['licence']}
    result['titles'] = []
    result['titles'].append({'title':rec['title']})
    result['descriptions'] = []
    element = {}
    element['description_type'] = 'Abstract'
    element['description'] = rec['description']
    result['descriptions'].append(element)
    result['contributors'] = []
    contributors = unique(rec['contributors'])
    contributors = unique(rec['contributors'])
    for contributor in contributors:
        element = {}
        element['contributor_type'] = "Other"
        element['contributor_name'] = contributor
        result['contributors'].append(element)
    result['keywords'] = unique(rec['keywords'])
    creators = unique(rec['creator'])
    result['creators'] = []
    for creator in creators:
        result['creators'].append({'creator_name':creator})

    result['publisher'] = rec.get('publisher', "https://b2share.eudat.eu")
    if rec.get('discipline'):
        result['disciplines'] = unique(rec.get('discipline'))
    if rec.get('language'):
        result['language'] = rec.get('language')
    if rec.get('version'):
        result['version'] = rec.get('version')
    if rec.get('embargo_date'):
        result['embargo_date'] = rec.get('embargo_date')

    #fetch community
    rec['domain'] = rec['domain'].upper()
    #hardcoded Aalto exception
    if rec['domain'] == 'AALTO':
        rec['domain'] = 'Aalto'
    comms = Community.get_all(0, 1, name=rec['domain'])
    if comms:
        community = comms[0]
        result['community'] = str(community.id)
    elif rec['domain'] == 'GENERIC':
        community = Community.get(name='EUDAT')
        result['community'] = str(community.id)
    elif rec['domain'] == 'LINGUISTICS':
        community = Community.get(name='CLARIN')
        result['community'] = str(community.id)
    else:
        raise Exception("Community not found for domain: `{}`".format(rec['domain']))
    result['alternate_identifiers'] = [{
        'alternate_identifier_type':'B2SHARE_V1_ID',
        'alternate_identifier': str(rec['record_id'])
    }]
    if 'PID' in rec.keys():
        result['alternate_identifiers'].append({
            'alternate_identifier_type':'ePIC_PID',
            'alternate_identifier': rec['PID']
        })
    if 'resource_type' in rec.keys():
        translate = {
            'Audio': 'Audiovisual',
            'Video': 'Audiovisual',
            'Time-series': 'Dataset',
            'Text': 'Text',
            'Image': 'Image',
            'Other': 'Other',
            'treebank':'Other',
            'Time-Series':'Dataset'
        }
        resource_types = unique([translate[r] for r in rec['resource_type']])
        result['resource_types'] = []
        for rt in resource_types:
            element = {'resource_type_general':rt}
            result['resource_types'].append(element)
    if not result['resource_types']:
        result['resource_types'] = [{'resource_type_general': "Other"}]
    result['community_specific'] = {}
    if 'domain_metadata' in rec.keys():
        result.update(
            _match_community_specific_metadata(rec, community)
            )
    return result
Exemplo n.º 32
0
 def community_id_match(match):
     community_name = match.group(1)
     community = Community.get(name=community_name)
     return str(community.id)
Exemplo n.º 33
0
 def community_id_match(match):
     community_name = match.group(1)
     community = Community.get(name=community_name)
     return str(community.id)
Exemplo n.º 34
0
def test_action_on_deleted(app, login_user, communities_permissions):
    """Test getting, deleting and updating a perviously deleted community."""
    with app.app_context():
        # create community
        created_community = Community.create_community(**community_metadata)
        community_id = created_community.id
        # create allowed user
        allowed_user = create_user('allowed')
        communities_permissions(allowed_user.id).delete_permission(
            True, community_id)
        db.session.commit()

    def assert_410_gone_result(res):
        """Check that the result is a 410 error with JSON message."""
        assert res.status_code == 410
        # check that the returned record matches the given data
        response_data = json.loads(res.get_data(as_text=True))
        assert response_data['status'] == 410

    with app.app_context():
        # delete the record
        Community.get(community_id).delete()
        with app.test_client() as client:
            login_user(allowed_user, client)
            #             # try DELETE
            #             headers = [('Content-Type', 'application/json'),
            #                        ('Accept', 'application/json')]
            #             res = client.delete(url_for('b2share_communities.communities_item',
            #                                         community_id=community_id),
            #                                 headers=headers)
            #             assert_410_gone_result(res)
            # try GET
            headers = [('Content-Type', 'application/json'),
                       ('Accept', 'application/json')]
            res = client.get(url_for('b2share_communities.communities_item',
                                     community_id=community_id),
                             headers=headers)
            assert_410_gone_result(res)
#             # try PUT
#             headers = [('Content-Type', 'application/json'),
#                        ('Accept', 'application/json')]
#             res = client.put(url_for('b2share_communities.communities_item',
#                                      community_id=community_id),
#                              data=json.dumps(patched_community_metadata),
#                              headers=headers)
#             assert_410_gone_result(res)
#             # try PATCH
#             headers = [('Content-Type', 'application/json-patch+json'),
#                        ('Accept', 'application/json')]
#             res = client.patch(url_for('b2share_communities.communities_item',
#                                        community_id=community_id),
#                                data=json.dumps(community_patch),
#                                headers=headers)
#             assert_410_gone_result(res)

# check that the record exist and is marked as deleted
    with app.app_context():
        # check database state
        with pytest.raises(CommunityDeletedError):
            Community.get(community_id)
        community = Community.get(community_id, with_deleted=True)
        assert community.deleted
        # check that one community has been created
        assert len(CommunityModel.query.all()) == 1
Exemplo n.º 35
0
def test_community_schema(app, flask_http_responses):
    """Test valid usage of the CommunitySchema API."""
    with app.app_context():
        new_community = Community.create_community(**communities_metadata[0])

        db.session.commit()

        BlockSchemaRef = namedtuple('BlockSchemaRef',
                                    ['block_schema', 'versions'])
        BlockSchemaVersionRef = namedtuple('BlockSchemaVersionRef',
                                           ['version', 'url'])

        # create root schemas
        root_schemas = [
            RootSchema.create_new_version(
                version=version,
                json_schema=root_schemas_json_schemas[version],
            ) for version in range(len(root_schemas_json_schemas))
        ]
        # create block schemas
        block_schemas = []
        for block_index in range(len(block_schemas_json_schemas)):
            block_schema = BlockSchema.create_block_schema(
                community_id=new_community.id,
                name="schema {}".format(block_index))
            block_schemas.append(
                BlockSchemaRef(
                    block_schema=block_schema,
                    versions=[
                        BlockSchemaVersionRef(
                            version=block_schema.create_version(
                                json_schema=block_schemas_json_schemas[
                                    block_index][version], ),
                            url=url_for(
                                'b2share_schemas.block_schema_versions_item',
                                schema_id=block_schema.id,
                                schema_version_nb=version,
                                _external=True,
                            )) for version in range(
                                len(block_schemas_json_schemas[block_index]))
                    ]))
        community_schemas = []
        # create a community schema
        community_schema_v1_json_schema = {
            '$schema': 'http://json-schema.org/draft-04/schema#',
            'type': 'object',
            'properties': {
                str(block_schemas[0].block_schema.id): {
                    '$ref':
                    "{}#/json_schema".format(block_schemas[0].versions[0].url),
                },
                str(block_schemas[1].block_schema.id): {
                    '$ref':
                    "{}#/json_schema".format(block_schemas[1].versions[0].url),
                },
            },
            'additionalProperties': False,
        }
        community_schemas.append(
            CommunitySchema.create_version(
                community_id=new_community.id,
                root_schema_version=root_schemas[0].version,
                community_schema=community_schema_v1_json_schema))

        # test with another block schema version
        community_schema_v2_json_schema = {
            '$schema': 'http://json-schema.org/draft-04/schema#',
            'type': 'object',
            'properties': {
                str(block_schemas[0].block_schema.id): {
                    '$ref':
                    "{}#/json_schema".format(block_schemas[0].versions[1].url),
                },
                str(block_schemas[1].block_schema.id): {
                    '$ref':
                    "{}#/json_schema".format(block_schemas[1].versions[0].url),
                },
            },
            'additionalProperties': False,
        }
        community_schemas.append(
            CommunitySchema.create_version(
                community_id=new_community.id,
                root_schema_version=root_schemas[0].version,
                community_schema=community_schema_v2_json_schema))

        # test with another root schema
        community_schemas.append(
            CommunitySchema.create_version(
                community_id=new_community.id,
                root_schema_version=root_schemas[1].version,
                community_schema=community_schema_v2_json_schema))

        db.session.commit()
        # create a metadata blcok matching each community schema
        metadatas = [{
            'authors': ['C. Arthur', 'D. Albert'],
            'community_specific': {
                str(block_schemas[0].block_schema.id): {
                    'experiment_nb': 42,
                },
                str(block_schemas[1].block_schema.id): {
                    'analysis_result': 'success',
                },
            }
        }, {
            'authors': ['C. Arthur', 'D. Albert'],
            'community_specific': {
                str(block_schemas[0].block_schema.id): {
                    'experiment_nb': 42,
                    'experiment_date': '4242'
                }
            }
        }, {
            'authors': ['C. Arthur', 'D. Albert'],
            'files': ['/path/to/the/file.txt'],
            'community_specific': {
                str(block_schemas[0].block_schema.id): {
                    'experiment_nb': 42,
                    'experiment_date': '4242'
                }
            }
        }]

        validation_schemas = [
            schema.build_json_schema() for schema in community_schemas
        ]
        for index in range(len(community_schemas)):
            with flask_http_responses():
                # check that the community schema validates the corresponding
                # JSON metadata
                jsonschema.validate(metadatas[index],
                                    validation_schemas[index])
                for index2 in range(len(community_schemas)):
                    if index != index2:
                        with pytest.raises(
                                jsonschema.exceptions.ValidationError):
                            # check that the community schema does not validate
                            # the others JSON metadata
                            jsonschema.validate(metadatas[index2],
                                                validation_schemas[index])