Skip to content

Commit

Permalink
fix: Removed share none option
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Feb 14, 2024
1 parent f91b98c commit b296aa4
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright © 2024 Technology Matters
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see https://www.gnu.org/licenses/.

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("core", "0049_sharedresource_share_fields"),
]

operations = [
migrations.RunSQL(
sql="UPDATE core_sharedresource SET share_access ='target_members' WHERE share_access='no';"
),
migrations.AlterField(
model_name="sharedresource",
name="share_access",
field=models.CharField(
choices=[
("all", "Anyone with the link"),
("target_members", "Only target members"),
],
default="target_members",
max_length=32,
),
),
]
10 changes: 3 additions & 7 deletions terraso_backend/apps/core/models/shared_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ class SharedResource(BaseModel):
Target represents the resource that is receiving the shared resource (Example: Landscape).
"""

SHARE_ACCESS_NONE = "no"
SHARE_ACCESS_ALL = "all"
SHARE_ACCESS_TARGET_MEMBERS = "target_members"
DEFAULT_SHARE_ACCESS = SHARE_ACCESS_NONE
DEFAULT_SHARE_ACCESS = SHARE_ACCESS_TARGET_MEMBERS

SHARE_ACCESS_TYPES = (
(SHARE_ACCESS_NONE, _("No share access")),
(SHARE_ACCESS_ALL, _("Anyone with the link")),
(SHARE_ACCESS_TARGET_MEMBERS, _("Only target members")),
)
Expand Down Expand Up @@ -70,10 +68,8 @@ class Meta:
@classmethod
def get_share_access_from_text(cls, share_access):
if not share_access:
return cls.SHARE_ACCESS_NONE
return cls.SHARE_ACCESS_TARGET_MEMBERS
lowered = share_access.lower()
if lowered == cls.SHARE_ACCESS_ALL:
return cls.SHARE_ACCESS_ALL
if lowered == cls.SHARE_ACCESS_TARGET_MEMBERS:
return cls.SHARE_ACCESS_TARGET_MEMBERS
return cls.SHARE_ACCESS_NONE
return cls.SHARE_ACCESS_TARGET_MEMBERS
3 changes: 1 addition & 2 deletions terraso_backend/apps/graphql/schema/shared_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,14 @@ def resolve_shared_resource(root, info, share_uuid=None):
).values("id")
)

share_access_none = Q(share_access=SharedResource.SHARE_ACCESS_NONE)
share_access_all = Q(share_access=SharedResource.SHARE_ACCESS_ALL)
share_access_members = Q(
Q(share_access=SharedResource.SHARE_ACCESS_TARGET_MEMBERS)
& Q(Q(target_object_id__in=user_groups_ids) | Q(target_object_id__in=user_landscape_ids))
)

return SharedResource.objects.filter(
Q(share_uuid=share_uuid) & ~share_access_none & Q(share_access_all | share_access_members)
Q(share_uuid=share_uuid) & Q(share_access_all | share_access_members)
).first()


Expand Down
3 changes: 0 additions & 3 deletions terraso_backend/apps/shared_data/permission_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ def allowed_to_delete_visualization_config(user, visualization_config):
def allowed_to_download_data_entry_file(user, shared_resource):
target = shared_resource.target

if shared_resource.share_access == SharedResource.SHARE_ACCESS_NONE:
return False

if shared_resource.share_access == SharedResource.SHARE_ACCESS_ALL:
return True

Expand Down
4 changes: 1 addition & 3 deletions terraso_backend/apps/shared_data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ def get(self, request, shared_resource_uuid, *args, **kwargs):
if shared_resource is None:
return HttpResponse("Not Found", status=404)

not_shared = shared_resource.share_access == SharedResource.SHARE_ACCESS_NONE
needs_authentication = (
shared_resource.share_access != SharedResource.SHARE_ACCESS_ALL
and not request.user.is_authenticated
)

if not_shared or needs_authentication:
if needs_authentication:
return HttpResponse("Not Found", status=404)

source = shared_resource.source
Expand Down
23 changes: 0 additions & 23 deletions terraso_backend/tests/graphql/test_shared_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,6 @@
pytestmark = pytest.mark.django_db


def test_shared_resource_access_no(client_query, data_entries):
data_entry = data_entries[0]
shared_resource = data_entry.shared_resources.all()[0]

shared_resource.share_access = SharedResource.SHARE_ACCESS_NONE
shared_resource.save()

response = client_query(
"""
{sharedResource(shareUuid: "%s") {
shareAccess
}}
"""
% shared_resource.share_uuid
)

json_response = response.json()

result = json_response["data"]["sharedResource"]

assert result is None


def test_shared_resource_access_all(client_query, data_entries):
data_entry = data_entries[0]
shared_resource = data_entry.shared_resources.all()[0]
Expand Down
14 changes: 0 additions & 14 deletions terraso_backend/tests/shared_data/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,3 @@ def shared_resource_data_entry_shared_target_members_user_1(users):
DataEntry, slug=None, created_by=creator, size=100, entry_type=DataEntry.ENTRY_TYPE_FILE
),
)


@pytest.fixture
def shared_resource_data_entry_shared_no_access(users):
creator = users[0]
return mixer.blend(
SharedResource,
share_access=SharedResource.SHARE_ACCESS_NONE,
share_uuid=uuid.uuid4(),
target=mixer.blend(Group),
source=mixer.blend(
DataEntry, slug=None, created_by=creator, size=100, entry_type=DataEntry.ENTRY_TYPE_FILE
),
)
15 changes: 0 additions & 15 deletions terraso_backend/tests/shared_data/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,3 @@ def test_download_data_entry_file_shared_target_members_fail(
response = logged_client.get(url)

assert response.status_code == 404


@mock.patch("apps.shared_data.models.data_entries.data_entry_file_storage.url")
def test_download_data_entry_file_shared_no_fail(
get_url_mock, logged_client, shared_resource_data_entry_shared_no_access
):
redirect_url = "https://example.org/s3_file.json"
get_url_mock.return_value = redirect_url
url = reverse(
"shared_data:download",
kwargs={"shared_resource_uuid": shared_resource_data_entry_shared_no_access.share_uuid},
)
response = logged_client.get(url)

assert response.status_code == 404

0 comments on commit b296aa4

Please sign in to comment.