From c0f43ce3436fab81878a69b8ca63049bd56babfe Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 8 Mar 2017 03:41:46 -0800 Subject: [PATCH] tests: Move PublicURLTest tests to test_urls.py. --- zerver/tests/test_signup.py | 93 -------------------------------- zerver/tests/test_urls.py | 102 +++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 94 deletions(-) diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index 2660aca50d..2bfa8ba240 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -53,102 +53,9 @@ from typing import Dict, List, Set, Optional from six.moves import urllib from six.moves import range -import six from typing import Any, Text import os -class PublicURLTest(ZulipTestCase): - """ - Account creation URLs are accessible even when not logged in. Authenticated - URLs redirect to a page. - """ - - def fetch(self, method, urls, expected_status): - # type: (str, List[str], int) -> None - for url in urls: - # e.g. self.client_post(url) if method is "post" - response = getattr(self, method)(url) - self.assertEqual(response.status_code, expected_status, - msg="Expected %d, received %d for %s to %s" % ( - expected_status, response.status_code, method, url)) - - def test_public_urls(self): - # type: () -> None - """ - Test which views are accessible when not logged in. - """ - # FIXME: We should also test the Tornado URLs -- this codepath - # can't do so because this Django test mechanism doesn't go - # through Tornado. - denmark_stream_id = Stream.objects.get(name='Denmark').id - get_urls = {200: ["/accounts/home/", "/accounts/login/" - "/en/accounts/home/", "/ru/accounts/home/", - "/en/accounts/login/", "/ru/accounts/login/", - "/help/"], - 302: ["/", "/en/", "/ru/"], - 401: ["/json/streams/%d/members" % (denmark_stream_id,), - "/api/v1/users/me/subscriptions", - "/api/v1/messages", - "/json/messages", - "/api/v1/streams", - ], - 404: ["/help/nonexistent"], - } - - # Add all files in 'templates/zerver/help' directory (except for 'main.html' and - # 'index.md') to `get_urls['200']` list. - for doc in os.listdir('./templates/zerver/help'): - if doc.startswith(".") or '~' in doc or '#' in doc: - continue # nocoverage -- just here for convenience - if doc not in {'main.html', 'index.md', 'include'}: - get_urls[200].append('/help/' + os.path.splitext(doc)[0]) # Strip the extension. - - post_urls = {200: ["/accounts/login/"], - 302: ["/accounts/logout/"], - 401: ["/json/messages", - "/json/invite_users", - "/json/settings/change", - "/json/subscriptions/exists", - "/json/subscriptions/property", - "/json/fetch_api_key", - "/json/users/me/pointer", - "/json/users/me/subscriptions", - "/api/v1/users/me/subscriptions", - ], - 400: ["/api/v1/external/github", - "/api/v1/fetch_api_key", - ], - } - put_urls = {401: ["/json/users/me/pointer"], - } - for status_code, url_set in six.iteritems(get_urls): - self.fetch("client_get", url_set, status_code) - for status_code, url_set in six.iteritems(post_urls): - self.fetch("client_post", url_set, status_code) - for status_code, url_set in six.iteritems(put_urls): - self.fetch("client_put", url_set, status_code) - - def test_get_gcid_when_not_configured(self): - # type: () -> None - with self.settings(GOOGLE_CLIENT_ID=None): - resp = self.client_get("/api/v1/fetch_google_client_id") - self.assertEqual(400, resp.status_code, - msg="Expected 400, received %d for GET /api/v1/fetch_google_client_id" % ( - resp.status_code,)) - data = ujson.loads(resp.content) - self.assertEqual('error', data['result']) - - def test_get_gcid_when_configured(self): - # type: () -> None - with self.settings(GOOGLE_CLIENT_ID="ABCD"): - resp = self.client_get("/api/v1/fetch_google_client_id") - self.assertEqual(200, resp.status_code, - msg="Expected 200, received %d for GET /api/v1/fetch_google_client_id" % ( - resp.status_code,)) - data = ujson.loads(resp.content) - self.assertEqual('success', data['result']) - self.assertEqual('ABCD', data['google_client_id']) - class AddNewUserHistoryTest(ZulipTestCase): def test_add_new_user_history_race(self): # type: () -> None diff --git a/zerver/tests/test_urls.py b/zerver/tests/test_urls.py index eacce9b55c..2be170a2f8 100644 --- a/zerver/tests/test_urls.py +++ b/zerver/tests/test_urls.py @@ -2,11 +2,111 @@ from __future__ import absolute_import from __future__ import print_function +import importlib +import os +import six +import ujson + import django.core.urlresolvers from django.test import TestCase -import importlib +from typing import List + +from zerver.lib.test_classes import ZulipTestCase +from zerver.models import Stream from zproject import urls +class PublicURLTest(ZulipTestCase): + """ + Account creation URLs are accessible even when not logged in. Authenticated + URLs redirect to a page. + """ + + def fetch(self, method, urls, expected_status): + # type: (str, List[str], int) -> None + for url in urls: + # e.g. self.client_post(url) if method is "post" + response = getattr(self, method)(url) + self.assertEqual(response.status_code, expected_status, + msg="Expected %d, received %d for %s to %s" % ( + expected_status, response.status_code, method, url)) + + def test_public_urls(self): + # type: () -> None + """ + Test which views are accessible when not logged in. + """ + # FIXME: We should also test the Tornado URLs -- this codepath + # can't do so because this Django test mechanism doesn't go + # through Tornado. + denmark_stream_id = Stream.objects.get(name='Denmark').id + get_urls = {200: ["/accounts/home/", "/accounts/login/" + "/en/accounts/home/", "/ru/accounts/home/", + "/en/accounts/login/", "/ru/accounts/login/", + "/help/"], + 302: ["/", "/en/", "/ru/"], + 401: ["/json/streams/%d/members" % (denmark_stream_id,), + "/api/v1/users/me/subscriptions", + "/api/v1/messages", + "/json/messages", + "/api/v1/streams", + ], + 404: ["/help/nonexistent"], + } + + # Add all files in 'templates/zerver/help' directory (except for 'main.html' and + # 'index.md') to `get_urls['200']` list. + for doc in os.listdir('./templates/zerver/help'): + if doc.startswith(".") or '~' in doc or '#' in doc: + continue # nocoverage -- just here for convenience + if doc not in {'main.html', 'index.md', 'include'}: + get_urls[200].append('/help/' + os.path.splitext(doc)[0]) # Strip the extension. + + post_urls = {200: ["/accounts/login/"], + 302: ["/accounts/logout/"], + 401: ["/json/messages", + "/json/invite_users", + "/json/settings/change", + "/json/subscriptions/exists", + "/json/subscriptions/property", + "/json/fetch_api_key", + "/json/users/me/pointer", + "/json/users/me/subscriptions", + "/api/v1/users/me/subscriptions", + ], + 400: ["/api/v1/external/github", + "/api/v1/fetch_api_key", + ], + } + put_urls = {401: ["/json/users/me/pointer"], + } + for status_code, url_set in six.iteritems(get_urls): + self.fetch("client_get", url_set, status_code) + for status_code, url_set in six.iteritems(post_urls): + self.fetch("client_post", url_set, status_code) + for status_code, url_set in six.iteritems(put_urls): + self.fetch("client_put", url_set, status_code) + + def test_get_gcid_when_not_configured(self): + # type: () -> None + with self.settings(GOOGLE_CLIENT_ID=None): + resp = self.client_get("/api/v1/fetch_google_client_id") + self.assertEqual(400, resp.status_code, + msg="Expected 400, received %d for GET /api/v1/fetch_google_client_id" % ( + resp.status_code,)) + data = ujson.loads(resp.content) + self.assertEqual('error', data['result']) + + def test_get_gcid_when_configured(self): + # type: () -> None + with self.settings(GOOGLE_CLIENT_ID="ABCD"): + resp = self.client_get("/api/v1/fetch_google_client_id") + self.assertEqual(200, resp.status_code, + msg="Expected 200, received %d for GET /api/v1/fetch_google_client_id" % ( + resp.status_code,)) + data = ujson.loads(resp.content) + self.assertEqual('success', data['result']) + self.assertEqual('ABCD', data['google_client_id']) + class URLResolutionTest(TestCase): def check_function_exists(self, module_name, view): # type: (str, str) -> None