Archived
Added check for registering of existing members.
This commit is contained in:
1 parent
f5bfcea72e
commit
2c2585fe5b
2 files changed
+29
-3
No files matched your search
+13
-3
@@ -72,9 +72,19 @@ class MemberResource(ModelResource):
|
||||
"""
|
||||
Catches POST and PATCH requests and intercepts data.
|
||||
"""
|
||||
# Check if the user exists and return an error when attempting to change the username
|
||||
if bundle.obj.user_ptr_id is not None and not bundle.data['username'] == bundle.obj.username:
|
||||
raise ImmediateHttpResponse(HttpForbidden("You can't change your username."))
|
||||
update_existing_user = bundle.obj.user_ptr_id is not None
|
||||
|
||||
if update_existing_user:
|
||||
# If the user already exists, return an error when attempting to change the username.
|
||||
if bundle.data['username'] != bundle.obj.username:
|
||||
raise ImmediateHttpResponse(HttpForbidden("You can't change your username."))
|
||||
else:
|
||||
# If creating new user, check if it already exists:
|
||||
member_count = len(Member.objects.filter(username=bundle.data['username'])) \
|
||||
+ len(Member.objects.filter(email=bundle.data['email'])) \
|
||||
+ len(Member.objects.filter(phone_number=bundle.data['phone_number']))
|
||||
if member_count > 0:
|
||||
raise ImmediateHttpResponse(HttpForbidden("User with given data already exists"))
|
||||
return bundle
|
||||
|
||||
def obj_update(self, bundle, request, **kwargs):
|
||||
|
||||
@@ -119,6 +119,22 @@ class MemberTest(MemberTestBase):
|
||||
|
||||
self.assertEquals(user.phone_number, data['phone_number'])
|
||||
|
||||
def test_post_existing_member(self):
|
||||
"""
|
||||
Tests that we can't create users with duplicate username
|
||||
"""
|
||||
data = {
|
||||
'username' : self.member.username,
|
||||
'password' : 'asdf',
|
||||
'email' : self.member.email,
|
||||
'phone_number' : self.member.phone_number
|
||||
}
|
||||
|
||||
resp = self.api_client.post(self.all_members_url,
|
||||
format = 'json',
|
||||
data = data)
|
||||
self.assertHttpForbidden(resp)
|
||||
|
||||
def test_patch_change_member(self):
|
||||
"""
|
||||
Tests that we can correctly change data of existing members.
|
||||
|
||||
Reference in new issue
Block a user