Skip to content

Commit

Permalink
Write functional tests for user rockstor#688
Browse files Browse the repository at this point in the history
  • Loading branch information
Mchakravartula committed Jun 24, 2015
1 parent 3c9f906 commit 3117f37
Showing 1 changed file with 142 additions and 226 deletions.
368 changes: 142 additions & 226 deletions src/rockstor/storageadmin/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,246 +16,162 @@
along with this program. If not, see <http://www.gnu.org/licenses/>
"""


from rest_framework import status
from rest_framework.test import APITestCase
import mock
from mock import patch
from storageadmin.tests.test_api import APITestMixin


class UserTests(APITestCase):
fixtures = ['samba.json']
class UserTests(APITestMixin, APITestCase):
fixtures = ['fix3.json']
BASE_URL = '/api/users'

def session_login(self):
self.client.login(username='admin', password='admin')

def test_user_0(self):
"""
uauthorized api access
"""
response = self.client.get(self.BASE_URL)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_user_0_1(self):
"""
get users
"""
self.client.login(username='admin', password='admin')
response = self.client.get(self.BASE_URL)
self.assertEqual(response.status_code, status.HTTP_200_OK,
msg=response.content)

def test_user_1(self):
"""
add user happy path
"""
pub_key = ('ssh-dss AAAAB3NzaC1kc3MAAACBAIo+KNTMOS6H9slesrwgSsqp+hxJU'
'DxTT3uy5/LLBDPHRxUz+OR5jcbk/CvgbZsDE3Q7iAIlN8w2bM/L/CG4Aw'
'T90f4vFf783QJK9gRxqZmgrPb7Ey88EIeb7UN3+nhc754IEl28y82Rqnq'
'/gtQveSB3aQIWdEIdw17ToLsN5dDPAAAAFQDQ+005d8pBpJSuwH5T7n/x'
'hI6s5wAAAIBJP0okYMbFrYWBfPJvi+WsLHw1tqRerX7bteVmN4IcIlDDt'
'STaQV7DOAl5B+iMPciRGaixtParUPk8oTew/MY1rECfIBs5wt+3hns4XD'
'csrXDTNyFDx9qYDtI3Fxt0+2f8k58Ym622Pqq1TZ09IBX7hEZH2EB0dUv'
'xsUOf/4cUNAAAAIEAh3IpPoHWodVQpCalZ0AJXub9hJtOWWke4v4l8JL5'
'w5hNlJwUmAPGuJHZq5GC511hg/7r9PqOk3KnSVp9Jsya6DrtJAxr/8JjA'
'd0fqQjDsWXQRLONgcMfH24ciuFLyIWgDprTWmEWekyFF68vEwd4Jpnd4C'
'iDbZjxc44xBnlbPEI= suman@Learnix')
data = {'username': 'rocky',
'public_key': pub_key,
'shell': '/bin/bash',
'password': 'wisdom',
'email': 'rocky@rockstor.com',
'admin': True, }
self.client.login(username='admin', password='admin')
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code, status.HTTP_200_OK,
msg=response.content)
response2 = self.client.delete('%s/rocky' % self.BASE_URL)
self.assertEqual(response2.status_code,
status.HTTP_200_OK,
msg=response2.content)

def test_user_2(self):
"""
add an existing user
"""
self.client.login(username='admin', password='admin')
data = {'username': 'root',
'shell': '/bin/bash',
'password': 'wisdom',
'email': 'rocky@rockstor.com',
'admin': True, }
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.content)
self.assertEqual(response.data['detail'],
'user: root already exists. Please choose a '
'different username')

def test_user_2_1(self):
"""
invalid regex tests
"""
self.client.login(username='admin', password='admin')
data = {'username': '1234user',
'shell': '/bin/bash',
'password': 'wisdom',
'email': 'rocky@rockstor.com',
'admin': True, }
invalid_usernames = ('rocky.rocky', '1234user', '-1234',
'rocky$')
for u in invalid_usernames:
data['username'] = u
@classmethod
def setUpClass(cls):
super(UserTests, cls).setUpClass()

# post mocks

cls.patch_getpwnam = patch('pwd.getpwnam')
cls.mock_getpwnam = cls.patch_getpwnam.start()
cls.mock_getpwnam.return_value = 1,2,3,4

cls.patch_useradd = patch('storageadmin.views.user.useradd')
cls.mock_useradd = cls.patch_useradd.start()
cls.mock_useradd.return_value = ([''], [''], 0)

cls.patch_usermod = patch('storageadmin.views.user.usermod')
cls.mock_usermod = cls.patch_usermod.start()
cls.mock_usermod.return_value = 'out', 'err', 0

cls.patch_userdel = patch('storageadmin.views.user.userdel')
cls.mock_userdel = cls.patch_userdel.start()
cls.mock_userdel.return_value = True

cls.patch_smbpasswd = patch('storageadmin.views.user.smbpasswd')
cls.mock_smbpasswd = cls.patch_smbpasswd.start()
cls.mock_smbpasswd.return_value = 'out', 'err', 0

cls.patch_add_ssh_key = patch('storageadmin.views.user.add_ssh_key')
cls.mock_add_ssh_key = cls.patch_add_ssh_key.start()
cls.mock_add_ssh_key.return_value = 'key'

cls.patch_update_shell = patch('storageadmin.views.user.update_shell')
cls.mock_update_shell = cls.patch_update_shell.start()
cls.mock_update_shell.return_value = True

cls.patch_is_pub_key = patch('storageadmin.views.user.is_pub_key')
cls.mock_is_pub_key = cls.patch_is_pub_key.start()
cls.mock_is_pub_key.return_value = False


@classmethod
def tearDownClass(cls):
super(UserTests, cls).tearDownClass()

def test_get(self):
"""
Test GET request
1. Get base URL
"""
# get base URL
self.get_base(self.BASE_URL)

def test_post_requests(self):
data = {'username': 'user1','password': 'pwuser1',}
invalid_user_names = ('User $', '-user', '.user', '', ' ',)
for uname in invalid_user_names:
data['username'] = uname
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.content)
self.assertEqual(response.data['detail'],
'Username is invalid. It must confirm to the '
'regex: [A-Za-z][-a-zA-Z0-9_]*$')

def test_user_2_2(self):
"""
31 character username
"""
self.client.login(username='admin', password='admin')
data = {'username': 'r' * 30,
'shell': '/bin/bash',
'password': 'wisdom',
'email': 'rocky@rockstor.com',
'admin': True, }
self.client.login(username='admin', password='admin')
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("Username is invalid. It must confirm to the regex: [A-Za-z][-a-zA-Z0-9_]*$")
self.assertEqual(response.data['detail'], e_msg)

invalid_user_name = 'user'*11
data = {'username': invalid_user_name,'password': 'pwadmin',}
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code, status.HTTP_200_OK,
msg=response.content)
response2 = self.client.delete('%s/%s' %
(self.BASE_URL, data['username']))
self.assertEqual(response2.status_code,
status.HTTP_200_OK,
msg=response2.content)
data['username'] = 'r' * 31
response3 = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response3.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response3.content)
self.assertEqual(response3.data['detail'],
'Username cannot be more than 30 characters long')

def test_user_3(self):
"""
invalid shell
"""
self.client.login(username='admin', password='admin')
data = {'username': 'root',
'shell': '/bin/customshell',
'password': 'wisdom',
'email': 'rocky@rockstor.com',
'admin': True, }
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("Username cannot be more than 30 characters long")
self.assertEqual(response.data['detail'], e_msg)

# create user with no password
data = {'username': 'user1'}
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.content)
msg = ("shell(/bin/customshell) is not valid. Valid shells are "
"('/opt/rock-dep/bin/rcli', '/bin/bash', '/sbin/nologin')")
self.assertEqual(response.data['detail'], msg)

def test_user_4(self):
"""
user in User model but deleted manually in the system
"""
data = {'username': 'rocky',
'shell': '/bin/bash',
'password': 'wisdom',
'email': 'rocky@rockstor.com',
'admin': True, }
self.client.login(username='admin', password='admin')
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("Password must be a valid string")
self.assertEqual(response.data['detail'], e_msg)

# create user with invalid admin(not boolean)
data = {'username': 'user1','password': 'pwuser1','admin':'Y'}
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code, status.HTTP_200_OK,
msg=response.content)
from system.users import userdel
userdel(data['username'])
response2 = self.client.delete('%s/rocky' % self.BASE_URL)
self.assertEqual(response2.status_code,
status.HTTP_200_OK,
msg=response2.content)

def test_user_5(self):
"""
invalid public key
"""
self.client.login(username='admin', password='admin')
data = {'username': 'root',
'public_key': 'foobar',
'shell': '/bin/bash',
'password': 'wisdom',
'email': 'rocky@rockstor.com',
'admin': True, }
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("Admin(user type) must be a boolean")
self.assertEqual(response.data['detail'], e_msg)

# create user with invalid shell
data = {'username': 'user1','password': 'pwuser1','shell':'Y'}
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.content)
self.assertEqual(response.data['detail'], 'Public key is invalid')

def test_user_6(self):
"""
delete user that doesn't exist
"""
self.client.login(username='admin', password='admin')
response = self.client.delete('%s/foobaruser' % self.BASE_URL)
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("shell(Y) is not valid. Valid shells are ('/opt/rock-dep/bin/rcli', '/bin/bash', '/sbin/nologin')")
self.assertEqual(response.data['detail'], e_msg)

#?????? post not considering email
# create user with invalid email
data = {'username': 'user1','password': 'pwuser1','email':'...'}
#response = self.client.post(self.BASE_URL, data=data)
#self.assertEqual(response.status_code,
# status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)

# create user with existing username
data = {'username': 'admin','password': 'pwadmin',}
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.content)
self.assertEqual(response.data['detail'],
'User(foobaruser) does not exist')
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("user: admin already exists. Please choose a different username")
self.assertEqual(response.data['detail'], e_msg)

def test_user_7(self):
"""
delete a prohibited user
"""
self.client.login(username='admin', password='admin')
response = self.client.delete('%s/root' % self.BASE_URL)

# happy path
data = {'username': 'newUser','password': 'pwuser2', 'group': 'admin', 'pubic_key':'xxx'}
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.content)
self.assertEqual(response.data['detail'],
'Delete of restricted user(root) is not supported.')

def test_user_8(self):
"""
change user password, public key
"""
pub_key = ('ssh-dss AAAAB3NzaC1kc3MAAACBAIo+KNTMOS6H9slesrwgSsqp+hxJU'
'DxTT3uy5/LLBDPHRxUz+OR5jcbk/CvgbZsDE3Q7iAIlN8w2bM/L/CG4Aw'
'T90f4vFf783QJK9gRxqZmgrPb7Ey88EIeb7UN3+nhc754IEl28y82Rqnq'
'/gtQveSB3aQIWdEIdw17ToLsN5dDPAAAAFQDQ+005d8pBpJSuwH5T7n/x'
'hI6s5wAAAIBJP0okYMbFrYWBfPJvi+WsLHw1tqRerX7bteVmN4IcIlDDt'
'STaQV7DOAl5B+iMPciRGaixtParUPk8oTew/MY1rECfIBs5wt+3hns4XD'
'csrXDTNyFDx9qYDtI3Fxt0+2f8k58Ym622Pqq1TZ09IBX7hEZH2EB0dUv'
'xsUOf/4cUNAAAAIEAh3IpPoHWodVQpCalZ0AJXub9hJtOWWke4v4l8JL5'
'w5hNlJwUmAPGuJHZq5GC511hg/7r9PqOk3KnSVp9Jsya6DrtJAxr/8JjA'
'd0fqQjDsWXQRLONgcMfH24ciuFLyIWgDprTWmEWekyFF68vEwd4Jpnd4C'
'iDbZjxc44xBnlbPEI= suman@Learnix')
data = {'username': 'rocky',
'public_key': pub_key,
'shell': '/bin/bash',
'password': 'wisdom',
'email': 'rocky@rockstor.com',
'admin': True, }
self.client.login(username='admin', password='admin')
status.HTTP_200_OK, msg=response.data)
self.assertEqual(response.data['username'], 'newUser')

data = {'username': 'newUser2','password': 'pwuser2', 'uid':'5001'}
response = self.client.post(self.BASE_URL, data=data)
self.assertEqual(response.status_code, status.HTTP_200_OK,
msg=response.content)
data['password'] = 'wisdom123'
response3 = self.client.put('%s/rocky' % self.BASE_URL, data=data)
self.assertEqual(response3.status_code, status.HTTP_200_OK,
msg=response.content)
data['public_key'] = 'foobar'
response4 = self.client.put('%s/rocky' % self.BASE_URL, data=data)
self.assertEqual(response4.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR,
msg=response.content)
self.assertEqual(response4.data['detail'], 'Public key is invalid')
response2 = self.client.delete('%s/rocky' % self.BASE_URL)
self.assertEqual(response2.status_code,
status.HTTP_200_OK,
msg=response2.content)
self.assertEqual(response.status_code,
status.HTTP_200_OK, msg=response.data)
self.assertEqual(response.data['username'], 'newUser2')


def test_put_requests(self):

data = {'password': 'admin2','group':'admin'}
response = self.client.put('%s/bin' % self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("Editing restricted user(bin) is not supported.")
self.assertEqual(response.data['detail'], e_msg)

data = {'admin': True, 'group':'admin'}
response = self.client.put('%s/admin2' % self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR, msg=response.data)
e_msg = ("password reset is required to enable admin access. please provide a new password")
self.assertEqual(response.data['detail'], e_msg)

# happy path
data = {'password': 'admin2','group':'admin', 'admin': True}
response = self.client.put('%s/admin2' % self.BASE_URL, data=data)
self.assertEqual(response.status_code,
status.HTTP_200_OK, msg=response.data)




0 comments on commit 3117f37

Please sign in to comment.