1111 # UserFactory,
1212)
1313from api_tests .utils import create_test_file
14+ from elasticsearch_metrics .tests .util import djelme_test_backends
1415
1516
1617COUNTED_USAGE_URL = '/_/metrics/events/counted_usage/'
@@ -69,6 +70,12 @@ def test_required_attributes(self, app, attrs):
6970
7071@pytest .mark .django_db
7172class TestComputedFields :
73+
74+ @pytest .fixture (autouse = True )
75+ def _real_elastic (self ):
76+ with djelme_test_backends ():
77+ yield
78+
7279 @pytest .fixture (autouse = True )
7380 def mock_domain (self ):
7481 domain = 'http://example.foo/'
@@ -81,15 +88,22 @@ def mock_now(self):
8188 with mock .patch ('django.utils.timezone.now' , return_value = timestamp ):
8289 yield timestamp
8390
91+ @pytest .fixture
92+ def preprint (self , request ):
93+ return PreprintFactory (
94+ is_public = True ,
95+ is_published = True ,
96+ )
97+
8498 @pytest .fixture ()
8599 def user (self ):
86100 with mock .patch ('osf.models.base.generate_guid' , return_value = 'guidy' ):
87101 return AuthUserFactory ()
88102
89- def test_by_client_session_id (self , app , mock_save , user ):
103+ def test_by_client_session_id (self , app , mock_save , user , preprint ):
90104 payload = counted_usage_payload (
91105 client_session_id = 'hello' ,
92- item_guid = 'zyxwv' ,
106+ item_guid = preprint . _id ,
93107 action_labels = ['view' , 'api' ],
94108 pageview_info = {'page_url' : 'http://example.foo/blahblah/blee' },
95109 )
@@ -104,7 +118,7 @@ def test_by_client_session_id(self, app, mock_save, user):
104118 expected_doc_id = '3239044c7462dd318edd0522a0ed7d84b9c6502ef16cb40dfcae6c1f456d57a2' ,
105119 expected_attrs = {
106120 'platform_iri' : 'http://example.foo/' ,
107- 'item_guid' : 'zyxwv' ,
121+ 'item_guid' : preprint . _id ,
108122 # session_id: sha256(b'hello|1981-01-01').hexdigest()
109123 'session_id' : '5b7c8b0a740a5b23712258a9d1164d2af008df02a8e3d339f16ead1d19595b34' ,
110124 'action_labels' : ['view' , 'api' ],
@@ -116,10 +130,10 @@ def test_by_client_session_id(self, app, mock_save, user):
116130 },
117131 )
118132
119- def test_by_client_session_id_anon (self , app , mock_save ):
133+ def test_by_client_session_id_anon (self , app , mock_save , preprint ):
120134 payload = counted_usage_payload (
121135 client_session_id = 'hello' ,
122- item_guid = 'zyxwv' ,
136+ item_guid = preprint . _id ,
123137 action_labels = ['view' , 'web' ],
124138 pageview_info = {
125139 'page_url' : 'http://example.foo/bliz/' ,
@@ -137,7 +151,7 @@ def test_by_client_session_id_anon(self, app, mock_save):
137151 expected_doc_id = 'd01759e963893f9dc9b2ccf016a5ef29135673779802b5578f31449543677e82' ,
138152 expected_attrs = {
139153 'platform_iri' : 'http://example.foo/' ,
140- 'item_guid' : 'zyxwv' ,
154+ 'item_guid' : preprint . _id ,
141155 # session_id: sha256(b'hello|1981-01-01').hexdigest()
142156 'session_id' : '5b7c8b0a740a5b23712258a9d1164d2af008df02a8e3d339f16ead1d19595b34' ,
143157 'action_labels' : ['view' , 'web' ],
@@ -151,9 +165,9 @@ def test_by_client_session_id_anon(self, app, mock_save):
151165 },
152166 )
153167
154- def test_by_user_auth (self , app , mock_save , user ):
168+ def test_by_user_auth (self , app , mock_save , user , preprint ):
155169 payload = counted_usage_payload (
156- item_guid = 'yxwvu' ,
170+ item_guid = preprint . _id ,
157171 action_labels = ['view' , 'web' ],
158172 pageview_info = {
159173 'page_url' : 'http://osf.io/mst3k' ,
@@ -171,7 +185,7 @@ def test_by_user_auth(self, app, mock_save, user):
171185 expected_doc_id = '7b8bc27c6d90fb45aa5bbd02deceba9f7384ed61b9a6e7253317c262020b94c2' ,
172186 expected_attrs = {
173187 'platform_iri' : 'http://example.foo/' ,
174- 'item_guid' : 'yxwvu' ,
188+ 'item_guid' : preprint . _id ,
175189 # session_id: sha256(b'guidy|1981-01-01|0').hexdigest()
176190 'session_id' : 'ec768abb16c3411570af99b9d635c2c32d1ca31d1b25eec8ee73759e7242e74a' ,
177191 'action_labels' : ['view' , 'web' ],
@@ -185,10 +199,14 @@ def test_by_user_auth(self, app, mock_save, user):
185199 },
186200 )
187201
188- def test_by_useragent_header (self , app , mock_save ):
202+ def test_by_useragent_header (self , app , mock_save , preprint ):
189203 payload = counted_usage_payload (
190- item_guid = 'yxwvu' ,
204+ item_guid = preprint . _id ,
191205 action_labels = ['view' , 'api' ],
206+ pageview_info = {
207+ 'page_url' : 'http://example.foo/bliz/' ,
208+ 'referer_url' : 'http://elsewhere.baz/index.php' ,
209+ },
192210 )
193211 headers = {
194212 'User-Agent' : 'haha' ,
@@ -198,21 +216,33 @@ def test_by_useragent_header(self, app, mock_save):
198216 assert_saved_with (
199217 mock_save ,
200218 # doc_id: sha256(b'http://example.foo/|yxwvu|97098dd3f7cd26053c0d0264d1c84eaeea8e08d2c55ca34017ffbe53c749ba5a|1981-01-01|3|api,view').hexdigest()
201- expected_doc_id = 'd669528b30f443ffe506e183537af9624ef290090e90a200ecce7b7ca19c77f7 ' ,
219+ expected_doc_id = '6d7549df6734bb955eb832c6316ffae46c2959c95b5817ab4fcb341dbc875c23 ' ,
202220 expected_attrs = {
203221 'platform_iri' : 'http://example.foo/' ,
204- 'item_guid' : 'yxwvu' ,
222+ 'item_guid' : preprint . _id ,
205223 # session_id: sha256(b'localhost:80|haha|1981-01-01|0').hexdigest()
206224 'session_id' : '97098dd3f7cd26053c0d0264d1c84eaeea8e08d2c55ca34017ffbe53c749ba5a' ,
207225 'action_labels' : ['view' , 'api' ],
208- 'pageview_info' : None ,
226+ 'pageview_info' : {
227+ 'page_url' : 'http://example.foo/bliz/' ,
228+ 'page_path' : '/bliz' ,
229+ 'referer_url' : 'http://elsewhere.baz/index.php' ,
230+ 'referer_domain' : 'elsewhere.baz' ,
231+ 'hour_of_day' : 0 ,
232+ },
209233 },
210234 )
211235
212236
213237@pytest .mark .parametrize ('item_public' , [True , False ])
214238@pytest .mark .django_db
215239class TestGuidFields :
240+
241+ @pytest .fixture (autouse = True )
242+ def _real_elastic (self ):
243+ with djelme_test_backends ():
244+ yield
245+
216246 @pytest .fixture
217247 def preprint (self , item_public ):
218248 return PreprintFactory (
@@ -257,7 +287,7 @@ def test_preprint_file(self, app, mock_save, preprint, item_public):
257287 item_guid = preprint ._id ,
258288 action_labels = ['view' , 'web' ],
259289 )
260- resp = app .post_json_api (COUNTED_USAGE_URL , payload )
290+ resp = app .post_json_api (COUNTED_USAGE_URL , payload , headers = { 'User-Agent' : 'blarg' } )
261291 assert resp .status_code == 201
262292 assert_saved_with (
263293 mock_save ,
@@ -276,7 +306,7 @@ def test_preprint_file(self, app, mock_save, preprint, item_public):
276306 item_guid = preprint .primary_file .get_guid (create = True )._id ,
277307 action_labels = ['view' , 'web' ],
278308 )
279- resp = app .post_json_api (COUNTED_USAGE_URL , payload )
309+ resp = app .post_json_api (COUNTED_USAGE_URL , payload , headers = { 'User-Agent' : 'blarg' } )
280310 assert resp .status_code == 201
281311 assert_saved_with (
282312 mock_save ,
@@ -295,7 +325,7 @@ def test_child_registration_file(self, app, mock_save, child_reg_file_guid, chil
295325 item_guid = child_reg_file_guid ,
296326 action_labels = ['view' , 'web' ],
297327 )
298- resp = app .post_json_api (COUNTED_USAGE_URL , payload )
328+ resp = app .post_json_api (COUNTED_USAGE_URL , payload , headers = { 'User-Agent' : 'blarg' } )
299329 assert resp .status_code == 201
300330 assert_saved_with (
301331 mock_save ,
@@ -318,7 +348,7 @@ def test_child_registration_file(self, app, mock_save, child_reg_file_guid, chil
318348 item_guid = child_reg ._id ,
319349 action_labels = ['view' , 'web' ],
320350 )
321- resp = app .post_json_api (COUNTED_USAGE_URL , payload )
351+ resp = app .post_json_api (COUNTED_USAGE_URL , payload , headers = { 'User-Agent' : 'blarg' } )
322352 assert resp .status_code == 201
323353 assert_saved_with (
324354 mock_save ,
@@ -340,7 +370,7 @@ def test_child_registration_file(self, app, mock_save, child_reg_file_guid, chil
340370 item_guid = parent_reg ._id ,
341371 action_labels = ['view' , 'web' ],
342372 )
343- resp = app .post_json_api (COUNTED_USAGE_URL , payload )
373+ resp = app .post_json_api (COUNTED_USAGE_URL , payload , headers = { 'User-Agent' : 'blarg' } )
344374 assert resp .status_code == 201
345375 assert_saved_with (
346376 mock_save ,
0 commit comments