Пример #1
0
    def test_claim(
        self,
        mock_verify_credentials,
        mock_register_webhook,
        mock_subscribe_to_webhook,
        mock_delete_webhook,
        mock_get_webhooks,
    ):
        mock_get_webhooks.return_value = [{"id": "webhook_id"}]
        mock_delete_webhook.return_value = {"ok", True}

        url = reverse("channels.types.twitter_activity.claim")
        self.login(self.admin)

        response = self.client.get(reverse("channels.channel_claim"))
        self.assertContains(response, "/channels/types/twitter_activity/claim")

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Connect Twitter")

        self.assertEqual(
            list(response.context["form"].fields.keys()),
            ["api_key", "api_secret", "access_token", "access_token_secret", "env_name", "loc"],
        )

        # try submitting empty form
        response = self.client.post(url, {})
        self.assertEqual(response.status_code, 200)
        self.assertFormError(response, "form", "api_key", "This field is required.")
        self.assertFormError(response, "form", "api_secret", "This field is required.")
        self.assertFormError(response, "form", "access_token", "This field is required.")
        self.assertFormError(response, "form", "access_token_secret", "This field is required.")

        # try submitting with invalid credentials
        mock_verify_credentials.side_effect = TwythonError("Invalid credentials")

        response = self.client.post(
            url, {"api_key": "ak", "api_secret": "as", "access_token": "at", "access_token_secret": "ats"}
        )
        self.assertEqual(response.status_code, 200)
        self.assertFormError(response, "form", None, "The provided Twitter credentials do not appear to be valid.")

        # error registering webhook
        mock_verify_credentials.return_value = {"id": "87654", "screen_name": "jimmy"}
        mock_verify_credentials.side_effect = None
        mock_register_webhook.side_effect = TwythonError("Exceeded number of webhooks")

        response = self.client.post(
            url,
            {
                "api_key": "ak",
                "api_secret": "as",
                "access_token": "at",
                "access_token_secret": "ats",
                "env_name": "production",
            },
        )
        self.assertEqual(response.status_code, 200)
        self.assertFormError(response, "form", None, "Exceeded number of webhooks")

        # try a valid submission
        mock_register_webhook.side_effect = None
        mock_register_webhook.return_value = {"id": "1234567"}

        response = self.client.post(
            url,
            {
                "api_key": "ak",
                "api_secret": "as",
                "access_token": "at",
                "access_token_secret": "ats",
                "env_name": "beta",
            },
        )
        self.assertEqual(response.status_code, 302)

        channel = Channel.objects.get(address="jimmy", is_active=True)
        self.assertEqual(
            channel.config,
            {
                "handle_id": "87654",
                "api_key": "ak",
                "api_secret": "as",
                "access_token": "at",
                "env_name": "beta",
                "access_token_secret": "ats",
                "webhook_id": "1234567",
                "callback_domain": channel.callback_domain,
            },
        )
        self.assertTrue(channel.get_type().has_attachment_support(channel))

        mock_register_webhook.assert_called_with(
            "beta", "https://%s/c/twt/%s/receive" % (channel.callback_domain, channel.uuid)
        )
        mock_subscribe_to_webhook.assert_called_with("beta")
Пример #2
0
    def test_claim(self, mock_verify_credentials, mock_register_webhook,
                   mock_subscribe_to_webhook):
        url = reverse('channels.claim_twitter_activity')

        self.login(self.admin)

        # check that channel is only available to beta users
        response = self.client.get(reverse('channels.channel_claim'))
        self.assertNotContains(response, 'channels/claim/twitter_activity/')

        Group.objects.get(name="Beta").user_set.add(self.admin)

        response = self.client.get(reverse('channels.channel_claim'))
        self.assertContains(response, 'channels/claim/twitter_activity/')

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Connect Twitter Activity API")

        self.assertEqual(list(response.context['form'].fields.keys()), [
            'api_key', 'api_secret', 'access_token', 'access_token_secret',
            'loc'
        ])

        # try submitting empty form
        response = self.client.post(url, {})
        self.assertEqual(response.status_code, 200)
        self.assertFormError(response, 'form', 'api_key',
                             "This field is required.")
        self.assertFormError(response, 'form', 'api_secret',
                             "This field is required.")
        self.assertFormError(response, 'form', 'access_token',
                             "This field is required.")
        self.assertFormError(response, 'form', 'access_token_secret',
                             "This field is required.")

        # try submitting with invalid credentials
        mock_verify_credentials.side_effect = TwythonError(
            "Invalid credentials")

        response = self.client.post(
            url, {
                'api_key': 'ak',
                'api_secret': 'as',
                'access_token': 'at',
                'access_token_secret': 'ats'
            })
        self.assertEqual(response.status_code, 200)
        self.assertFormError(
            response, 'form', None,
            "The provided Twitter credentials do not appear to be valid.")

        # try submitting for handle which already has a channel
        mock_verify_credentials.side_effect = None
        mock_verify_credentials.return_value = {
            'id': '345678',
            'screen_name': "beta_bob"
        }

        response = self.client.post(
            url, {
                'api_key': 'ak',
                'api_secret': 'as',
                'access_token': 'at',
                'access_token_secret': 'ats'
            })
        self.assertEqual(response.status_code, 200)
        self.assertFormError(
            response, 'form', None,
            "A Twitter channel already exists for that handle.")

        # try a valid submission
        mock_verify_credentials.return_value = {
            'id': '87654',
            'screen_name': "jimmy"
        }
        mock_register_webhook.return_value = {'id': "1234567"}

        response = self.client.post(
            url, {
                'api_key': 'ak',
                'api_secret': 'as',
                'access_token': 'at',
                'access_token_secret': 'ats'
            })
        self.assertEqual(response.status_code, 302)

        channel = Channel.objects.get(address='jimmy')
        self.assertEqual(
            json.loads(channel.config), {
                'handle_id': '87654',
                'api_key': 'ak',
                'api_secret': 'as',
                'access_token': 'at',
                'access_token_secret': 'ats',
                'webhook_id': '1234567'
            })

        mock_register_webhook.assert_called_once_with(
            'https://temba.ngrok.io/c/twt/%s/receive' % channel.uuid)
        mock_subscribe_to_webhook.assert_called_once_with("1234567")
Пример #3
0
    def test_claim(self, mock_verify_credentials, mock_register_webhook,
                   mock_subscribe_to_webhook):
        url = reverse("channels.types.twitter_activity.claim")

        self.login(self.admin)

        # check that channel is only available to beta users
        response = self.client.get(reverse("channels.channel_claim"))
        self.assertNotContains(response,
                               "/channels/types/twitter_activity/claim")

        Group.objects.get(name="Beta").user_set.add(self.admin)

        response = self.client.get(reverse("channels.channel_claim"))
        self.assertContains(response, "/channels/types/twitter_activity/claim")

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Connect Twitter Activity API")

        self.assertEqual(
            list(response.context["form"].fields.keys()),
            [
                "api_key", "api_secret", "access_token", "access_token_secret",
                "env_name", "loc"
            ],
        )

        # try submitting empty form
        response = self.client.post(url, {})
        self.assertEqual(response.status_code, 200)
        self.assertFormError(response, "form", "api_key",
                             "This field is required.")
        self.assertFormError(response, "form", "api_secret",
                             "This field is required.")
        self.assertFormError(response, "form", "access_token",
                             "This field is required.")
        self.assertFormError(response, "form", "access_token_secret",
                             "This field is required.")

        # try submitting with invalid credentials
        mock_verify_credentials.side_effect = TwythonError(
            "Invalid credentials")

        response = self.client.post(
            url, {
                "api_key": "ak",
                "api_secret": "as",
                "access_token": "at",
                "access_token_secret": "ats"
            })
        self.assertEqual(response.status_code, 200)
        self.assertFormError(
            response, "form", None,
            "The provided Twitter credentials do not appear to be valid.")

        # try submitting for handle which already has a channel
        mock_verify_credentials.side_effect = None
        mock_verify_credentials.return_value = {
            "id": "345678",
            "screen_name": "beta_bob"
        }

        response = self.client.post(
            url, {
                "api_key": "ak",
                "api_secret": "as",
                "access_token": "at",
                "access_token_secret": "ats"
            })
        self.assertEqual(response.status_code, 200)
        self.assertFormError(
            response, "form", None,
            "A Twitter channel already exists for that handle.")

        # try a valid submission
        mock_verify_credentials.return_value = {
            "id": "87654",
            "screen_name": "jimmy"
        }
        mock_register_webhook.return_value = {"id": "1234567"}

        response = self.client.post(
            url,
            {
                "api_key": "ak",
                "api_secret": "as",
                "access_token": "at",
                "access_token_secret": "ats",
                "env_name": "beta",
            },
        )
        self.assertEqual(response.status_code, 302)

        channel = Channel.objects.get(address="jimmy")
        self.assertEqual(
            channel.config,
            {
                "handle_id": "87654",
                "api_key": "ak",
                "api_secret": "as",
                "access_token": "at",
                "env_name": "beta",
                "access_token_secret": "ats",
                "callback_domain": channel.callback_domain,
            },
        )

        mock_register_webhook.assert_called_once_with(
            "beta", "https://%s/c/twt/%s/receive" %
            (channel.callback_domain, channel.uuid))
        mock_subscribe_to_webhook.assert_called_once_with("beta")