-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added model for QR code, viewset and serializer * finished create and destroy methods, and mades tests for these * altered create viewset to take user from request * fixed tests * changed status code for deletion of qr code to 200 since frontend dosent accept 204 status code.... * format * removed earlier unused method * format * Trigger Build * changed exception to take all errors with making a blob * format * added azure key to settings file * format * changed name of exception for blbo not found * altered ci.yaml file to add azure connecntion string as env * fix ci file * changed model to take content, and serializer not upload image to azure * format
- Loading branch information
Showing
16 changed files
with
290 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import factory | ||
from factory.django import DjangoModelFactory | ||
|
||
from app.content.factories.user_factory import UserFactory | ||
from app.content.models import QRCode | ||
|
||
|
||
class QRCodeFactory(DjangoModelFactory): | ||
class Meta: | ||
model = QRCode | ||
|
||
name = factory.Sequence(lambda n: f"QRCode {n}") | ||
user = factory.SubFactory(UserFactory) | ||
image = "https://tihldestorage.blob.core.windows.net/imagepng/0331423a-11b3-4e6b-a505-f84e0991b696TestCode" |
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,47 @@ | ||
# Generated by Django 4.2.5 on 2023-10-08 20:45 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("content", "0053_event_contact_person"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="QRCode", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("created_at", models.DateTimeField(auto_now_add=True)), | ||
("updated_at", models.DateTimeField(auto_now=True)), | ||
("image", models.URLField(blank=True, max_length=600, null=True)), | ||
("image_alt", models.CharField(blank=True, max_length=200, null=True)), | ||
("name", models.CharField(max_length=50)), | ||
("url", models.URLField(max_length=600)), | ||
( | ||
"user", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="qr_codes", | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
options={ | ||
"verbose_name": "qr_code", | ||
"verbose_name_plural": "qr_codes", | ||
}, | ||
), | ||
] |
23 changes: 23 additions & 0 deletions
23
app/content/migrations/0055_remove_qrcode_url_qrcode_content.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,23 @@ | ||
# Generated by Django 4.2.5 on 2023-10-21 13:57 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("content", "0054_qrcode"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="qrcode", | ||
name="url", | ||
), | ||
migrations.AddField( | ||
model_name="qrcode", | ||
name="content", | ||
field=models.CharField(default=None, max_length=600), | ||
preserve_default=False, | ||
), | ||
] |
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 |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
get_strike_description, | ||
get_strike_strike_size, | ||
) | ||
from app.content.models.qr_code import QRCode |
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,22 @@ | ||
from django.db import models | ||
|
||
from app.common.enums import Groups | ||
from app.common.permissions import BasePermissionModel | ||
from app.content.models import User | ||
from app.util.models import BaseModel, OptionalImage | ||
|
||
|
||
class QRCode(BaseModel, OptionalImage, BasePermissionModel): | ||
write_access = (Groups.TIHLDE,) | ||
read_access = (Groups.TIHLDE,) | ||
|
||
name = models.CharField(max_length=50) | ||
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="qr_codes") | ||
content = models.CharField(max_length=600) | ||
|
||
class Meta: | ||
verbose_name = "qr_code" | ||
verbose_name_plural = "qr_codes" | ||
|
||
def __str__(self): | ||
return f"{self.name} - {self.user.user_id}" |
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,19 @@ | ||
from rest_framework import serializers | ||
|
||
from app.common.serializers import BaseModelSerializer | ||
from app.content.models import QRCode | ||
|
||
|
||
class QRCodeSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = QRCode | ||
fields = ("id", "name", "created_at", "updated_at", "content") | ||
|
||
|
||
class QRCodeCreateSerializer(BaseModelSerializer): | ||
class Meta: | ||
model = QRCode | ||
fields = ( | ||
"name", | ||
"content", | ||
) |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
|
||
from app.common.permissions import BasicViewPermission | ||
from app.common.viewsets import BaseViewSet | ||
from app.content.models import QRCode, User | ||
from app.content.serializers.qr_code import ( | ||
QRCodeCreateSerializer, | ||
QRCodeSerializer, | ||
) | ||
|
||
|
||
class QRCodeViewSet(BaseViewSet): | ||
serializer_class = QRCodeSerializer | ||
queryset = QRCode.objects.all() | ||
permission_classes = [BasicViewPermission] | ||
|
||
def get_queryset(self): | ||
if hasattr(self, "action") and self.action == "retrieve": | ||
return super().get_queryset() | ||
user = get_object_or_404(User, user_id=self.request.id) | ||
return super().get_queryset().filter(user=user) | ||
|
||
def create(self, request, *args, **kwargs): | ||
user = get_object_or_404(User, user_id=request.id) | ||
data = request.data | ||
|
||
serializer = QRCodeCreateSerializer(data=data, context={"request": request}) | ||
|
||
if serializer.is_valid(): | ||
qr_code = super().perform_create(serializer, user=user) | ||
serializer = QRCodeSerializer(qr_code, context={"request": request}) | ||
return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
|
||
return Response( | ||
{"detail": serializer.errors}, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
|
||
def destroy(self, request, *args, **kwargs): | ||
super().destroy(request, *args, **kwargs) | ||
return Response({"detail": "QR-koden ble slettet"}, status=status.HTTP_200_OK) |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from rest_framework import status | ||
|
||
import pytest | ||
|
||
API_QR_CODE_BASE_URL = "/qr-codes/" | ||
|
||
|
||
def get_data(): | ||
return {"name": "Test QR Code", "content": "https://tihlde.org"} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_list_qr_codes_as_anonymous_user(default_client): | ||
""" | ||
An anonymous user should not be able to list QR Codes. | ||
""" | ||
|
||
response = default_client.get(API_QR_CODE_BASE_URL) | ||
|
||
assert response.status_code == status.HTTP_403_FORBIDDEN | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_list_qr_codes_as_member(member, api_client): | ||
""" | ||
A member of TIHLDE should be able to list QR Codes. | ||
""" | ||
|
||
client = api_client(user=member) | ||
response = client.get(API_QR_CODE_BASE_URL) | ||
|
||
assert response.status_code == status.HTTP_200_OK | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_create_qr_code_as_member(member, api_client): | ||
""" | ||
A member of TIHLDE should be able to create a QR Code. | ||
""" | ||
|
||
data = get_data() | ||
|
||
client = api_client(user=member) | ||
response = client.post(API_QR_CODE_BASE_URL, data=data) | ||
|
||
assert response.status_code == status.HTTP_201_CREATED | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_create_qr_code_as_anonymous_user(default_client): | ||
""" | ||
An anonymous user should not be able to create a QR Code. | ||
""" | ||
|
||
data = get_data() | ||
|
||
response = default_client.post(API_QR_CODE_BASE_URL, data=data) | ||
|
||
assert response.status_code == status.HTTP_403_FORBIDDEN | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_delete_qr_code_with_invalid_blob_as_member(member, api_client, qr_code): | ||
""" | ||
A member of TIHLDE should be able to delete a QR Code. | ||
""" | ||
|
||
qr_code.user = member | ||
qr_code.save() | ||
|
||
client = api_client(user=member) | ||
response = client.delete(f"{API_QR_CODE_BASE_URL}{qr_code.id}/") | ||
|
||
assert response.status_code == status.HTTP_200_OK | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_delete_qr_code_with_invalid_blob_as_anonymous_user(default_client, qr_code): | ||
""" | ||
An anonymous user should not be able to delete a QR Code. | ||
""" | ||
|
||
response = default_client.delete(f"{API_QR_CODE_BASE_URL}{qr_code.id}/") | ||
|
||
assert response.status_code == status.HTTP_403_FORBIDDEN | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_delete_other_users_qr_code(member, api_client, qr_code): | ||
""" | ||
A member of TIHLDE should not be able to delete another users QR Code. | ||
""" | ||
|
||
client = api_client(user=member) | ||
response = client.delete(f"{API_QR_CODE_BASE_URL}{qr_code.id}/") | ||
|
||
assert response.status_code == status.HTTP_404_NOT_FOUND |