-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add credential endpoint with ability to return only raw data
Signed-off-by: Akiff Manji <akiff.manji@quartech.com>
- Loading branch information
Showing
4 changed files
with
118 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
server/vcr-server/api/v4/tests/test_rest_view_credential.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from django.urls import reverse | ||
from rest_framework.test import APITestCase | ||
|
||
from api.v2.models import Credential, CredentialType, Issuer, Schema, Topic | ||
|
||
|
||
class TestRestViewCredential(APITestCase): | ||
def setUp(self) -> None: | ||
"""Add a test credential type and credential to the database.""" | ||
|
||
self.test_credential_type = CredentialType( | ||
schema=Schema.objects.create( | ||
name="test_schema", version="0.0.1", origin_did="a:did:123" | ||
), | ||
issuer=Issuer.objects.create( | ||
did="a:did:123", | ||
name="Test Issuer 1", | ||
abbreviation="TI-1", | ||
email="", | ||
url="", | ||
), | ||
) | ||
self.test_credential_type.save() | ||
|
||
self.test_topic = Topic(source_id="test_source_id", type="test_topic_type") | ||
self.test_topic.save() | ||
|
||
self.test_credential = Credential( | ||
credential_id="test_credential", | ||
credential_type=self.test_credential_type, | ||
topic=self.test_topic, | ||
) | ||
self.test_credential.save() | ||
|
||
def test_get_credential(self): | ||
"""Test that the API returns the correct credential data.""" | ||
response = self.client.get("/api/v4/credential/test_credential") | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.data.get("credential_id"), "test_credential") | ||
self.assertEqual( | ||
response.data.get("credential_type"), self.test_credential_type.id | ||
) | ||
self.assertIn("attributes", response.data) | ||
self.assertIn("names", response.data) | ||
|
||
def test_get_credential_raw(self): | ||
"""Test that the API returns the correct credential data when raw_data is requested""" | ||
response = self.client.get("/api/v4/credential/test_credential?raw_data=true") | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.data, {}) | ||
self.assertNotIn("credential_id", response.data) | ||
self.assertNotIn("credential_type", response.data) | ||
self.assertNotIn("attributes", response.data) | ||
self.assertNotIn("names", response.data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from django.http import Http404 | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import mixins | ||
from rest_framework.response import Response | ||
from rest_framework.viewsets import GenericViewSet | ||
|
||
from api.v2.models.Credential import Credential | ||
|
||
from api.v4.serializers.rest.credential import CredentialSerializer, RawCredentialSerializer | ||
|
||
class RestView(mixins.RetrieveModelMixin, GenericViewSet): | ||
serializer_class = CredentialSerializer | ||
queryset = Credential.objects.all() | ||
lookup_field = "credential_id" | ||
|
||
def get_serializer_class(self): | ||
if self.request.query_params.get("raw_data", None) == "true": | ||
return RawCredentialSerializer | ||
return CredentialSerializer | ||
|
||
def get_object(self): | ||
credential_id = self.kwargs.get("credential_id", None) | ||
|
||
if not credential_id: | ||
raise Http404() | ||
|
||
filter = {"credential_id": credential_id} | ||
# If the input parameter is a pure int, treat as an internal database pk | ||
try: | ||
filter = {"pk": int(credential_id)} | ||
except (ValueError, TypeError): | ||
pass | ||
|
||
queryset = self.filter_queryset(self.get_queryset()) | ||
obj = get_object_or_404(queryset, **filter) | ||
|
||
# May raise a permission denied | ||
self.check_object_permissions(self.request, obj) | ||
|
||
return obj |