标签: django-webtest

Django 视图:request.data 什么时候是 dict 还是 QueryDict?

我在这个问题上遇到了一些麻烦,request.data 有时是一个dict(尤其是在测试时),有时是一个QueryDict实例(使用 curl 时)。

这尤其是一个问题,因为在使用 curl 调用视图时显然存在很大差异,如下所示:

curl -X POST --data "some_float=1.23456789012123123" "http://localhost:8000/myview"
Run Code Online (Sandbox Code Playgroud)

或者像这样使用 django_webtest 客户端:

class APIViewTest(WebTest):
    def test_testsomething(self):
        self.app.post(url=url, params=json.dumps({some_float=1.26356756467}))
Run Code Online (Sandbox Code Playgroud)

然后将该 QueryDict 转换为这样的字典

new_dict = dict(**request.data)
my_float = float(new_dict['some_float'])
Run Code Online (Sandbox Code Playgroud)

在测试中一切正常,因为有request.data一个dict,但在生产中,视图崩溃了,因为new_dict['some_float']实际上是一个包含一个元素的列表,而不是预期的浮点数。

我已经考虑过像这样解决这个问题:

    if type(request.data) is dict:
        new_dict = dict(**request.data)
    else:
        new_dict = dict(**request.data.dict())
Run Code Online (Sandbox Code Playgroud)

这感觉非常错误,因为测试只会测试第 2 行,并且(一些?全部?)生产代码将运行第 4 行。

因此,当我想知道为什么 QueryDict 会以这种方式运行时,我宁愿知道为什么以及何时 response.data 是 a QueryDict。以及我如何使用 django 测试来模拟这种行为。生产和测试系统有不同的条件总是很麻烦,有时是不可避免的,但在这种情况下,我觉得它可以解决。或者这是与 django_webtest 相关的特定问题?

python django django-rest-framework django-webtest

8
推荐指数
2
解决办法
5859
查看次数

使用Django和Webtest测试图像上传

有谁知道如何使用WebTest测试图像上传.我目前的代码是:

form['avatar'] =('avatar', os.path.join(settings.PROJECT_PATH, 'static', 'img', 'avatar.png'))
res = form.submit()
Run Code Online (Sandbox Code Playgroud)

在响应中,我收到以下错误"上传有效图像.您上传的文件不是图像或损坏的图像.".

任何帮助将不胜感激.

django tdd webtest django-webtest

5
推荐指数
1
解决办法
1137
查看次数

我可以使用WebTest添加非现有字段吗?

我正在使用WebTest测试表单.但是,某些字段是使用JS动态创建的,因此这些字段不在表单中.我尝试设置其中一个字段时出错:

>>> resp.form['new_field'] = 'value'
or
>>> resp.form.set('new_field', 'value')
or
>>> resp.form.set('new_field', 'value', index=0)
or 
>>> resp.form['new_field'].force_value('value')

*** AssertionError: No field by the name 'new_field' found

有没有办法创建一个字段?

django webtest django-webtest

5
推荐指数
1
解决办法
450
查看次数

测试Django 1-5重置密码表单 - 如何为测试生成令牌?

通过以下测试,令牌不会被识别为有效.在我的手动测试中,它正在工作,所以我错过了生成密码的方式我想.

def test_actual_reset_password(self):
    new_password = "myNewPassword012*"
    token_generator = PasswordResetTokenGenerator()
    user = UserFactory.create()
    token = token_generator.make_token(user=user)

    response = self.assert_page_loading(path="/forgot-password/reset/{0}/".format(token))
    print response 
    # That loads the page with the error message mentioning that the token was already used        

    # So I cannot carry on:
    form = response.form
    form['new_password1'] = new_password
    form['new_password2'] = new_password

    response = form.submit()
Run Code Online (Sandbox Code Playgroud)

在django源代码中,在PasswordResetForm中,我找到了这段代码; 我看不出有什么区别:

def save(self, ..., token_generator=default_token_generator, ...):
    """
    Generates a one-use only link for resetting password and sends to the
    user.
    """
    ...
    for user in …
Run Code Online (Sandbox Code Playgroud)

authentication django django-webtest

4
推荐指数
1
解决办法
1556
查看次数

如何使用django_webtest的基本身份验证来访问Django REST框架?

我在将数据发布到我的测试中使用Django REST框架的一些视图时遇到了麻烦.我正在使用django_webtest来测试我的用户API.我遇到了以下代码的问题:

class UserApiTest(WebTest):

    def setUp(self):
        AdminFactory()

    def test_accessing_user_list_shows_one_user(self):
        user_list = self.app.get('/quickstart/users/', user='admin')
        assert_that(user_list.json, has_entry('count', 1))

    def test_posting_new_user_returns_url_for_user_detail(self):
        post_data = {'username': 'john', 'email': 'john.doe@example.com'} 
        user_create = self.app.post('/quickstart/users/', post_data, user='admin')
        url = 'http://localhost:80/quickstart/users/2/'
        assert_that(user_create.json, has_entry('url', url))
Run Code Online (Sandbox Code Playgroud)

问题是我在第二次测试运行时遇到CSRF错误.查看Django REST Framework文档,我读到只有在使用基于会话的身份验证时才会触发CSRF错误.所以,我想我会尝试基本身份验证,根据Django的文档,只需要设置REMOTE_USER环境变量:

class UserApiTest(WebTest):

    extra_environ = {'REMOTE_USER': 'admin'}

    def setUp(self):
        AdminFactory()

    def test_accessing_user_list_shows_one_user(self):
        user_list = self.app.get('/quickstart/users/')
        assert_that(user_list.json, has_entry('count', 1))

    def test_posting_new_user_returns_url_for_user_detail(self):
        post_data = {'username': 'john', 'email': 'john.doe@example.com'} 
        user_create = self.app.post('/quickstart/users/', post_data)
        url = 'http://localhost:80/quickstart/users/2/'
        assert_that(user_create.json, has_entry('url', url))
Run Code Online (Sandbox Code Playgroud)

这更糟糕,因为用户甚至没有被授权查看这些页面(即访问URL的返回403).

我的问题是:如何使用django_webtest正确设置基本身份验证?

python django-rest-framework django-webtest

3
推荐指数
1
解决办法
1692
查看次数