def test_reservations_export(self):
        self.login_manager()

        resource = self.create_resource()
        sc = resource.scheduler()

        start = datetime(2012, 2, 1, 12, 0)
        end = datetime(2012, 2, 1, 16, 0)
        dates = (start, end)

        reservation_email = u'*****@*****.**'
        sc.allocate(dates, approve_manually=False, quota=2)[0]

        token1 = sc.reserve(reservation_email,
                            dates,
                            data=utils.mock_data_dictionary({
                                'stop': u'hammertime!',
                                'bust': u'a move!'
                            }))

        token2 = sc.reserve(reservation_email,
                            dates,
                            data=utils.mock_data_dictionary({
                                'never': u'gonna',
                                'give': u'you up'
                            }))

        dataset = exports.reservations.dataset({resource.uuid(): resource},
                                               'en')

        self.assertEqual(len(dataset), 2)
        self.assertEqual(len(dataset.headers), 11 + 4)

        self.assertEqual(dataset.dict[0]['Token'], utils.string_uuid(token1))
        self.assertEqual(dataset.dict[0]['Mocktest.stop'], u'hammertime!')
        self.assertEqual(dataset.dict[0]['Mocktest.bust'], u'a move!')
        self.assertEqual(dataset.dict[0]['Mocktest.never'], None)
        self.assertEqual(dataset.dict[0]['Mocktest.give'], None)

        self.assertEqual(dataset.dict[1]['Token'], utils.string_uuid(token2))
        self.assertEqual(dataset.dict[1]['Mocktest.stop'], None)
        self.assertEqual(dataset.dict[1]['Mocktest.bust'], None)
        self.assertEqual(dataset.dict[1]['Mocktest.never'], u'gonna')
        self.assertEqual(dataset.dict[1]['Mocktest.give'], u'you up')

        # just make sure these don't raise exceptions
        dataset.xls
        dataset.json
        dataset.csv
    def test_pre_reserve_script_variables(self):

        class MockContext(object):
            def getPhysicalPath(self):
                return ['', 'foo', 'bar']

        start, end = datetime.now(), datetime.now()
        data = utils.mock_data_dictionary({
            'is_test': True,
            'description': 'This is the description'
        })

        settings.set('pre_reservation_script', u'exit()')

        locals_ = {}
        run_pre_reserve_script(MockContext(), start, end, data, locals_)

        self.assertEqual(locals_['path'], '/foo/bar')
        self.assertFalse(locals_['is_group_reservation'])
        self.assertEqual(locals_['start'], start)
        self.assertEqual(locals_['end'], end)
        self.assertEqual(locals_['date'], date)
        self.assertEqual(locals_['datetime'], datetime)
        self.assertEqual(locals_['timedelta'], timedelta)
        self.assertTrue(locals_['formset_available']('mock'))
    def test_reservations_export(self):
        self.login_manager()

        resource = self.create_resource()
        sc = resource.scheduler()

        start = datetime(2012, 2, 1, 12, 0)
        end = datetime(2012, 2, 1, 16, 0)
        dates = (start, end)

        reservation_email = u"*****@*****.**"
        sc.allocate(dates, approve_manually=False, quota=2)[0]

        token1 = sc.reserve(
            reservation_email, dates, data=utils.mock_data_dictionary({"stop": u"hammertime!", "bust": u"a move!"})
        )

        token2 = sc.reserve(
            reservation_email, dates, data=utils.mock_data_dictionary({"never": u"gonna", "give": u"you up"})
        )

        dataset = exports.reservations.dataset({resource.uuid(): resource}, "en")

        self.assertEqual(len(dataset), 2)
        self.assertEqual(len(dataset.headers), 11 + 4)

        self.assertEqual(dataset.dict[0]["Token"], utils.string_uuid(token1))
        self.assertEqual(dataset.dict[0]["Mocktest.stop"], u"hammertime!")
        self.assertEqual(dataset.dict[0]["Mocktest.bust"], u"a move!")
        self.assertEqual(dataset.dict[0]["Mocktest.never"], None)
        self.assertEqual(dataset.dict[0]["Mocktest.give"], None)

        self.assertEqual(dataset.dict[1]["Token"], utils.string_uuid(token2))
        self.assertEqual(dataset.dict[1]["Mocktest.stop"], None)
        self.assertEqual(dataset.dict[1]["Mocktest.bust"], None)
        self.assertEqual(dataset.dict[1]["Mocktest.never"], u"gonna")
        self.assertEqual(dataset.dict[1]["Mocktest.give"], u"you up")

        # just make sure these don't raise exceptions
        dataset.xls
        dataset.json
        dataset.csv
    def test_pre_reserve_script_example(self):

        class MockContext(object):
            def getPhysicalPath(self):
                return ['', 'foo', 'bar']

        script = u"""
        if is_group_reservation:
            exit()

        if not formset_available('personalien'):
            exit()

        if personalien.zipcode != '1337':
            error('People with uncool zipcodes are not welcome.')
        """

        settings.set('pre_reservation_script', dedent(script))

        start, end = None, None
        data = utils.mock_data_dictionary({})

        run = lambda: run_pre_reserve_script(MockContext(), start, end, data)

        run()  # exits early because it's a group reservation

        start, end = datetime.now(), datetime.now()

        run()  # exits early because the formset is not available

        data = utils.mock_data_dictionary({
            'zipcode': '1234'
        }, 'personalien')

        self.assertRaises(CustomReservationError, run)  # uncool zipcode

        data = utils.mock_data_dictionary({
            'zipcode': '1337'
        }, 'personalien')

        run()  # ok
    def test_reservations_export(self):
        self.login_manager()

        resource = self.create_resource()
        sc = resource.scheduler()

        start = datetime(2012, 2, 1, 12, 0, tzinfo=timezone('UTC'))
        end = datetime(2012, 2, 1, 16, 0, tzinfo=timezone('UTC'))
        some_date = datetime(2014, 1, 30, 13, 37, tzinfo=timezone('UTC'))
        dates = (start, end)

        sc.allocate(dates, approve_manually=False, quota=2)[0]

        token1 = sc.reserve(
            u'*****@*****.**', dates,
            data=utils.mock_data_dictionary(
                {
                    'stop': u'hammertime!',
                    'bust': u'a move!',
                    'when': some_date
                }
            )
        )

        token2 = sc.reserve(
            u'*****@*****.**', dates,
            data=utils.mock_data_dictionary(
                {
                    'never': u'gonna',
                    'give': u'you up',
                    'when': some_date
                }
            )
        )

        dataset = exports.reservations.dataset(
            {resource.uuid(): resource}, 'en', 'all', 'all'
        )

        self.assertEqual(len(dataset), 2)

        existing_columns = 11
        token_1_unique = 2
        token_2_unique = 2
        token_common = 1

        self.assertEqual(
            len(dataset.headers),
            existing_columns + token_1_unique + token_2_unique + token_common
        )

        self.assertEqual(dataset.dict[0]['Token'], utils.string_uuid(token1))
        self.assertEqual(dataset.dict[0]['Mocktest.stop'], u'hammertime!')
        self.assertEqual(dataset.dict[0]['Mocktest.bust'], u'a move!')
        self.assertEqual(dataset.dict[0]['Mocktest.never'], None)
        self.assertEqual(dataset.dict[0]['Mocktest.give'], None)

        self.assertEqual(dataset.dict[1]['Token'], utils.string_uuid(token2))
        self.assertEqual(dataset.dict[1]['Mocktest.stop'], None)
        self.assertEqual(dataset.dict[1]['Mocktest.bust'], None)
        self.assertEqual(dataset.dict[1]['Mocktest.never'], u'gonna')
        self.assertEqual(dataset.dict[1]['Mocktest.give'], u'you up')

        self.assertEqual(dataset.dict[0]['Start'], start)
        self.assertEqual(dataset.dict[1]['Start'], start)
        self.assertEqual(dataset.dict[0]['End'], end)
        self.assertEqual(dataset.dict[1]['End'], end)

        self.assertEqual(dataset.dict[0]['Mocktest.when'], some_date)
        self.assertEqual(dataset.dict[1]['Mocktest.when'], some_date)

        # just make sure these don't raise exceptions
        for format in ('xls', 'xlsx', 'json', 'csv'):
            transform_record = lambda r: prepare_record(r, format)
            dataset = exports.reservations.dataset(
                {resource.uuid(): resource},
                'en', 'all', 'all', transform_record
            )
            getattr(dataset, format)
示例#6
0
    def test_email(self):
        self.login_manager()

        mail = self.mailhost
        resource = self.create_resource()
        sc = resource.scheduler()

        start = datetime(2012, 2, 29, 15, 0)
        end = datetime(2012, 2, 29, 19, 0)
        dates = (start, end)

        datestr = start.strftime('%d.%m.%Y %H:%M - ') + end.strftime('%H:%M')

        data = utils.mock_data_dictionary(
            {
                'name': u'Björn',
                'icanhazcharacter': '%s'
            }
        )

        def assert_data_in_mail(message, assert_as=True):
            self.assertEqual('name' in message, assert_as)
            self.assertEqual('Bj=C3=B6rn' in message, assert_as)
            self.assertEqual('icanhazcharacter' in message, assert_as)
            self.assertEqual('%s' in message, assert_as)

            # the date should always be there
            self.assertTrue(datestr in message)

        # do an unapproved reservation
        start = datetime(2012, 2, 29, 15, 0)
        end = datetime(2012, 2, 29, 19, 0)
        dates = (start, end)

        # one email is sent because we do not have a manager yet
        session_id = new_uuid()

        allocation = sc.allocate(dates, approve_manually=False)[0]
        token = sc.reserve(
            reservation_email,
            dates, data=data, session_id=session_id
        )
        db.confirm_reservations_for_session(session_id)

        self.assertEqual(len(mail.messages), 1)
        self.assertTrue('Your Reservations' in mail.messages[0])
        self.assertTrue(resource.title in mail.messages[0])
        self.assertTrue(reservation_email in mail.messages[0])

        # autoapproved reservations do not have a star
        self.assertFalse('* {}'.format(resource.title) in mail.messages[0])

        # the mail sending happens before the (automatic) approval,
        # so there should be no change after
        sc.approve_reservation(token)

        self.assertEqual(len(mail.messages), 1)
        mail.messages = []

        # there is an email again when the reservation is revoked
        sc.revoke_reservation(token, u'no-reason')
        self.assertEqual(len(mail.messages), 1)
        self.assertTrue(u'no-reason' in mail.messages[0])
        mail.messages = []

        # unless it should not be sent
        token = sc.reserve(
            reservation_email,
            dates, data=data, session_id=session_id
        )
        sc.approve_reservation(token)
        mail.messages = []
        sc.revoke_reservation(token, u'', send_email=False)
        self.assertEqual(len(mail.messages), 0)

        sc.remove_allocation(allocation.id)

        # make multiple reservations in one session to different email
        # recipients. this should yield multiple mails
        allocation = sc.allocate(dates, approve_manually=False, quota=3)[0]

        tokens = (
            sc.reserve(
                u'*****@*****.**', dates, data=data, session_id=session_id
            ),
            sc.reserve(
                u'*****@*****.**', dates, data=data, session_id=session_id
            ),
            sc.reserve(
                u'*****@*****.**',
                dates, data=data, session_id=session_id
            )
        )
        db.confirm_reservations_for_session(session_id)
        map(sc.deny_reservation, tokens)

        self.assertEqual(len(mail.messages), 2)
        self.assertTrue('*****@*****.**' in mail.messages[0])
        self.assertFalse('*****@*****.**' in mail.messages[0])
        self.assertFalse('*****@*****.**' in mail.messages[1])
        self.assertTrue('*****@*****.**' in mail.messages[1])

        sc.remove_allocation(allocation.id)
        mail.messages = []

        # now let's try with an approved reservation
        allocation = sc.allocate(dates, approve_manually=True)[0]
        token = sc.reserve(
            reservation_email,
            dates, data=data, session_id=session_id
        )
        db.confirm_reservations_for_session(session_id)

        # no manager is defined, so we expect to have one email to the reservee
        self.assertEqual(len(mail.messages), 1)
        self.assertTrue('Your Reservations' in mail.messages[0])
        self.assertTrue(reservation_email in mail.messages[0])

        # reservations which require approval have a star
        self.assertTrue('* {}'.format(resource.title) in mail.messages[0])

        # let's decline that one
        sc.deny_reservation(token)
        self.assertEqual(len(mail.messages), 2)
        self.assertTrue('Denied' in mail.messages[1])
        self.assertTrue(reservation_email in mail.messages[0])
        assert_data_in_mail(mail.messages[1], False)  # no data here

        # add a manager to the resource and start anew
        self.assign_reservation_manager('*****@*****.**', resource)
        mail.messages = []

        token = sc.reserve(
            reservation_email,
            dates, data=data, session_id=session_id
        )
        db.confirm_reservations_for_session(session_id)
        self.assertEqual(len(mail.messages), 2)

        # the first email is the one sent to the reservee
        self.assertTrue('Your Reservations' in mail.messages[0])
        self.assertTrue(reservation_email in mail.messages[0])

        # the second is the on sent to the manager
        self.assertTrue('New Reservation' in mail.messages[1])
        self.assertTrue('To approve' in mail.messages[1])
        self.assertTrue('To deny' in mail.messages[1])
        self.assertTrue(reservation_email in mail.messages[1])
        self.assertTrue(str(token) in mail.messages[1])
        assert_data_in_mail(mail.messages[1])

        # approval results in the last mail
        sc.approve_reservation(token)

        self.assertEqual(len(mail.messages), 3)
        self.assertTrue('Reservation for' in mail.messages[2])
        self.assertTrue(reservation_email in mail.messages[2])

        sc.remove_reservation(token)
        sc.remove_allocation(allocation.id)
        mail.messages = []

        # try an auto approved allocation again, this time we get two emails
        # because there's a manager
        session_id = new_uuid()
        allocation = sc.allocate(dates, approve_manually=False)[0]
        token = sc.reserve(
            reservation_email,
            dates, data=data, session_id=session_id
        )
        db.confirm_reservations_for_session(session_id)

        self.assertEqual(len(mail.messages), 2)

        # the reservee mail is sent first
        self.assertTrue('Your Reservations' in mail.messages[0])
        self.assertTrue(reservation_email in mail.messages[0])

        # the second is the one sent to the manager
        self.assertTrue('New Reservation' in mail.messages[1])
        self.assertFalse('To approve' in mail.messages[1])
        self.assertFalse('To deny' in mail.messages[1])
        self.assertTrue('To cancel' in mail.messages[1])
        self.assertTrue(reservation_email in mail.messages[1])
        self.assertTrue(str(token) in mail.messages[1])
        assert_data_in_mail(mail.messages[1])
示例#7
0
    def test_reservations_export(self):
        self.login_manager()

        resource = self.create_resource()
        sc = resource.scheduler()

        start = datetime(2012, 2, 1, 12, 0, tzinfo=timezone('UTC'))
        end = datetime(2012, 2, 1, 16, 0, tzinfo=timezone('UTC'))
        some_date = datetime(2014, 1, 30, 13, 37, tzinfo=timezone('UTC'))
        dates = (start, end)

        sc.allocate(dates, approve_manually=False, quota=2)[0]

        token1 = sc.reserve(u'*****@*****.**',
                            dates,
                            data=utils.mock_data_dictionary({
                                'stop': u'hammertime!',
                                'bust': u'a move!',
                                'when': some_date
                            }))

        token2 = sc.reserve(u'*****@*****.**',
                            dates,
                            data=utils.mock_data_dictionary({
                                'never': u'gonna',
                                'give': u'you up',
                                'when': some_date
                            }))

        dataset = exports.reservations.dataset({resource.uuid(): resource},
                                               'en', 'all', 'all')

        self.assertEqual(len(dataset), 2)

        existing_columns = 11
        token_1_unique = 2
        token_2_unique = 2
        token_common = 1

        self.assertEqual(
            len(dataset.headers),
            existing_columns + token_1_unique + token_2_unique + token_common)

        self.assertEqual(dataset.dict[0]['Token'], utils.string_uuid(token1))
        self.assertEqual(dataset.dict[0]['Mocktest.stop'], u'hammertime!')
        self.assertEqual(dataset.dict[0]['Mocktest.bust'], u'a move!')
        self.assertEqual(dataset.dict[0]['Mocktest.never'], None)
        self.assertEqual(dataset.dict[0]['Mocktest.give'], None)

        self.assertEqual(dataset.dict[1]['Token'], utils.string_uuid(token2))
        self.assertEqual(dataset.dict[1]['Mocktest.stop'], None)
        self.assertEqual(dataset.dict[1]['Mocktest.bust'], None)
        self.assertEqual(dataset.dict[1]['Mocktest.never'], u'gonna')
        self.assertEqual(dataset.dict[1]['Mocktest.give'], u'you up')

        self.assertEqual(dataset.dict[0]['Start'], start)
        self.assertEqual(dataset.dict[1]['Start'], start)
        self.assertEqual(dataset.dict[0]['End'], end)
        self.assertEqual(dataset.dict[1]['End'], end)

        self.assertEqual(dataset.dict[0]['Mocktest.when'], some_date)
        self.assertEqual(dataset.dict[1]['Mocktest.when'], some_date)

        # just make sure these don't raise exceptions
        for format in ('xls', 'xlsx', 'json', 'csv'):
            transform_record = lambda r: prepare_record(r, format)
            dataset = exports.reservations.dataset({resource.uuid(): resource},
                                                   'en', 'all', 'all',
                                                   transform_record)
            getattr(dataset, format)