From fec9268ef53b0259fbc99bee3eaf44c6b28741a7 Mon Sep 17 00:00:00 2001 From: Suman Chakravartula Date: Mon, 6 Jul 2015 11:35:27 -0700 Subject: [PATCH] update test cases and bump up the coverage. Fixes #688 --- src/rockstor/storageadmin/tests/test_user.py | 46 ++++++++++++-------- src/rockstor/storageadmin/views/user.py | 3 +- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/rockstor/storageadmin/tests/test_user.py b/src/rockstor/storageadmin/tests/test_user.py index ca87e2903..82511226a 100644 --- a/src/rockstor/storageadmin/tests/test_user.py +++ b/src/rockstor/storageadmin/tests/test_user.py @@ -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: @@ -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) @@ -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) @@ -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 @@ -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 @@ -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)) @@ -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) \ No newline at end of file + status.HTTP_200_OK, msg=response.data) diff --git a/src/rockstor/storageadmin/views/user.py b/src/rockstor/storageadmin/views/user.py index 8198ab0fb..becb67797 100644 --- a/src/rockstor/storageadmin/views/user.py +++ b/src/rockstor/storageadmin/views/user.py @@ -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