diff --git a/docs/testing.md b/docs/testing.md index b3716818a2..21ddc63250 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -79,6 +79,25 @@ if you're working on new database migrations. To do this, run: [lxc-sf]: https://github.com/fgrehm/vagrant-lxc/wiki/FAQ#help-my-shared-folders-have-the-wrong-owner +### Internet access inside test suits + +Zulip has a policy of requiring its backend tests to not depend upon Internet +connectivity. We currently enforce this policy by overriding library functions +which might be used to access internet (eg. `httplib2.Http().request`). Though +this might not be a full proof blockade to internet access but we intend to +improve upon this in near time. + +- If you are seeing test failures with tracebacks as below, you should +probably mock the element trying to access the network. You can consult our +[guide](/docs/testing-with-django.htmls#mocks-and-stubs) on +mocking if you want to learn how to write mockups. + + ``` + File "tools/test-backend", line 120, in internet_guard + raise Exception("Zulip doesn't allow network access to test suits." + Exception: Zulip doesn't allow network access to test suits. + You need to mock any network access calls in testsuits. + ``` ## Schema and initial data changes diff --git a/tools/test-backend b/tools/test-backend index 332199a457..823f31801e 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -2,13 +2,14 @@ from __future__ import print_function from __future__ import absolute_import -from typing import List +from typing import List, Any import glob import optparse import os import sys import subprocess import ujson +import httplib2 # check for the venv from lib import sanity_check @@ -110,7 +111,21 @@ def write_failed_tests(failed_tests): with open(FAILED_TEST_PATH, 'w') as f: ujson.dump(failed_tests, f) +def block_internet(): + # type: () -> None + # We are blocking internet currently by assuming mostly any test would use + # httplib2 to access internet. + def internet_guard(*args, **kwargs): + # type: (*Any, **Any) -> None + raise Exception("Zulip doesn't allow network access to test suits. " + "You need to mock any network access calls in test" + "suits. Checkout our docs on testing here for details." + "https://zulip.readthedocs.io/en/latest/testing.html#internet-access-inside-test-suits") + + httplib2.Http.request = internet_guard + if __name__ == "__main__": + block_internet() TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) os.chdir(os.path.dirname(TOOLS_DIR)) sys.path.insert(0, os.path.dirname(TOOLS_DIR))