~twom/django-openid-auth/django-1.9-urls

« back to all changes in this revision

Viewing changes to django_openid_auth/tests/test_admin.py

  • Committer: anthony.lenton at canonical
  • Date: 2012-08-29 14:37:47 UTC
  • mfrom: (88.2.3 django-1.3-fixes)
  • Revision ID: anthony.lenton@canonical.com-20120829143747-vhp3oy82yrpd6s01
[r=jamesh] Small changes to make it work with django-1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# django-openid-auth -  OpenID integration for django.contrib.auth
 
2
#
 
3
# Copyright (C) 2009-2012 Canonical Ltd.
 
4
#
 
5
# Redistribution and use in source and binary forms, with or without
 
6
# modification, are permitted provided that the following conditions
 
7
# are met:
 
8
#
 
9
# * Redistributions of source code must retain the above copyright
 
10
# notice, this list of conditions and the following disclaimer.
 
11
#
 
12
# * Redistributions in binary form must reproduce the above copyright
 
13
# notice, this list of conditions and the following disclaimer in the
 
14
# documentation and/or other materials provided with the distribution.
 
15
#
 
16
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
17
# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
18
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 
19
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 
20
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 
21
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 
22
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
23
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
24
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
27
# POSSIBILITY OF SUCH DAMAGE.
 
28
"""
 
29
Tests for the django_openid_auth Admin login form replacement.
 
30
"""
 
31
 
 
32
import os
 
33
import unittest
 
34
 
 
35
from django.conf import settings
 
36
from django.contrib.auth.models import User, AnonymousUser
 
37
 
 
38
settings.OPENID_USE_AS_ADMIN_LOGIN = True
 
39
from django_openid_auth import admin
 
40
 
 
41
from django.test import TestCase
 
42
 
 
43
 
 
44
def create_user(is_staff=False, authenticated=True):
 
45
    """
 
46
    Create and return a user, either the AnonymousUser or a normal Django user,
 
47
    setting the is_staff attribute if appropriate.
 
48
    """
 
49
    if not authenticated:
 
50
        return AnonymousUser()
 
51
    else:
 
52
        user = User(
 
53
            username=u'testing', email='testing@example.com',
 
54
            is_staff=is_staff)
 
55
        user.set_password(u'test')
 
56
        user.save()
 
57
 
 
58
 
 
59
class SiteAdminTests(TestCase):
 
60
    """
 
61
    TestCase for accessing /admin/ when the django_openid_auth form replacement
 
62
    is in use.
 
63
    """
 
64
 
 
65
    def test_admin_site_with_openid_login_authenticated_non_staff(self):
 
66
        """
 
67
        If the request has an authenticated user, who is not flagged as a
 
68
        staff member, then they get a failure response.
 
69
        """
 
70
        create_user()
 
71
        self.client.login(username='testing', password='test')
 
72
        response = self.client.get('/admin/')
 
73
        self.assertTrue('User testing does not have admin access.' in
 
74
                        response.content, 'Missing error message in response')
 
75
 
 
76
    def test_admin_site_with_openid_login_non_authenticated_user(self):
 
77
        """
 
78
        Unauthenticated users accessing the admin page should be directed to
 
79
        the OpenID login url.
 
80
        """
 
81
        response = self.client.get('/admin/')
 
82
        self.assertEqual(302, response.status_code)
 
83
        self.assertEqual('http://testserver/openid/login/?next=/admin/',
 
84
                         response['Location'])
 
85
 
 
86
 
 
87
def suite():
 
88
    return unittest.TestLoader().loadTestsFromName(__name__)