~ubuntu-branches/ubuntu/trusty/django-openid-auth/trusty

« back to all changes in this revision

Viewing changes to django_openid_auth/tests/test_auth.py

  • Committer: Package Import Robot
  • Author(s): Michael Fladischer
  • Date: 2012-06-27 08:25:19 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120627082519-dath0g790cw1xoyy
Tags: 0.4-1
* New upstream release.
* Lower dependency on python-django to (>= 1.1.2).
* Bump B-D on python-all to (>= 2.6) and XS-P-V to >= 2.6.
* Fix tests for django (>= 1.4) FTBFS (Closes: #669482).
  - Add patch for Django 1.2 style DATABASE configuration
  - Bump B-D on python-django to (>= 1.2) because of new configuration
* Fix DEP5 lintian format warnings.
* Fix DEP5 copyright line format in d/copyright.
* Make sure .pyc files are not used.
* Run wrap-and-sort.
* Change order of my name.
* Update DEP-5 URL to 1.0.
* Fix d/watch for HTTPS URLs.
* Bump Standards-Version to 3.9.3.
* Update years in d/copyright.
* Include openid.txt in documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
import unittest
30
30
 
 
31
from django.contrib.auth.models import User
31
32
from django.test import TestCase
32
33
 
33
34
from django_openid_auth.auth import OpenIDBackend
60
61
                                "last_name": "User",
61
62
                                "email": "foo@example.com"})
62
63
 
 
64
    def make_response_ax(self, schema="http://axschema.org/",
 
65
        fullname="Some User", nickname="someuser", email="foo@example.com",
 
66
        first=None, last=None):
 
67
        endpoint = OpenIDServiceEndpoint()
 
68
        message = Message(OPENID2_NS)
 
69
        attributes = [
 
70
            ("nickname", schema + "namePerson/friendly", nickname),
 
71
            ("fullname", schema + "namePerson", fullname),
 
72
            ("email", schema + "contact/email", email),
 
73
            ]
 
74
        if first:
 
75
            attributes.append(
 
76
                ("first", "http://axschema.org/namePerson/first", first))
 
77
        if last:
 
78
            attributes.append(
 
79
                ("last", "http://axschema.org/namePerson/last", last))
 
80
 
 
81
        message.setArg(AX_NS, "mode", "fetch_response")
 
82
        for (alias, uri, value) in attributes:
 
83
            message.setArg(AX_NS, "type.%s" % alias, uri)
 
84
            message.setArg(AX_NS, "value.%s" % alias, value)
 
85
        return SuccessResponse(
 
86
            endpoint, message, signed_fields=message.toPostArgs().keys())
 
87
 
63
88
    def test_extract_user_details_ax(self):
64
 
        endpoint = OpenIDServiceEndpoint()
65
 
        message = Message(OPENID2_NS)
66
 
        attributes = [
67
 
            ("nickname", "http://axschema.org/namePerson/friendly", "someuser"),
68
 
            ("fullname", "http://axschema.org/namePerson", "Some User"),
69
 
            ("email", "http://axschema.org/contact/email", "foo@example.com"),
70
 
            ]
71
 
        message.setArg(AX_NS, "mode", "fetch_response")
72
 
        for (alias, uri, value) in attributes:
73
 
            message.setArg(AX_NS, "type.%s" % alias, uri)
74
 
            message.setArg(AX_NS, "value.%s" % alias, value)
75
 
        response = SuccessResponse(
76
 
            endpoint, message, signed_fields=message.toPostArgs().keys())
 
89
        response = self.make_response_ax(fullname="Some User",
 
90
            nickname="someuser", email="foo@example.com")
77
91
 
78
92
        data = self.backend._extract_user_details(response)
 
93
 
79
94
        self.assertEqual(data, {"nickname": "someuser",
80
95
                                "first_name": "Some",
81
96
                                "last_name": "User",
82
97
                                "email": "foo@example.com"})
83
98
 
84
99
    def test_extract_user_details_ax_split_name(self):
85
 
        endpoint = OpenIDServiceEndpoint()
86
 
        message = Message(OPENID2_NS)
87
 
        attributes = [
88
 
            ("nickname", "http://axschema.org/namePerson/friendly", "someuser"),
89
 
            # Include this key too to show that the split data takes
90
 
            # precedence.
91
 
            ("fullname", "http://axschema.org/namePerson", "Bad Data"),
92
 
            ("first", "http://axschema.org/namePerson/first", "Some"),
93
 
            ("last", "http://axschema.org/namePerson/last", "User"),
94
 
            ("email", "http://axschema.org/contact/email", "foo@example.com"),
95
 
            ]
96
 
        message.setArg(AX_NS, "mode", "fetch_response")
97
 
        for (alias, uri, value) in attributes:
98
 
            message.setArg(AX_NS, "type.%s" % alias, uri)
99
 
            message.setArg(AX_NS, "value.%s" % alias, value)
100
 
        response = SuccessResponse(
101
 
            endpoint, message, signed_fields=message.toPostArgs().keys())
 
100
        # Include fullname too to show that the split data takes
 
101
        # precedence.
 
102
        response = self.make_response_ax(
 
103
            fullname="Bad Data", first="Some", last="User")
102
104
 
103
105
        data = self.backend._extract_user_details(response)
 
106
 
104
107
        self.assertEqual(data, {"nickname": "someuser",
105
108
                                "first_name": "Some",
106
109
                                "last_name": "User",
107
110
                                "email": "foo@example.com"})
108
111
 
109
112
    def test_extract_user_details_ax_broken_myopenid(self):
110
 
        endpoint = OpenIDServiceEndpoint()
111
 
        message = Message(OPENID2_NS)
112
 
        attributes = [
113
 
            ("nickname", "http://schema.openid.net/namePerson/friendly",
114
 
             "someuser"),
115
 
            ("fullname", "http://schema.openid.net/namePerson", "Some User"),
116
 
            ("email", "http://schema.openid.net/contact/email",
117
 
             "foo@example.com"),
118
 
            ]
119
 
        message.setArg(AX_NS, "mode", "fetch_response")
120
 
        for (alias, uri, value) in attributes:
121
 
            message.setArg(AX_NS, "type.%s" % alias, uri)
122
 
            message.setArg(AX_NS, "value.%s" % alias, value)
123
 
        response = SuccessResponse(
124
 
            endpoint, message, signed_fields=message.toPostArgs().keys())
 
113
        response = self.make_response_ax(
 
114
            schema="http://schema.openid.net/", fullname="Some User",
 
115
            nickname="someuser", email="foo@example.com")
125
116
 
126
117
        data = self.backend._extract_user_details(response)
 
118
 
127
119
        self.assertEqual(data, {"nickname": "someuser",
128
120
                                "first_name": "Some",
129
121
                                "last_name": "User",
130
122
                                "email": "foo@example.com"})
131
123
 
 
124
    def test_update_user_details_long_names(self):
 
125
        response = self.make_response_ax()
 
126
        user = User.objects.create_user('someuser', 'someuser@example.com',
 
127
            password=None)
 
128
        data = dict(first_name=u"Some56789012345678901234567890123",
 
129
            last_name=u"User56789012345678901234567890123",
 
130
            email=u"someotheruser@example.com")
 
131
 
 
132
        self.backend.update_user_details(user, data, response)
 
133
 
 
134
        self.assertEqual("Some56789012345678901234567890",  user.first_name)
 
135
        self.assertEqual("User56789012345678901234567890",  user.last_name)
 
136
 
 
137
    def test_extract_user_details_name_with_trailing_space(self):
 
138
        response = self.make_response_ax(fullname="SomeUser ")
 
139
 
 
140
        data = self.backend._extract_user_details(response)
 
141
 
 
142
        self.assertEqual("", data['first_name'])
 
143
        self.assertEqual("SomeUser", data['last_name'])
 
144
 
 
145
    def test_extract_user_details_name_with_thin_space(self):
 
146
        response = self.make_response_ax(fullname=u"Some\u2009User")
 
147
 
 
148
        data = self.backend._extract_user_details(response)
 
149
 
 
150
        self.assertEqual("Some", data['first_name'])
 
151
        self.assertEqual("User", data['last_name'])
 
152
 
 
153
 
132
154
def suite():
133
155
    return unittest.TestLoader().loadTestsFromName(__name__)