|
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 | | - PreprintFactory, |
9 | 10 | NodeFactory, |
| 11 | + PreprintFactory, |
| 12 | + PrivateLinkFactory, |
| 13 | + ProjectFactory, |
10 | 14 | RegistrationFactory, |
11 | 15 | # UserFactory, |
12 | 16 | ) |
| 17 | +from osf.utils.permissions import ADMIN, READ, WRITE |
13 | 18 | from api_tests.utils import create_test_file |
14 | 19 |
|
15 | 20 |
|
@@ -351,3 +356,125 @@ def test_child_registration_file(self, app, mock_save, child_reg_file_guid, chil |
351 | 356 | 'surrounding_guids': None, |
352 | 357 | }, |
353 | 358 | ) |
| 359 | + |
| 360 | + |
| 361 | +@pytest.mark.django_db |
| 362 | +class TestContributorExclusion: |
| 363 | + |
| 364 | + def test_creator_pageview_not_recorded(self, app, mock_save): |
| 365 | + user = AuthUserFactory() |
| 366 | + project = ProjectFactory(creator=user) |
| 367 | + payload = counted_usage_payload( |
| 368 | + item_guid=project._id, |
| 369 | + action_labels=['view', 'web'], |
| 370 | + pageview_info={'page_url': f'https://osf.io/{project._id}/'}, |
| 371 | + ) |
| 372 | + resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=user.auth) |
| 373 | + assert resp.status_code == 204 |
| 374 | + assert mock_save.call_count == 0 |
| 375 | + |
| 376 | + @pytest.mark.parametrize( |
| 377 | + 'permissions', |
| 378 | + [READ, WRITE, ADMIN], |
| 379 | + ids=['read', 'write', 'admin'], |
| 380 | + ) |
| 381 | + def test_contributor_pageview_not_recorded(self, app, mock_save, permissions): |
| 382 | + creator = AuthUserFactory() |
| 383 | + contributor = AuthUserFactory() |
| 384 | + project = ProjectFactory(creator=creator) |
| 385 | + project.add_contributor(contributor, permissions=permissions, auth=Auth(creator)) |
| 386 | + payload = counted_usage_payload( |
| 387 | + item_guid=project._id, |
| 388 | + action_labels=['view', 'web'], |
| 389 | + pageview_info={'page_url': f'https://osf.io/{project._id}/analytics/'}, |
| 390 | + ) |
| 391 | + resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=contributor.auth) |
| 392 | + assert resp.status_code == 204 |
| 393 | + assert mock_save.call_count == 0 |
| 394 | + |
| 395 | + def test_non_contributor_pageview_recorded(self, app, mock_save): |
| 396 | + creator = AuthUserFactory() |
| 397 | + visitor = AuthUserFactory() |
| 398 | + project = ProjectFactory(creator=creator, is_public=True) |
| 399 | + payload = counted_usage_payload( |
| 400 | + item_guid=project._id, |
| 401 | + action_labels=['view', 'web'], |
| 402 | + pageview_info={'page_url': f'https://osf.io/{project._id}/'}, |
| 403 | + ) |
| 404 | + resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=visitor.auth) |
| 405 | + assert resp.status_code == 201 |
| 406 | + assert mock_save.call_count == 1 |
| 407 | + |
| 408 | + def test_parent_contributor_not_on_child_component_pageview_recorded(self, app, mock_save): |
| 409 | + creator = AuthUserFactory() |
| 410 | + child_owner = AuthUserFactory() |
| 411 | + parent_reader = AuthUserFactory() |
| 412 | + parent = ProjectFactory(creator=creator, is_public=True) |
| 413 | + child = NodeFactory(parent=parent, creator=child_owner, is_public=True) |
| 414 | + parent.add_contributor(parent_reader, permissions=ADMIN, auth=Auth(creator)) |
| 415 | + assert not child.contributors_and_group_members.filter(guids___id=parent_reader._id).exists() |
| 416 | + payload = counted_usage_payload( |
| 417 | + item_guid=child._id, |
| 418 | + action_labels=['view', 'web'], |
| 419 | + pageview_info={'page_url': f'https://osf.io/{child._id}/'}, |
| 420 | + ) |
| 421 | + resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=parent_reader.auth) |
| 422 | + assert resp.status_code == 201 |
| 423 | + assert mock_save.call_count == 1 |
| 424 | + |
| 425 | + def test_anonymous_view_only_link_visitor_pageview_recorded(self, app, mock_save): |
| 426 | + creator = AuthUserFactory() |
| 427 | + project = ProjectFactory(creator=creator, is_public=False) |
| 428 | + link = PrivateLinkFactory(anonymous=True, creator=creator) |
| 429 | + link.nodes.add(project) |
| 430 | + payload = counted_usage_payload( |
| 431 | + item_guid=project._id, |
| 432 | + action_labels=['view', 'web'], |
| 433 | + client_session_id='vol-client-session', |
| 434 | + pageview_info={ |
| 435 | + 'page_url': f'https://osf.io/{project._id}/?view_only={link.key}', |
| 436 | + }, |
| 437 | + ) |
| 438 | + resp = app.post_json_api(COUNTED_USAGE_URL, payload) |
| 439 | + assert resp.status_code == 201 |
| 440 | + assert mock_save.call_count == 1 |
| 441 | + |
| 442 | + def test_logged_in_non_contributor_view_only_link_pageview_recorded(self, app, mock_save): |
| 443 | + creator = AuthUserFactory() |
| 444 | + visitor = AuthUserFactory() |
| 445 | + project = ProjectFactory(creator=creator, is_public=False) |
| 446 | + link = PrivateLinkFactory(anonymous=False, creator=creator) |
| 447 | + link.nodes.add(project) |
| 448 | + payload = counted_usage_payload( |
| 449 | + item_guid=project._id, |
| 450 | + action_labels=['view', 'web'], |
| 451 | + pageview_info={ |
| 452 | + 'page_url': f'https://osf.io/{project._id}/files/?view_only={link.key}', |
| 453 | + }, |
| 454 | + ) |
| 455 | + resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=visitor.auth) |
| 456 | + assert resp.status_code == 201 |
| 457 | + assert mock_save.call_count == 1 |
| 458 | + |
| 459 | + @pytest.mark.parametrize( |
| 460 | + 'permissions', |
| 461 | + [READ, WRITE, ADMIN], |
| 462 | + ids=['read', 'write', 'admin'], |
| 463 | + ) |
| 464 | + def test_logged_in_contributor_view_only_link_pageview_not_recorded(self, app, mock_save, permissions): |
| 465 | + creator = AuthUserFactory() |
| 466 | + contributor = AuthUserFactory() |
| 467 | + project = ProjectFactory(creator=creator, is_public=False) |
| 468 | + project.add_contributor(contributor, permissions=permissions, auth=Auth(creator)) |
| 469 | + link = PrivateLinkFactory(anonymous=False, creator=creator) |
| 470 | + link.nodes.add(project) |
| 471 | + payload = counted_usage_payload( |
| 472 | + item_guid=project._id, |
| 473 | + action_labels=['view', 'web'], |
| 474 | + pageview_info={ |
| 475 | + 'page_url': f'https://osf.io/{project._id}/?view_only={link.key}', |
| 476 | + }, |
| 477 | + ) |
| 478 | + resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=contributor.auth) |
| 479 | + assert resp.status_code == 204 |
| 480 | + assert mock_save.call_count == 0 |
0 commit comments