-
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.
Merge pull request #795 from bcgov/feat/w3c
feat: add credential endpoints to api/v4
- Loading branch information
Showing
12 changed files
with
126 additions
and
76 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
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
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
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,35 @@ | ||
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} | ||
|
||
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 |
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
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 |
---|---|---|
@@ -1,32 +1,22 @@ | ||
from django.http import Http404, HttpResponse, JsonResponse | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework.decorators import action | ||
from rest_framework.response import Response | ||
from rest_framework.viewsets import ReadOnlyModelViewSet | ||
from django_filters import rest_framework as filters | ||
import json | ||
|
||
from drf_yasg.utils import swagger_auto_schema | ||
|
||
|
||
from api.v2.models.CredentialType import Issuer, Schema | ||
from api.v2.models.CredentialType import Schema | ||
|
||
from api.v4.serializers.rest.credential import SchemaSerializer | ||
|
||
|
||
class RestView(ReadOnlyModelViewSet): | ||
serializer_class = SchemaSerializer | ||
queryset = Schema.objects.all() | ||
filter_backends = (filters.DjangoFilterBackend,) | ||
filter_backends = (filters.DjangoFilterBackend,) | ||
filterset_fields = ("name", "origin_did") | ||
|
||
@swagger_auto_schema(responses={200: SchemaSerializer(many=True)}) | ||
def list(self, request): | ||
data = SchemaSerializer(self.queryset, many=True).data | ||
cred_type_x_schema = {} | ||
for schema in data: | ||
key = f"{schema['name']}:{schema['origin_did']}" | ||
if not key in cred_type_x_schema: | ||
cred_type_x_schema[key] = [] | ||
[cred_type_x_schema[key].append(x) for x in schema['credential_types']] | ||
return Response(cred_type_x_schema) | ||
paging = request.query_params.get("paging", None) | ||
if paging and paging == "false": | ||
self.pagination_class = None | ||
response = super(ReadOnlyModelViewSet, self).list(request) | ||
item_count = self.queryset.count() | ||
response["item_count"] = item_count | ||
return response |
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