Skip to content

Commit

Permalink
use datetime.timezone.utc
Browse files Browse the repository at this point in the history
to be dj5 ready
  • Loading branch information
patroqueeet committed Aug 22, 2024
1 parent 1d4f0b3 commit 0d08592
Showing 1 changed file with 63 additions and 42 deletions.
105 changes: 63 additions & 42 deletions django_walletpass/tests/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
from django.urls import reverse
from django.utils import timezone
from rest_framework import status
from rest_framework.test import APITestCase, APIRequestFactory
from rest_framework.test import APIRequestFactory, APITestCase

from django_walletpass import crypto
from django_walletpass.admin import PassAdmin
from django_walletpass.classviews import FORMAT, RegisterPassViewSet, LogViewSet
from django_walletpass.models import Pass, PassBuilder, Registration, Log

from django_walletpass.classviews import FORMAT, LogViewSet, RegisterPassViewSet
from django_walletpass.models import Log, Pass, PassBuilder, Registration
from django_walletpass.settings import dwpconfig as WALLETPASS_CONF


Expand Down Expand Up @@ -42,8 +41,8 @@ def test_wallet_pass(self):
instance.save()

self.assertTrue(
"<img src='/static/admin/passbook_icon.svg'/>" in
admin_view.wallet_pass_(instance)
"<img src='/static/admin/passbook_icon.svg'/>"
in admin_view.wallet_pass_(instance)
)


Expand Down Expand Up @@ -147,84 +146,106 @@ def setUp(self):
instance.save()
self.pass_instance = instance
self.pass_type_id = instance.pass_type_identifier
self.device_library_id = 'ebc6fdbd52ffd906fc294aba259f239c'
self.device_library_id = "ebc6fdbd52ffd906fc294aba259f239c"
self.registration = Registration.objects.create(
device_library_identifier=self.device_library_id,
pazz=self.pass_instance
device_library_identifier=self.device_library_id, pazz=self.pass_instance
)
self.view = RegisterPassViewSet.as_view({'delete': 'destroy'})
self.view = RegisterPassViewSet.as_view({"delete": "destroy"})

def test_destroy_registration_exists(self):
existing_url = reverse(
'walletpass_register_pass',
"walletpass_register_pass",
args=[
self.device_library_id,
self.pass_type_id,
self.pass_instance.serial_number
self.pass_instance.serial_number,
],
urlconf='django_walletpass.urls')
urlconf="django_walletpass.urls",
)

self.assertEqual(Registration.objects.count(), 1)
request = self.factory.delete(existing_url,
HTTP_AUTHORIZATION=f'ApplePass {self.pass_instance.authentication_token}')
response = self.view(request,
device_library_id=self.device_library_id,
pass_type_id=self.pass_instance.pass_type_identifier,
serial_number=self.pass_instance.serial_number)
request = self.factory.delete(
existing_url,
HTTP_AUTHORIZATION=f"ApplePass {self.pass_instance.authentication_token}",
)
response = self.view(
request,
device_library_id=self.device_library_id,
pass_type_id=self.pass_instance.pass_type_identifier,
serial_number=self.pass_instance.serial_number,
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertFalse(Registration.objects.filter(device_library_identifier=self.device_library_id,
pazz=self.pass_instance).exists())
self.assertFalse(
Registration.objects.filter(
device_library_identifier=self.device_library_id,
pazz=self.pass_instance,
).exists()
)
self.assertEqual(Registration.objects.count(), 0)

def test_destroy_registration_not_exists(self):
non_existing_device_library_id = 'd4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9'
non_existing_device_library_id = "d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9"
non_existing_url = reverse(
'walletpass_register_pass',
"walletpass_register_pass",
args=[
non_existing_device_library_id,
self.pass_type_id,
self.pass_instance.serial_number
self.pass_instance.serial_number,
],
urlconf='django_walletpass.urls'
urlconf="django_walletpass.urls",
)
self.assertEqual(Registration.objects.count(), 1)

request = self.factory.delete(non_existing_url,
HTTP_AUTHORIZATION=f'ApplePass {self.pass_instance.authentication_token}')
response = self.view(request,
device_library_id=non_existing_device_library_id,
pass_type_id=self.pass_type_id,
serial_number=self.pass_instance.serial_number)
request = self.factory.delete(
non_existing_url,
HTTP_AUTHORIZATION=f"ApplePass {self.pass_instance.authentication_token}",
)
response = self.view(
request,
device_library_id=non_existing_device_library_id,
pass_type_id=self.pass_type_id,
serial_number=self.pass_instance.serial_number,
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(Registration.objects.count(), 1)
self.assertTrue(Registration.objects.filter(device_library_identifier=self.device_library_id,
pazz=self.pass_instance).exists())
self.assertTrue(
Registration.objects.filter(
device_library_identifier=self.device_library_id,
pazz=self.pass_instance,
).exists()
)


class LogViewSetTestCase(APITestCase):
def setUp(self):
self.factory = APIRequestFactory()

def test_create_log(self):
url = reverse('walletpass_log', urlconf='django_walletpass.urls')
url = reverse("walletpass_log", urlconf="django_walletpass.urls")
expected_timestamp_str = "2024-07-08 10:22:35 AM +0200"
data = {
'logs': [f"[{expected_timestamp_str}] Web service error for pass.com.develatio.devpubs.example ("
"https://example.com/passes/): Response to 'What changed?' "
"request included 1 serial numbers but the lastUpdated tag (2024-07-08T08:03:13.588412+00:00) "
"remained the same."]
"logs": [
f"[{expected_timestamp_str}] Web service error for pass.com.develatio.devpubs.example ("
"https://example.com/passes/): Response to 'What changed?' "
"request included 1 serial numbers but the lastUpdated tag (2024-07-08T08:03:13.588412+00:00) "
"remained the same."
]
}
request = self.factory.post(url, data=json.dumps(data), content_type='application/json')
view = LogViewSet.as_view({'post': 'create'})
request = self.factory.post(
url, data=json.dumps(data), content_type="application/json"
)
view = LogViewSet.as_view({"post": "create"})
response = view(request)

self.assertEqual(response.status_code, status.HTTP_200_OK)

created_log = Log.objects.first()
self.assertIsNotNone(created_log)
expected_timestamp = datetime.datetime.strptime(expected_timestamp_str, "%Y-%m-%d %I:%M:%S %p %z")
expected_utc_timestamp = expected_timestamp.astimezone(timezone.utc)
expected_timestamp = datetime.datetime.strptime(
expected_timestamp_str, "%Y-%m-%d %I:%M:%S %p %z"
)
expected_utc_timestamp = expected_timestamp.astimezone(datetime.timezone.utc)

self.assertEqual(created_log.created_at, expected_utc_timestamp)

0 comments on commit 0d08592

Please sign in to comment.