|
3 | 3 | import pytest |
4 | 4 | from unittest import mock |
5 | 5 |
|
| 6 | +from framework.auth.core import Auth |
| 7 | + |
6 | 8 | from osf_tests.factories import ( |
7 | 9 | AuthUserFactory, |
8 | 10 | PreprintFactory, |
9 | 11 | NodeFactory, |
| 12 | + ProjectFactory, |
10 | 13 | RegistrationFactory, |
11 | 14 | # UserFactory, |
12 | 15 | ) |
| 16 | +from osf.utils.permissions import READ |
13 | 17 | from api_tests.utils import create_test_file |
14 | 18 |
|
15 | 19 |
|
@@ -351,3 +355,63 @@ def test_child_registration_file(self, app, mock_save, child_reg_file_guid, chil |
351 | 355 | 'surrounding_guids': None, |
352 | 356 | }, |
353 | 357 | ) |
| 358 | + |
| 359 | + |
| 360 | +@pytest.mark.django_db |
| 361 | +class TestContributorExclusion: |
| 362 | + |
| 363 | + def test_creator_pageview_not_recorded(self, app, mock_save): |
| 364 | + user = AuthUserFactory() |
| 365 | + project = ProjectFactory(creator=user) |
| 366 | + payload = counted_usage_payload( |
| 367 | + item_guid=project._id, |
| 368 | + action_labels=['view', 'web'], |
| 369 | + pageview_info={'page_url': f'https://osf.io/{project._id}/'}, |
| 370 | + ) |
| 371 | + resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=user.auth) |
| 372 | + assert resp.status_code == 204 |
| 373 | + assert mock_save.call_count == 0 |
| 374 | + |
| 375 | + def test_read_contributor_pageview_not_recorded(self, app, mock_save): |
| 376 | + creator = AuthUserFactory() |
| 377 | + reader = AuthUserFactory() |
| 378 | + project = ProjectFactory(creator=creator) |
| 379 | + project.add_contributor(reader, permissions=READ, auth=Auth(creator)) |
| 380 | + payload = counted_usage_payload( |
| 381 | + item_guid=project._id, |
| 382 | + action_labels=['view', 'web'], |
| 383 | + pageview_info={'page_url': f'https://osf.io/{project._id}/analytics/'}, |
| 384 | + ) |
| 385 | + resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=reader.auth) |
| 386 | + assert resp.status_code == 204 |
| 387 | + assert mock_save.call_count == 0 |
| 388 | + |
| 389 | + def test_non_contributor_pageview_recorded(self, app, mock_save): |
| 390 | + creator = AuthUserFactory() |
| 391 | + visitor = AuthUserFactory() |
| 392 | + project = ProjectFactory(creator=creator, is_public=True) |
| 393 | + payload = counted_usage_payload( |
| 394 | + item_guid=project._id, |
| 395 | + action_labels=['view', 'web'], |
| 396 | + pageview_info={'page_url': f'https://osf.io/{project._id}/'}, |
| 397 | + ) |
| 398 | + resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=visitor.auth) |
| 399 | + assert resp.status_code == 201 |
| 400 | + assert mock_save.call_count == 1 |
| 401 | + |
| 402 | + def test_parent_contributor_not_on_child_component_pageview_recorded(self, app, mock_save): |
| 403 | + creator = AuthUserFactory() |
| 404 | + child_owner = AuthUserFactory() |
| 405 | + parent_reader = AuthUserFactory() |
| 406 | + parent = ProjectFactory(creator=creator, is_public=True) |
| 407 | + child = NodeFactory(parent=parent, creator=child_owner, is_public=True) |
| 408 | + parent.add_contributor(parent_reader, permissions=READ, auth=Auth(creator)) |
| 409 | + assert not child.contributors_and_group_members.filter(guids___id=parent_reader._id).exists() |
| 410 | + payload = counted_usage_payload( |
| 411 | + item_guid=child._id, |
| 412 | + action_labels=['view', 'web'], |
| 413 | + pageview_info={'page_url': f'https://osf.io/{child._id}/'}, |
| 414 | + ) |
| 415 | + resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=parent_reader.auth) |
| 416 | + assert resp.status_code == 201 |
| 417 | + assert mock_save.call_count == 1 |
0 commit comments