سؤال

أحاول تسجيل الدخول إلى عميل الاختبار باستخدام وظيفة تسجيل الدخول المضمنة. أحاول وحدة الاختبار وأحتاج إلى تسجيل الدخول لاختبار بعضها. لقد كنت أحاول القيام بذلك لفترة طويلة وأحتاج إلى مساعدة. بعض الملاحظات:

Create_User () يقوم بإنشاء مستخدم صالح ، وقد تم استخدامه في مواقع أخرى.

من ما رأيته حول Client.login () يعيد منطقية ، عندما أجريت اختباراتي ، كان الفشل "خطأ غير صحيح" ، لذلك يبدو أن هذا صحيح.

الطريقة الوحيدة التي قمت بتسجيل الدخول بنجاح هي الاتصال بالعميل. post ("/my/login/url" ، {اسم المستخدم وكلمة المرور في القول الذي أجده غريبًا جدًا.

def setUp(self):
    """
    Initializes the test client and logs it in.
    """
    self.user = create_user()
    self.logged_in = self.client.login(username=self.user.username, password=self.user.password)

def test_valid(self):
    self.assertTrue(self.logged_in)

لقد غيرته إلى ما يلي:

def setUp(self):
    """
    Initializes the test client and logs it in.
    """
    self.password = "password"
    self.user = create_user(password=self.password)
    self.logged_in = self.client.login(username=self.user.username, password=self.password)

لا يزال يفشل في تسجيل الدخول.

إنشاء مستخدم في الفئة "ثابتة" وله مستخدم user_count تم تهيئته على أنه 0 ، فإن الوظيفة هي كما يلي:

def create_user(username=None, password=None, email=None, is_superuser=False):
    if username is None:
        username = "user%d" % Static.user_count
        while User.objects.filter(username=username).count() != 0:
            Static.user_count += 1
            username = "user%d" % Static.user_count
    if password is None:
        password = "password"
    if email is None:
        email="user%d@test.com" % Static.user_count

    Static.user_count += 1
    user = User.objects.create(username=username, password=password,   is_superuser=is_superuser)
هل كانت مفيدة؟

المحلول

لا يمكنك الوصول إلى كلمة المرور مباشرة. ال password السمة مشفرة. (نرى إدارة كلمة المرور في Django.)

على سبيل المثال ، هنا عينة إخراج كلمة المرور.

>>> user = User.objects.create_user(username='asdf', email='asdf@example.com', password='xxxx')
>>> user.password
'sha1$166e7$4028738f0c0df0e7ec3cec06843c35d2b5a1aae8'

كما ترون، user.password ليس xxxx أنا أعطيت.

سأعدل create_user لقبول معلمة كلمة المرور الاختيارية. وتمرير كلمة المرور على حد سواء create_user, ، و client.login على النحو التالي:

def setUp(self):
    """
    Initializes the test client and logs it in.
    """
    password = 'secret'
    self.user = create_user(password=password)
    self.logged_in = self.client.login(username=self.user.username, password=password)

تحديث

create_user يجب استخدام User.objects.create_user بدلاً من User.objects.create. ويجب إرجاع كائن المستخدم الذي تم إنشاؤه:

def create_user(username=None, password=None, email=None, is_superuser=False):
    if username is None:
        username = "user%d" % Static.user_count
        while User.objects.filter(username=username).count() != 0:
            Static.user_count += 1
            username = "user%d" % Static.user_count
    if password is None:
        password = "password"
    if email is None:
        email="user%d@test.com" % Static.user_count

    Static.user_count += 1
    user = User.objects.create_user(username=username, password=password)
    #                   ^^^^^^^^^^^
    user.is_superuser = is_superuser
    user.save()
    return user # <---
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top