Skip to content

Commit c8b2a6b

Browse files
committed
modify tests
1 parent 92c869f commit c8b2a6b

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

api_tests/metrics/test_counted_usage.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,22 @@ def mock_now(self):
8888
with mock.patch('django.utils.timezone.now', return_value=timestamp):
8989
yield timestamp
9090

91+
@pytest.fixture
92+
def preprint(self, request):
93+
return PreprintFactory(
94+
is_public=True,
95+
is_published=True,
96+
)
97+
9198
@pytest.fixture()
9299
def user(self):
93100
with mock.patch('osf.models.base.generate_guid', return_value='guidy'):
94101
return AuthUserFactory()
95102

96-
def test_by_client_session_id(self, app, mock_save, user):
103+
def test_by_client_session_id(self, app, mock_save, user, preprint):
97104
payload = counted_usage_payload(
98105
client_session_id='hello',
99-
item_guid='zyxwv',
106+
item_guid=preprint._id,
100107
action_labels=['view', 'api'],
101108
pageview_info={'page_url': 'http://example.foo/blahblah/blee'},
102109
)
@@ -111,7 +118,7 @@ def test_by_client_session_id(self, app, mock_save, user):
111118
expected_doc_id='3239044c7462dd318edd0522a0ed7d84b9c6502ef16cb40dfcae6c1f456d57a2',
112119
expected_attrs={
113120
'platform_iri': 'http://example.foo/',
114-
'item_guid': 'zyxwv',
121+
'item_guid': preprint._id,
115122
# session_id: sha256(b'hello|1981-01-01').hexdigest()
116123
'session_id': '5b7c8b0a740a5b23712258a9d1164d2af008df02a8e3d339f16ead1d19595b34',
117124
'action_labels': ['view', 'api'],
@@ -123,10 +130,10 @@ def test_by_client_session_id(self, app, mock_save, user):
123130
},
124131
)
125132

126-
def test_by_client_session_id_anon(self, app, mock_save):
133+
def test_by_client_session_id_anon(self, app, mock_save, preprint):
127134
payload = counted_usage_payload(
128135
client_session_id='hello',
129-
item_guid='zyxwv',
136+
item_guid=preprint._id,
130137
action_labels=['view', 'web'],
131138
pageview_info={
132139
'page_url': 'http://example.foo/bliz/',
@@ -144,7 +151,7 @@ def test_by_client_session_id_anon(self, app, mock_save):
144151
expected_doc_id='d01759e963893f9dc9b2ccf016a5ef29135673779802b5578f31449543677e82',
145152
expected_attrs={
146153
'platform_iri': 'http://example.foo/',
147-
'item_guid': 'zyxwv',
154+
'item_guid': preprint._id,
148155
# session_id: sha256(b'hello|1981-01-01').hexdigest()
149156
'session_id': '5b7c8b0a740a5b23712258a9d1164d2af008df02a8e3d339f16ead1d19595b34',
150157
'action_labels': ['view', 'web'],
@@ -158,9 +165,9 @@ def test_by_client_session_id_anon(self, app, mock_save):
158165
},
159166
)
160167

161-
def test_by_user_auth(self, app, mock_save, user):
168+
def test_by_user_auth(self, app, mock_save, user, preprint):
162169
payload = counted_usage_payload(
163-
item_guid='yxwvu',
170+
item_guid=preprint._id,
164171
action_labels=['view', 'web'],
165172
pageview_info={
166173
'page_url': 'http://osf.io/mst3k',
@@ -178,7 +185,7 @@ def test_by_user_auth(self, app, mock_save, user):
178185
expected_doc_id='7b8bc27c6d90fb45aa5bbd02deceba9f7384ed61b9a6e7253317c262020b94c2',
179186
expected_attrs={
180187
'platform_iri': 'http://example.foo/',
181-
'item_guid': 'yxwvu',
188+
'item_guid': preprint._id,
182189
# session_id: sha256(b'guidy|1981-01-01|0').hexdigest()
183190
'session_id': 'ec768abb16c3411570af99b9d635c2c32d1ca31d1b25eec8ee73759e7242e74a',
184191
'action_labels': ['view', 'web'],
@@ -192,10 +199,14 @@ def test_by_user_auth(self, app, mock_save, user):
192199
},
193200
)
194201

195-
def test_by_useragent_header(self, app, mock_save):
202+
def test_by_useragent_header(self, app, mock_save, preprint):
196203
payload = counted_usage_payload(
197-
item_guid='yxwvu',
204+
item_guid=preprint._id,
198205
action_labels=['view', 'api'],
206+
pageview_info={
207+
'page_url': 'http://example.foo/bliz/',
208+
'referer_url': 'http://elsewhere.baz/index.php',
209+
},
199210
)
200211
headers = {
201212
'User-Agent': 'haha',
@@ -205,14 +216,20 @@ def test_by_useragent_header(self, app, mock_save):
205216
assert_saved_with(
206217
mock_save,
207218
# doc_id: sha256(b'http://example.foo/|yxwvu|97098dd3f7cd26053c0d0264d1c84eaeea8e08d2c55ca34017ffbe53c749ba5a|1981-01-01|3|api,view').hexdigest()
208-
expected_doc_id='d669528b30f443ffe506e183537af9624ef290090e90a200ecce7b7ca19c77f7',
219+
expected_doc_id='6d7549df6734bb955eb832c6316ffae46c2959c95b5817ab4fcb341dbc875c23',
209220
expected_attrs={
210221
'platform_iri': 'http://example.foo/',
211-
'item_guid': 'yxwvu',
222+
'item_guid': preprint._id,
212223
# session_id: sha256(b'localhost:80|haha|1981-01-01|0').hexdigest()
213224
'session_id': '97098dd3f7cd26053c0d0264d1c84eaeea8e08d2c55ca34017ffbe53c749ba5a',
214225
'action_labels': ['view', 'api'],
215-
'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+
},
216233
},
217234
)
218235

0 commit comments

Comments
 (0)