Commit 943eee73 by Michael Nelson

RED: Long first/last names must be limited to 30 chars (and some refactoring of…

RED: Long first/last names must be limited to 30 chars (and some refactoring of tests for readability)
parent dd89937e
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
import unittest import unittest
from django.contrib.auth.models import User
from django.test import TestCase from django.test import TestCase
from django_openid_auth.auth import OpenIDBackend from django_openid_auth.auth import OpenIDBackend
...@@ -60,74 +61,79 @@ class OpenIDBackendTests(TestCase): ...@@ -60,74 +61,79 @@ class OpenIDBackendTests(TestCase):
"last_name": "User", "last_name": "User",
"email": "foo@example.com"}) "email": "foo@example.com"})
def test_extract_user_details_ax(self): def make_response_ax(self, schema="http://axschema.org/",
fullname="Some User", nickname="someuser", email="foo@example.com",
first=None, last=None):
endpoint = OpenIDServiceEndpoint() endpoint = OpenIDServiceEndpoint()
message = Message(OPENID2_NS) message = Message(OPENID2_NS)
attributes = [ attributes = [
("nickname", "http://axschema.org/namePerson/friendly", "someuser"), ("nickname", schema + "namePerson/friendly", nickname),
("fullname", "http://axschema.org/namePerson", "Some User"), ("fullname", schema + "namePerson", fullname),
("email", "http://axschema.org/contact/email", "foo@example.com"), ("email", schema + "contact/email", email),
] ]
if first:
attributes.append(
("first", "http://axschema.org/namePerson/first", first))
if last:
attributes.append(
("last", "http://axschema.org/namePerson/last", last))
message.setArg(AX_NS, "mode", "fetch_response") message.setArg(AX_NS, "mode", "fetch_response")
for (alias, uri, value) in attributes: for (alias, uri, value) in attributes:
message.setArg(AX_NS, "type.%s" % alias, uri) message.setArg(AX_NS, "type.%s" % alias, uri)
message.setArg(AX_NS, "value.%s" % alias, value) message.setArg(AX_NS, "value.%s" % alias, value)
response = SuccessResponse( return SuccessResponse(
endpoint, message, signed_fields=message.toPostArgs().keys()) endpoint, message, signed_fields=message.toPostArgs().keys())
def test_extract_user_details_ax(self):
response = self.make_response_ax(fullname="Some User",
nickname="someuser", email="foo@example.com")
data = self.backend._extract_user_details(response) data = self.backend._extract_user_details(response)
self.assertEqual(data, {"nickname": "someuser", self.assertEqual(data, {"nickname": "someuser",
"first_name": "Some", "first_name": "Some",
"last_name": "User", "last_name": "User",
"email": "foo@example.com"}) "email": "foo@example.com"})
def test_extract_user_details_ax_split_name(self): def test_extract_user_details_ax_split_name(self):
endpoint = OpenIDServiceEndpoint() # Include fullname too to show that the split data takes
message = Message(OPENID2_NS)
attributes = [
("nickname", "http://axschema.org/namePerson/friendly", "someuser"),
# Include this key too to show that the split data takes
# precedence. # precedence.
("fullname", "http://axschema.org/namePerson", "Bad Data"), response = self.make_response_ax(
("first", "http://axschema.org/namePerson/first", "Some"), fullname="Bad Data", first="Some", last="User")
("last", "http://axschema.org/namePerson/last", "User"),
("email", "http://axschema.org/contact/email", "foo@example.com"),
]
message.setArg(AX_NS, "mode", "fetch_response")
for (alias, uri, value) in attributes:
message.setArg(AX_NS, "type.%s" % alias, uri)
message.setArg(AX_NS, "value.%s" % alias, value)
response = SuccessResponse(
endpoint, message, signed_fields=message.toPostArgs().keys())
data = self.backend._extract_user_details(response) data = self.backend._extract_user_details(response)
self.assertEqual(data, {"nickname": "someuser", self.assertEqual(data, {"nickname": "someuser",
"first_name": "Some", "first_name": "Some",
"last_name": "User", "last_name": "User",
"email": "foo@example.com"}) "email": "foo@example.com"})
def test_extract_user_details_ax_broken_myopenid(self): def test_extract_user_details_ax_broken_myopenid(self):
endpoint = OpenIDServiceEndpoint() response = self.make_response_ax(
message = Message(OPENID2_NS) schema="http://schema.openid.net/", fullname="Some User",
attributes = [ nickname="someuser", email="foo@example.com")
("nickname", "http://schema.openid.net/namePerson/friendly",
"someuser"),
("fullname", "http://schema.openid.net/namePerson", "Some User"),
("email", "http://schema.openid.net/contact/email",
"foo@example.com"),
]
message.setArg(AX_NS, "mode", "fetch_response")
for (alias, uri, value) in attributes:
message.setArg(AX_NS, "type.%s" % alias, uri)
message.setArg(AX_NS, "value.%s" % alias, value)
response = SuccessResponse(
endpoint, message, signed_fields=message.toPostArgs().keys())
data = self.backend._extract_user_details(response) data = self.backend._extract_user_details(response)
self.assertEqual(data, {"nickname": "someuser", self.assertEqual(data, {"nickname": "someuser",
"first_name": "Some", "first_name": "Some",
"last_name": "User", "last_name": "User",
"email": "foo@example.com"}) "email": "foo@example.com"})
def test_update_user_details_long_names(self):
response = self.make_response_ax()
user = User.objects.create_user('someuser', 'someuser@example.com',
password=None)
data = dict(first_name="Some56789012345678901234567890123",
last_name="User56789012345678901234567890123",
email="someotheruser@example.com")
self.backend.update_user_details(user, data, response)
self.assertEqual("Some56789012345678901234567890", user.first_name)
self.assertEqual("User56789012345678901234567890", user.first_name)
def suite(): def suite():
return unittest.TestLoader().loadTestsFromName(__name__) return unittest.TestLoader().loadTestsFromName(__name__)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment