*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
conftest.py 3.08 KiB
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 - 2021 TU Wien.
#
# Invenio-Config-TUW is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""Pytest configuration.

See https://pytest-invenio.readthedocs.io/ for documentation on which test
fixtures are available.
"""


import pytest
from flask_security.utils import hash_password, login_user
from flask_webpackext.manifest import (
    JinjaManifest,
    JinjaManifestEntry,
    JinjaManifestLoader,
)
from invenio_access.permissions import system_identity
from invenio_accounts.testutils import login_user_via_session
from invenio_app.factory import create_app as create_invenio
from invenio_records_resources.proxies import current_service_registry
from invenio_vocabularies.proxies import current_service as vocab_svc


#
# Mock the webpack manifest to avoid having to compile the full assets.
#
class MockJinjaManifest(JinjaManifest):
    """Mock manifest."""

    def __getitem__(self, key):
        """Get a manifest entry."""
        return JinjaManifestEntry(key, [key])

    def __getattr__(self, name):
        """Get a manifest entry."""
        return JinjaManifestEntry(name, [name])


class MockManifestLoader(JinjaManifestLoader):
    """Manifest loader creating a mocked manifest."""

    def load(self, filepath):
        """Load the manifest."""
        return MockJinjaManifest()


@pytest.fixture(scope="module")
def create_app(instance_path):
    """Create test app."""
    return create_invenio


@pytest.fixture(scope="module")
def app_config(app_config):
    """Testing configuration."""
    app_config["MAIL_SUPPRESS_SEND"] = True
    app_config["WEBPACKEXT_MANIFEST_LOADER"] = MockManifestLoader
    return app_config


@pytest.fixture()
def users(app, db):
    """Create example user."""
    with db.session.begin_nested():
        datastore = app.extensions["security"].datastore
        user1 = datastore.create_user(
            email="info@inveniosoftware.org",
            password=hash_password("password"),
            active=True,
        )
        user2 = datastore.create_user(
            email="ser-testalot@inveniosoftware.org",
            password=hash_password("beetlesmasher"),
            active=True,
        )

    db.session.commit()
    return [user1, user2]


@pytest.fixture()
def client_with_login(client, users):
    """A test client for the app with a logged-in user."""
    user = users[0]
    login_user(user)
    login_user_via_session(client, email=user.email)
    client._user = user
    return client


@pytest.fixture()
def affiliations(db):
    """Creates the required affiliations vocabulary for the tests."""
    vocab_svc.create_type(system_identity, "affiliations", "aff")

    service = current_service_registry.get("affiliations")
    service.create(
        system_identity,
        {
            "acronym": "TUW",
            "id": "04d836q62",
            "identifiers": [{"identifier": "04d836q62", "scheme": "ror"}],
            "name": "TU Wien",
            "title": {"de": "Technische Universit\xE4t Wien", "en": "TU Wien"},
        },
    )