-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_resolver_gateway.py
240 lines (202 loc) · 11.4 KB
/
test_resolver_gateway.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import sys
import os
PROJECT_HOME = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../'))
sys.path.append(PROJECT_HOME)
from flask_testing import TestCase
import unittest
import json
import mock
import resolverway.app as app
from resolverway.views import LinkRequest, redis_db
from resolverway.tests.unittests.stubdata import data
class test_resolver(TestCase):
def create_app(self):
self.current_app = app.create_app(**{'TESTING': True})
return self.current_app
def test_route(self):
"""
Tests for the existence of a /link_gateway route, and that it returns
properly formatted JSON data when the URL is supplied
"""
r= self.client.get('/link_gateway/1987gady.book.....B/ABSTRACT/https://ui.adsabs.harvard.edu/abs/1987gady.book.....B/ABSTRACT')
self.assertEqual(r.status_code, 302)
def test_route_error_invalid_url(self):
"""
test that if given invalid url returns error
:return:
"""
r = self.client.get('/link_gateway/1987gady.book.....B/ABSTRACT/invalid_url')
self.assertEqual(r.status_code, 400)
def test_single_link(self):
"""
Tests single link response
:return:
"""
the_json = {"action": "redirect",
"link": "http://archive.stsci.edu/mastbibref.php?bibcode=2013MNRAS.435.1904M",
"service": "https://ui.adsabs.harvard.edu/abs/2013MNRAS.435.1904/ESOURCE"}
r = LinkRequest('1987gady.book.....B', 'ABSTRACT', '').process_resolver_response(the_json, True)
self.assertEqual(r[1], 302)
def test_multiple_links(self):
"""
Tests multiple link response
:return:
"""
the_json = {"action": "display",
"links": {"count": 4, "link_type": "ESOURCE", "bibcode": "2013MNRAS.435.1904", "records": [
{"url": "http://arxiv.org/abs/1307.6556", "title": "http://arxiv.org/abs/1307.6556"},
{"url": "http://arxiv.org/pdf/1307.6556", "title": "http://arxiv.org/pdf/1307.6556"},
{"url": "http://dx.doi.org/10.1093%2Fmnras%2Fstt1379", "title": "http://dx.doi.org/10.1093%2Fmnras%2Fstt1379"},
{"url": "http://mnras.oxfordjournals.org/content/435/3/1904.full.pdf", "title": "http://mnras.oxfordjournals.org/content/435/3/1904.full.pdf"}]},
"service": ""}
r = LinkRequest('1987gady.book.....B', 'ABSTRACT', '').process_resolver_response(the_json, False)
self.maxDiff = None;
self.assertTrue(data.html_data in r[0])
self.assertEqual(r[1], 200)
def test_identification_link(self):
"""
Tests single link response for identification link types
:return:
"""
the_json = {"action": "redirect",
"link": "http://dx.doi.org/10.1088/2041-8205/713/2/L10",
"service": "https://ui.adsabs.harvard.edu/abs/2010ApJ...713L.103B/DOI:10.1088/2041-8205/713/2/L103"}
r = LinkRequest('2010ApJ...713L.103B', 'DOI', '10.1088,2041-8205,713,2,L10').process_resolver_response(the_json, True)
self.assertEqual(r[1], 302)
the_json = {"action": "redirect",
"link": "http://arxiv.org/abs/1803.03598",
"service": "https://ui.adsabs.harvard.edu/abs/2018arXiv180303598K/ARXIV:1803.03598"}
r = LinkRequest('2018arXiv180303598K', 'ARXIV', '1803.03598').process_resolver_response(the_json, False)
self.assertEqual(r[1], 302)
def test_action_error(self):
"""
Test if unrecognizable action is returned
:return:
"""
the_json = {"action": "redirecterror",
"link": "http://archive.stsci.edu/mastbibref.php?bibcode=2013MNRAS.435.1904M",
"service": "https://ui.adsabs.harvard.edu/abs/2013MNRAS.435.1904/ESOURCE"}
r = LinkRequest('1987gady.book.....B', 'ABSTRACT', '').process_resolver_response(the_json, True)
self.assertEqual(r[1], 400)
def test_route_error_invalid_link_type(self):
"""
Tests for wrong link type
"""
r = self.client.get('/link_gateway/1987gady.book.....B/ERROR')
self.assertNotEqual(r.status_code, 200)
def test_with_header_info(self):
"""
Test sending referrer in header and getting user info
:return:
"""
header = {'Referer': 'https://www.google.com/'}
r = self.client.get('/link_gateway/1987gady.book.....B/ABSTRACT/https://ui.adsabs.harvard.edu/abs/1987gady.book.....B/ABSTRACT', headers=header)
self.assertEqual(r.status_code, 302)
def test_redis_available(self):
"""
Test that redis available
:return:
"""
# Test that redis is available
self.assertNotEqual(self.current_app.extensions['redis'], None)
def test_redis_put_get(self):
"""
add an entry to redis, pass the same session id and verify that it was fetched
next do not pass session id and verifty that it was not fetched
note from python 3 set/get of redis do not accept dict, so need to store dict as json string
also note that from python 3 cookies cannot be included in the header, it has to be added using set_cookie
:return:
"""
# add an entry
account1 = {"source": "session:client_id", "hashed_client_id": "013c1b1280353b3319133b9c528fb29ba998ae3b7af9b669166a786bc6796c9d", "anonymous": True, "hashed_user_id": "ec43c30b9a81ed89765a2b8a04cac38925058eeacd5b5264389b1d4a7df2b28c"}
dict_to_json = json.dumps(account1)
self.current_app.extensions['redis'].set(self.current_app.config['REDIS_NAME_PREFIX']+'key1', dict_to_json)
# verify that when the same session id is passed as cookie, the entry was fetched from redis
self.client.set_cookie('/','session','key1')
r = self.client.get('/link_gateway/2018AAS...23130709A/ABSTRACT/https://ui.adsabs.harvard.edu/abs/2018AAS...23130709A/ABSTRACT', headers={'x-real-ip': '0.0.0.0'})
self.assertEqual(r.headers['user_id'], 'ec43c30b9a81ed89765a2b8a04cac38925058eeacd5b5264389b1d4a7df2b28c')
# verify that when no cookie is send, session_id is None
self.client.cookie_jar.clear()
r = self.client.get('/link_gateway/2018AAS...23130709A/ABSTRACT/https://ui.adsabs.harvard.edu/abs/2018AAS...23130709A/ABSTRACT')
self.assertEqual(r.headers['user_id'], 'None')
def test_redis_exception(self):
"""
set max_connections to 1 and close it, now try to fetch the key to get exception
:return:
"""
self.current_app.extensions['redis']._redis_client.connection_pool.max_connections = 0
self.current_app.extensions['redis']._redis_client.connection_pool.disconnect()
header = {'cookie': 'session=key1', 'x-real-ip': '0.0.0.0'}
r = self.client.get('/link_gateway/2018AAS...23130709A/ABSTRACT/https://ui.adsabs.harvard.edu/abs/2018AAS...23130709A/ABSTRACT', headers=header)
self.assertEqual(r.headers['user_id'], 'None')
def test_adsws_call(self):
"""
:return:
"""
# verify when None is send in for session, account is None
account = LinkRequest('2018AAS...23130709A', 'ABSTRACT', 'https://ui.adsabs.harvard.edu/abs/2018AAS...23130709A/ABSTRACT').get_user_info_from_adsws(None)
self.assertEqual(account, None)
# verify when successfully user info is set, True is returned
with mock.patch.object(self.current_app.client, 'get') as get_mock:
get_mock.return_value = mock_response = mock.Mock()
mock_response.status_code = 200
mock_response.json.return_value = {"source": "session:key1", "hashed_client_id": "013c1b1280353b3319133b9c528fb29ba998ae3b7af9b669166a786bc6796c9d", "anonymous": True, "hashed_user_id": "ec43c30b9a81ed89765a2b8a04cac38925058eeacd5b5264389b1d4a7df2b28c"}
self.client.set_cookie('/', 'session', 'key1')
status = LinkRequest('2018AAS...23130709A', 'ABSTRACT', 'https://ui.adsabs.harvard.edu/abs/2018AAS...23130709A/ABSTRACT').set_user_info(get_mock)
self.assertEqual(status, True)
# verify when successfully user info is read, dict of user is returned
with mock.patch.object(self.current_app.client, 'get') as get_mock:
get_mock.return_value = mock_response = mock.Mock()
mock_response.status_code = 200
mock_response.json.return_value = {"source": "session:key1", "hashed_client_id": "013c1b1280353b3319133b9c528fb29ba998ae3b7af9b669166a786bc6796c9d", "anonymous": True, "hashed_user_id": "ec43c30b9a81ed89765a2b8a04cac38925058eeacd5b5264389b1d4a7df2b28c"}
self.client.set_cookie('/', 'session', 'client_id')
account = LinkRequest('2018AAS...23130709A', 'ABSTRACT', 'https://ui.adsabs.harvard.edu/abs/2018AAS...23130709A/ABSTRACT').get_user_info_from_adsws('key1')
self.assertEqual(account['hashed_client_id'], "013c1b1280353b3319133b9c528fb29ba998ae3b7af9b669166a786bc6796c9d")
self.assertEqual(account['hashed_user_id'], "ec43c30b9a81ed89765a2b8a04cac38925058eeacd5b5264389b1d4a7df2b28c")
def test_verify_url(self):
"""
:return:
"""
with mock.patch.object(self.current_app.client, 'get') as get_mock:
get_mock.return_value = mock_response = mock.Mock()
mock_response.status_code = 200
mock_response.json.return_value = {"link": "verified"}
r = self.client.get('/link_gateway/1987gady.book.....B/ABSTRACT/https://dev.adsabs.harvard.edu/abs/1987gady.book.....B/ABSTRACT')
self.assertEqual(r.status_code, 302)
def test_url_with_params(self):
"""
:return:
"""
with mock.patch.object(self.current_app.client, 'get') as get_mock:
get_mock.return_value = mock_response = mock.Mock()
mock_response.status_code = 200
mock_response.headers = {
'content-type': 'application/json',
'content-length': '149',
}
mock_response.json.return_value = {"link": "verified"}
r = self.client.get('/link_gateway/2021evn..confE...1S/PRESENTATION/https://www.youtube.com/watch?v=NP0yvK2KK7I')
self.assertEqual(r.location, 'https://www.youtube.com/watch?v=NP0yvK2KK7I&')
self.assertEqual(r.status_code, 302)
def test_url_with_params_from_ads(self):
"""
:return:
"""
with mock.patch.object(self.current_app.client, 'get') as get_mock:
get_mock.return_value = mock_response = mock.Mock()
mock_response.status_code = 200
mock_response.headers = {
'content-type': 'application/json',
'content-length': '149',
}
mock_response.json.return_value = {
"service": "https://arxiv.org/abs/2308.16228",
"action": "redirect",
"link": "https://arxiv.org/abs/2308.16228",
"link_type": "ESOURCE|EPRINT_HTML",
}
r = self.client.get('/link_gateway/2023arXiv230816228G/EPRINT_HTML?utm_source=myads&utm_medium=email&utm_campaign=type:arxiv&utm_term=10444&utm_content=rank:3')
self.assertEqual(r.location, 'https://arxiv.org/abs/2308.16228')
self.assertEqual(r.status_code, 302)
if __name__ == '__main__':
unittest.main()