Exemple #1
0
def test_create():
    request = req('get')
    request.user = Struct(is_staff=True)
    c = Admin.create(request=request, app_name='tests', model_name='foo')
    p = c.bind(request=request)
    assert list(p.parts.create_tests_foo.fields.keys()) == ['foo']

    assert Foo.objects.count() == 0
    p = c.bind(request=req('post', foo=7, **{'-submit': ''}))
    assert p.parts.create_tests_foo.is_valid()
    p.render_to_response()
    assert Foo.objects.count() == 1
    assert Foo.objects.get().foo == 7
Exemple #2
0
def test_create(mock_messages, settings):
    settings.ROOT_URLCONF = __name__

    request = staff_req('get')
    c = Admin.create(request=request, app_name='tests', model_name='foo')
    p = c.bind(request=request)
    assert list(p.parts.create_tests_foo.fields.keys()) == ['foo']

    assert Foo.objects.count() == 0

    # Check access control for not logged in
    request = req('post', foo=7, **{'-submit': ''})
    assert isinstance(
        Admin.create(request=request, app_name='tests', model_name='foo'),
        HttpResponseRedirect)
    assert Foo.objects.count() == 0

    # Check access control for not staff
    request = user_req('post', foo=7, **{'-submit': ''})
    with pytest.raises(Http404):
        Admin.create(request=request, app_name='tests', model_name='foo')

    # Now for real
    request = staff_req('post', foo=7, **{'-submit': ''})
    c = Admin.create(request=request, app_name='tests', model_name='foo')
    p = c.bind(request=staff_req('post', foo=7, **{'-submit': ''}))
    assert p.parts.create_tests_foo.is_valid()
    p.render_to_response()

    assert Foo.objects.count() == 1
    f = Foo.objects.get()
    assert f.foo == 7

    mock_messages.add_message.assert_called_with(request,
                                                 mock_messages.INFO,
                                                 f'Foo {f} was created',
                                                 fail_silently=True)