Пример #1
0
def test_page_render():
    # Working around some weird issue with pypy3+django3.0
    from django.conf import settings
    settings.DEBUG = False

    # end workaround

    class MyPage(Page):
        header = html.h1('Foo')
        body = html.div('bar bar')

    my_page = MyPage()
    my_page = my_page.bind(request=user_req('get'))

    response = my_page.render_to_response()

    expected_html = '''
        <!DOCTYPE html>
        <html>
            <head>
                <title></title>
            </head>
            <body>
                 <h1> Foo </h1>
                 <div> bar bar </div>
            </body>
        </html>
    '''

    prettified_expected = prettify(expected_html)
    prettified_actual = prettify(response.content)
    assert prettified_expected == prettified_actual
Пример #2
0
def test_404_for_non_staff(settings, view, kwargs):
    settings.ROOT_URLCONF = __name__
    if 'pk' in kwargs:
        Foo.objects.create(pk=kwargs['pk'], foo=1)
    request = user_req('get')

    with pytest.raises(Http404):
        view(request=request, **kwargs)
Пример #3
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)
Пример #4
0
def test_profiler_no_access():
    assert middleware(req('get')) is sentinel
    assert middleware(user_req('get')) is sentinel
    assert middleware(user_req('get', _iommi_prof='')) is sentinel