Skip to content

Commit

Permalink
Add check_credentials tests
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Nov 27, 2024
1 parent 887da08 commit e37fbd3
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
42 changes: 42 additions & 0 deletions temba/channels/types/facebookapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}))
4 changes: 4 additions & 0 deletions temba/channels/types/facebookapp/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
39 changes: 39 additions & 0 deletions temba/channels/types/instagram/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}))
4 changes: 4 additions & 0 deletions temba/channels/types/instagram/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</p>
<div class="mt-4">
<div onclick="javascript:history.go(-1)" class="button-light">{% trans "Go Back" %}</div>
<a class="button-primary mt-4 ml-2" href="{{ update_token_url }}">{% trans "Reconnect Facebook page" %}</a>
<a class="button-primary mt-4 ml-2" href="{{ update_token_url }}">{% trans "Reconnect Facebook Page" %}</a>
</div>
</div>
{% endblock content %}

0 comments on commit e37fbd3

Please sign in to comment.