简体   繁体   English

使用Model Mommy测试登录

[英]Testing Login with Model Mommy

I having a problem testing my login in my functional test. 我在功能测试中测试登录时遇到问题。 I am using model mommy to create aa user call Megan with a password, but my test still does not pass since when the information is sent it throws up and error on the html page of "Please enter a correct username and password. Note that both fields may be case-sensitive." 我正在使用模型妈妈创建一个使用密码的用户调用梅根,但是我的测试仍然没有通过,因为在发送信息时,它抛出了错误,并且在html页面上出现了错误,请输入正确的用户名和密码。请注意,字段可能区分大小写。” So I suspect that the test user is not being created or something like that. 所以我怀疑没有创建测试用户或类似的东西。

functional_test: 功能测试:

from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from model_mommy import mommy
from django.contrib.auth.models import User


class NewUserTest(LiveServerTestCase):

    def setUp(self):
        self.user = mommy.make('User', 
        username='Megan',
        password = 'password',
        is_active = True)

        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(15)

    def tearDown(self):
        self.browser.quit()

    def test_user_can_start_a_new_movement(self):
        #some code
        self.browser.get(self.live_server_url)
        self.assertIn('P2', self.browser.title)

        #Megan first logs in 
        login_link = self.browser.find_element_by_link_text('Log In')
        login_link.click()
        #Megan then puts in her user information
        username = self.browser.find_element_by_id('id_username')
        password = self.browser.find_element_by_id('id_password')
        submit = self.browser.find_element_by_id('id_submit')
        username.send_keys('Megan')
        password.send_keys('password')
        submit.click()

login.html login.html

{% extends 'base.html' %}

{% block body_block %}

<form action="/accounts/login/" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="submit" id='id_submit'>

</form>

{% endblock %}

users.urls.py: users.urls.py:

from django.conf.urls import url, patterns
from django.contrib.auth.views import login, logout

from movements import views 

urlpatterns = [
    url(r'^login/', login,
 {'template_name': 'login.html'}, name='login'),
]

if you still insist on using model mommy: 如果您仍然坚持使用模型妈妈:

class NewUserTest(LiveServerTestCase):
    def setUp(self):
        self.user = mommy.make('User', 
            username='Megan',
            is_active = True)
        self.user.set_password('password')
        self.user.save()

I believe your code is not working at the moment because mommy.make is not hashing the password. 我相信您的代码目前无法正常工作,因为mommy.make不会对密码进行哈希处理。 I don't see any advantage of using mommy in this case, so it should be fine to use the regular create_user method instead. 我看不到在这种情况下使用mommy任何优势,因此可以改用常规的create_user方法。

self.user = User.objects.create_user 
    username='Megan',
    email='megan@example.com',
    password = 'password',
)

This will ensure that the password is hashed properly. 这将确保正确地对密码进行哈希处理。

If you still want to use model mommy so that it creates foreign keys to other models, you can call set_password to correctly hash the password. 如果仍要使用模型妈妈来创建其他模型的外键,则可以调用set_password正确地对密码进行哈希处理。 Thanks mislavcimpersak for suggesting this in their answer. 感谢mislavcimpersak在回答中提出的建议。

    self.user = mommy.make('User', 
    username='Megan',
    is_active = True)
    self.user.set_password('password')
    self.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM