AC-156. Only use configured LDAP authentication when needed for LDAP tests.

This commit is contained in:
Chris Church 2013-09-07 16:42:12 -04:00
parent 916dd713e3
commit c1e8b3c842
2 changed files with 22 additions and 15 deletions

View File

@ -1,6 +1,7 @@
# Copyright (c) 2013 AnsibleWorks, Inc.
# All Rights Reserved.
# Python
import contextlib
import datetime
import json
@ -8,11 +9,19 @@ import os
import shutil
import tempfile
# PyYAML
import yaml
from django.conf import settings
# Django
from django.conf import settings, UserSettingsHolder
from django.contrib.auth.models import User
import django.test
from django.test.client import Client
# Django-Auth-LDAP
from django_auth_ldap.backend import LDAPSettings
# AWX
from awx.main.models import *
class BaseTestMixin(object):
@ -26,12 +35,23 @@ class BaseTestMixin(object):
self._temp_project_dirs = []
self._current_auth = None
self._user_passwords = {}
# Wrap settings so we can redefine them within each test.
self._wrapped = settings._wrapped
settings._wrapped = UserSettingsHolder(settings._wrapped)
# Set all AUTH_LDAP_* settings to defaults to avoid using LDAP for
# tests unless expicitly configured.
for name, value in LDAPSettings.defaults.items():
if name == 'SERVER_URI':
value = ''
setattr(settings, 'AUTH_LDAP_%s' % name, value)
def tearDown(self):
super(BaseTestMixin, self).tearDown()
for project_dir in self._temp_project_dirs:
if os.path.exists(project_dir):
shutil.rmtree(project_dir, True)
# Restore previous settings after each test.
settings._wrapped = self._wrapped
@contextlib.contextmanager
def current_user(self, user_or_username, password=None):

View File

@ -7,15 +7,12 @@ import json
import urllib
# Django
from django.conf import settings, UserSettingsHolder
from django.conf import settings
from django.contrib.auth.models import User
import django.test
from django.test.client import Client
from django.core.urlresolvers import reverse
# Django-Auth-LDAP
from django_auth_ldap.backend import LDAPSettings
# AWX
from awx.main.models import *
from awx.main.tests.base import BaseTest
@ -645,20 +642,10 @@ class LdapTest(BaseTest):
self.ldap_password = getattr(settings, 'TEST_AUTH_LDAP_PASSWORD', None)
if not self.ldap_password:
self.skipTest('no test LDAP password defined')
# Wrap settings so we can redfine them for each test.
self._wrapped = settings._wrapped
settings._wrapped = UserSettingsHolder(settings._wrapped)
# Reset all AUTH_LDAP_* settings to defaults.
for name, value in LDAPSettings.defaults.items():
setattr(settings, 'AUTH_LDAP_%s' % name, value)
# Set test LDAP settings that are always needed.
for name in ('SERVER_URI', 'BIND_DN', 'BIND_PASSWORD', 'USE_TLS'):
self.use_test_setting(name)
def tearDown(self):
super(LdapTest, self).tearDown()
settings._wrapped = self._wrapped
def check_login(self, username=None, password=None, should_fail=False):
username = username or self.ldap_username
password = password or self.ldap_password