Vấn đề của tôi là tôi muốn đưa ra một cơ sở dữ liệu thử nghiệm riêng biệt, tách biệt với cơ sở dữ liệu phát triển của tôi. Bản thân ứng dụng gần giống với hướng dẫn bắt đầu nhanh của Django-Rest-Framework, chỉ là tôi sử dụng phần phụ trợ LDAP. Cơ sở dữ liệu phát triển của tôi sử dụng MySQL. Tôi có tệp cài đặt riêng để thử nghiệm.Django thử nghiệm trên cơ sở dữ liệu cho riêng "OperationalError: không có bảng như: auth_user"
Full lỗi traceback tôi đặt ở đây: http://dpaste.com/1W3TX1E, nhưng phần thú vị là:
sqlite3.OperationalError: no such table: auth_user
Full test-settings đang ở đây: http://dpaste.com/1K7KHK4. cài đặt liên quan của tôi là (pyldap và django-ldap-auth đang thiếu từ các ứng dụng được cài đặt, chúng được cài đặt bằng tay với 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:',
}
}
Ngay bây giờ các ứng dụng không cơ bản chỉ xác thực với LDAP. Tôi nghĩ rằng đó là lý do tại sao tôi có một lỗi - khi tôi làm bài kiểm tra tôi không có gì trong cơ sở dữ liệu của tôi - không có người dùng, không có nhóm. Nếu người dùng được xác thực chống lại máy chủ LDAP, sử dụng được tạo ra trong cơ sở dữ liệu cùng với tất cả các nhóm anh/cô ấy là trong, vì vậy nó sẽ xảy ra theo yêu cầu.
gì hoạt động: phiên bản phát triển ứng dụng của tôi hoạt động tốt với LDAP bên ngoài và cũng thử nghiệm làm việc với cơ sở dữ liệu phát triển.
gì không hoạt động: Kiểm tra với sqllite3 cơ sở dữ liệu trong bộ nhớ (Tôi lặp lại, thử nghiệm với cơ sở dữ liệu phát triển và các công trình ldap chế giễu).
Mã tôi thử nghiệm với là ở đây:
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
Mã cũng có sẵn ở đây: http://dpaste.com/3D2H4NK (cú pháp nổi bật).
Tôi không chắc chắn, vấn đề là gì. Điều duy nhất tôi có thể nghĩ là bởi vì không có người dùng trong cơ sở dữ liệu lúc tạo, cơ sở dữ liệu không được tạo ra, nhưng tôi không chắc chắn. Bất kỳ sự giúp đỡ nào cũng được đánh giá cao.
Tôi đã làm tất cả các di cư.
(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.