Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
from django.conf import settings
from django.test import TestCase
from django.urls import reverse
from rest_framework_simplejwt.tokens import RefreshToken

from gene2phenotype_app.models import (
LGDVariantTypeComment,
LocusGenotypeDisease,
User,
)


class LGDEditVariantTypeCommentEndpoint(TestCase):
"""
Test endpoint to delete variant type comments from a record (LGD)
"""

fixtures = [
"gene2phenotype_app/fixtures/attribs.json",
"gene2phenotype_app/fixtures/cv_molecular_mechanism.json",
"gene2phenotype_app/fixtures/disease.json",
"gene2phenotype_app/fixtures/g2p_stable_id.json",
"gene2phenotype_app/fixtures/lgd_panel.json",
"gene2phenotype_app/fixtures/locus_genotype_disease.json",
"gene2phenotype_app/fixtures/locus.json",
"gene2phenotype_app/fixtures/publication.json",
"gene2phenotype_app/fixtures/sequence.json",
"gene2phenotype_app/fixtures/user_panels.json",
"gene2phenotype_app/fixtures/ontology_term.json",
"gene2phenotype_app/fixtures/source.json",
"gene2phenotype_app/fixtures/lgd_publication.json",
"gene2phenotype_app/fixtures/lgd_variant_type.json",
"gene2phenotype_app/fixtures/lgd_variant_type_publication.json",
"gene2phenotype_app/fixtures/lgd_variant_type_comment.json",
]

def setUp(self):
self.url_delete = reverse(
"lgd_variant_type_comment", kwargs={"stable_id": "G2P00002"}
)
self.comment_to_delete = {"comment_id": 1}

def _authenticate(self, email):
user = User.objects.get(email=email)
refresh = RefreshToken.for_user(user)
access_token = str(refresh.access_token)
self.client.cookies[settings.SIMPLE_JWT["AUTH_COOKIE"]] = access_token

def test_invalid_delete(self):
"""
Cannot delete a variant type comment that does not exist.
"""
self._authenticate("john@test.ac.uk")

response = self.client.patch(
self.url_delete, {"comment_id": 1000}, content_type="application/json"
)
self.assertEqual(response.status_code, 404)
self.assertEqual(
response.json()["error"],
"Cannot find variant type comment for record 'G2P00002'",
)

response_2 = self.client.patch(
self.url_delete, {}, content_type="application/json"
)
self.assertEqual(response_2.status_code, 400)

def test_delete_non_superuser(self):
"""
Only super users can delete variant type comments.
"""
self._authenticate("mary@test.ac.uk")

response = self.client.patch(
self.url_delete, self.comment_to_delete, content_type="application/json"
)
self.assertEqual(response.status_code, 403)
self.assertEqual(
response.json()["error"],
"You do not have permission to perform this action.",
)

def test_delete_no_permission(self):
"""
Super users still need panel permission to edit the record.
"""
self._authenticate("sofia@test.ac.uk")

response = self.client.patch(
self.url_delete, self.comment_to_delete, content_type="application/json"
)
self.assertEqual(response.status_code, 403)
self.assertEqual(
response.json()["error"], "No permission to update record 'G2P00002'"
)

def test_lgd_variant_type_comment_delete(self):
"""
Test successfully deleting the variant type comment from the record (LGD)
"""
self._authenticate("john@test.ac.uk")

lgd_variant_type_comments = LGDVariantTypeComment.objects.filter(
lgd_variant_type__lgd__stable_id__stable_id="G2P00002", is_deleted=0
)
self.assertEqual(len(lgd_variant_type_comments), 1)

response = self.client.patch(
self.url_delete, self.comment_to_delete, content_type="application/json"
)
self.assertEqual(response.status_code, 200)

lgd_deleted_variant_type_comments = LGDVariantTypeComment.objects.filter(
lgd_variant_type__lgd__stable_id__stable_id="G2P00002", is_deleted=1
)
self.assertEqual(len(lgd_deleted_variant_type_comments), 1)

history_records = LGDVariantTypeComment.history.all()
self.assertEqual(len(history_records), 1)
self.assertEqual(history_records[0].is_deleted, 1)

history_records_lgd = LocusGenotypeDisease.history.all()
self.assertEqual(len(history_records_lgd), 0)
7 changes: 7 additions & 0 deletions gene2phenotype_project/gene2phenotype_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ def perform_create(self, serializer):
views.LGDEditVariantTypes.as_view(),
name="lgd_variant_type",
),
# Delete variant type comment from LGD record.
# Action: PATCH
path(
"lgd/<str:stable_id>/variant_type/comment/",
views.LGDEditVariantTypeComment.as_view(),
name="lgd_variant_type_comment",
),
# Add or delete variant description(s) from LGD record.
# Actions: PATCH (to delete one variant description), POST (to add multiple variant descriptions)
path(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
LocusGenotypeDiseaseDetail,
LGDEditCCM,
LGDEditComment,
LGDEditVariantTypeComment,
LGDEditVariantConsequences,
LGDEditVariantTypes,
LGDEditVariantTypeDescriptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,74 @@ def patch(self, request, stable_id):
)


@extend_schema(exclude=True)
class LGDEditVariantTypeComment(CustomPermissionAPIView):
"""
Delete a variant type comment from a G2P record (LGD).
"""

http_method_names = ["patch", "options"]

method_permissions = {
"patch": [permissions.IsAuthenticated, IsSuperUser],
}

@transaction.atomic
def patch(self, request, stable_id):
"""
Soft-delete a variant type comment by comment_id.
"""
comment_id = request.data.get("comment_id", None)
user = request.user

if not comment_id:
return Response(
{"error": "Missing input key 'comment_id'"},
status=status.HTTP_400_BAD_REQUEST,
)

lgd_obj = get_object_or_404(
LocusGenotypeDisease, stable_id__stable_id=stable_id, is_deleted=0
)

# Check if user has permission to update panel
user_obj = get_object_or_404(User, email=user, is_active=1)
serializer_user = UserSerializer(user_obj, context={"user": user})
user_panel_list = [panel for panel in serializer_user.panels_names(user_obj)]
has_common = LocusGenotypeDiseaseSerializer(
lgd_obj, context={"user": user}
).check_user_permission(lgd_obj, user_panel_list)
if has_common is False:
return Response(
{"error": f"No permission to update record '{stable_id}'"},
status=status.HTTP_403_FORBIDDEN,
)

try:
variant_type_comment = LGDVariantTypeComment.objects.get(
lgd_variant_type__lgd=lgd_obj, id=comment_id, is_deleted=0
)
except LGDVariantTypeComment.DoesNotExist:
return Response(
{
"error": f"Cannot find variant type comment for record '{stable_id}'"
},
status=status.HTTP_404_NOT_FOUND,
)

variant_type_comment.is_deleted = 1
variant_type_comment.save()
lgd_obj.date_review = get_date_now()
lgd_obj.save_without_historical_record()

return Response(
{
"message": f"Variant type comment successfully deleted for record '{stable_id}'"
},
status=status.HTTP_200_OK,
)


@extend_schema(exclude=True)
class LGDEditReview(APIView):
http_method_names = ["post", "options"]
Expand Down
Loading