Example #1
0
class TestDeleteEventView(TestCase):
    def setUp(self):
        self.password = "******"
        self.user = User.objects.create_user(username="******",
                                             password=self.password)
        self.now = dt.now().strftime("%Y-%m-%d")
        self.event = Event(date=self.now, author=self.user)
        self.event.save()

    def test_url_resolves_to_view(self):
        found = resolve("/events/delete/")
        self.assertEqual(found.func, bbq_views.delete_event)

    def test_delete_existing_event(self):
        body = {"slug": self.event.slug}

        query_event = Event.objects.filter(slug=self.event.slug)
        self.assertEqual(len(query_event), 1)

        self.client.login(username="******", password="******")
        response = self.client.post(
            "/events/delete/",
            data=json.dumps(body),
            content_type="application/json",
        )

        query_event = Event.objects.filter(slug=self.event.slug)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(query_event), 0)

    def test_delete_non_logged_in(self):
        body = {"slug": self.event.slug}

        response = self.client.post(
            "/events/delete/",
            data=json.dumps(body),
            content_type="application/json",
        )

        url = response.url.split("?")[0]
        self.assertEqual(response.status_code, 302)
        self.assertEqual(url, "/accounts/login/")

    def test_delete_non_existing(self):
        body = {"slug": "does_not_exist"}

        self.client.login(username="******", password="******")
        response = self.client.post(
            "/events/delete/",
            data=json.dumps(body),
            content_type="application/json",
        )

        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.url, "/error/")
Example #2
0
class TestEventsListView(TestCase):
    def setUp(self):
        self.password = "******"
        self.user = User.objects.create_user(username="******",
                                             password=self.password)
        self.now = dt.now().strftime("%Y-%m-%d")
        self.event_1 = Event(date=self.now, author=self.user)
        self.event_1.save()

        self.event_2 = Event(date=self.now, author=self.user)
        self.event_2.save()

    def test_url_resolves_to_view(self):
        found = resolve("/events/list/")
        self.assertEqual(found.func, bbq_views.events_list)

    def test_all_events_shown(self):
        self.client.login(username="******", password="******")
        response = self.client.get("/events/list/")

        request_event_set = {self.event_1, self.event_2}
        response_event_set = {event for event in response.context["events"]}
        self.assertEqual(request_event_set, response_event_set)