~homeworkprod/byceps

ref: 1181a35dd06d48c779208a6d2a4b3672dd38d812 byceps/tests/services/ticketing/test_user_check_in.py -rw-r--r-- 2.7 KiB
1181a35d — Jochen Kupperschmidt Ported tests from nose2 to pytest 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
:Copyright: 2006-2017 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from pytest import raises

from byceps.services.ticketing import category_service, event_service, \
    ticket_service
from byceps.services.ticketing.ticket_service import TicketIsRevoked, \
    TicketLacksUser, UserAlreadyCheckIn

from tests.base import AbstractAppTestCase


class UserCheckInTest(AbstractAppTestCase):

    def setUp(self):
        super().setUp()

        self.create_brand_and_party()

        self.category_id = self.create_category('Premium').id
        self.orga_id = self.create_user('Party_Orga').id
        self.owner_id = self.create_user('Ticket_Owner').id
        self.user_id = self.create_user('Ticket_User').id

    def test_check_in_user(self):
        ticket_before = ticket_service.create_ticket(self.category_id, self.owner_id)

        ticket_before.used_by_id = self.user_id
        self.db.session.commit()

        assert not ticket_before.user_checked_in

        events_before = event_service.get_events_for_ticket(ticket_before.id)
        assert len(events_before) == 0

        # -------------------------------- #

        ticket_id = ticket_before.id

        ticket_service.check_in_user(ticket_id, self.orga_id)

        # -------------------------------- #

        ticket_after = ticket_service.find_ticket(ticket_id)
        assert ticket_before.user_checked_in

        events_after = event_service.get_events_for_ticket(ticket_after.id)
        assert len(events_after) == 1

        ticket_revoked_event = events_after[0]
        assert ticket_revoked_event.event_type == 'user-checked-in'
        assert ticket_revoked_event.data == {
            'checked_in_user_id': str(self.user_id),
            'initiator_id': str(self.orga_id),
        }

    def test_check_in_user_with_revoked_ticket(self):
        ticket = ticket_service.create_ticket(self.category_id, self.owner_id)

        ticket.revoked = True
        ticket.used_by_id = self.user_id
        self.db.session.commit()

        with raises(TicketIsRevoked):
            ticket_service.check_in_user(ticket.id, self.orga_id)

    def test_check_in_user_with_ticket_user_already_checked_in(self):
        ticket = ticket_service.create_ticket(self.category_id, self.owner_id)

        ticket.used_by_id = self.user_id
        ticket.user_checked_in = True
        self.db.session.commit()

        with raises(UserAlreadyCheckIn):
            ticket_service.check_in_user(ticket.id, self.orga_id)

    # -------------------------------------------------------------------- #
    # helpers

    def create_category(self, title):
        return category_service.create_category(self.party.id, title)