def test_mail_not_send(self):
        contact = cfactories.Contact()
        org_name = 'Test org'
        org_phone = '1234 56'

        invitee = uuid.uuid4()

        appointment = Appointment.objects.create(
            owner=self.user_uuid,
            name="Test Name 2",
            start_date=datetime(2018, 1, 1, 12, 15, tzinfo=pytz.UTC),
            end_date=datetime(2018, 1, 1, 12, 30, tzinfo=pytz.UTC),
            address="Oderberger Straße 16A",
            invitee_uuids=[invitee],
            notes="Test note",
            type=["Test Type"],
            organization_uuid=uuid.uuid4(),
            contact_uuid=str(contact.uuid))

        appointment_notification = AppointmentNotification.objects.create(
            subject="Test Mail",
            message="Test Body",
            recipient='*****@*****.**',
            appointment=appointment,
            sent_at=datetime.now(),
            org_name=org_name,
            org_phone=org_phone)

        ap_mail = AppointmentNotificationEMail(appointment_notification)

        ap_mail.notify_recipient()

        self.assertEqual(len(mail.outbox), 0)
    def test_message_body_generation(self):
        contact = cfactories.Contact()

        invitee = uuid.uuid4()

        appointment = Appointment.objects.create(
            owner=self.user_uuid,
            name="Test Name 2",
            start_date=datetime(2018, 1, 1, 12, 15, tzinfo=pytz.UTC),
            end_date=datetime(2018, 1, 1, 12, 30, tzinfo=pytz.UTC),
            address="Oderberger Straße 16A",
            invitee_uuids=[invitee],
            notes="Test note",
            type=["Test Type"],
            organization_uuid=uuid.uuid4(),
            contact_uuid=str(contact.uuid))

        appointment_notification = AppointmentNotification.objects.create(
            subject="Test Mail",
            message="Test Body",
            recipient='*****@*****.**',
            appointment=appointment,
            sent_at=datetime.now())

        ap_mail = AppointmentNotificationEMail(appointment_notification)

        message = ap_mail.generate_message()[1]

        regex = r'.*{}.*'.format(appointment.start_date.strftime('%M:%H'))
        self.assertRegexpMatches(message, regex)
        self.assertEqual(len(mail.outbox), 0)
    def test_message_subject_generation(self):
        contact = cfactories.Contact()
        invitee = uuid.uuid4()

        time_offset = 4
        start_date = datetime.utcnow().replace(tzinfo=pytz.utc) + timedelta(
            days=time_offset)
        end_date = start_date + timedelta(hours=2)

        appointment = Appointment.objects.create(
            owner=self.user_uuid,
            name="Test Name 2",
            start_date=start_date,
            end_date=end_date,
            address="Oderberger Straße 16A",
            invitee_uuids=[invitee],
            notes="Test note",
            type=["Test Type"],
            organization_uuid=uuid.uuid4(),
            contact_uuid=str(contact.uuid))

        appointment_notification = AppointmentNotification.objects.create(
            recipient='*****@*****.**',
            appointment=appointment,
            sent_at=datetime.now())

        ap_mail = AppointmentNotificationEMail(appointment_notification)

        subject = ap_mail.generate_message()[0]

        regex = r'Erinnerung an Ihren Termin in {} Tagen.*'.format(
            time_offset - 1)
        self.assertRegexpMatches(subject, regex)
        self.assertEqual(len(mail.outbox), 0)
    def test_list_appointments_denormalized(self):
        contact = contact_mfactories.Contact()
        wflvl2_uuid = uuid.uuid4()
        mfactories.Appointment(
            name='John Tester',
            contact_uuid=contact.uuid,
            organization_uuid=self.organization_uuid,
            workflowlevel2_uuids=[wflvl2_uuid]
        )

        request = self.factory.get('?denormalize=true')
        request.user = self.user
        request.session = self.session
        view = AppointmentViewSet.as_view({'get': 'list'})
        response = view(request)

        appointment_data = response.data['results'][0]

        self.assertTrue('id' in appointment_data)
        self.assertEqual(appointment_data['name'], 'John Tester')
        self.assertEqual(appointment_data['workflowlevel2_uuids'][0],
                         str(wflvl2_uuid))
        self.assertEqual(appointment_data['contact_uuid'], str(contact.uuid))
        self.assertEqual(appointment_data['contact']['first_name'],
                         contact.first_name)
        self.assertEqual(appointment_data['contact']['middle_name'],
                         contact.middle_name)
        self.assertEqual(appointment_data['contact']['last_name'],
                         contact.last_name)
    def test_paginate_and_denormalize(self):
        contact = contact_mfactories.Contact(
            first_name='Kamasi', last_name='Washington')
        for i in range(0, 2):
            wflvl2_uuid = uuid.uuid4()
            mfactories.Appointment(
                name='John Tester',
                contact_uuid=contact.uuid,
                organization_uuid=self.organization_uuid,
                workflowlevel2_uuids=[wflvl2_uuid]
            )

        request = self.factory.get(
            '?paginate=true&page_size=1&denormalize=true')
        request.user = self.user
        request.session = self.session
        view = AppointmentViewSet.as_view({'get': 'list'})
        response = view(request)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data['results']), 1)
        self.assertEqual(response.data['results'][0]['contact']['first_name'],
                         'Kamasi')
        self.assertEqual(response.data['results'][0]['contact']['last_name'],
                         'Washington')
        self.assertIsNotNone(response.data['next'])
        self.assertIsNone(response.data['previous'])
 def test_contact_found(self):
     contact = cfactories.Contact()
     obj = Mock(contact_uuid=contact.uuid)
     field = ContactNameField()
     self.assertEqual(
         field.get_attribute(obj), {
             'first_name': contact.first_name,
             'last_name': contact.last_name,
             'middle_name': contact.middle_name,
             'uuid': contact.uuid
         })
    def test_retrieve_appointment_denormalized(self):
        contact = contact_mfactories.Contact()
        invitee_user_uuid = uuid.uuid4()

        appointment = mfactories.Appointment(
            name='John Tester',
            contact_uuid=contact.uuid,
            owner=self.user_uuid,
            invitee_uuids=[invitee_user_uuid],
            organization_uuid=self.organization_uuid
        )

        request = self.factory.get('?denormalize=true')
        request.user = self.user
        request.session = self.session
        view = AppointmentViewSet.as_view({'get': 'retrieve'})
        response = view(request, pk=appointment.pk)
        self.assertEqual(response.status_code, 200)

        appointment_data = appointment.__dict__
        appointment_data['invitee_uuids'][0] = str(
            appointment_data['invitee_uuids'][0])

        for field in ('owner', '_state'):
            del appointment_data[field]

        for field in ('contact_uuid', 'organization_uuid', 'uuid'):
            appointment_data[field] = str(appointment_data[field])

        data = response.data

        local = pytz.timezone(settings.TIME_ZONE)
        start_date_naive = datetime.strptime(
            data['start_date'], "%Y-%m-%dT%H:%M:%S+01:00")
        start_date_local = local.localize(start_date_naive, is_dst=None)
        data['start_date'] = start_date_local.astimezone(pytz.utc)

        end_date_naive = datetime.strptime(
            data['end_date'], "%Y-%m-%dT%H:%M:%S+01:00")
        end_date_local = local.localize(end_date_naive, is_dst=None)
        data['end_date'] = end_date_local.astimezone(pytz.utc)

        self.assertDictContainsSubset(appointment_data, data)
        self.assertEqual(data['contact_uuid'], str(contact.uuid))
        self.assertEqual(data['contact']['first_name'],
                         contact.first_name)
        self.assertEqual(data['contact']['middle_name'],
                         contact.middle_name)
        self.assertEqual(data['contact']['last_name'],
                         contact.last_name)