conftest.py 1.88 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
23
24
25
26
27
28
29
30
31
32
33
from flask import current_app
from invenio_accounts.models import Role, User
from invenio_oauth2server.models import Token


@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
34
    user = User(email="test@example.com", active=True)
35
36
    db.session.add(user)

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

    db.session.commit()

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

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

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


Carina Antunes's avatar
Carina Antunes committed
52
@pytest.fixture(scope="module")
Carina Antunes's avatar
Carina Antunes committed
53
def app_config(app_config):
54
55
56
    """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
57
58
59
    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
60

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


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


Carina Antunes's avatar
Carina Antunes committed
70
@pytest.fixture(scope="module")
71
72
73
74
75
76
77
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