Use override_settings in tests

This commit is contained in:
Valentin Samir 2016-06-28 19:27:03 +02:00
parent 44acd005ee
commit 97a15f749c

View File

@ -1,8 +1,8 @@
from .default_settings import settings from .default_settings import settings
import django import django
from django.test import TestCase from django.test import TestCase, Client
from django.test import Client from django.test.utils import override_settings
import re import re
import six import six
@ -138,6 +138,7 @@ class CheckPasswordCase(TestCase):
) )
@override_settings(CAS_AUTH_CLASS='cas_server.auth.TestAuthUser')
class LoginTestCase(TestCase): class LoginTestCase(TestCase):
"""Tests for the login view""" """Tests for the login view"""
def setUp(self): def setUp(self):
@ -147,7 +148,6 @@ class LoginTestCase(TestCase):
* create a service pattern for https://www.example.com/** * create a service pattern for https://www.example.com/**
* Set the service pattern to return all user attributes * Set the service pattern to return all user attributes
""" """
settings.CAS_AUTH_CLASS = 'cas_server.auth.TestAuthUser'
# For general purpose testing # For general purpose testing
self.service_pattern = models.ServicePattern.objects.create( self.service_pattern = models.ServicePattern.objects.create(
@ -559,12 +559,9 @@ class LoginTestCase(TestCase):
self.assertEqual(data["detail"], "confirmation needed") self.assertEqual(data["detail"], "confirmation needed")
@override_settings(CAS_AUTH_CLASS='cas_server.auth.TestAuthUser')
class LogoutTestCase(TestCase): class LogoutTestCase(TestCase):
def setUp(self):
"""prepare logout test context"""
settings.CAS_AUTH_CLASS = 'cas_server.auth.TestAuthUser'
def test_logout(self): def test_logout(self):
"""logout is idempotent""" """logout is idempotent"""
client = Client() client = Client()
@ -678,9 +675,9 @@ class LogoutTestCase(TestCase):
self.assertEqual(data["detail"], "logout") self.assertEqual(data["detail"], "logout")
self.assertEqual(data['session_nb'], nb_client) self.assertEqual(data['session_nb'], nb_client)
@override_settings(CAS_REDIRECT_TO_LOGIN_AFTER_LOGOUT=True)
def test_redirect_after_logout(self): def test_redirect_after_logout(self):
"""Test redirect to login after logout parameter""" """Test redirect to login after logout parameter"""
settings.CAS_REDIRECT_TO_LOGIN_AFTER_LOGOUT = True
client = get_auth_client() client = get_auth_client()
response = client.get('/logout') response = client.get('/logout')
@ -692,11 +689,9 @@ class LogoutTestCase(TestCase):
self.assertFalse(client.session.get("username")) self.assertFalse(client.session.get("username"))
self.assertFalse(client.session.get("authenticated")) self.assertFalse(client.session.get("authenticated"))
settings.CAS_REDIRECT_TO_LOGIN_AFTER_LOGOUT = False @override_settings(CAS_REDIRECT_TO_LOGIN_AFTER_LOGOUT=True)
def test_redirect_after_logout_to_service(self): def test_redirect_after_logout_to_service(self):
"""test prevalence of redirect url/service parameter over redirect to login after logout""" """test prevalence of redirect url/service parameter over redirect to login after logout"""
settings.CAS_REDIRECT_TO_LOGIN_AFTER_LOGOUT = True
client = get_auth_client() client = get_auth_client()
response = client.get('/logout?url=https://www.example.com') response = client.get('/logout?url=https://www.example.com')
@ -705,11 +700,9 @@ class LogoutTestCase(TestCase):
response = client.get('/logout?service=https://www.example.com') response = client.get('/logout?service=https://www.example.com')
self.assert_redirect_to_service(client, response) self.assert_redirect_to_service(client, response)
settings.CAS_REDIRECT_TO_LOGIN_AFTER_LOGOUT = False @override_settings(CAS_REDIRECT_TO_LOGIN_AFTER_LOGOUT=True)
def test_ajax_redirect_after_logout(self): def test_ajax_redirect_after_logout(self):
"""Test ajax redirect to login after logout parameter""" """Test ajax redirect to login after logout parameter"""
settings.CAS_REDIRECT_TO_LOGIN_AFTER_LOGOUT = True
client = get_auth_client() client = get_auth_client()
response = client.get('/logout', HTTP_X_AJAX='on') response = client.get('/logout', HTTP_X_AJAX='on')
@ -720,9 +713,8 @@ class LogoutTestCase(TestCase):
self.assertEqual(data['session_nb'], 1) self.assertEqual(data['session_nb'], 1)
self.assertEqual(data['url'], '/login') self.assertEqual(data['url'], '/login')
settings.CAS_REDIRECT_TO_LOGIN_AFTER_LOGOUT = False
@override_settings(CAS_AUTH_CLASS='cas_server.auth.TestAuthUser')
class AuthTestCase(TestCase): class AuthTestCase(TestCase):
""" """
Test for the auth view, used for external services Test for the auth view, used for external services
@ -730,16 +722,15 @@ class AuthTestCase(TestCase):
""" """
def setUp(self): def setUp(self):
"""preparing test context""" """preparing test context"""
settings.CAS_AUTH_CLASS = 'cas_server.auth.TestAuthUser'
self.service = 'https://www.example.com' self.service = 'https://www.example.com'
models.ServicePattern.objects.create( models.ServicePattern.objects.create(
name="example", name="example",
pattern="^https://www\.example\.com(/.*)?$" pattern="^https://www\.example\.com(/.*)?$"
) )
@override_settings(CAS_AUTH_SHARED_SECRET='test')
def test_auth_view_goodpass(self): def test_auth_view_goodpass(self):
"""successful request are awsered by yes""" """successful request are awsered by yes"""
settings.CAS_AUTH_SHARED_SECRET = 'test'
client = Client() client = Client()
response = client.post( response = client.post(
'/auth', '/auth',
@ -753,9 +744,9 @@ class AuthTestCase(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'yes\n') self.assertEqual(response.content, b'yes\n')
@override_settings(CAS_AUTH_SHARED_SECRET='test')
def test_auth_view_badpass(self): def test_auth_view_badpass(self):
""" bag user password => no""" """ bag user password => no"""
settings.CAS_AUTH_SHARED_SECRET = 'test'
client = Client() client = Client()
response = client.post( response = client.post(
'/auth', '/auth',
@ -769,9 +760,9 @@ class AuthTestCase(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'no\n') self.assertEqual(response.content, b'no\n')
@override_settings(CAS_AUTH_SHARED_SECRET='test')
def test_auth_view_badservice(self): def test_auth_view_badservice(self):
"""bad service => no""" """bad service => no"""
settings.CAS_AUTH_SHARED_SECRET = 'test'
client = Client() client = Client()
response = client.post( response = client.post(
'/auth', '/auth',
@ -785,9 +776,9 @@ class AuthTestCase(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'no\n') self.assertEqual(response.content, b'no\n')
@override_settings(CAS_AUTH_SHARED_SECRET='test')
def test_auth_view_badsecret(self): def test_auth_view_badsecret(self):
"""bad api key => no""" """bad api key => no"""
settings.CAS_AUTH_SHARED_SECRET = 'test'
client = Client() client = Client()
response = client.post( response = client.post(
'/auth', '/auth',
@ -803,7 +794,6 @@ class AuthTestCase(TestCase):
def test_auth_view_badsettings(self): def test_auth_view_badsettings(self):
"""api not set => error""" """api not set => error"""
settings.CAS_AUTH_SHARED_SECRET = None
client = Client() client = Client()
response = client.post( response = client.post(
'/auth', '/auth',
@ -817,9 +807,9 @@ class AuthTestCase(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b"no\nplease set CAS_AUTH_SHARED_SECRET") self.assertEqual(response.content, b"no\nplease set CAS_AUTH_SHARED_SECRET")
@override_settings(CAS_AUTH_SHARED_SECRET='test')
def test_auth_view_missing_parameter(self): def test_auth_view_missing_parameter(self):
"""missing parameter in request => no""" """missing parameter in request => no"""
settings.CAS_AUTH_SHARED_SECRET = 'test'
client = Client() client = Client()
params = { params = {
'username': settings.CAS_TEST_USER, 'username': settings.CAS_TEST_USER,
@ -835,11 +825,11 @@ class AuthTestCase(TestCase):
self.assertEqual(response.content, b'no\n') self.assertEqual(response.content, b'no\n')
@override_settings(CAS_AUTH_CLASS='cas_server.auth.TestAuthUser')
class ValidateTestCase(TestCase): class ValidateTestCase(TestCase):
"""tests for the validate view""" """tests for the validate view"""
def setUp(self): def setUp(self):
"""preparing test context""" """preparing test context"""
settings.CAS_AUTH_CLASS = 'cas_server.auth.TestAuthUser'
self.service = 'https://www.example.com' self.service = 'https://www.example.com'
self.service_pattern = models.ServicePattern.objects.create( self.service_pattern = models.ServicePattern.objects.create(
name="example", name="example",
@ -914,11 +904,11 @@ class ValidateTestCase(TestCase):
self.assertEqual(response.content, b'no\n') self.assertEqual(response.content, b'no\n')
@override_settings(CAS_AUTH_CLASS='cas_server.auth.TestAuthUser')
class ValidateServiceTestCase(TestCase): class ValidateServiceTestCase(TestCase):
"""tests for the serviceValidate view""" """tests for the serviceValidate view"""
def setUp(self): def setUp(self):
"""preparing test context""" """preparing test context"""
settings.CAS_AUTH_CLASS = 'cas_server.auth.TestAuthUser'
self.service = 'http://127.0.0.1:45678' self.service = 'http://127.0.0.1:45678'
self.service_pattern = models.ServicePattern.objects.create( self.service_pattern = models.ServicePattern.objects.create(
name="localhost", name="localhost",
@ -1245,10 +1235,10 @@ class ValidateServiceTestCase(TestCase):
self.assertEqual(error[0].text, "you must specify a service and a ticket") self.assertEqual(error[0].text, "you must specify a service and a ticket")
@override_settings(CAS_AUTH_CLASS='cas_server.auth.TestAuthUser')
class ProxyTestCase(TestCase): class ProxyTestCase(TestCase):
def setUp(self): def setUp(self):
settings.CAS_AUTH_CLASS = 'cas_server.auth.TestAuthUser'
self.service = 'http://127.0.0.1' self.service = 'http://127.0.0.1'
self.service_pattern = models.ServicePattern.objects.create( self.service_pattern = models.ServicePattern.objects.create(
name="localhost", name="localhost",