def test_get_placeholders_empty_string(self):
        # Verify an empty string has no placeholders
        content = ''

        m = Message(content=content,
                    schedule='',
                    start_date='',
                    start_time='',
                    title='')

        assert len(m.get_placeholders()) == 0
    def test_get_placeholders_valid_template_string(self):
        # Verify a valid template string has placeholders
        # https://docs.python.org/3/library/string.html#template-strings
        content = 'Test ${template} string'

        m = Message(content=content,
                    schedule='',
                    start_date='',
                    start_time='',
                    title='')

        assert len(m.get_placeholders()) == 1
    def test_get_message_time_missing_schedule(self):
        start_date = '${PH01}'
        start_time = '${PH02}'
        schedule = ''
        placeholders = {'PH01': '07-25-2020', 'PH02': '4:30pm'}

        m = Message(content='',
                    schedule=schedule,
                    start_date=start_date,
                    start_time=start_time,
                    title='')
        m.set_placeholders(placeholders=placeholders)

        # Verify that message_time is essentially now.
        expected = datetime.now(tz=tz.gettz('America/Los_Angeles'))
        assert (m.get_message_time() - expected).microseconds < 1000
    def test_request_handler_success(self):
        messages = [Message(content='${Q43}', schedule='now', start_date='07-27-2020', start_time='*-2h',
                            title='test title')]
        with mock.patch('motivationalboost.mbconfig.MBConfig.get_apptoto_api_token', return_value=''), \
             mock.patch('motivationalboost.mbconfig.MBConfig.get_apptoto_user', return_value=''), \
             mock.patch('motivationalboost.message_container.MessageContainer.get_messages', return_value=messages),\
             mock.patch('motivationalboost.apptoto.Apptoto.post_events') as mock_post:
            config = MBConfig()
            survey_output = {'Q43': 'some text'}
            handler = RequestHandler(config=config, survey_output=survey_output)

            handler.handle_request()

            assert mock_post.called
    def test_get_message_time_relative_time_days(self):
        start_date = '${PH01}'
        start_time = '${PH02}'
        schedule = '-1d'
        placeholders = {'PH01': '07-25-2020', 'PH02': '4:30pm'}

        m = Message(content='',
                    schedule=schedule,
                    start_date=start_date,
                    start_time=start_time,
                    title='')
        m.set_placeholders(placeholders=placeholders)

        # Expected time is one day before start_time, because of the relative offset from schedule
        temp = get_expected_date(placeholders['PH01'])
        expected = datetime(year=temp.year,
                            month=temp.month,
                            day=temp.day - 1,
                            hour=16,
                            minute=30,
                            tzinfo=tz.gettz('America/Los_Angeles'))

        assert m.get_message_time() == expected
    def test_get_message_time(self, start_date_str):
        start_date = '${PH01}'
        start_time = '${PH02}'
        schedule = '-2h'
        placeholders = {'PH01': start_date_str, 'PH02': '1pm'}

        m = Message(content='',
                    schedule=schedule,
                    start_date=start_date,
                    start_time=start_time,
                    title='')
        m.set_placeholders(placeholders=placeholders)

        # Expected time is two hours before start_time, because of the relative offset from schedule
        temp = get_expected_date(placeholders['PH01'])
        expected = datetime(year=temp.year,
                            month=temp.month,
                            day=temp.day,
                            hour=11,
                            minute=0,
                            tzinfo=tz.gettz('America/Los_Angeles'))

        assert m.get_message_time() == expected
    def test_get_content(self):
        content = 'Test ${PH01} string ${PH02}'
        placeholders = {'PH01': 'failure', 'PH02': 'cheese'}

        m = Message(content=content,
                    schedule='',
                    start_date='',
                    start_time='',
                    title='')
        m.set_placeholders(placeholders=placeholders)

        actual = m.get_content()

        assert actual == 'Test failure string cheese'
        # Verify that title is an empty string because it has no placeholders
        assert m.get_title() == ''
    def test_get_message_time_invalid_date_string(self):
        start_date = '${PH01}'
        start_time = '${PH02}'
        schedule = '-2h'
        placeholders = {'PH01': 'Invalid date string', 'PH02': '4:30pm'}

        m = Message(content='',
                    schedule=schedule,
                    start_date=start_date,
                    start_time=start_time,
                    title='')
        m.set_placeholders(placeholders=placeholders)

        # Verify ValueError exception is raised when input date string is invalid.
        with pytest.raises(ValueError):
            m.get_message_time()