def test_dry_run_create_existing_user(self): self.create_user(username1) self.assertCountEqual([username1], list(backend.list_users())) resp = self.post('/test/users/', {'user': username1}) self.assertEqual(resp.status_code, http_client.CONFLICT) self.assertCountEqual([username1], list(backend.list_users()))
def test_existing_properties(self): backend.create_user(username2) backend.create_property(user=username2, key=propkey1, value=propval3) # propval1 -> json backend.create_property(user=username2, key="date joined", value=propval3) path = os.path.join(self.base, 'users1.json') with capture() as (stdout, stderr): restauth_import([path]) self.assertCountEqual(backend.list_users(), [username1, username2, username3]) pattern = r'^%s: Property "%s" already exists\.$' % (username2, propkey1) self.assertHasLine(stdout, pattern) self.assertHasLine(stdout, r'^%s: Property "date joined" already exists\.$' % username2) expected_props = { propkey1: propval3, # propva1 is in json-file - we don't overwrite! propkey2: propval2, u'last login': u'2013-12-01 19:27:44', # date from json file } props = backend.get_properties(user=username2) # delete 'date joined' prop because it was created by the backend and # restauth-import doesn't overwrite in this invocation: del props['date joined'] self.assertEqual(props, expected_props)
def get(self, request, largs, *args, **kwargs): """Get all users.""" if not request.user.has_perm('Users.users_list'): return HttpResponseForbidden() names = [n.lower() for n in backend.list_users()] return HttpRestAuthResponse(request, names)
def test_users_django_hash(self, overwrite=False): path = os.path.join(self.base, 'users5.json') with capture() as (stdout, stderr): cmd = [path] if overwrite: cmd = ['--overwrite-properties', path] with override_settings(PASSWORD_HASHERS=PASSWORD_HASHERS): restauth_import(cmd) self.assertHasLine(stdout, r'^\* %s: Set hash from input data\.$' % username3) self.assertTrue(backend.check_password(user=username3, password='******')) self.assertCountEqual(backend.list_users(), [username3])
def test_users(self, overwrite=False): path = os.path.join(self.base, 'users1.json') cmd = [path] if overwrite: cmd = ['--overwrite-properties', path] with capture() as (stdout, stderr): with override_settings(PASSWORD_HASHERS=PASSWORD_HASHERS): restauth_import(cmd) self.assertHasLine(stdout, r'^\* %s: Set hash from input data\.$' % username3) self.assertTrue(backend.check_password(user=username3, password='******')) self.assertCountEqual(backend.list_users(), [username1, username2, username3]) props = { propkey1: propval1, propkey2: propval2, # timestamps of when we wrote this test: # u'date joined': u'2013-12-01 19:27:50', u'last login': u'2013-12-01 19:27:44', } self.assertProperties(username2, props)
def test_wrong_content(self): resp = self.c.post('/users/', 'no_json_at_all}}}', content_type='application/json') self.assertEqual(resp.status_code, http_client.BAD_REQUEST) self.assertCountEqual(backend.list_users(), [])
def test_wrong_accept_header(self): resp = self.c.get('/users/', HTTP_ACCEPT='foo/bar') self.assertEqual(resp.status_code, http_client.NOT_ACCEPTABLE) self.assertCountEqual(backend.list_users(), [])
def test_wrong_content_type_header(self): content = self.handler.marshal_dict({'user': username1}) resp = self.c.post('/users/', content, content_type='foo/bar') self.assertEqual(resp.status_code, http_client.UNSUPPORTED_MEDIA_TYPE) self.assertCountEqual(backend.list_users(), [])
def test_create_with_too_short_pass(self): resp = self.post('/test/users/', {'user': username1, 'password': '******'}) self.assertEqual(resp.status_code, http_client.PRECONDITION_FAILED) self.assertEqual(list(backend.list_users()), [])
def test_dry_run_create_short_user(self): resp = self.post('/test/users/', {'user': '******'}) self.assertEqual(resp.status_code, http_client.PRECONDITION_FAILED) self.assertEqual(list(backend.list_users()), [])
def test_dry_run_create_user_with_pass_and_props(self): content = {'user': username1, 'password': password1, 'properties': {'foo': 'bar'}} resp = self.post('/test/users/', content) self.assertEqual(resp.status_code, http_client.CREATED) self.assertEqual(list(backend.list_users()), [])
def test_dry_run_create_user_with_pass(self): resp = self.post('/test/users/', {'user': username1, 'password': password1}) self.assertEqual(resp.status_code, http_client.CREATED) self.assertEqual(list(backend.list_users()), [])
def main(args=None): # parse arguments args = parser.parse_args(args=args) if args.action == 'add': password = args.get_password(args) if args.password_generated: print(args.pwd) backend.set_password(user=args.user, password=password) elif args.action in ['ls', 'list']: for username in sorted(backend.list_users()): if six.PY3: # pragma: py3 print(username) else: # pragma: py2 print(username.encode('utf-8')) elif args.action == 'verify': if not args.pwd: # pragma: no cover args.pwd = getpass.getpass('password: '******'Ok.') else: print('Failed.') sys.exit(1) elif args.action == 'set-password': password = args.get_password(args) if args.password_generated: print(args.pwd) backend.set_password(user=args.user, password=args.pwd) elif args.action == 'view': props = backend.get_properties(user=args.user) if 'date joined' in props: print('Joined: %s' % props['date joined']) if 'last login' in props: print('Last login: %s' % props['last login']) if args.service: groups = backend.list_groups(service=args.service, user=args.user) if groups: print('Groups: %s' % ', '.join(sorted(groups))) else: print('No groups.') else: if backend.SUPPORTS_GROUP_VISIBILITY: groups = {} none_groups = backend.list_groups(service=None, user=args.user) for service in Service.objects.all(): subgroups = backend.list_groups(service=service, user=args.user) if subgroups: groups[service.username] = subgroups if groups or none_groups: print('Groups:') if none_groups: print('* no service: %s' % ', '.join(sorted(none_groups))) for service, groups in sorted(groups.items(), key=lambda t: t[0]): print('* %s: %s' % (service, ', '.join(sorted(groups)))) else: print('No groups.') else: groups = backend.list_groups(service=None, user=args.user) if groups: print('Groups: %s' % ', '.join(sorted(groups))) else: print('No groups.') elif args.action == 'rename': try: backend.rename_user(user=args.user, name=args.name) except UserNotFound as e: parser.error("%s: %s" % (args.name, e)) except UserExists as e: parser.error("%s: %s" % (args.name, e)) elif args.action in ['delete', 'rm', 'remove']: # pragma: no branch backend.remove_user(user=args.user)