Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate fully-qualified usernames with linkTo. #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions xmantissa/test/test_websharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _verifyPath(self, linkURL):
self.failUnless(isinstance(linkURL, url.URL),
"linkTo should return a nevow.url.URL, not %r" %
(type(linkURL)))
self.assertEquals(str(linkURL), '/users/right/loginsystem')
self.assertEquals(str(linkURL), '/users/right%40host/loginsystem')


def test_linkToProxy(self):
Expand All @@ -126,8 +126,11 @@ def test_linkToProxy(self):
link to.
"""
self._verifyPath(
websharing.linkTo(sharing.getShare(self.s, sharing.getEveryoneRole(
self.s), u'loginsystem')))
websharing.linkTo(
sharing.getShare(
self.s,
sharing.getEveryoneRole(self.s),
u'loginsystem')))


def test_shareURLInjectsShareID(self):
Expand Down Expand Up @@ -191,9 +194,11 @@ def test_defaultShareIDInteractionMatching(self):
share = sharing.getShare(
self.s, sharing.getEveryoneRole(self.s), u'share-id')
url = websharing.linkTo(share)
self.assertEqual(str(url), '/users/right/')
self.assertEqual(str(url), '/users/right%40host/')
# and if we call child()
self.assertEqual(str(url.child('child')), '/users/right/share-id/child')
self.assertEqual(
str(url.child('child')),
'/users/right%40host/share-id/child')


def test_defaultShareIDInteractionNoMatch(self):
Expand All @@ -207,7 +212,7 @@ def test_defaultShareIDInteractionNoMatch(self):
share = sharing.getShare(
self.s, sharing.getEveryoneRole(self.s), u'not-the-share-id')
url = websharing.linkTo(share)
self.assertEqual(str(url), '/users/right/not-the-share-id')
self.assertEqual(str(url), '/users/right%40host/not-the-share-id')


def test_appStoreLinkTo(self):
Expand Down Expand Up @@ -265,6 +270,19 @@ class UserIdentificationTestCase(_UserIdentificationMixin, TestCase):
"""
Tests for L{xmantissa.websharing._storeFromUsername}
"""
def test_fullyQualifiedUsername(self):
"""
L{xmantissa.websharing._storeFromUsername} will parse a fully-qualified
username and use the domain to perform a more specific query.
"""
self.signup.createUser(
u'', u'username', u'localhost', u'', u'username@internet')
self.assertIdentical(
websharing._storeFromUsername(self.siteStore, u'username@localhost'),
self.loginSystem.accountByAddress(
u'username', u'localhost').avatars.open())


def test_sameLocalpartAndUsername(self):
"""
Test that L{xmantissa.websharing._storeFromUsername} doesn't get
Expand Down Expand Up @@ -405,6 +423,7 @@ class UserIndexPageTestCase(_UserIdentificationMixin, TestCase):
"""
username = u'alice'
domain = u'example.com'
fqUsername = u'{}%40{}'.format(username, domain)
shareID = u'ashare'

def setUp(self):
Expand Down Expand Up @@ -460,10 +479,11 @@ def test_locateChild(self):
def test_linkToMatchesUserURL(self):
"""
Test that L{xmantissa.websharing.linkTo} generates a URL using the
localpart of the account's internal L{axiom.userbase.LoginMethod}
localpart and domain of the account's internal
L{axiom.userbase.LoginMethod}
"""
pathString = str(websharing.linkTo(self.share))
expected = u'/users/%s/%s' % (self.username, self.shareID)
expected = u'/users/%s/%s' % (self.fqUsername, self.shareID)
self.assertEqual(pathString, expected.encode('ascii'))


Expand Down
18 changes: 13 additions & 5 deletions xmantissa/websharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ def linkTo(sharedProxyOrItem):
else:
for lm in userbase.getLoginMethods(userStore):
if lm.internal:
path = ['users', lm.localpart.encode('ascii')]
username = '{}@{}'.format(
lm.localpart.encode('ascii'),
lm.domain.encode('ascii'))
path = ['users', username]
break
else:
raise RuntimeError(
Expand All @@ -216,16 +219,21 @@ def _storeFromUsername(store, username):
@param store: site-store
@type store: L{axiom.store.Store}

@param username: the name a user signed up with
@param username: the name a user signed up with, may be fully qualified
@type username: C{unicode}

@rtype: L{axiom.store.Store} or C{None}
"""
parts = username.split(u'@', 1)
criteria = [
userbase.LoginMethod.localpart == parts[0],
userbase.LoginMethod.internal == True]
if len(parts) == 2:
criteria.append(userbase.LoginMethod.domain == parts[1])

lm = store.findUnique(
userbase.LoginMethod,
attributes.AND(
userbase.LoginMethod.localpart == username,
userbase.LoginMethod.internal == True),
attributes.AND(*criteria),
default=None)
if lm is not None:
return lm.account.avatars.open()
Expand Down