From 6fba0879a4ed0fb12d65a09246ed90d51d58caff Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Sat, 4 Jun 2016 11:28:02 -0700 Subject: [PATCH] Annotate much of the rest of zerver/tests. --- zerver/tests/test_auth_backends.py | 22 +++++++++++++++++++--- zerver/tests/test_i18n.py | 9 ++++++++- zerver/tests/test_management_commands.py | 1 + zerver/tests/test_templates.py | 8 ++++++++ zerver/tests/test_unread.py | 15 ++++++++++++++- zerver/tests/test_urls.py | 3 +++ 6 files changed, 53 insertions(+), 5 deletions(-) diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index 8102775b60..4ca9e294a0 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -2,7 +2,7 @@ from django.conf import settings from django.test import TestCase from django_auth_ldap.backend import _LDAPUser -from typing import Dict +from typing import Any, Callable, Dict import mock @@ -19,17 +19,19 @@ from zproject.backends import ZulipDummyBackend, EmailAuthBackend, \ GoogleMobileOauth2Backend, ZulipRemoteUserBackend, ZulipLDAPAuthBackend, \ ZulipLDAPUserPopulator, DevAuthBackend +from six import text_type import ujson class AuthBackendTest(TestCase): def verify_backend(self, backend, good_args=None, good_kwargs=None, bad_kwargs=None, email_to_username=None): + # type: (Any, List[Any], Dict[str, Any], Dict[str, Any], Callable[[text_type], text_type]) -> None if good_args is None: good_args = [] if good_kwargs is None: good_kwargs = {} - email = "hamlet@zulip.com" + email = u"hamlet@zulip.com" user_profile = get_user_profile_by_email(email) username = email @@ -63,11 +65,13 @@ class AuthBackendTest(TestCase): self.assertEqual(user_profile, result) def test_dummy_backend(self): + # type: () -> None self.verify_backend(ZulipDummyBackend(), good_kwargs=dict(use_dummy_backend=True), bad_kwargs=dict(use_dummy_backend=False)) def test_email_auth_backend(self): + # type: () -> None email = "hamlet@zulip.com" user_profile = get_user_profile_by_email(email) password = "testpassword" @@ -78,6 +82,7 @@ class AuthBackendTest(TestCase): good_kwargs=dict(password=password)) def test_email_auth_backend_disabled_password_auth(self): + # type: () -> None email = "hamlet@zulip.com" user_profile = get_user_profile_by_email(email) password = "testpassword" @@ -88,6 +93,7 @@ class AuthBackendTest(TestCase): self.assertIsNone(EmailAuthBackend().authenticate(email, dict(password=password))) def test_google_backend(self): + # type: () -> None email = "hamlet@zulip.com" backend = GoogleMobileOauth2Backend() payload = dict(email_verified=True, @@ -112,6 +118,7 @@ class AuthBackendTest(TestCase): self.assertTrue(ret["valid_attestation"]) def test_ldap_backend(self): + # type: () -> None email = "hamlet@zulip.com" password = "test_password" backend = ZulipLDAPAuthBackend() @@ -129,37 +136,44 @@ class AuthBackendTest(TestCase): mock.patch('django_auth_ldap.backend._LDAPUser._check_requirements'), \ mock.patch('django_auth_ldap.backend._LDAPUser._get_user_attrs', return_value=dict(full_name=['Hamlet'])): - self.verify_backend(backend, good_args=dict(password=password)) + self.verify_backend(backend, good_kwargs=dict(password=password)) def test_devauth_backend(self): + # type: () -> None self.verify_backend(DevAuthBackend()) def test_remote_user_backend(self): + # type: () -> None self.verify_backend(ZulipRemoteUserBackend()) def test_remote_user_backend_sso_append_domain(self): + # type: () -> None with self.settings(SSO_APPEND_DOMAIN='zulip.com'): self.verify_backend(ZulipRemoteUserBackend(), email_to_username=email_to_username) class FetchAPIKeyTest(AuthedTestCase): def setUp(self): + # type: () -> None self.email = "hamlet@zulip.com" self.user_profile = get_user_profile_by_email(self.email) def test_success(self): + # type: () -> None result = self.client.post("/api/v1/fetch_api_key", dict(username=self.email, password=initial_password(self.email))) self.assert_json_success(result) def test_wrong_password(self): + # type: () -> None result = self.client.post("/api/v1/fetch_api_key", dict(username=self.email, password="wrong")) self.assert_json_error(result, "Your username or password is incorrect.", 403) def test_password_auth_disabled(self): + # type: () -> None with mock.patch('zproject.backends.password_auth_enabled', return_value=False): result = self.client.post("/api/v1/fetch_api_key", dict(username=self.email, @@ -167,6 +181,7 @@ class FetchAPIKeyTest(AuthedTestCase): self.assert_json_error_contains(result, "Password auth is disabled", 403) def test_inactive_user(self): + # type: () -> None do_deactivate_user(self.user_profile) result = self.client.post("/api/v1/fetch_api_key", dict(username=self.email, @@ -174,6 +189,7 @@ class FetchAPIKeyTest(AuthedTestCase): self.assert_json_error_contains(result, "Your account has been disabled", 403) def test_deactivated_realm(self): + # type: () -> None do_deactivate_realm(self.user_profile.realm) result = self.client.post("/api/v1/fetch_api_key", dict(username=self.email, diff --git a/zerver/tests/test_i18n.py b/zerver/tests/test_i18n.py index 8c20c7a364..ee28685b33 100644 --- a/zerver/tests/test_i18n.py +++ b/zerver/tests/test_i18n.py @@ -1,8 +1,11 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import +from typing import Any + from django.test import TestCase from django.conf import settings +from django.http import HttpResponse from http.cookies import SimpleCookie @@ -12,8 +15,9 @@ class TranslationTestCase(TestCase): aware. """ + # e.g. self.client.post(url) if method is "post" def fetch(self, method, url, expected_status, **kwargs): - # e.g. self.client.post(url) if method is "post" + # type: (str, str, int, **Any) -> HttpResponse response = getattr(self.client, method)(url, **kwargs) self.assertEqual(response.status_code, expected_status, msg="Expected %d, received %d for %s to %s" % ( @@ -21,6 +25,7 @@ class TranslationTestCase(TestCase): return response def test_accept_language_header(self): + # type: () -> None languages = [('en', 'Register'), ('de', 'Registrieren'), ('sr', 'Региструј се'), @@ -33,6 +38,7 @@ class TranslationTestCase(TestCase): self.assertTrue(word in response.content) def test_cookie(self): + # type: () -> None languages = [('en', 'Register'), ('de', 'Registrieren'), ('sr', 'Региструј се'), @@ -46,6 +52,7 @@ class TranslationTestCase(TestCase): self.assertTrue(word in response.content) def test_i18n_urls(self): + # type: () -> None languages = [('en', 'Register'), ('de', 'Registrieren'), ('sr', 'Региструј се'), diff --git a/zerver/tests/test_management_commands.py b/zerver/tests/test_management_commands.py index 1e51d49048..2691917bbb 100644 --- a/zerver/tests/test_management_commands.py +++ b/zerver/tests/test_management_commands.py @@ -10,6 +10,7 @@ class TestSendWebhookFixtureMessage(TestCase): COMMAND_NAME = 'send_webhook_fixture_message' def setUp(self): + # type: () -> None self.fixture_path = os.path.join('some', 'fake', 'path.json') self.url = '/some/url/with/hook' diff --git a/zerver/tests/test_templates.py b/zerver/tests/test_templates.py index 33f93f6f8f..f26618fc44 100644 --- a/zerver/tests/test_templates.py +++ b/zerver/tests/test_templates.py @@ -1,9 +1,11 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import +from typing import Any, Dict, Iterable import logging from django.test import TestCase +from django.template import Template from django.template.loader import get_template from zerver.models import get_user_profile_by_email @@ -12,9 +14,11 @@ from zerver.lib.test_helpers import get_all_templates class get_form_value(object): def __init__(self, value): + # type: (Any) -> None self._value = value def value(self): + # type: () -> Any return self._value @@ -33,6 +37,8 @@ class TemplateTestCase(TestCase): function documentation for more information. """ def test_templates(self): + # type: () -> None + # Just add the templates whose context has a conflict with other # templates' context in `exclude`. exclude = ['analytics/activity.html'] @@ -44,6 +50,7 @@ class TemplateTestCase(TestCase): self.render_templates(exclude, self.get_context(**update)) def render_templates(self, templates, context): + # type: (Iterable[Template], Dict[str, Any]) -> None for template in templates: template = get_template(template) try: @@ -52,6 +59,7 @@ class TemplateTestCase(TestCase): logging.exception("Exception while rendering '{}'".format(template.template.name)) def get_context(self, **kwargs): + # type: (**Any) -> Dict[str, Any] """Get the dummy context for shallow testing. The context returned will always contain a parameter called diff --git a/zerver/tests/test_unread.py b/zerver/tests/test_unread.py index b7d5d717bb..7b6810b4dc 100644 --- a/zerver/tests/test_unread.py +++ b/zerver/tests/test_unread.py @@ -13,6 +13,7 @@ import ujson class PointerTest(AuthedTestCase): def test_update_pointer(self): + # type: () -> None """ Posting a pointer to /update (in the form {"pointer": pointer}) changes the pointer we store for your UserProfile. @@ -25,6 +26,7 @@ class PointerTest(AuthedTestCase): self.assertEqual(get_user_profile_by_email("hamlet@zulip.com").pointer, msg_id) def test_api_update_pointer(self): + # type: () -> None """ Same as above, but for the API view """ @@ -37,6 +39,7 @@ class PointerTest(AuthedTestCase): self.assertEqual(get_user_profile_by_email(email).pointer, msg_id) def test_missing_pointer(self): + # type: () -> None """ Posting json to /json/users/me/pointer which does not contain a pointer key/value pair returns a 400 and error message. @@ -48,6 +51,7 @@ class PointerTest(AuthedTestCase): self.assertEqual(get_user_profile_by_email("hamlet@zulip.com").pointer, -1) def test_invalid_pointer(self): + # type: () -> None """ Posting json to /json/users/me/pointer with an invalid pointer returns a 400 and error message. @@ -59,6 +63,7 @@ class PointerTest(AuthedTestCase): self.assertEqual(get_user_profile_by_email("hamlet@zulip.com").pointer, -1) def test_pointer_out_of_range(self): + # type: () -> None """ Posting json to /json/users/me/pointer with an out of range (< 0) pointer returns a 400 and error message. @@ -71,13 +76,15 @@ class PointerTest(AuthedTestCase): class UnreadCountTests(AuthedTestCase): def setUp(self): + # type: () -> None self.unread_msg_ids = [self.send_message( "iago@zulip.com", "hamlet@zulip.com", Recipient.PERSONAL, "hello"), self.send_message( "iago@zulip.com", "hamlet@zulip.com", Recipient.PERSONAL, "hello2")] + # Sending a new message results in unread UserMessages being created def test_new_message(self): - # Sending a new message results in unread UserMessages being created + # type: () -> None self.login("hamlet@zulip.com") content = "Test message for unset read bit" last_msg = self.send_message("hamlet@zulip.com", "Verona", Recipient.STREAM, content) @@ -89,6 +96,7 @@ class UnreadCountTests(AuthedTestCase): self.assertFalse(um.flags.read) def test_update_flags(self): + # type: () -> None self.login("hamlet@zulip.com") result = self.client.post("/json/messages/flags", @@ -118,6 +126,7 @@ class UnreadCountTests(AuthedTestCase): self.assertEqual(msg['flags'], []) def test_update_all_flags(self): + # type: () -> None self.login("hamlet@zulip.com") message_ids = [self.send_message("hamlet@zulip.com", "iago@zulip.com", @@ -140,6 +149,7 @@ class UnreadCountTests(AuthedTestCase): self.assertEqual(msg['flags'], []) def test_mark_all_in_stream_read(self): + # type: () -> None self.login("hamlet@zulip.com") user_profile = get_user_profile_by_email("hamlet@zulip.com") self.subscribe_to_stream(user_profile.email, "test_stream", user_profile.realm) @@ -181,6 +191,7 @@ class UnreadCountTests(AuthedTestCase): def test_mark_all_in_invalid_stream_read(self): + # type: () -> None self.login("hamlet@zulip.com") invalid_stream_name = "" result = self.client.post("/json/messages/flags", {"messages": ujson.dumps([]), @@ -190,6 +201,7 @@ class UnreadCountTests(AuthedTestCase): self.assert_json_error(result, 'No such stream \'\'') def test_mark_all_in_stream_topic_read(self): + # type: () -> None self.login("hamlet@zulip.com") user_profile = get_user_profile_by_email("hamlet@zulip.com") self.subscribe_to_stream(user_profile.email, "test_stream", user_profile.realm) @@ -229,6 +241,7 @@ class UnreadCountTests(AuthedTestCase): def test_mark_all_in_invalid_topic_read(self): + # type: () -> None self.login("hamlet@zulip.com") invalid_topic_name = "abc" result = self.client.post("/json/messages/flags", {"messages": ujson.dumps([]), diff --git a/zerver/tests/test_urls.py b/zerver/tests/test_urls.py index 2d5bb8da12..aa3df73fcd 100644 --- a/zerver/tests/test_urls.py +++ b/zerver/tests/test_urls.py @@ -9,11 +9,13 @@ from zproject import urls class URLResolutionTest(TestCase): def check_function_exists(self, module_name, view): + # type: (str, str) -> None module = importlib.import_module(module_name) self.assertTrue(hasattr(module, view), "View %s.%s does not exist" % (module_name, view)) # Tests that all views in urls.v1_api_and_json_patterns exist def test_rest_api_url_resolution(self): + # type: () -> None for pattern in urls.v1_api_and_json_patterns: if not (hasattr(pattern, "_callback_str") and hasattr(pattern, "default_args")): continue @@ -26,6 +28,7 @@ class URLResolutionTest(TestCase): # whether the function exists. We at present do not test the # class-based views. def test_non_api_url_resolution(self): + # type: () -> None for pattern in urls.urlpatterns: if not hasattr(pattern, "_callback_str"): continue