Skip to content

Commit

Permalink
update test cases and bump up the coverage. Fixes #688
Browse files Browse the repository at this point in the history
  • Loading branch information
schakrava committed Jul 6, 2015
1 parent 538ae74 commit fec9268
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
46 changes: 27 additions & 19 deletions src/rockstor/storageadmin/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_get(self):
self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response)

def test_post_requests(self):

data = {'username': 'user1','password': 'pwuser1',}
invalid_user_names = ('User $', '-user', '.user', '', ' ',)
for uname in invalid_user_names:
Expand Down Expand Up @@ -129,15 +129,15 @@ def test_post_requests(self):
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("user: admin already exists. Please choose a different username")
e_msg = ("User(admin) already exists. Please choose a different username")
self.assertEqual(response.data['detail'], e_msg)

# create user with existing username admin2 and uid
# create user with existing username admin2 and uid
data = {'username': 'admin2', 'password': 'pwadmin2', 'uid':'0000'}
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("user: admin2 already exists. Please choose a different username")
e_msg = ("User(admin2) already exists. Please choose a different username")
self.assertEqual(response.data['detail'], e_msg)

# create a user with existing uid (admin2 has uid 1001)
Expand All @@ -148,6 +148,14 @@ def test_post_requests(self):
e_msg = ("uid: 1001 already exists. Please choose a different one.")
self.assertEqual(response.data['detail'], e_msg)

# create a user that is already a system user(eg: root)
data = {'username': 'root', 'password': 'rootpw'}
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("User(root) already exists. Please choose a different username")
self.assertEqual(response.data['detail'], e_msg)

# happy path
data = {'username': 'newUser','password': 'pwuser2', 'group': 'admin', 'pubic_key':'xxx'}
response = self.client.post(self.BASE_URL, data=data)
Expand All @@ -161,28 +169,28 @@ def test_post_requests(self):
status.HTTP_200_OK, msg=response.data)
self.assertEqual(response.data['username'], 'newUser2')

def test_duplicate_name2(self):

def test_duplicate_name2(self):

# create user with existing user with name exists in system user
data = {'username': 'chrony','password': 'pwadmin2',}
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("user: chrony already exists. Please choose a different username")
e_msg = ("User(chrony) already exists. Please choose a different username")
self.assertEqual(response.data['detail'], e_msg)

def test_duplicate_name1(self):

# create user with existing username admin2 (throwing appropriate error if uid sent in data)
data = {'username': 'admin2','password': 'pwadmin2'}
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("user: admin2 already exists. Please choose a different username")
e_msg = ("User(admin2) already exists. Please choose a different username")
self.assertEqual(response.data['detail'], e_msg)


def test_email_validation(self):

# create user with invalid email
Expand Down Expand Up @@ -243,8 +251,8 @@ def test_put_requests(self):
response = self.client.put('%s/admin2' % self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_200_OK, msg=response.data)


def test_delete_requests(self):

# delete user that does not exists
Expand All @@ -254,8 +262,8 @@ def test_delete_requests(self):
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("User(admin5) does not exist")
self.assertEqual(response.data['detail'], e_msg)


# delete user that does not exists
username = 'bin'
response = self.client.delete('%s/%s' % (self.BASE_URL,username))
Expand Down Expand Up @@ -297,4 +305,4 @@ def test_delete_requests(self):
username = 'games'
response = self.client.delete('%s/%s' % (self.BASE_URL,username))
self.assertEqual(response.status_code,
status.HTTP_200_OK, msg=response.data)
status.HTTP_200_OK, msg=response.data)
3 changes: 1 addition & 2 deletions src/rockstor/storageadmin/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,11 @@ def put(self, request, username):
u.user.set_password(new_pw)
u.user.save()
else:
u.admin = admin
if (u.user is not None):
auser = u.user
u.user = None
auser.delete()

u.admin = admin
u.public_key = public_key
if (email is not None and email != ''):
u.email = email
Expand Down

0 comments on commit fec9268

Please sign in to comment.