forked from CSCfi/fairdata-etsin-qvain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasetest.py
167 lines (134 loc) · 4.74 KB
/
basetest.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# This file is part of the Etsin service
#
# Copyright 2017-2018 Ministry of Education and Culture, Finland
#
# :author: CSC - IT Center for Science Ltd., Espoo Finland <servicedesk@csc.fi>
# :license: MIT
"""Base functionalities for tests"""
import os
import pytest
from .utils import get_test_catalog_record
class BaseTest():
"""Use as base class for any tests. Contains fixtures and monkeypatched methods"""
@pytest.fixture
def app(self):
"""
App fixture where testing flag is set as True
:return:
"""
os.environ['TESTING'] = 'True'
from etsin_finder.finder import app
return app
@pytest.fixture
def authd_client(self, app, monkeypatch):
"""
User-authenticated Flask test client
:param app:
:param monkeypatch:
:return:
"""
from etsin_finder import authentication
monkeypatch.setattr(authentication, 'is_authenticated', lambda: True)
client = app.test_client()
with client as c:
with c.session_transaction() as sess:
sess['samlUserdata'] = {
'urn:oid:2.5.4.3': ['Teppo Testaaja'],
'urn:oid:1.3.6.1.4.1.8057.2.80.9': ['teppo@yliopisto.fi']
}
return client
@pytest.fixture
def unauthd_client(self, app, monkeypatch):
"""
Non-User-authenticated Flask test client
:param app:
:param monkeypatch:
:return:
"""
from etsin_finder import authentication
monkeypatch.setattr(authentication, 'is_authenticated', lambda: False)
client = app.test_client()
return client
@pytest.fixture
def nonexisting_catalog_record(self, monkeypatch):
"""
Nonexisting catalog record
:param monkeypatch:
:return:
"""
from etsin_finder import cr_service
monkeypatch.setattr(cr_service, 'get_catalog_record', lambda x, y, z: None)
@pytest.fixture
def open_catalog_record(self, monkeypatch):
"""
Open access_type catalog record
:param monkeypatch:
:return:
"""
from etsin_finder import cr_service
monkeypatch.setattr(cr_service, 'get_catalog_record', lambda x, y, z: get_test_catalog_record('open'))
@pytest.fixture
def login_catalog_record(self, monkeypatch):
"""
Login access_type catalog record
:param monkeypatch:
:return:
"""
from etsin_finder import cr_service
monkeypatch.setattr(cr_service, 'get_catalog_record', lambda x, y, z: get_test_catalog_record('login'))
@pytest.fixture
def permit_catalog_record(self, monkeypatch):
"""
Permit access_type catalog record
:param monkeypatch:
:return:
"""
from etsin_finder import cr_service
monkeypatch.setattr(cr_service, 'get_catalog_record', lambda x, y, z: get_test_catalog_record('permit'))
@pytest.fixture
def embargo_passed_catalog_record(self, monkeypatch):
"""
Embargo access_type catalog record with embargo date passed
:param monkeypatch:
:return:
"""
from etsin_finder import cr_service
monkeypatch.setattr(cr_service, 'get_catalog_record', lambda x, y, z: get_test_catalog_record('embargo', True))
@pytest.fixture
def embargo_not_passed_catalog_record(self, monkeypatch):
"""
Embargo access_type catalog record with embargo date not passed
:param monkeypatch:
:return:
"""
from etsin_finder import cr_service
monkeypatch.setattr(cr_service, 'get_catalog_record', lambda x, y, z: get_test_catalog_record('embargo', False))
@pytest.fixture
def restricted_catalog_record(self, monkeypatch):
"""
Restricted access_type catalog record
:param monkeypatch:
:return:
"""
from etsin_finder import cr_service
monkeypatch.setattr(cr_service, 'get_catalog_record', lambda x, y, z: get_test_catalog_record('restricted'))
@pytest.fixture
def has_rems_permit(self, monkeypatch):
"""
Rems entitlement given
:param monkeypatch:
:return:
"""
from etsin_finder import rems_service
monkeypatch.setattr(rems_service, 'get_user_rems_permission_for_catalog_record', lambda x, y: True)
@pytest.fixture
def no_rems_permit(self, monkeypatch):
"""
No Rems entitlement given
:param monkeypatch:
:return:
"""
from etsin_finder import rems_service
monkeypatch.setattr(rems_service, 'get_user_rems_permission_for_catalog_record', lambda x, y: False)
if __name__ == '__main__':
pytest.main()