summaryrefslogtreecommitdiffstats
path: root/python/tests/fixtures.py
blob: e0bb1aae9a3a957a056486645284b28e85c2cc72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

import pytest
from dotenv import load_dotenv
import elasticsearch

import fatcat_web
import fatcat_openapi_client
from fatcat_openapi_client import *
from fatcat_tools import authenticated_api


ES_CONTAINER_STATS_RESP = {
    'timed_out': False,
    'aggregations': {
        'container_stats': {'buckets': {
            'is_preserved': {'doc_count': 461939},
            'in_kbart': {'doc_count': 461939},
            'in_web': {'doc_count': 2797},
        }},
        'preservation': {
            'buckets': [
            {'key': 'bright', 'doc_count': 444},
            {'key': 'dark', 'doc_count': 111},
            ],
            'sum_other_doc_count': 0,
        },
        'release_type': {
            'buckets': [
            {'key': 'article-journal', 'doc_count': 456},
            {'key': 'book', 'doc_count': 123},
            ],
            'sum_other_doc_count': 0,
        },
    },
    'hits': {'total': 461939, 'hits': [], 'max_score': 0.0},
    '_shards': {'successful': 5, 'total': 5, 'skipped': 0, 'failed': 0},
    'took': 50
}

# TODO: this should not be empty
ES_CONTAINER_RANDOM_RESP = {
    'timed_out': False,
    'hits': {'total': 461939, 'hits': [], 'max_score': 0.0},
    '_shards': {'successful': 5, 'total': 5, 'skipped': 0, 'failed': 0},
    'took': 50
}

ES_RELEASE_EMPTY_RESP = {
    'timed_out': False,
    'hits': {'total': 0, 'hits': [], 'max_score': 0.0},
    '_shards': {'successful': 5, 'total': 5, 'skipped': 0, 'failed': 0},
    'took': 50
}

@pytest.fixture
def full_app(mocker):
    load_dotenv(dotenv_path="./example.env")
    fatcat_web.app.testing = True
    fatcat_web.app.debug = False
    fatcat_web.app.config['WTF_CSRF_ENABLED'] = False

    # mock out ES client requests, so they at least fail fast
    fatcat_web.app.es_client = elasticsearch.Elasticsearch("mockbackend")
    mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request')
    return fatcat_web.app

@pytest.fixture
def app(full_app):
    return full_app.test_client()

@pytest.fixture
def app_admin(app):
    ADMIN_DEV_TOKEN = "AgEPZGV2LmZhdGNhdC53aWtpAhYyMDE5MDEwMS1kZXYtZHVtbXkta2V5AAImZWRpdG9yX2lkID0gYWFhYWFhYWFhYWFhYmt2a2FhYWFhYWFhYWkAAht0aW1lID4gMjAxOS0wNC0wNFQyMzozMjo0NloAAAYgrN3jjy0mgEqIydTFfsOLYSS55dz6Fh2d1CGMNQFLwcQ="
    rv = app.post('/auth/token_login',
        data=dict(token=ADMIN_DEV_TOKEN),
        follow_redirects=True)
    assert rv.status_code == 200
    return app

@pytest.fixture
def api():
    load_dotenv(dotenv_path="./example.env")
    api_client = authenticated_api("http://localhost:9411/v0")
    api_client.editor_id = "aaaaaaaaaaaabkvkaaaaaaaaae"
    return api_client

@pytest.fixture
def api_dummy_entities(api):
    """
    This is a sort of bleh fixture. Should probably create an actual
    object/class with create/accept/clean-up methods?
    """

    c1 = CreatorEntity(display_name="test creator deletion")
    j1 = ContainerEntity(name="test journal deletion")
    r1 = ReleaseEntity(title="test release creator deletion", ext_ids=ReleaseExtIds())
    f1 = FileEntity()
    fs1 = FilesetEntity()
    wc1 = WebcaptureEntity(
        timestamp="2000-01-01T12:34:56Z",
        original_url="http://example.com/",
    )

    eg = quick_eg(api)
    c1 = api.get_creator(api.create_creator(eg.editgroup_id, c1).ident)
    j1 = api.get_container(api.create_container(eg.editgroup_id, j1).ident)
    r1 = api.get_release(api.create_release(eg.editgroup_id, r1).ident)
    w1 = api.get_work(r1.work_id)
    f1 = api.get_file(api.create_file(eg.editgroup_id, f1).ident)
    fs1 = api.get_fileset(api.create_fileset(eg.editgroup_id, fs1).ident)
    wc1 = api.get_webcapture(api.create_webcapture(eg.editgroup_id, wc1).ident)

    return {
        "api": api,
        "editgroup": eg,
        "creator": c1,
        "container": j1,
        "file": f1,
        "fileset": fs1,
        "webcapture": wc1,
        "release": r1,
        "work": w1,
    }

def test_get_changelog_entry(api):
    """Check that fixture is working"""
    cl = api.get_changelog_entry(1)
    assert cl

## Helpers ##################################################################

def quick_eg(api_inst):
    eg = api_inst.create_editgroup(fatcat_openapi_client.Editgroup())
    return eg