2016-07-15 62 views
6

我的問題是,我想建立一個單獨的測試數據庫,與我的開發數據庫分開。應用程序本身幾乎與Django-Rest-Framework快速入門教程相同,只是我使用LDAP後端。我的開發數據庫使用MySQL。我有單獨的設置文件進行測試。Django測試單獨的數據庫給「OperationalError:沒有這樣的表:auth_user」

完整的錯誤追蹤我擺在這裏:http://dpaste.com/1W3TX1E,但有趣的部分是:

sqlite3.OperationalError: no such table: auth_user 

全面測試的設置在這裏:http://dpaste.com/1K7KHK4。我的相關設置(pyldap和Django的LDAP的身份驗證是由安裝的應用程序丟失,它們是手動安裝與PIP):

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'rest_framework', 
    'njord', 
    'permissions', 
] 
DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': ':memory:', 
    } 
} 

眼下應用基本上沒有與LDAP唯一的身份驗證。我認爲這就是爲什麼我有一個錯誤 - 當我做測試時,我的數據庫中沒有任何東西 - 沒有用戶,沒有組。如果用戶通過LDAP服務器進行身份驗證,則用戶將與其所在的所有組一起在數據庫中創建,因此可以按需進行。

什麼作品:我的應用程序的開發版本可以很好地對付外部LDAP,也可以用開發數據庫進行測試。

什麼不工作:與sqllite3內存數據庫測試(我再說一遍,以開發數據庫和LDAP嘲笑作品測試)。

我測試的代碼是在這裏:

from django.test import TestCase, Client 
import django 
import ldap 

from mockldap import MockLdap 

class AuthenticationTests(TestCase): 
    """ 
    Set Up the structure (Accounts, Groups) and try authentication 
    """ 
    # Top level hierarchy 
    top = ('dc=ee', {'dc': ['ee']}) 
    test = ('dc=test,dc=ee', {'dc': ['test']}) 

    # Top level groups 
    people = ('ou=people,dc=test,dc=ee', {'ou': ['people'], 'objectClass': ['organizationalUnit', 'top']}) 
    groups = ('ou=groups,dc=test,dc=ee', {'ou': ['groups'], 'objectClass': ['organizationalUnit', 'top']}) 

    # Groups 
    admins = ('cn=admins,ou=groups,dc=test,dc=ee', {'cn': ['admins'], 'memberUid': ['admin'], 
               'objectClass': ['sambaGroupMapping', 'posixGroup', 'top'], 
               'gidNumber': ['1']}) 

    group1 = ('cn=group1,ou=groups,dc=test,dc=ee', {'cn': ['group1'], 
               'memberUid': ['alice', 'bob'], 
             'objectClass': ['sambaGroupMapping', 'posixGroup', 'top']}) 

    group2 = ('cn=group2,ou=groups,dc=test,dc=ee', {'cn': ['group2'], 'memberUid': ['admin', 'bob', 'karl'], 
             'objectClass': ['sambaGroupMapping', 'posixGroup', 'top']}) 

    # Users 
    admin = ('uid=admin,ou=people,dc=test,dc=ee', {'uid': ['admin'], 'userPassword': ['ldaptest'], 'sn': ['Joe'], 
               'cn': ['Admin Joe'], 'mail': ['[email protected]'], 
               'givenName': ['Admin'], 'objectClass': 
                ['top', 'person', 'posixAccount', 'shadowAccount', 
                'inetOrgPerson', 'sambaSamAccount']}) 

    alice = ('uid=alice,ou=people,dc=test,dc=ee', {'uid': ['alice'], 'userPassword': ['ldaptest'], 'sn': ['Cooper'], 
               'cn': ['Alice Cooper'], 'mail': ['[email protected]'], 
               'givenName': ['Alice'], 'objectClass': 
                ['top', 'person', 'posixAccount', 'shadowAccount', 
                'inetOrgPerson', 'sambaSamAccount']}) 

    bob = ('uid=bob,ou=people,dc=test,dc=ee', {'uid': ['bob'], 'userPassword': ['ldaptest'], 'sn': ['Marley'], 
              'cn': ['Bob Marley'], 'mail': ['[email protected]'], 
              'givenName': ['Bob'], 'objectClass': 
               ['top', 'person', 'posixAccount', 'shadowAccount', 
               'inetOrgPerson', 'sambaSamAccount']}) 

    karl = ('uid=karl,ou=people,dc=test,dc=ee', {'uid': ['karl'], 'userPassword': ['ldaptest'], 'sn': ['Suur'], 
              'cn': ['Karl Suur'], 'mail': ['[email protected]'], 
              'givenName': ['Karl'], 'objectClass': 
               ['top', 'person', 'posixAccount', 'shadowAccount', 
                'inetOrgPerson', 'sambaSamAccount']}) 

    # This is the content of our mock LDAP directory. It takes the form 
    # {dn: {attr: [value, ...], ...}, ...}. 
    directory = dict([top, test, people, groups, admins, group1, group2, admin, alice, bob, karl]) 

    @classmethod 
    def setUpTestData(cls): 
     # We only need to create the MockLdap instance once. The content we 
     # pass in will be used for all LDAP connections. 
     cls.mockldap = MockLdap(cls.directory) 

    @classmethod 
    def tearDownClass(cls): 
     del cls.mockldap 

    def setUp(self): 
     # Patch ldap.initialize 
     django.setup() 
     self.mockldap.start() 
     self.ldapobj = self.mockldap['ldap://localhost/'] 

    def tearDown(self): 
     # Stop patching ldap.initialize and reset state. 
     self.mockldap.stop() 
     del self.ldapobj 

    def test_some_basic_mockldap_auth(self): 
     searchStr = 'uid=alice,ou=people,dc=test,dc=ee' 
     results = _do_simple_ldap_search(searchStr) 
     ldapName = results[0][0] 
     ldapPropDict = results[0][1] 
     self.assertEqual(searchStr, ldapName) 
     self.assertEqual(len(ldapPropDict), 7) 

    def test_index_visible_for_all(self): 
     c = Client() 
     response = c.get("/") 
     self.assertContains(response, "https://stackoverflow.com/users/", 1) 
     self.assertContains(response, "/groups/", 1) 

    def test_login(self): 
     c = Client() 
     response = c.post("/api-auth/login/", {'username': 'bob', 'password': 'ldaptest'}) 
     print(response.status_code) 
     response = c.get("https://stackoverflow.com/users/") 
     print(response._container) 

def _do_simple_ldap_search(searchStr): 
    conn = ldap.initialize('ldap://localhost/') 
    conn.simple_bind_s(searchStr, 'ldaptest') 
    results = conn.search_s(searchStr, ldap.SCOPE_SUBTREE,) 

    return results 

代碼也可以在這裏:http://dpaste.com/3D2H4NK(語法高亮)。

我不確定,問題是什麼。唯一我能想到的是,因爲在創建數據庫時沒有用戶,數據庫不會被創建,但我不確定。任何幫助深表感謝。

我已經完成了所有的遷移。

(venv)[email protected]:~/workspace/fileshare/njord$ python manage.py showmigrationsadmin 
[X] 0001_initial 
[X] 0002_logentry_remove_auto_add 
auth 
[X] 0001_initial 
[X] 0002_alter_permission_name_max_length 
[X] 0003_alter_user_email_max_length 
[X] 0004_alter_user_username_opts 
[X] 0005_alter_user_last_login_null 
[X] 0006_require_contenttypes_0002 
[X] 0007_alter_validators_add_error_messages 
contenttypes 
[X] 0001_initial 
[X] 0002_remove_content_type_name 
sessions 
[X] 0001_initial 
(venv)[email protected]:~/workspace/fileshare/njord$ python manage.py makemigrationsNo changes detected 
(venv)[email protected]:~/workspace/fileshare/njord$ python manage.py migrate 
Operations to perform: 
    Apply all migrations: admin, sessions, auth, contenttypes 
Running migrations: 
    No migrations to apply. 

回答

0

什麼必須做的解決方案,是添加這些行manage.py:

if __name__ == "__main__": 
    if 'test' in sys.argv: 
     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.test_settings") 
    else: 
     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") 

加入之後,如果「測試」的事情manage.py的主要方法,該應用程序開始正常工作之後,python manage.py測試和python manage.py runserver都使用正確的設置。

不過,我還沒有得到這與PyCharm工作,目前正在測試它,如何得到它的工作。

0

運行

INSTALLED_APPS每個應用程序(特別的應用程序,對auth_user一個ForeignKey字段)

我得到不同的錯誤,讓我在這裏,但認爲原因是一樣的。測試時,不執行命令makemigrations只是沒有創建
django.db.utils.OperationalError: (1005, "Can't create table '<test_db>.#sql-3821_1c9' (errno: 150)")

在這兩種情況下,有關auth模塊的所有表。
我發現從here