Skip to content

Commit 784c423

Browse files
committed
revise some assumptions about input data, make sure the tests all pass
1 parent 045e8dc commit 784c423

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

tests/test_functional.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,6 @@ def test_user_adobeid_unicode():
5151
"useAdobeID": True}
5252

5353

54-
def test_user_adobeid_unicode_error_unicode_dot_above():
55-
with pytest.raises(ValueError) as excinfo:
56-
UserAction(email=u"lwałę[email protected]")
57-
assert excinfo.type == ArgumentError
58-
if six.PY2:
59-
assert excinfo.match(u"lwałę[email protected]".encode('utf8'))
60-
with pytest.raises(ValueError) as excinfo:
61-
UserAction(email=u"lwałę[email protected]".encode('utf8'))
62-
assert excinfo.type == ArgumentError
63-
assert excinfo.match(u"lwałę[email protected]".encode('utf8'))
64-
65-
66-
def test_user_adobeid_unicode_error_trailing_dot():
67-
with pytest.raises(ValueError):
68-
UserAction(email=u"[email protected]")
69-
70-
7154
def test_user_enterpriseid():
7255
user = UserAction(id_type=IdentityTypes.enterpriseID, email="[email protected]")
7356
assert user.wire_dict() == {"do": [], "user": "[email protected]"}
@@ -88,11 +71,6 @@ def test_user_federatedid_username():
8871
assert user.wire_dict() == {"do": [], "user": "dbrotsky", "domain": "k.on-the-side.net"}
8972

9073

91-
def test_user_federatedid_username_unicode_error():
92-
with pytest.raises(ValueError):
93-
UserAction(id_type=IdentityTypes.federatedID, username=u"lwałęsa", domain="k.on-the-side.net")
94-
95-
9674
def test_create_user_adobeid():
9775
user = UserAction(email="[email protected]")
9876
user.create()
@@ -220,15 +198,6 @@ def test_different_email_username():
220198
"user": "[email protected]"}
221199

222200

223-
def test_malformed_email_type_username():
224-
"""
225-
Test email-type username with malformed email address
226-
"""
227-
user = UserAction(id_type=IdentityTypes.federatedID, email="[email protected]")
228-
with pytest.raises(ValueError):
229-
user.update(username="@[email protected]")
230-
231-
232201
def test_update_user_adobeid():
233202
user = UserAction(id_type=IdentityTypes.adobeID, email="[email protected]")
234203
user.update(first_name="Johnny", last_name="Danger")

umapi_client/functional.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,29 @@ def __init__(self, id_type=IdentityTypes.adobeID, email=None, username=None, dom
8383
self.id_type = id_type
8484
self.email = None
8585
self.domain = domain
86+
if username is not None:
87+
if email and username.lower() == email.lower():
88+
# ignore the username if it's the same as the email (policy default)
89+
username = None
90+
if domain:
91+
self.domain = domain
92+
elif id_type is not IdentityTypes.federatedID:
93+
raise ArgumentError("Username must match email except for Federated ID")
8694
if email is not None:
95+
if '@' not in email:
96+
raise ArgumentError("Invalid email address: %s" % email)
8797
self.email = email
88-
if self.domain is None:
98+
if not self.domain:
8999
atpos = email.index('@')
90100
self.domain = email[atpos + 1:]
91-
if id_type == IdentityTypes.adobeID:
101+
elif not username:
102+
raise ArgumentError("No user identity specified.")
103+
elif not domain:
104+
raise ArgumentError("Both username and domain must be specified")
105+
106+
if username:
107+
Action.__init__(self, user=username, domain=self.domain, **kwargs)
108+
elif id_type == IdentityTypes.adobeID:
92109
# by default if two users have the same email address, the UMAPI server will prefer the matching
93110
# Federated or Enterprise ID user; so we use the undocumented option to prefer the AdobeID match
94111
Action.__init__(self, user=email, useAdobeID=True, **kwargs)
@@ -116,6 +133,8 @@ def create(self, first_name=None, last_name=None, country=None, email=None,
116133
raise ArgumentError("You must specify email when creating a user")
117134
elif self.email is None:
118135
self.email = email
136+
elif self.email.lower() != email.lower():
137+
raise ArgumentError("Specified email (%s) doesn't match user's email (%s)" % (email, self.email))
119138
create_params["email"] = self.email
120139
if on_conflict in IfAlreadyExistsOptions.__members__:
121140
on_conflict = IfAlreadyExistsOptions[on_conflict]
@@ -145,6 +164,8 @@ def update(self, email=None, username=None, first_name=None, last_name=None, cou
145164
:param country: new country for this user
146165
:return: the User, so you can do User(...).update(...).add_to_groups(...)
147166
"""
167+
if self.id_type != IdentityTypes.federatedID and email != username:
168+
raise ArgumentError("Username and email address must match except for Federated ID types")
148169
updates = {}
149170
for k, v in six.iteritems(dict(email=email, username=username,
150171
firstname=first_name, lastname=last_name,

0 commit comments

Comments
 (0)