conftest.py 2.08 KB
Newer Older
Carina Antunes's avatar
Carina Antunes committed
1
#!/usr/bin/python
2
3
4
# -*- coding: utf-8 -*-
#
# This file is part of CERN Search.
Carina Antunes's avatar
Carina Antunes committed
5
# Copyright (C) 2018-2021 CERN.
6
#
Carina Antunes's avatar
Carina Antunes committed
7
# Citadel Search is free software; you can redistribute it and/or modify it
8
# under the terms of the MIT License; see LICENSE file for more details.
Carina Antunes's avatar
Carina Antunes committed
9
"""Pytest configuration.
10

Carina Antunes's avatar
Carina Antunes committed
11
12
13
See https://pytest-invenio.readthedocs.io/ for documentation on which test
fixtures are available.
"""
Pablo Panero's avatar
Pablo Panero committed
14

Carina Antunes's avatar
Carina Antunes committed
15
from __future__ import absolute_import, print_function
Pablo Panero's avatar
Pablo Panero committed
16

17
18
import logging

Carina Antunes's avatar
Carina Antunes committed
19
import pytest
20
21
22
from flask import current_app
from invenio_accounts.models import Role, User
from invenio_oauth2server.models import Token
23
from sqlalchemy_continuum import versioning_manager
24
25
26
27
28
29
30
31
32
33
34


@pytest.fixture()
def app(app, logger):
    """Application factory fixture."""
    yield app


@pytest.fixture()
def user(db, app):
    """File system location."""
Carina Antunes's avatar
Carina Antunes committed
35
    user = User(email="test@example.com", active=True)
36
37
    db.session.add(user)

Carina Antunes's avatar
Carina Antunes committed
38
    role = Role(name="search-admin")
39
40
41
42
43
    role.users.append(user)
    db.session.add(role)

    db.session.commit()

Carina Antunes's avatar
Carina Antunes committed
44
    token = Token.create_personal("test", user.id)
45
46
    db.session.commit()

Carina Antunes's avatar
Carina Antunes committed
47
48
    app.config["API_TOKEN"] = token.access_token
    app.config["SEARCH_USE_EGROUPS"] = True
49
50

    yield user
Carina Antunes's avatar
Carina Antunes committed
51
52


Carina Antunes's avatar
Carina Antunes committed
53
@pytest.fixture(scope="module")
Carina Antunes's avatar
Carina Antunes committed
54
def app_config(app_config):
55
56
57
    """Application configuration fixture."""
    # Missing because they're set in invenio base image:
    # More info: https://github.com/inveniosoftware/docker-invenio#environment-variables
Carina Antunes's avatar
Carina Antunes committed
58
59
60
    app_config["WORKING_DIR"] = "/opt/invenio"
    app_config["USER_ID"] = 1000
    app_config["INSTANCE_PATH"] = "/opt/invenio/var/instance"
Carina Antunes's avatar
Carina Antunes committed
61

Carina Antunes's avatar
Carina Antunes committed
62
    return app_config
63
64
65
66
67
68
69
70


@pytest.fixture()
def logger(appctx, caplog):
    """Set logger level to debug."""
    current_app.logger.setLevel(logging.DEBUG)


Carina Antunes's avatar
Carina Antunes committed
71
@pytest.fixture(scope="module")
72
73
74
75
76
77
78
def instance_path():
    """Connect instance path.

    Overwrite pytest-invenio fixture to avoid setting static folder to
    `os.path.join(sys.prefix, 'var/instance/static')`
    """
    pass
79
80
81
82
83
84
85
86


@pytest.fixture(scope="function")
def db(db):
    """Re-initialize versioning."""
    versioning_manager.track_session(db.session)

    yield db