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 0c0dfde
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
36 changes: 36 additions & 0 deletions temba/channels/types/facebookapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,39 @@ 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.assertFalse(FacebookAppType().check_credentials({}))

self.login(self.admin)

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}}))
self.assertTrue(FacebookAppType().check_credentials({Channel.CONFIG_AUTH_TOKEN: "Token"}))

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"])

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"}))
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
33 changes: 33 additions & 0 deletions temba/channels/types/instagram/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,36 @@ 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.assertFalse(InstagramType().check_credentials({}))

self.login(self.admin)

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}}))
self.assertTrue(InstagramType().check_credentials({Channel.CONFIG_AUTH_TOKEN: "Token"}))

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"])

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"}))
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

0 comments on commit 0c0dfde

Please sign in to comment.