2020-05-26 07:16:25 +02:00
from unittest . mock import MagicMock , patch
2020-06-11 00:54:34 +02:00
2020-08-07 01:09:47 +02:00
import orjson
2020-06-11 00:54:34 +02:00
2019-04-14 15:28:19 +02:00
from zerver . lib . test_classes import ZulipTestCase
2020-06-11 00:54:34 +02:00
from zerver . models import Message , Stream , get_realm , get_user
2019-04-14 15:28:19 +02:00
class TestIntegrationsDevPanel ( ZulipTestCase ) :
zulip_realm = get_realm ( " zulip " )
def test_check_send_webhook_fixture_message_for_error ( self ) - > None :
2021-02-12 08:20:45 +01:00
bot = get_user ( " webhook-bot@zulip.com " , self . zulip_realm )
2020-06-09 00:25:09 +02:00
url = f " /api/v1/external/airbrake?api_key= { bot . api_key } "
2019-04-14 15:28:19 +02:00
target_url = " /devtools/integrations/check_send_webhook_fixture_message "
2021-12-17 07:03:22 +01:00
body = " {} " # This empty body should generate a ValidationError on the webhook code side.
2019-04-14 15:28:19 +02:00
data = {
" url " : url ,
2019-05-16 09:23:15 +02:00
" body " : body ,
2019-06-21 04:41:30 +02:00
" custom_headers " : " {} " ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" is_json " : " true " ,
2019-04-14 15:28:19 +02:00
}
2020-07-19 13:53:05 +02:00
with self . assertLogs ( level = " ERROR " ) as logs :
response = self . client_post ( target_url , data )
self . assertEqual ( response . status_code , 500 ) # Since the response would be forwarded.
expected_response = { " result " : " error " , " msg " : " Internal server error " }
2020-08-07 01:09:47 +02:00
self . assertEqual ( orjson . loads ( response . content ) , expected_response )
2020-07-19 13:53:05 +02:00
2021-12-17 07:03:22 +01:00
# Intention of this test looks like to trigger ValidationError
# so just testing ValidationError is printed along with Traceback in logs
self . assertTrue ( " ValidationError " in logs . output [ 0 ] )
2020-07-19 13:53:05 +02:00
self . assertTrue ( " Traceback (most recent call last) " in logs . output [ 0 ] )
2021-02-12 08:19:30 +01:00
self . assertEqual (
logs . output [ 1 ] , " ERROR:django.request:Internal Server Error: /api/v1/external/airbrake "
)
2019-04-14 15:28:19 +02:00
2019-05-16 09:23:15 +02:00
def test_check_send_webhook_fixture_message_for_success_without_headers ( self ) - > None :
2021-02-12 08:20:45 +01:00
bot = get_user ( " webhook-bot@zulip.com " , self . zulip_realm )
2021-05-10 07:02:14 +02:00
url = f " /api/v1/external/airbrake?api_key= { bot . api_key } &stream=Denmark&topic=Airbrake notifications "
2019-04-14 15:28:19 +02:00
target_url = " /devtools/integrations/check_send_webhook_fixture_message "
2020-04-09 21:51:58 +02:00
with open ( " zerver/webhooks/airbrake/fixtures/error_message.json " ) as f :
2019-04-14 15:28:19 +02:00
body = f . read ( )
data = {
" url " : url ,
" body " : body ,
2019-06-21 04:41:30 +02:00
" custom_headers " : " {} " ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" is_json " : " true " ,
2019-04-14 15:28:19 +02:00
}
response = self . client_post ( target_url , data )
2021-02-12 08:19:30 +01:00
expected_response = {
2021-02-12 08:20:45 +01:00
" responses " : [ { " status_code " : 200 , " message " : { " result " : " success " , " msg " : " " } } ] ,
" result " : " success " ,
" msg " : " " ,
2021-02-12 08:19:30 +01:00
}
2020-08-07 01:09:47 +02:00
response_content = orjson . loads ( response . content )
2021-02-12 08:19:30 +01:00
response_content [ " responses " ] [ 0 ] [ " message " ] = orjson . loads (
response_content [ " responses " ] [ 0 ] [ " message " ]
)
2019-04-14 15:28:19 +02:00
self . assertEqual ( response . status_code , 200 )
2019-05-18 20:59:37 +02:00
self . assertEqual ( response_content , expected_response )
2019-04-14 15:28:19 +02:00
2021-02-12 08:20:45 +01:00
latest_msg = Message . objects . latest ( " id " )
expected_message = ' [ZeroDivisionError](https://zulip.airbrake.io/projects/125209/groups/1705190192091077626): " Error message from logger " occurred. '
2019-04-14 15:28:19 +02:00
self . assertEqual ( latest_msg . content , expected_message )
self . assertEqual ( Stream . objects . get ( id = latest_msg . recipient . type_id ) . name , " Denmark " )
2021-05-10 07:02:14 +02:00
self . assertEqual ( latest_msg . topic_name ( ) , " Airbrake notifications " )
2019-04-14 15:28:19 +02:00
2019-05-16 09:23:15 +02:00
def test_check_send_webhook_fixture_message_for_success_with_headers ( self ) - > None :
2021-02-12 08:20:45 +01:00
bot = get_user ( " webhook-bot@zulip.com " , self . zulip_realm )
2021-05-10 07:02:14 +02:00
url = f " /api/v1/external/github?api_key= { bot . api_key } &stream=Denmark&topic=GitHub notifications "
2019-05-16 09:23:15 +02:00
target_url = " /devtools/integrations/check_send_webhook_fixture_message "
2020-04-09 21:51:58 +02:00
with open ( " zerver/webhooks/github/fixtures/ping__organization.json " ) as f :
2019-05-16 09:23:15 +02:00
body = f . read ( )
data = {
" url " : url ,
" body " : body ,
2020-08-07 01:09:47 +02:00
" custom_headers " : orjson . dumps ( { " X_GITHUB_EVENT " : " ping " } ) . decode ( ) ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" is_json " : " true " ,
2019-05-16 09:23:15 +02:00
}
response = self . client_post ( target_url , data )
self . assertEqual ( response . status_code , 200 )
2021-02-12 08:20:45 +01:00
latest_msg = Message . objects . latest ( " id " )
2019-05-16 09:23:15 +02:00
expected_message = " GitHub webhook has been successfully configured by eeshangarg. "
self . assertEqual ( latest_msg . content , expected_message )
self . assertEqual ( Stream . objects . get ( id = latest_msg . recipient . type_id ) . name , " Denmark " )
2021-05-10 07:02:14 +02:00
self . assertEqual ( latest_msg . topic_name ( ) , " GitHub notifications " )
2019-05-16 09:23:15 +02:00
2021-02-12 08:19:30 +01:00
def test_check_send_webhook_fixture_message_for_success_with_headers_and_non_json_fixtures (
self ,
) - > None :
2021-02-12 08:20:45 +01:00
bot = get_user ( " webhook-bot@zulip.com " , self . zulip_realm )
2021-05-10 07:02:14 +02:00
url = f " /api/v1/external/wordpress?api_key= { bot . api_key } &stream=Denmark&topic=WordPress notifications "
2019-05-16 20:29:18 +02:00
target_url = " /devtools/integrations/check_send_webhook_fixture_message "
2020-04-09 21:51:58 +02:00
with open ( " zerver/webhooks/wordpress/fixtures/publish_post_no_data_provided.txt " ) as f :
2019-05-16 20:29:18 +02:00
body = f . read ( )
data = {
" url " : url ,
" body " : body ,
2021-02-12 08:19:30 +01:00
" custom_headers " : orjson . dumps (
{ " Content-Type " : " application/x-www-form-urlencoded " }
) . decode ( ) ,
2019-11-13 08:17:49 +01:00
" is_json " : " false " ,
2019-05-16 20:29:18 +02:00
}
response = self . client_post ( target_url , data )
self . assertEqual ( response . status_code , 200 )
2021-02-12 08:20:45 +01:00
latest_msg = Message . objects . latest ( " id " )
2021-05-10 07:02:14 +02:00
expected_message = " New post published: \n * [New WordPress post](WordPress post URL) "
2019-05-16 20:29:18 +02:00
self . assertEqual ( latest_msg . content , expected_message )
self . assertEqual ( Stream . objects . get ( id = latest_msg . recipient . type_id ) . name , " Denmark " )
2021-05-10 07:02:14 +02:00
self . assertEqual ( latest_msg . topic_name ( ) , " WordPress notifications " )
2019-05-16 20:29:18 +02:00
2022-02-08 00:13:33 +01:00
def test_get_fixtures_for_nonexistent_integration ( self ) - > None :
target_url = " /devtools/integrations/somerandomnonexistentintegration/fixtures "
2019-04-14 15:28:19 +02:00
response = self . client_get ( target_url )
2021-02-12 08:19:30 +01:00
expected_response = {
2021-07-04 08:52:23 +02:00
" code " : " BAD_REQUEST " ,
2022-02-08 00:13:33 +01:00
" msg " : ' " somerandomnonexistentintegration " is not a valid webhook integration. ' ,
2021-02-12 08:20:45 +01:00
" result " : " error " ,
2021-02-12 08:19:30 +01:00
}
2019-04-14 15:28:19 +02:00
self . assertEqual ( response . status_code , 404 )
2020-08-07 01:09:47 +02:00
self . assertEqual ( orjson . loads ( response . content ) , expected_response )
2019-04-14 15:28:19 +02:00
@patch ( " zerver.views.development.integrations.os.path.exists " )
2021-02-12 08:19:30 +01:00
def test_get_fixtures_for_integration_without_fixtures (
self , os_path_exists_mock : MagicMock
) - > None :
2019-04-14 15:28:19 +02:00
os_path_exists_mock . return_value = False
target_url = " /devtools/integrations/airbrake/fixtures "
response = self . client_get ( target_url )
2021-02-12 08:19:30 +01:00
expected_response = {
2021-07-04 08:52:23 +02:00
" code " : " BAD_REQUEST " ,
2021-02-12 08:20:45 +01:00
" msg " : ' The integration " airbrake " does not have fixtures. ' ,
" result " : " error " ,
2021-02-12 08:19:30 +01:00
}
2019-04-14 15:28:19 +02:00
self . assertEqual ( response . status_code , 404 )
2020-08-07 01:09:47 +02:00
self . assertEqual ( orjson . loads ( response . content ) , expected_response )
2019-04-14 15:28:19 +02:00
def test_get_fixtures_for_success ( self ) - > None :
target_url = " /devtools/integrations/airbrake/fixtures "
response = self . client_get ( target_url )
self . assertEqual ( response . status_code , 200 )
2020-08-07 01:09:47 +02:00
self . assertIsNotNone ( orjson . loads ( response . content ) [ " fixtures " ] )
2019-04-14 15:28:19 +02:00
def test_get_dev_panel_page ( self ) - > None :
# Just to satisfy the test suite.
target_url = " /devtools/integrations/ "
response = self . client_get ( target_url )
self . assertEqual ( response . status_code , 200 )
2019-05-16 20:29:18 +02:00
def test_send_all_webhook_fixture_messages_for_success ( self ) - > None :
2021-02-12 08:20:45 +01:00
bot = get_user ( " webhook-bot@zulip.com " , self . zulip_realm )
2021-05-10 07:02:14 +02:00
url = f " /api/v1/external/appfollow?api_key= { bot . api_key } &stream=Denmark&topic=Appfollow bulk notifications "
2019-05-16 20:29:18 +02:00
target_url = " /devtools/integrations/send_all_webhook_fixture_messages "
data = {
" url " : url ,
2019-06-21 04:41:30 +02:00
" custom_headers " : " {} " ,
2019-05-16 20:29:18 +02:00
" integration_name " : " appfollow " ,
}
response = self . client_post ( target_url , data )
expected_responses = [
{
" fixture_name " : " sample.json " ,
" status_code " : 200 ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" message " : { " msg " : " " , " result " : " success " } ,
2019-05-16 20:29:18 +02:00
} ,
{
" fixture_name " : " review.json " ,
" status_code " : 200 ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" message " : { " msg " : " " , " result " : " success " } ,
} ,
2019-05-16 20:29:18 +02:00
]
2020-08-07 01:09:47 +02:00
responses = orjson . loads ( response . content ) [ " responses " ]
2019-05-16 20:29:18 +02:00
for r in responses :
2020-08-07 01:09:47 +02:00
r [ " message " ] = orjson . loads ( r [ " message " ] )
2019-05-16 20:29:18 +02:00
self . assertEqual ( response . status_code , 200 )
for r in responses :
2019-06-21 04:41:30 +02:00
# We have to use this roundabout manner since the order may vary each time.
# This is not an issue.
2019-05-16 20:29:18 +02:00
self . assertTrue ( r in expected_responses )
expected_responses . remove ( r )
2021-02-12 08:20:45 +01:00
new_messages = Message . objects . order_by ( " -id " ) [ 0 : 2 ]
2021-02-12 08:19:30 +01:00
expected_messages = [
" Webhook integration was successful. \n Test User / Acme (Google Play) " ,
" Acme - Group chat \n App Store, Acme Technologies, Inc. \n ★★★★★ United States \n **Great for Information Management** \n Acme enables me to manage the flow of information quite well. I only wish I could create and edit my Acme Post files in the iOS app. \n *by* **Mr RESOLUTIONARY** *for v3.9* \n [Permalink](http://appfollow.io/permalink) · [Add tag](http://watch.appfollow.io/add_tag) " ,
]
2019-05-16 20:29:18 +02:00
for msg in new_messages :
# new_messages -> expected_messages or expected_messages -> new_messages shouldn't make
# a difference since equality is commutative.
self . assertTrue ( msg . content in expected_messages )
expected_messages . remove ( msg . content )
self . assertEqual ( Stream . objects . get ( id = msg . recipient . type_id ) . name , " Denmark " )
2021-05-10 07:02:14 +02:00
self . assertEqual ( msg . topic_name ( ) , " Appfollow bulk notifications " )
2019-05-16 20:29:18 +02:00
2019-05-16 20:29:18 +02:00
def test_send_all_webhook_fixture_messages_for_success_with_non_json_fixtures ( self ) - > None :
2021-02-12 08:20:45 +01:00
bot = get_user ( " webhook-bot@zulip.com " , self . zulip_realm )
2021-05-10 07:02:14 +02:00
url = f " /api/v1/external/wordpress?api_key= { bot . api_key } &stream=Denmark&topic=WordPress bulk notifications "
2019-05-16 20:29:18 +02:00
target_url = " /devtools/integrations/send_all_webhook_fixture_messages "
data = {
" url " : url ,
2019-06-21 04:41:30 +02:00
" custom_headers " : " {} " ,
2019-05-16 20:29:18 +02:00
" integration_name " : " wordpress " ,
}
response = self . client_post ( target_url , data )
expected_responses = [
{
2021-02-12 08:19:30 +01:00
" message " : {
2021-05-10 07:02:14 +02:00
" msg " : " Unknown WordPress webhook action: WordPress action " ,
2021-02-12 08:20:45 +01:00
" result " : " error " ,
2021-06-30 18:35:50 +02:00
" code " : " BAD_REQUEST " ,
2021-02-12 08:19:30 +01:00
} ,
2019-05-16 20:29:18 +02:00
" fixture_name " : " user_register.txt " ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" status_code " : 400 ,
2019-05-16 20:29:18 +02:00
} ,
{
2021-02-12 08:19:30 +01:00
" message " : {
2021-05-10 07:02:14 +02:00
" msg " : " Unknown WordPress webhook action: WordPress action " ,
2021-02-12 08:20:45 +01:00
" result " : " error " ,
2021-06-30 18:35:50 +02:00
" code " : " BAD_REQUEST " ,
2021-02-12 08:19:30 +01:00
} ,
2019-05-16 20:29:18 +02:00
" fixture_name " : " publish_post_no_data_provided.txt " ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" status_code " : 400 ,
2019-05-16 20:29:18 +02:00
} ,
{
2021-02-12 08:19:30 +01:00
" message " : {
2021-05-10 07:02:14 +02:00
" msg " : " Unknown WordPress webhook action: WordPress action " ,
2021-02-12 08:20:45 +01:00
" result " : " error " ,
2021-06-30 18:35:50 +02:00
" code " : " BAD_REQUEST " ,
2021-02-12 08:19:30 +01:00
} ,
2019-05-16 20:29:18 +02:00
" fixture_name " : " unknown_action_no_data.txt " ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" status_code " : 400 ,
2019-05-16 20:29:18 +02:00
} ,
{
2021-02-12 08:19:30 +01:00
" message " : {
2021-05-10 07:02:14 +02:00
" msg " : " Unknown WordPress webhook action: WordPress action " ,
2021-02-12 08:20:45 +01:00
" result " : " error " ,
2021-06-30 18:35:50 +02:00
" code " : " BAD_REQUEST " ,
2021-02-12 08:19:30 +01:00
} ,
2019-05-16 20:29:18 +02:00
" fixture_name " : " publish_page.txt " ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" status_code " : 400 ,
2019-05-16 20:29:18 +02:00
} ,
{
2021-02-12 08:19:30 +01:00
" message " : {
2021-05-10 07:02:14 +02:00
" msg " : " Unknown WordPress webhook action: WordPress action " ,
2021-02-12 08:20:45 +01:00
" result " : " error " ,
2021-06-30 18:35:50 +02:00
" code " : " BAD_REQUEST " ,
2021-02-12 08:19:30 +01:00
} ,
2019-05-16 20:29:18 +02:00
" fixture_name " : " unknown_action_no_hook_provided.txt " ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" status_code " : 400 ,
2019-05-16 20:29:18 +02:00
} ,
{
2021-02-12 08:19:30 +01:00
" message " : {
2021-05-10 07:02:14 +02:00
" msg " : " Unknown WordPress webhook action: WordPress action " ,
2021-02-12 08:20:45 +01:00
" result " : " error " ,
2021-06-30 18:35:50 +02:00
" code " : " BAD_REQUEST " ,
2021-02-12 08:19:30 +01:00
} ,
2019-05-16 20:29:18 +02:00
" fixture_name " : " publish_post_type_not_provided.txt " ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" status_code " : 400 ,
2019-05-16 20:29:18 +02:00
} ,
{
2021-02-12 08:19:30 +01:00
" message " : {
2021-05-10 07:02:14 +02:00
" msg " : " Unknown WordPress webhook action: WordPress action " ,
2021-02-12 08:20:45 +01:00
" result " : " error " ,
2021-06-30 18:35:50 +02:00
" code " : " BAD_REQUEST " ,
2021-02-12 08:19:30 +01:00
} ,
2019-05-16 20:29:18 +02:00
" fixture_name " : " wp_login.txt " ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" status_code " : 400 ,
2019-05-16 20:29:18 +02:00
} ,
{
2021-02-12 08:19:30 +01:00
" message " : {
2021-05-10 07:02:14 +02:00
" msg " : " Unknown WordPress webhook action: WordPress action " ,
2021-02-12 08:20:45 +01:00
" result " : " error " ,
2021-06-30 18:35:50 +02:00
" code " : " BAD_REQUEST " ,
2021-02-12 08:19:30 +01:00
} ,
2019-05-16 20:29:18 +02:00
" fixture_name " : " publish_post.txt " ,
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
" status_code " : 400 ,
} ,
2019-05-16 20:29:18 +02:00
]
2020-08-07 01:09:47 +02:00
responses = orjson . loads ( response . content ) [ " responses " ]
2019-05-16 20:29:18 +02:00
for r in responses :
2020-08-07 01:09:47 +02:00
r [ " message " ] = orjson . loads ( r [ " message " ] )
2019-05-16 20:29:18 +02:00
self . assertEqual ( response . status_code , 200 )
for r in responses :
# We have to use this roundabout manner since the order may vary each time. This is not
# an issue. Basically, we're trying to compare 2 lists and since we're not resorting to
# using sets or a sorted order, we're sticking with O(n*m) time complexity for this
2020-03-28 01:25:56 +01:00
# comparison (where n and m are the lengths of the two lists respectively). But since
2019-05-16 20:29:18 +02:00
# this is just a unit test and more importantly n = m = some-low-number we don't really
# care about the time complexity being what it is.
self . assertTrue ( r in expected_responses )
expected_responses . remove ( r )
2019-05-16 20:29:18 +02:00
@patch ( " zerver.views.development.integrations.os.path.exists " )
2021-02-12 08:19:30 +01:00
def test_send_all_webhook_fixture_messages_for_missing_fixtures (
self , os_path_exists_mock : MagicMock
) - > None :
2019-05-16 20:29:18 +02:00
os_path_exists_mock . return_value = False
2021-02-12 08:20:45 +01:00
bot = get_user ( " webhook-bot@zulip.com " , self . zulip_realm )
2021-05-10 07:02:14 +02:00
url = f " /api/v1/external/appfollow?api_key= { bot . api_key } &stream=Denmark&topic=Appfollow bulk notifications "
2019-05-16 20:29:18 +02:00
data = {
" url " : url ,
2019-06-21 04:41:30 +02:00
" custom_headers " : " {} " ,
2019-05-16 20:29:18 +02:00
" integration_name " : " appfollow " ,
}
2021-02-12 08:19:30 +01:00
response = self . client_post (
" /devtools/integrations/send_all_webhook_fixture_messages " , data
)
expected_response = {
2021-07-04 08:52:23 +02:00
" code " : " BAD_REQUEST " ,
2021-02-12 08:20:45 +01:00
" msg " : ' The integration " appfollow " does not have fixtures. ' ,
" result " : " error " ,
2021-02-12 08:19:30 +01:00
}
2019-05-16 20:29:18 +02:00
self . assertEqual ( response . status_code , 404 )
2020-08-07 01:09:47 +02:00
self . assertEqual ( orjson . loads ( response . content ) , expected_response )