def test_create_review_slack_message_payload_with_no_data(
            self, get_username, get_unbygh, get_octocat, get_env):
        """ Should raise a BadRequest Exception. """
        get_octocat.return_value = 'octocat'
        get_env.return_value = None
        get_username.return_value = None
        get_unbygh.return_value = None

        with self.assertRaises(BadRequest):
            slack._create_slack_message_payload({})
    def test_create_slack_review_message_payload_with_default_channel(
            self, get_username, get_unbygh, get_octocat, get_env):
        """ Should create a slack payload with a default (env var) channel. """
        get_username.return_value = '@luke'
        get_unbygh.return_value = None
        get_octocat.return_value = 'octocat'
        get_env.return_value = '#default-channel'

        payload = slack._create_slack_message_payload(SAMPLE_GITHUB_PAYLOAD)
        self.assertEqual(payload.get('channel'), get_env.return_value)
    def test_create_assignment_slack_message_payload(self, get_channel,
                                                     get_username, get_unbygh,
                                                     get_octocat, get_env):
        """ Should create a fully valid slack message payload. """
        get_channel.return_value = '#leia'
        get_username.return_value = '@luke'
        get_unbygh.return_value = 'obiwan'
        get_octocat.return_value = 'octocat'
        get_env.return_value = '#default-channel'

        request_payload = SAMPLE_GITHUB_PAYLOAD.copy()
        request_payload['action'] = 'assigned'

        payload = slack._create_slack_message_payload(request_payload)
        self.assertEqual(
            payload.get('text'),
            "You've been assigned a pull request by @obiwan. Lucky you!")
    def test_create_full_review_slack_message_payload(self, get_channel,
                                                      get_username, get_unbygh,
                                                      get_octocat, get_env):
        """ Should create a fully valid slack message payload. """
        get_channel.return_value = '#leia'
        get_username.return_value = '@luke'
        get_unbygh.return_value = 'obiwan'
        get_octocat.return_value = 'octocat'
        get_env.return_value = '#default-channel'

        payload = slack._create_slack_message_payload(SAMPLE_GITHUB_PAYLOAD)

        self.assertEqual(
            payload.get('text'),
            "You've been asked by @obiwan to review a pull request. Lucky you!"
        )
        self.assertTrue(payload.get('as_user'))
        self.assertTrue(payload.get('link_names'))
        self.assertEqual(payload.get('channel'), '#leia')

        attachment = payload.get('attachments')[0]
        self.assertEqual(attachment.get('fallback'),
                         "<http://www.example.com|PR Title>")
        self.assertEqual(attachment.get('color'), '#36a64f')
        self.assertEqual(attachment.get('author_name'),
                         'Example Repository pull request #1')
        self.assertEqual(attachment.get('author_link'),
                         'http://www.example.com')
        self.assertEqual(attachment.get('author_icon'),
                         'https://github.com/favicon.ico')
        self.assertEqual(attachment.get('title'), 'PR Title')
        self.assertEqual(attachment.get('title_link'),
                         'http://www.example.com')
        self.assertEqual(attachment.get('text'), 'An example pull request.')
        self.assertEqual(attachment.get('thumb_url'), 'octocat')
        self.assertEqual(attachment.get('footer'), 'Github PR Notifier')
        self.assertEqual(attachment.get('footer_icon'),
                         'https://github.com/apple-touch-icon-180x180.png')
        self.assertEqual(attachment.get('ts'),
                         int(datetime.datetime.now().timestamp()))