Skip to content

Commit

Permalink
Generate fully-qualified usernames with linkTo.
Browse files Browse the repository at this point in the history
URLs with a localpart-only username will continue to work in share URLs,
but `linkTo` will stop generating them.
  • Loading branch information
jonathanj authored and Jonathan Jacobs committed Oct 31, 2020
1 parent 53e5502 commit 68486da
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
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

0 comments on commit 68486da

Please sign in to comment.