|
| 1 | +import pytest |
| 2 | + |
| 3 | +from osf_tests.factories import ( |
| 4 | + ProjectFactory, |
| 5 | + CollectionFactory, |
| 6 | + AuthUserFactory |
| 7 | +) |
| 8 | +from api.base.settings.defaults import API_BASE |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.django_db |
| 12 | +class TestNodeCollectionsList: |
| 13 | + @pytest.fixture() |
| 14 | + def user(self): |
| 15 | + return AuthUserFactory() |
| 16 | + |
| 17 | + @pytest.fixture() |
| 18 | + def user_two(self): |
| 19 | + return AuthUserFactory() |
| 20 | + |
| 21 | + @pytest.fixture() |
| 22 | + def public_project(self, user): |
| 23 | + return ProjectFactory(is_public=True, creator=user) |
| 24 | + |
| 25 | + @pytest.fixture() |
| 26 | + def private_project(self, user): |
| 27 | + return ProjectFactory(is_public=False, creator=user) |
| 28 | + |
| 29 | + @pytest.fixture() |
| 30 | + def collection(self, user): |
| 31 | + return CollectionFactory(creator=user) |
| 32 | + |
| 33 | + def test_public_node_collections_list_logged_out(self, app, public_project, collection): |
| 34 | + collection.collect_object(public_project, collector=collection.creator) |
| 35 | + res = app.get(f'/{API_BASE}nodes/{public_project._id}/collections/') |
| 36 | + assert res.status_code == 200 |
| 37 | + assert len(res.json['data']) == 1 |
| 38 | + assert res.json['data'][0]['id'] == collection._id |
| 39 | + |
| 40 | + def test_public_node_collections_list_logged_in(self, app, user_two, public_project, collection): |
| 41 | + collection.collect_object(public_project, collector=collection.creator) |
| 42 | + res = app.get(f'/{API_BASE}nodes/{public_project._id}/collections/', auth=user_two.auth) |
| 43 | + assert res.status_code == 200 |
| 44 | + assert len(res.json['data']) == 1 |
| 45 | + assert res.json['data'][0]['id'] == collection._id |
| 46 | + |
| 47 | + def test_private_node_collections_list_admin(self, app, user, private_project, collection): |
| 48 | + collection.collect_object(private_project, collector=user) |
| 49 | + res = app.get(f'/{API_BASE}nodes/{private_project._id}/collections/', auth=user.auth) |
| 50 | + assert res.status_code == 200 |
| 51 | + assert len(res.json['data']) == 1 |
| 52 | + assert res.json['data'][0]['id'] == collection._id |
| 53 | + |
| 54 | + def test_private_node_collections_list_non_contrib(self, app, user_two, private_project, collection): |
| 55 | + collection.collect_object(private_project, collector=collection.creator) |
| 56 | + res = app.get( |
| 57 | + f'/{API_BASE}nodes/{private_project._id}/collections/', |
| 58 | + auth=user_two.auth, |
| 59 | + expect_errors=True |
| 60 | + ) |
| 61 | + assert res.status_code == 403 |
| 62 | + |
| 63 | + def test_private_node_collections_list_logged_out(self, app, private_project, collection): |
| 64 | + collection.collect_object(private_project, collector=collection.creator) |
| 65 | + res = app.get(f'/{API_BASE}nodes/{private_project._id}/collections/', expect_errors=True) |
| 66 | + assert res.status_code == 401 |
| 67 | + |
| 68 | + def test_multiple_collections_linked_to_node(self, app, public_project, user): |
| 69 | + collection1 = CollectionFactory(creator=user) |
| 70 | + collection2 = CollectionFactory(creator=user) |
| 71 | + collection3 = CollectionFactory(creator=user) |
| 72 | + |
| 73 | + collection1.collect_object(public_project, collector=user) |
| 74 | + collection2.collect_object(public_project, collector=user) |
| 75 | + collection3.collect_object(public_project, collector=user) |
| 76 | + |
| 77 | + res = app.get(f'/{API_BASE}nodes/{public_project._id}/collections/') |
| 78 | + assert res.status_code == 200 |
| 79 | + ids = [col['id'] for col in res.json['data']] |
| 80 | + assert set(ids) == {collection1._id, collection2._id, collection3._id} |
| 81 | + |
| 82 | + def test_remove_node_from_collection(self, app, public_project, user): |
| 83 | + collection1 = CollectionFactory(creator=user) |
| 84 | + collection2 = CollectionFactory(creator=user) |
| 85 | + |
| 86 | + collection1.collect_object(public_project, collector=user) |
| 87 | + collection2.collect_object(public_project, collector=user) |
| 88 | + |
| 89 | + # Remove public_project from collection2 |
| 90 | + collection2.collectionsubmission_set.first().delete() |
| 91 | + |
| 92 | + res = app.get(f'/{API_BASE}nodes/{public_project._id}/collections/') |
| 93 | + assert res.status_code == 200 |
| 94 | + ids = [col['id'] for col in res.json['data']] |
| 95 | + assert set(ids) == {collection1._id} |
| 96 | + |
| 97 | + def test_unlinked_collections_not_included(self, app, public_project, user): |
| 98 | + linked = CollectionFactory(creator=user) |
| 99 | + unlinked = CollectionFactory(creator=user) |
| 100 | + |
| 101 | + linked.collect_object(public_project, collector=user) |
| 102 | + |
| 103 | + res = app.get(f'/{API_BASE}nodes/{public_project._id}/collections/') |
| 104 | + assert res.status_code == 200 |
| 105 | + ids = [col['id'] for col in res.json['data']] |
| 106 | + assert linked._id in ids |
| 107 | + assert unlinked._id not in ids |
0 commit comments