Files
bugsink/events/tests.py
2024-06-12 17:45:06 +02:00

71 lines
3.1 KiB
Python

import datetime
from django.test import TestCase as DjangoTestCase, TransactionTestCase
from django.contrib.auth import get_user_model
from django.conf import settings
from django.utils import timezone
from projects.models import Project, ProjectMembership
from issues.models import Issue
from issues.factories import denormalized_issue_fields
from .factories import create_event
User = get_user_model()
class ViewTests(TransactionTestCase):
# we start with minimal "does this show something and not fully crash" tests and will expand from there.
def setUp(self):
self.user = User.objects.create_user(username='test', password='test')
self.project = Project.objects.create()
ProjectMembership.objects.create(project=self.project, user=self.user)
self.issue = Issue.objects.create(project=self.project, **denormalized_issue_fields())
self.event = create_event(self.project, self.issue)
self.client.force_login(self.user)
def test_event_download(self):
response = self.client.get(f"/events/event/{self.event.pk}/download/")
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'application/json')
self.assertTrue("platform" in response.json())
def test_event_raw(self):
response = self.client.get(f"/events/event/{self.event.pk}/raw/")
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'application/json')
self.assertTrue("platform" in response.json())
def test_event_plaintext(self):
response = self.client.get(f"/events/event/{self.event.pk}/plain/")
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'text/plain')
class TimeZoneTesCase(DjangoTestCase):
"""This class contains some tests that formalize my understanding of how Django works; they are not strictly tests
of bugsink code.
We put this in events/tests.py because that's a place where we use Django's TestCase, and we want to test in that
context, as well as the one of Event models.
"""
def test_datetimes_are_in_utc_when_retrieved_from_the_database_with_default_conf(self):
# check our default conf
self.assertEquals("Europe/Amsterdam", settings.TIME_ZONE)
# save an event in the database; it will be saved in UTC (because that's what Django does)
e = create_event()
# we activate a timezone that is not UTC to ensure our tests run even when we're in a different timezone
with timezone.override('America/Chicago'):
self.assertEquals(datetime.timezone.utc, e.timestamp.tzinfo)
def test_datetimes_are_in_utc_when_retrieved_from_the_database_no_matter_the_active_timezone_when_creating(self):
with timezone.override('America/Chicago'):
# save an event in the database; it will be saved in UTC (because that's what Django does); even when a
# different timezone is active
e = create_event()
self.assertEquals(datetime.timezone.utc, e.timestamp.tzinfo)