Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with LogViewSet JSON data handling and timestamp parsing #35

Merged
merged 1 commit into from
Jul 15, 2024
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
3 changes: 1 addition & 2 deletions django_walletpass/classviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ class LogViewSet(viewsets.ViewSet):
permission_classes = (AllowAny, )

def create(self, request):
json_body = json.loads(request.body)
for message in json_body['logs']:
for message in request.data.get('logs', []):
log = Log(message=message)
Log.parse_log(log, message)
return Response({}, status=status.HTTP_200_OK)
2 changes: 1 addition & 1 deletion django_walletpass/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def parse_log(cls, log, message):
elif 'warning' in status:
status = 'warning'

log.created_at = datetime.datetime.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S %z")
log.created_at = datetime.datetime.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S %p %z")
log.status = status
log.task_type = task_type
log.device_id = device_id
Expand Down
36 changes: 34 additions & 2 deletions django_walletpass/tests/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import datetime
import json
from unittest import mock

from dateutil.parser import parse
from django.contrib import admin
from django.test import TestCase
from django.urls import reverse
from django.utils import timezone
from rest_framework import status
from rest_framework.test import APITestCase, APIRequestFactory

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


Expand Down Expand Up @@ -116,3 +121,30 @@ def test_push_notification(self, send_notification_mock, get_registrations_mock)
send_notification_mock.assert_called_with(mock.ANY)
request = send_notification_mock.call_args_list[0][0][0]
self.assertEqual(request.message, {"aps": {}})


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

def test_create_log(self):
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."]
}
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)

self.assertEqual(created_log.created_at, expected_utc_timestamp)
Loading