Skip to content

Commit

Permalink
Update datasource detail page only accessible to superuser when deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljcollinsuk committed Oct 11, 2023
1 parent 9c6fc90 commit 7c01998
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
13 changes: 10 additions & 3 deletions controlpanel/frontend/jinja2/datasource-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@
<h1 class="govuk-heading-xl">{{ page_title }}</h1>

<p class="govuk-body">
<a href="{{ bucket.aws_url }}" class="govuk-button govuk-button--secondary" target="_blank" rel="noopener">
Open on AWS
</a>
{% if bucket.is_deleted %}
<p>
This bucket was deleted by <a href="{{ url('manage-user', kwargs={ "pk": bucket.deleted_by.auth0_id }) }}">{{ user_name(bucket.deleted_by) }}</a> on {{ bucket.deleted_at.strftime("%Y/%m/%d %H:%M:%S") }}.
</p>
{% else %}
<a href="{{ bucket.aws_url }}" class="govuk-button govuk-button--secondary" target="_blank" rel="noopener">
Open on AWS
</a>
{% endif %}
</p>

<section class="cpanel-section track_task">
Expand Down Expand Up @@ -212,6 +218,7 @@ <h2 class="govuk-heading-m">Data access log</h2>
</section>
{% endif %}

<!-- TODO replace with a restore button when bucket has been soft-deleted -->
{% if request.user.has_perm('api.destroy_s3bucket', bucket) %}
<section class="cpanel-section">
<form action="{{ url('delete-datasource', kwargs={ "pk": bucket.id }) }}" method="post">
Expand Down
6 changes: 6 additions & 0 deletions controlpanel/frontend/views/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ class BucketDetail(
permission_required = "api.retrieve_s3bucket"
template_name = "datasource-detail.html"

def get_queryset(self):
queryset = super().get_queryset()
if not self.request.user.is_superuser:
queryset = queryset.filter(is_deleted=False)
return queryset

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
bucket = kwargs["object"]
Expand Down
37 changes: 35 additions & 2 deletions tests/frontend/views/test_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ def list_all(client, *args):
return client.get(reverse("list-all-datasources"))


def detail(client, buckets, *args):
def detail(client, buckets, *args, bucket=None):
bucket = bucket or buckets["warehouse1"]
return client.get(
reverse("manage-datasource", kwargs={"pk": buckets["warehouse1"].id})
reverse("manage-datasource", kwargs={"pk": bucket.id})
)


Expand Down Expand Up @@ -416,3 +417,35 @@ def test_delete_calls_soft_delete(client, buckets, users, bucket, success_url):
assert bucket.deleted_by == admin
assert bucket.deleted_at is not None
assert response.url == success_url


@pytest.mark.parametrize(
"user, bucket, expected_status",
[
("superuser", "app_data1", status.HTTP_200_OK),
("superuser", "app_data2", status.HTTP_200_OK),
("superuser", "warehouse1", status.HTTP_200_OK),
("superuser", "warehouse2", status.HTTP_200_OK),
("superuser", "other", status.HTTP_200_OK),
("bucket_viewer", "app_data1", status.HTTP_404_NOT_FOUND),
("bucket_viewer", "app_data2", status.HTTP_404_NOT_FOUND),
("bucket_viewer", "warehouse1", status.HTTP_404_NOT_FOUND),
("bucket_viewer", "warehouse2", status.HTTP_404_NOT_FOUND),
("bucket_viewer", "other", status.HTTP_404_NOT_FOUND),
("bucket_admin", "app_data1", status.HTTP_404_NOT_FOUND),
("bucket_admin", "app_data2", status.HTTP_404_NOT_FOUND),
("bucket_admin", "warehouse1", status.HTTP_404_NOT_FOUND),
("bucket_admin", "warehouse2", status.HTTP_404_NOT_FOUND),
("bucket_admin", "other", status.HTTP_404_NOT_FOUND),
]
)
def test_detail_for_deleted_datasouce(client, buckets, users, user, bucket, expected_status):
user = users[user]
bucket = buckets[bucket]
bucket.soft_delete(deleted_by=user)

client.force_login(user)
response = detail(client, user, bucket=bucket)

assert response.status_code == expected_status

0 comments on commit 7c01998

Please sign in to comment.