tests: Refactor use of test and webhook data fixtures.

This commit is contained in:
Preston Hansen
2018-04-19 20:57:21 -05:00
committed by Tim Abbott
parent 76d6c71595
commit e168f9938c
47 changed files with 67 additions and 76 deletions

View File

@@ -241,7 +241,7 @@ class HelloWorldHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("helloworld", fixture_name, file_type="json")
return self.webhook_fixture_data("helloworld", fixture_name, file_type="json")
```
@@ -260,7 +260,7 @@ value from the fixture. If these don't match, the test will fail.
you would provide a webhook URL to the 3rd party service. `api_key={api_key}` says
that an API key is expected.
In `get_body`, the first argument in the call to `self.fixture_data` specifies the
In `get_body`, the first argument in the call to `self.webhook_fixture_data` specifies the
prefix of your fixture file names, and `file_type` their type. Common types are
`json` and `txt`.
@@ -504,7 +504,7 @@ class QuerytestHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("querytest", fixture_name, file_type="json")
return self.webhook_fixture_data("querytest", fixture_name, file_type="json")
```
You can also override `get_body` if your test data needs to be constructed in

View File

@@ -509,13 +509,20 @@ class ZulipTestCase(TestCase):
for substring in substrings:
self.assertNotIn(substring, decoded)
def fixture_data(self, type: Text, action: Text, file_type: Text='json') -> Text:
def webhook_fixture_data(self, type: Text, action: Text, file_type: Text='json') -> Text:
fn = os.path.join(
os.path.dirname(__file__),
"../webhooks/%s/fixtures/%s.%s" % (type, action, file_type)
)
return open(fn).read()
def fixture_data(self, file_name: Text, type: Text='') -> Text:
fn = os.path.join(
os.path.dirname(__file__),
"../tests/fixtures/%s/%s" % (type, file_name)
)
return open(fn).read()
def make_stream(self, stream_name: Text, realm: Optional[Realm]=None,
invite_only: Optional[bool]=False) -> Stream:
if realm is None:
@@ -685,7 +692,7 @@ class WebhookTestCase(ZulipTestCase):
def get_body(self, fixture_name: Text) -> Union[Text, Dict[str, Text]]:
"""Can be implemented either as returning a dictionary containing the
post parameters or as string containing the body of the request."""
return ujson.dumps(ujson.loads(self.fixture_data(self.FIXTURE_DIR_NAME, fixture_name)))
return ujson.dumps(ujson.loads(self.webhook_fixture_data(self.FIXTURE_DIR_NAME, fixture_name)))
def do_test_subject(self, msg: Message, expected_subject: Optional[Text]) -> None:
if expected_subject is not None:

View File

@@ -713,7 +713,7 @@ class DeactivatedRealmTest(ZulipTestCase):
user_profile = self.example_user("hamlet")
url = "/api/v1/external/jira?api_key=%s&stream=jira_custom" % (
user_profile.api_key,)
data = self.fixture_data('jira', "created_v2")
data = self.webhook_fixture_data('jira', 'created_v2')
result = self.client_post(url, data,
content_type="application/json")
self.assert_json_error_contains(result, "has been deactivated", status_code=400)
@@ -886,7 +886,7 @@ class InactiveUserTest(ZulipTestCase):
url = "/api/v1/external/jira?api_key=%s&stream=jira_custom" % (
user_profile.api_key,)
data = self.fixture_data('jira', "created_v2")
data = self.webhook_fixture_data('jira', 'created_v2')
result = self.client_post(url, data,
content_type="application/json")
self.assert_json_error_contains(result, "Account not active", status_code=400)

View File

@@ -325,8 +325,6 @@ class TestReplyExtraction(ZulipTestCase):
self.assertEqual(message.content, 'Reply')
MAILS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tests", "fixtures", "email")
class TestScriptMTA(ZulipTestCase):
@@ -338,9 +336,7 @@ class TestScriptMTA(ZulipTestCase):
stream = get_stream("Denmark", get_realm("zulip"))
stream_to_address = encode_email_address(stream)
template_path = os.path.join(MAILS_DIR, "simple.txt")
with open(template_path) as template_file:
mail_template = template_file.read()
mail_template = self.fixture_data('simple.txt', type='email')
mail = mail_template.format(stream_to_address=stream_to_address, sender=sender)
read_pipe, write_pipe = os.pipe()
os.write(write_pipe, mail.encode())
@@ -356,9 +352,7 @@ class TestScriptMTA(ZulipTestCase):
sender = self.example_email('hamlet')
stream = get_stream("Denmark", get_realm("zulip"))
stream_to_address = encode_email_address(stream)
template_path = os.path.join(MAILS_DIR, "simple.txt")
with open(template_path) as template_file:
mail_template = template_file.read()
mail_template = self.fixture_data('simple.txt', type='email')
mail = mail_template.format(stream_to_address=stream_to_address, sender=sender)
read_pipe, write_pipe = os.pipe()
os.write(write_pipe, mail.encode())
@@ -399,9 +393,7 @@ class TestEmailMirrorTornadoView(ZulipTestCase):
@mock.patch('zerver.lib.email_mirror.queue_json_publish')
def send_offline_message(self, to_address: str, sender: str,
mock_queue_json_publish: mock.Mock) -> HttpResponse:
template_path = os.path.join(MAILS_DIR, "simple.txt")
with open(template_path) as template_file:
mail_template = template_file.read()
mail_template = self.fixture_data('simple.txt', type='email')
mail = mail_template.format(stream_to_address=to_address, sender=sender)
def check_queue_json_publish(queue_name: str,

View File

@@ -484,9 +484,7 @@ class TestMissedMessages(ZulipTestCase):
# Run `relative_to_full_url()` function over test fixtures present in
# 'markdown_test_cases.json' and check that it converts all the relative
# URLs to absolute URLs.
fixtures_file = os.path.join(settings.DEPLOY_ROOT, "zerver", "tests",
"fixtures", "markdown_test_cases.json")
fixtures = ujson.load(open(fixtures_file))
fixtures = ujson.loads(self.fixture_data("markdown_test_cases.json"))
test_fixtures = {}
for test in fixtures['regular_tests']:
test_fixtures[test['name']] = test

View File

@@ -43,7 +43,6 @@ from zilencer.models import RemoteZulipServer, RemotePushDeviceToken
from django.utils.timezone import now
ZERVER_DIR = os.path.dirname(os.path.dirname(__file__))
FIXTURES_FILE_PATH = os.path.join(ZERVER_DIR, "tests", "fixtures", "markdown_test_cases.json")
class BouncerTestCase(ZulipTestCase):
def setUp(self) -> None:
@@ -1245,8 +1244,7 @@ class TestReceivesNotificationsFunctions(ZulipTestCase):
class TestPushNotificationsContent(ZulipTestCase):
def test_fixtures(self) -> None:
with open(FIXTURES_FILE_PATH) as fp:
fixtures = ujson.load(fp)
fixtures = ujson.loads(self.fixture_data("markdown_test_cases.json"))
tests = fixtures["regular_tests"]
for test in tests:
if "text_content" in test:

View File

@@ -153,9 +153,6 @@ class TestReport(ZulipTestCase):
annotate.assert_called_once_with('"trace"')
def test_report_csp_violations(self) -> None:
fixture_data_file = open(os.path.join(os.path.dirname(__file__),
'fixtures/csp_report.json'), 'r')
fixture_data = ujson.load(fixture_data_file)
params = ujson.dumps(fixture_data)
result = self.client_post("/report/csp_violations", params, content_type="application/json")
fixture_data = self.fixture_data('csp_report.json')
result = self.client_post("/report/csp_violations", fixture_data, content_type="application/json")
self.assert_json_success(result)

View File

@@ -489,9 +489,8 @@ class SlackImporter(ZulipTestCase):
# Also the unzipped data file should be removed if the test fails at 'do_convert_data'
rm_tree(test_slack_unzipped_file)
user_data_fixture = os.path.join(settings.DEPLOY_ROOT, "zerver", "tests", "fixtures",
"slack_fixtures", "user_data.json")
mock_get_slack_api_data.side_effect = [ujson.load(open(user_data_fixture))['members'], {}]
user_data_fixture = ujson.loads(self.fixture_data('user_data.json', type='slack_fixtures'))
mock_get_slack_api_data.side_effect = [user_data_fixture['members'], {}]
do_convert_data(test_slack_zip_file, output_dir, token)
self.assertTrue(os.path.exists(output_dir))

View File

@@ -48,7 +48,7 @@ Acme enables me to manage the flow of information quite well. I only wish I coul
self.URL_TEMPLATE = original_url_template
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("appfollow", fixture_name, file_type="json")
return self.webhook_fixture_data("appfollow", fixture_name, file_type="json")
class ConvertMarkdownTest(TestCase):
def test_convert_bold(self) -> None:

View File

@@ -140,4 +140,4 @@ class BeanstalkHookTests(WebhookTestCase):
content_type=None)
def get_body(self, fixture_name: Text) -> Dict[str, Text]:
return {'payload': self.fixture_data('beanstalk', fixture_name)}
return {'payload': self.webhook_fixture_data('beanstalk', fixture_name)}

View File

@@ -37,4 +37,4 @@ class BeeminderHookTests(WebhookTestCase):
content_type="application/json")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("beeminder", fixture_name, file_type="json")
return self.webhook_fixture_data("beeminder", fixture_name, file_type="json")

View File

@@ -75,4 +75,4 @@ class BitbucketHookTests(WebhookTestCase):
self.assert_json_success(result)
def get_body(self, fixture_name: Text) -> Union[Text, Dict[str, Text]]:
return self.fixture_data(self.FIXTURE_DIR_NAME, fixture_name)
return self.webhook_fixture_data(self.FIXTURE_DIR_NAME, fixture_name)

View File

@@ -32,4 +32,4 @@ class DelightedHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("delighted", fixture_name, file_type="json")
return self.webhook_fixture_data("delighted", fixture_name, file_type="json")

View File

@@ -56,4 +56,4 @@ class DeskDotComHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("deskdotcom", fixture_name, file_type="txt")
return self.webhook_fixture_data("deskdotcom", fixture_name, file_type="txt")

View File

@@ -61,6 +61,6 @@ class DialogflowHookTests(WebhookTestCase):
content_type="application/json")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("dialogflow",
fixture_name,
file_type="json")
return self.webhook_fixture_data("dialogflow",
fixture_name,
file_type="json")

View File

@@ -16,7 +16,7 @@ class DropboxHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("dropbox", fixture_name, file_type="json")
return self.webhook_fixture_data("dropbox", fixture_name, file_type="json")
def test_verification_request(self) -> None:
self.subscribe(self.test_user, self.STREAM_NAME)

View File

@@ -87,4 +87,4 @@ class FlockHookTests(WebhookTestCase):
content_type="application/json")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("flock", fixture_name, file_type="json")
return self.webhook_fixture_data("flock", fixture_name, file_type="json")

View File

@@ -77,4 +77,4 @@ Priority: **High** => **Low**"""
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("freshdesk", fixture_name, file_type="json")
return self.webhook_fixture_data("freshdesk", fixture_name, file_type="json")

View File

@@ -180,4 +180,4 @@ class FrontHookTests(WebhookTestCase):
self.assert_json_error(result, "Unknown webhook request")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data('front', fixture_name, file_type="json")
return self.webhook_fixture_data('front', fixture_name, file_type="json")

View File

@@ -40,7 +40,7 @@ class GithubV1HookTests(WebhookTestCase):
def get_body(self, fixture_name: Text) -> Dict[str, Text]:
api_key = self.test_user.api_key
data = ujson.loads(self.fixture_data(self.FIXTURE_DIR_NAME, 'v1_' + fixture_name))
data = ujson.loads(self.webhook_fixture_data(self.FIXTURE_DIR_NAME, 'v1_' + fixture_name))
data.update({'email': self.TEST_USER_EMAIL,
'api-key': api_key,
'payload': ujson.dumps(data['payload'])})
@@ -165,7 +165,7 @@ class GithubV2HookTests(WebhookTestCase):
def get_body(self, fixture_name: Text) -> Dict[str, Text]:
api_key = self.test_user.api_key
data = ujson.loads(self.fixture_data(self.FIXTURE_DIR_NAME, 'v2_' + fixture_name))
data = ujson.loads(self.webhook_fixture_data(self.FIXTURE_DIR_NAME, 'v2_' + fixture_name))
data.update({'email': self.TEST_USER_EMAIL,
'api-key': api_key,
'payload': ujson.dumps(data['payload'])})

View File

@@ -40,4 +40,4 @@ class GocdHookTests(WebhookTestCase):
)
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("gocd", fixture_name, file_type="json")
return self.webhook_fixture_data("gocd", fixture_name, file_type="json")

View File

@@ -17,4 +17,4 @@ class GoSquaredHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("gosquared", fixture_name, file_type="json")
return self.webhook_fixture_data("gosquared", fixture_name, file_type="json")

View File

@@ -63,4 +63,4 @@ class GreenhouseHookTests(WebhookTestCase):
content_type=self.CONTENT_TYPE)
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("greenhouse", fixture_name, file_type="json")
return self.webhook_fixture_data("greenhouse", fixture_name, file_type="json")

View File

@@ -122,4 +122,4 @@ class GrooveHookTests(WebhookTestCase):
self.assert_json_error(result, 'Missing required data')
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("groove", fixture_name, file_type="json")
return self.webhook_fixture_data("groove", fixture_name, file_type="json")

View File

@@ -24,4 +24,4 @@ class HelloSignHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded", topic=expected_subject)
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("hellosign", fixture_name, file_type="json")
return self.webhook_fixture_data("hellosign", fixture_name, file_type="json")

View File

@@ -57,4 +57,4 @@ class HelloWorldHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("helloworld", fixture_name, file_type="json")
return self.webhook_fixture_data("helloworld", fixture_name, file_type="json")

View File

@@ -16,4 +16,4 @@ class HerokuHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("heroku", fixture_name, file_type="txt")
return self.webhook_fixture_data("heroku", fixture_name, file_type="txt")

View File

@@ -22,4 +22,4 @@ class HomeAssistantHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("homeassistant", fixture_name, file_type="json")
return self.webhook_fixture_data("homeassistant", fixture_name, file_type="json")

View File

@@ -32,4 +32,4 @@ class InspingHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("insping", fixture_name, file_type="json")
return self.webhook_fixture_data("insping", fixture_name, file_type="json")

View File

@@ -18,4 +18,4 @@ class IntercomWebHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data('intercom', fixture_name, file_type="json")
return self.webhook_fixture_data('intercom', fixture_name, file_type="json")

View File

@@ -120,4 +120,4 @@ Adding a comment. Oh, what a comment it is!"""
self.send_and_test_stream_message('change_status_v2', expected_subject, expected_message)
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data('jira', fixture_name)
return self.webhook_fixture_data('jira', fixture_name)

View File

@@ -12,8 +12,8 @@ class LibratoHookTests(WebhookTestCase):
def get_body(self, fixture_name: Text) -> Text:
if self.IS_ATTACHMENT:
return self.fixture_data("librato", fixture_name, file_type='json')
return urllib.parse.urlencode({'payload': self.fixture_data("librato", fixture_name, file_type='json')})
return self.webhook_fixture_data("librato", fixture_name, file_type='json')
return urllib.parse.urlencode({'payload': self.webhook_fixture_data("librato", fixture_name, file_type='json')})
def test_alert_message_with_default_topic(self) -> None:
expected_subject = 'Alert alert.name'

View File

@@ -22,4 +22,4 @@ class MentionHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("mention", fixture_name, file_type="json")
return self.webhook_fixture_data("mention", fixture_name, file_type="json")

View File

@@ -23,4 +23,4 @@ Description sent via curl\n\nChangelog string'
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("newrelic", fixture_name, file_type="txt")
return self.webhook_fixture_data("newrelic", fixture_name, file_type="txt")

View File

@@ -153,4 +153,4 @@ class OpsGenieHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("opsgenie", fixture_name, file_type="json")
return self.webhook_fixture_data("opsgenie", fixture_name, file_type="json")

View File

@@ -41,4 +41,4 @@ May 18 20:30:02 abc cron OR server1:
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("papertrail", fixture_name, file_type="json")
return self.webhook_fixture_data("papertrail", fixture_name, file_type="json")

View File

@@ -70,7 +70,7 @@ class PivotalV3HookTests(WebhookTestCase):
self.send_and_test_stream_message('type_changed', expected_subject, expected_message, content_type="application/xml")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data('pivotal', fixture_name, file_type='xml')
return self.webhook_fixture_data('pivotal', fixture_name, file_type='xml')
class PivotalV5HookTests(WebhookTestCase):
STREAM_NAME = 'pivotal'
@@ -146,4 +146,4 @@ Try again next time
self.send_and_test_stream_message('type_changed', expected_subject, expected_message, content_type="application/xml")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data('pivotal', "v5_{}".format(fixture_name), file_type='json')
return self.webhook_fixture_data('pivotal', "v5_{}".format(fixture_name), file_type='json')

View File

@@ -164,4 +164,4 @@ class RaygunHookTests(WebhookTestCase):
"application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("raygun", fixture_name, file_type="json")
return self.webhook_fixture_data("raygun", fixture_name, file_type="json")

View File

@@ -26,4 +26,4 @@ class SemaphoreHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("semaphore", fixture_name, file_type="json")
return self.webhook_fixture_data("semaphore", fixture_name, file_type="json")

View File

@@ -52,4 +52,4 @@ class SlackWebhookTests(WebhookTestCase):
def get_body(self, fixture_name: str) -> str:
return self.fixture_data("slack", fixture_name, file_type="txt")
return self.webhook_fixture_data("slack", fixture_name, file_type="txt")

View File

@@ -58,4 +58,4 @@ class SolanoHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data(self.FIXTURE_DIR_NAME, fixture_name, file_type="json")
return self.webhook_fixture_data(self.FIXTURE_DIR_NAME, fixture_name, file_type="json")

View File

@@ -105,4 +105,4 @@ class SplunkHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("splunk", fixture_name, file_type="json")
return self.webhook_fixture_data("splunk", fixture_name, file_type="json")

View File

@@ -35,4 +35,4 @@ from **operational** to **under_maintenance**"
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("statuspage", fixture_name, file_type="json")
return self.webhook_fixture_data("statuspage", fixture_name, file_type="json")

View File

@@ -142,4 +142,4 @@ class StripeHookTests(WebhookTestCase):
content_type="application/x-www-form-urlencoded")
def get_body(self, fixture_name: Text) -> Text:
return self.fixture_data("stripe", fixture_name, file_type="json")
return self.webhook_fixture_data("stripe", fixture_name, file_type="json")

View File

@@ -28,7 +28,7 @@ class TeamcityHookTests(WebhookTestCase):
def test_teamcity_personal(self) -> None:
expected_message = u"Your personal build of Project :: Compile build 5535 - CL 123456 is broken with status Exit code 1 (new)! :thumbs_down:\nDetails: [changes](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952&tab=buildChangesDiv), [build log](http://teamcity/viewLog.html?buildTypeId=Project_Compile&buildId=19952)"
payload = ujson.dumps(ujson.loads(self.fixture_data(self.FIXTURE_DIR_NAME, 'personal')))
payload = ujson.dumps(ujson.loads(self.webhook_fixture_data(self.FIXTURE_DIR_NAME, 'personal')))
self.client_post(self.url, payload, content_type="application/json")
msg = self.get_last_message()

View File

@@ -56,4 +56,4 @@ class TravisHookTests(WebhookTestCase):
)
def get_body(self, fixture_name: Text) -> Text:
return urllib.parse.urlencode({'payload': self.fixture_data("travis", fixture_name, file_type="json")})
return urllib.parse.urlencode({'payload': self.webhook_fixture_data("travis", fixture_name, file_type="json")})

View File

@@ -89,4 +89,4 @@ class WordPressHookTests(WebhookTestCase):
self.assert_json_error(result, "Unknown WordPress webhook action: WordPress Action")
def get_body(self, fixture_name: str) -> str:
return self.fixture_data("wordpress", fixture_name, file_type="txt")
return self.webhook_fixture_data("wordpress", fixture_name, file_type="txt")