1 #!/usr/bin/python 2 # 3 # Copyright 2008 Google Inc. All Rights Reserved. 4 5 """Test for user.""" 6 7 import unittest, os, sys 8 9 import common 10 from autotest_lib.cli import cli_mock, user 11 12 13 class user_list_unittest(cli_mock.cli_unittest): 14 def test_parse_user_not_required(self): 15 ul = user.user_list() 16 sys.argv = ['atest'] 17 (options, leftover) = ul.parse() 18 self.assertEqual([], ul.users) 19 self.assertEqual([], leftover) 20 21 22 def test_parse_with_users(self): 23 ul = user.user_list() 24 ufile = cli_mock.create_file('user0\nuser3\nuser4\n') 25 sys.argv = ['atest', 'user1', '--ulist', ufile.name, 'user3'] 26 (options, leftover) = ul.parse() 27 self.assertEqualNoOrder(['user0', 'user1','user3', 'user4'], 28 ul.users) 29 self.assertEqual(leftover, []) 30 ufile.clean() 31 32 33 def test_parse_with_acl(self): 34 ul = user.user_list() 35 sys.argv = ['atest', '--acl', 'acl0'] 36 (options, leftover) = ul.parse() 37 self.assertEqual('acl0', ul.acl) 38 self.assertEqual(leftover, []) 39 40 41 def test_parse_with_access_level(self): 42 ul = user.user_list() 43 sys.argv = ['atest', '--access_level', '3'] 44 (options, leftover) = ul.parse() 45 self.assertEqual('3', ul.access_level) 46 self.assertEqual(leftover, []) 47 48 49 def test_parse_with_all(self): 50 ul = user.user_list() 51 ufile = cli_mock.create_file('user0\nuser3\nuser4\n') 52 sys.argv = ['atest', 'user1', '--ulist', ufile.name, 'user3', 53 '-l', '4', '-a', 'acl0'] 54 (options, leftover) = ul.parse() 55 self.assertEqualNoOrder(['user0', 'user1','user3', 'user4'], 56 ul.users) 57 self.assertEqual('acl0', ul.acl) 58 self.assertEqual('4', ul.access_level) 59 self.assertEqual(leftover, []) 60 ufile.clean() 61 62 63 def test_execute_list_all(self): 64 self.run_cmd(argv=['atest', 'user', 'list'], 65 rpcs=[('get_users', {}, 66 True, 67 [{u'access_level': 0, 68 u'login': u'user0', 69 u'id': 41}, 70 {u'access_level': 0, 71 u'login': u'user5', 72 u'id': 42}, 73 {u'access_level': 2, 74 u'login': u'user0', 75 u'id': 3}])], 76 out_words_ok=['user0', 'user5'], 77 out_words_no=['1', '3', '41', '42']) 78 79 80 def test_execute_list_all_with_user(self): 81 self.run_cmd(argv=['atest', 'user', 'list', 'user0'], 82 rpcs=[('get_users', {'login__in': ['user0']}, 83 True, 84 [{u'access_level': 2, 85 u'login': u'user0', 86 u'id': 3}])], 87 out_words_ok=['user0'], 88 out_words_no=['2', '3']) 89 90 91 def test_execute_list_all_with_acl(self): 92 self.run_cmd(argv=['atest', 'user', 'list', '--acl', 'acl0'], 93 rpcs=[('get_users', {'aclgroup__name__in': ['acl0']}, 94 True, 95 [{u'access_level': 2, 96 u'login': u'user0', 97 u'id': 3}])], 98 out_words_ok=['user0'], 99 out_words_no=['2', '3']) 100 101 102 def test_execute_list_all_with_access_level(self): 103 self.run_cmd(argv=['atest', 'user', 'list', '--access_level', '2'], 104 rpcs=[('get_users', {'access_level__in': ['2']}, 105 True, 106 [{u'access_level': 2, 107 u'login': u'user0', 108 u'id': 3}])], 109 out_words_ok=['user0'], 110 out_words_no=['2', '3']) 111 112 113 def test_execute_list_all_verbose(self): 114 self.run_cmd(argv=['atest', 'user', 'list', '--verbose'], 115 rpcs=[('get_users', {}, 116 True, 117 [{u'access_level': 0, 118 u'login': u'user0', 119 u'id': 41}, 120 {u'access_level': 0, 121 u'login': u'user5', 122 u'id': 42}, 123 {u'access_level': 5, 124 u'login': u'user0', 125 u'id': 3}])], 126 out_words_ok=['user0', 'user5', '41', '42', '0', '5']) 127 128 129 def test_execute_list_all_with_user_verbose(self): 130 ufile = cli_mock.create_file('user0 user1\n') 131 self.run_cmd(argv=['atest', 'user', 'list', '--ulist', ufile.name, 132 '-v'], 133 rpcs=[('get_users', {'login__in': ['user0', 'user1']}, 134 True, 135 [{u'access_level': 2, 136 u'login': u'user0', 137 u'id': 3}, 138 {u'access_level': 5, 139 u'login': u'user1', 140 u'id': 4}])], 141 out_words_ok=['user0', 'user1', '3', '4', '5']) 142 ufile.clean() 143 144 145 def test_execute_list_all_with_acl_verbose(self): 146 self.run_cmd(argv=['atest', 'user', 'list', '--acl', 'acl0', '-v'], 147 rpcs=[('get_users', {'aclgroup__name__in': ['acl0']}, 148 True, 149 [{u'access_level': 2, 150 u'login': u'user0', 151 u'id': 3}])], 152 out_words_ok=['user0', '3', '2']) 153 154 155 def test_execute_list_all_with_access_level_verbose(self): 156 self.run_cmd(argv=['atest', 'user', 'list', 157 '--access_level', '2', '-v'], 158 rpcs=[('get_users', {'access_level__in': ['2']}, 159 True, 160 [{u'access_level': 2, 161 u'login': u'user0', 162 u'id': 3}])], 163 out_words_ok=['user0', '2', '3']) 164 165 166 if __name__ == '__main__': 167 unittest.main() 168