From e37fbd3b35be5eb3d96972f3e1c577b8216c3044 Mon Sep 17 00:00:00 2001
From: Norbert Kwizera
Date: Wed, 27 Nov 2024 13:26:17 +0200
Subject: [PATCH] Add check_credentials tests
---
temba/channels/types/facebookapp/tests.py | 42 +++++++++++++++++++
temba/channels/types/facebookapp/type.py | 4 ++
temba/channels/types/instagram/tests.py | 39 +++++++++++++++++
temba/channels/types/instagram/type.py | 4 ++
.../types/facebookapp/check_credentials.html | 2 +-
5 files changed, 90 insertions(+), 1 deletion(-)
diff --git a/temba/channels/types/facebookapp/tests.py b/temba/channels/types/facebookapp/tests.py
index 3c73c4eba2..ccc101ed0a 100644
--- a/temba/channels/types/facebookapp/tests.py
+++ b/temba/channels/types/facebookapp/tests.py
@@ -323,3 +323,45 @@ def test_get_error_ref_url(self):
"https://developers.facebook.com/docs/messenger-platform/error-codes",
FacebookAppType().get_error_ref_url(None, "190"),
)
+
+ @override_settings(FACEBOOK_APPLICATION_ID="FB_APP_ID", FACEBOOK_APPLICATION_SECRET="FB_APP_SECRET")
+ @patch("requests.get")
+ def test_check_credentials(self, mock_get):
+ check_credentials_url = reverse("channels.types.facebookapp.check_credentials", args=(self.channel.uuid,))
+
+ self.login(self.admin)
+
+ mock_get.return_value = MockResponse(200, json.dumps({"success": True, "data": {}}))
+ response = self.client.get(check_credentials_url)
+ self.assertContains(response, "Reconnect Facebook Page")
+ self.assertContains(
+ response, "Error with token, you need to reconnect the Facebook page by clicking the button below"
+ )
+ self.assertEqual(
+ response.context["update_token_url"], f"{reverse("channels.types.facebookapp.claim")}?update=1"
+ )
+ self.assertFalse(response.context["valid_token"])
+
+ mock_get.return_value = MockResponse(200, json.dumps({"success": True, "data": {"is_valid": True}}))
+
+ response = self.client.get(check_credentials_url)
+ self.assertContains(response, "Reconnect Facebook Page")
+ self.assertContains(response, "Everything looks good. No need to reconnect")
+ self.assertEqual(
+ response.context["update_token_url"], f"{reverse("channels.types.facebookapp.claim")}?update=1"
+ )
+ self.assertTrue(response.context["valid_token"])
+
+ @override_settings(FACEBOOK_APPLICATION_ID="FB_APP_ID", FACEBOOK_APPLICATION_SECRET="FB_APP_SECRET")
+ @patch("requests.get")
+ def test_type_check_credentials(self, mock_get):
+ self.assertFalse(FacebookAppType().check_credentials({}))
+
+ mock_get.return_value = MockResponse(200, json.dumps({"success": True, "data": {}}))
+ self.assertFalse(FacebookAppType().check_credentials({Channel.CONFIG_AUTH_TOKEN: "Token"}))
+
+ mock_get.return_value = MockResponse(400, json.dumps({"error": True}))
+ self.assertFalse(FacebookAppType().check_credentials({Channel.CONFIG_AUTH_TOKEN: "Token"}))
+
+ mock_get.return_value = MockResponse(200, json.dumps({"success": True, "data": {"is_valid": True}}))
+ self.assertTrue(FacebookAppType().check_credentials({Channel.CONFIG_AUTH_TOKEN: "Token"}))
diff --git a/temba/channels/types/facebookapp/type.py b/temba/channels/types/facebookapp/type.py
index 5c52d2a754..398ae5011f 100644
--- a/temba/channels/types/facebookapp/type.py
+++ b/temba/channels/types/facebookapp/type.py
@@ -95,6 +95,10 @@ def check_credentials(self, config: dict) -> bool:
app_id = settings.FACEBOOK_APPLICATION_ID
app_secret = settings.FACEBOOK_APPLICATION_SECRET
url = "https://graph.facebook.com/v18.0/debug_token"
+
+ if Channel.CONFIG_AUTH_TOKEN not in config:
+ return False
+
params = {
"access_token": f"{app_id}|{app_secret}",
"input_token": config[Channel.CONFIG_AUTH_TOKEN],
diff --git a/temba/channels/types/instagram/tests.py b/temba/channels/types/instagram/tests.py
index 3b6df34453..58210db95a 100644
--- a/temba/channels/types/instagram/tests.py
+++ b/temba/channels/types/instagram/tests.py
@@ -356,3 +356,42 @@ def test_get_error_ref_url(self):
"https://developers.facebook.com/docs/instagram-api/reference/error-codes",
InstagramType().get_error_ref_url(None, "36000"),
)
+
+ @override_settings(FACEBOOK_APPLICATION_ID="FB_APP_ID", FACEBOOK_APPLICATION_SECRET="FB_APP_SECRET")
+ @patch("requests.get")
+ def test_check_credentials(self, mock_get):
+ check_credentials_url = reverse("channels.types.instagram.check_credentials", args=(self.channel.uuid,))
+
+ self.login(self.admin)
+
+ mock_get.return_value = MockResponse(200, json.dumps({"success": True, "data": {}}))
+ response = self.client.get(check_credentials_url)
+ self.assertContains(response, "Reconnect Instagram Business Account")
+ self.assertContains(
+ response,
+ "Error with token, you need to reconnect the Instagram Business Account by clicking the button below",
+ )
+ self.assertEqual(response.context["update_token_url"], f"{reverse("channels.types.instagram.claim")}?update=1")
+ self.assertFalse(response.context["valid_token"])
+
+ mock_get.return_value = MockResponse(200, json.dumps({"success": True, "data": {"is_valid": True}}))
+
+ response = self.client.get(check_credentials_url)
+ self.assertContains(response, "Reconnect Instagram Business Account")
+ self.assertContains(response, "Everything looks good. No need to reconnect")
+ self.assertEqual(response.context["update_token_url"], f"{reverse("channels.types.instagram.claim")}?update=1")
+ self.assertTrue(response.context["valid_token"])
+
+ @override_settings(FACEBOOK_APPLICATION_ID="FB_APP_ID", FACEBOOK_APPLICATION_SECRET="FB_APP_SECRET")
+ @patch("requests.get")
+ def test_type_check_credentials(self, mock_get):
+ self.assertFalse(InstagramType().check_credentials({}))
+
+ mock_get.return_value = MockResponse(200, json.dumps({"success": True, "data": {}}))
+ self.assertFalse(InstagramType().check_credentials({Channel.CONFIG_AUTH_TOKEN: "Token"}))
+
+ mock_get.return_value = MockResponse(400, json.dumps({"error": True}))
+ self.assertFalse(InstagramType().check_credentials({Channel.CONFIG_AUTH_TOKEN: "Token"}))
+
+ mock_get.return_value = MockResponse(200, json.dumps({"success": True, "data": {"is_valid": True}}))
+ self.assertTrue(InstagramType().check_credentials({Channel.CONFIG_AUTH_TOKEN: "Token"}))
diff --git a/temba/channels/types/instagram/type.py b/temba/channels/types/instagram/type.py
index a312dcd365..e7e132528e 100644
--- a/temba/channels/types/instagram/type.py
+++ b/temba/channels/types/instagram/type.py
@@ -62,6 +62,10 @@ def check_credentials(self, config: dict) -> bool:
app_id = settings.FACEBOOK_APPLICATION_ID
app_secret = settings.FACEBOOK_APPLICATION_SECRET
url = "https://graph.facebook.com/v18.0/debug_token"
+
+ if Channel.CONFIG_AUTH_TOKEN not in config:
+ return False
+
params = {
"access_token": f"{app_id}|{app_secret}",
"input_token": config[Channel.CONFIG_AUTH_TOKEN],
diff --git a/templates/channels/types/facebookapp/check_credentials.html b/templates/channels/types/facebookapp/check_credentials.html
index 2aae8233be..c2a264f53b 100644
--- a/templates/channels/types/facebookapp/check_credentials.html
+++ b/templates/channels/types/facebookapp/check_credentials.html
@@ -12,7 +12,7 @@
{% endblock content %}