def test_update_user(self): ia = self.fake_client_set.identity_admin # prepare (name, email, passw) = ('user-upd', '*****@*****.**', 'orange') fullname = "User Userovich Upd" ia.users.get('new-user').AndReturn('new-user') ia.users.update('new-user', name=name, email=email, fullname=fullname).AndReturn('new-user') ia.users.update_password('new-user', passw).AndReturn('new-user') ia.users.get('new-user').AndReturn('new-user') users.user_to_view('new-user').AndReturn('new-user-dict') self.mox.ReplayAll() # test post_params = { "name": name, "email": email, "password": passw, "fullname": fullname } rv = self.client.put('/v1/users/new-user', data=json.dumps(post_params), content_type='application/json') data = self.check_and_parse_response(rv) self.assertEquals(data, 'new-user-dict')
def test_create_user_with_projects(self): client = self.fake_client_set (name, email, passw) = ('user-a', '*****@*****.**', 'bananas') client.identity_admin.users.list().AndReturn([self.user]) client.identity_admin.users.create(name=name, password=passw, email=email, enabled=True).AndReturn('new-user') users.member_role_id().AndReturn('member-role') client.identity_admin.roles.add_user_role(user='******', role='member-role', tenant='PID1') client.identity_admin.roles.add_user_role(user='******', role='member-role', tenant='PID2') users.user_to_view('new-user').AndReturn('new-user-dict') self.mox.ReplayAll() data = self._interact({ "name": name, "email": email, "password": passw, "projects": ['PID1', 'PID2'], "admin": False, }) self.assertEquals(data, 'new-user-dict')
def test_get_me(self): self.fake_client_set.http_client.access['user']['id'] = 'user-a' self.fake_client_set.identity_admin.users\ .get('user-a').AndReturn('user-a') users.user_to_view('user-a').AndReturn('dict-a') self.mox.ReplayAll() rv = self.client.get('/v1/me') data = self.check_and_parse_response(rv) self.assertEquals(data, 'dict-a')
def test_get_user(self): # prepare self.fake_client_set.identity_admin.users\ .get('user-a').AndReturn('user-a') users.user_to_view('user-a').AndReturn('dict-a') self.mox.ReplayAll() # test rv = self.client.get('/v1/users/user-a') # verify data = self.check_and_parse_response(rv) self.assertEquals(data, 'dict-a')
def test_resend_no_mail(self): ConfigDAO.get('invitations', 'enabled').AndReturn(True) self.fake_client_set.identity_admin.users.get(self.uid)\ .AndReturn(self.user) users.InvitesDAO.create(self.uid, self.user.email)\ .AndReturn(self.invite) users.auth.assert_admin() users.user_to_view(self.user, self.invite, send_code=True)\ .AndReturn('REPLY') self.mox.ReplayAll() data = self.interact({'send-invite-mail': False}) self.assertEquals(data, 'REPLY')
def test_resend_works(self): ConfigDAO.get('invitations', 'enabled').AndReturn(True) self.fake_client_set.identity_admin.users.get(self.uid)\ .AndReturn(self.user) users.InvitesDAO.create(self.uid, self.user.email)\ .AndReturn(self.invite) users.send_invitation(self.user.email, self.invite.code, None, greeting=self.user.fullname) users.user_to_view(self.user, self.invite, send_code=False)\ .AndReturn('REPLY') self.mox.ReplayAll() data = self.interact({}) self.assertEquals(data, 'REPLY')
def test_user_to_view_with_my_projects(self): tenant = doubles.make(self.mox, doubles.Tenant, id='PID', name='ptest') user = doubles.make(self.mox, doubles.User, id=u'42', name=u'iv', email=u'*****@*****.**', fullname=u'Example User', enabled=True) user.list_roles().AndReturn(self.roles) self.fake_client_set.identity_public.tenants.list()\ .AndReturn([tenant]) expected_projects = [{ u'id': 'PID', u'name': 'ptest', u'href': '/v1/projects/PID' }] self.mox.ReplayAll() with self.app.test_request_context(): self.install_fake_auth() flask.g.my_projects = True data = users.user_to_view(user) self.assertEquals(data['projects'], expected_projects)
def test_user_to_view_invite(self): user = doubles.make(self.mox, doubles.User, id=u'42', name=u'iv', email=u'*****@*****.**', fullname=u'Example User', enabled=False) date = datetime(2012, 9, 15, 15, 03, 00) invite = Token(user_id=user.id, created_at=date, complete=False) user.list_roles().AndReturn([]) users.InvitesDAO.get_for_user(user.id).AndReturn(invite) expected = { u'id': u'42', u'name': u'iv', u'href': u'/v1/users/42', u'email': u'*****@*****.**', u'fullname': 'Example User', u'admin': False, u'projects': [], u'completed-registration': False, u'invited-at': date, u'links': { u'send-invite': '/v1/users/42/send-invite', u'ssh-keys': '/v1/users/42/ssh-keys/', } } self.mox.ReplayAll() with self.app.test_request_context(): self.install_fake_auth() data = users.user_to_view(user) self.assertEquals(data, expected)
def test_user_to_view_send_invite_code(self): """What we got when resending invite without disabling user""" user = doubles.make(self.mox, doubles.User, id=u'42', name=u'iv', email=u'*****@*****.**', fullname=u'Example User', enabled=True) date = datetime(2012, 9, 15, 15, 03, 00) invite = Token(user_id=user.id, created_at=date, complete=False, code='THE_CODE') user.list_roles().AndReturn([]) expected = { u'id': u'42', u'name': u'iv', u'href': u'/v1/users/42', u'email': u'*****@*****.**', u'fullname': 'Example User', u'admin': False, u'projects': [], u'completed-registration': True, # because user can work u'invited-at': date, u'invitation-code': 'THE_CODE', u'links': { u'send-invite': '/v1/users/42/send-invite', u'ssh-keys': '/v1/users/42/ssh-keys/', } } self.mox.ReplayAll() with self.app.test_request_context(): self.install_fake_auth() data = users.user_to_view(user, invite, send_code=True) self.assertEquals(data, expected)
def test_update_self(self): users.fetch_user(self.user.id, False).AndReturn(self.user) self.fake_client_set.identity_admin \ .users.update(self.user, name=self.name) \ .AndReturn('new-user') users.fetch_user(self.user.id, False).AndReturn('new-user') users.user_to_view('new-user').AndReturn('new-user-dict') self.mox.ReplayAll() post_params = {"name": self.name} rv = self.client.put('/v1/users/%s' % self.user.id, data=json.dumps(post_params), content_type='application/json') data = self.check_and_parse_response(rv) self.assertEquals(data, 'new-user-dict')
def test_update_self(self): users.fetch_user(self.user.id, False).AndReturn(self.user) self.fake_client_set.identity_admin \ .users.update(self.user, name=self.name) \ .AndReturn('new-user') users.fetch_user(self.user.id, False).AndReturn('new-user') users.user_to_view('new-user').AndReturn('new-user-dict') self.mox.ReplayAll() post_params = { "name": self.name } rv = self.client.put('/v1/users/%s' % self.user.id, data=json.dumps(post_params), content_type='application/json') data = self.check_and_parse_response(rv) self.assertEquals(data, 'new-user-dict')
def test_user_to_view_disabled_noadmin(self): user = doubles.make(self.mox, doubles.User, id=u'42', name=u'iv', email=u'*****@*****.**', fullname=u'Example User', enabled=False) user.list_roles().AndReturn([]) users.InvitesDAO.get_for_user(user.id).AndReturn(None) expected = { u'id': u'42', u'name': u'iv', u'href': u'/v1/users/42', u'email': u'*****@*****.**', u'fullname': 'Example User', u'admin': False, u'projects': [], u'completed-registration': False, u'links': { u'send-invite': '/v1/users/42/send-invite', u'ssh-keys': '/v1/users/42/ssh-keys/', } } self.mox.ReplayAll() with self.app.test_request_context(): self.install_fake_auth() data = users.user_to_view(user) self.assertEquals(data, expected)
def test_update_grant_admin(self): client = self.fake_client_set uid = u'user-a' user = doubles.make(self.mox, doubles.User, id=uid) client.identity_admin.users.get(uid).AndReturn(user) client.identity_admin.roles.add_user_role(u'user-a', u'ADMIN_ROLE_ID', u'SYSTENANT_ID') client.identity_admin.users.get(uid).AndReturn('same-user') users.user_to_view('same-user').AndReturn('REPLY') self.mox.ReplayAll() rv = self.client.put(u'/v1/users/%s' % uid, data=json.dumps({'admin': True}), content_type='application/json') data = self.check_and_parse_response(rv) self.assertEquals(data, 'REPLY')
def test_update_revoke_admin(self): client = self.fake_client_set uid = u'user-a' user = doubles.make(self.mox, doubles.User, id=uid) client.identity_admin.users.get(uid).AndReturn(user) client.identity_admin.roles.remove_user_role( u'user-a', u'ADMIN_ROLE_ID', u'SYSTENANT_ID') client.identity_admin.users.get(uid).AndReturn('same-user') users.user_to_view('same-user').AndReturn('REPLY') self.mox.ReplayAll() rv = self.client.put(u'/v1/users/%s' % uid, data=json.dumps({'admin': False}), content_type='application/json') data = self.check_and_parse_response(rv) self.assertEquals(data, 'REPLY')
def test_list_users(self): self.fake_client_set.identity_admin \ .users.list().AndReturn(['user-a', 'user-b']) users.user_to_view('user-a').AndReturn('dict-a') users.user_to_view('user-b').AndReturn('dict-b') expected = { 'collection': { 'name': 'users', 'size': 2 }, 'users': [ 'dict-a', 'dict-b' ] } self.mox.ReplayAll() rv = self.client.get('/v1/users/') data = self.check_and_parse_response(rv) self.assertEquals(data, expected)
def test_list_users(self): self.fake_client_set.identity_admin \ .users.list().AndReturn(['user-a', 'user-b']) users.user_to_view('user-a').AndReturn('dict-a') users.user_to_view('user-b').AndReturn('dict-b') expected = { 'collection': { 'name': 'users', 'size': 2 }, 'users': ['dict-a', 'dict-b'] } self.mox.ReplayAll() rv = self.client.get('/v1/users/') data = self.check_and_parse_response(rv) self.assertEquals(data, expected)
def test_update_revoke_admin_idempotent(self): client = self.fake_client_set uid = u'user-a' user = doubles.make(self.mox, doubles.User, id=uid) client.identity_admin.users.get(uid).AndReturn(user) client.identity_admin.roles.remove_user_role( u'user-a', u'ADMIN_ROLE_ID', u'SYSTENANT_ID')\ .AndRaise(osc_exc.NotFound('failure')) # raised, but nothing should happen client.identity_admin.users.get(uid).AndReturn('same-user') users.user_to_view('same-user').AndReturn('REPLY') self.mox.ReplayAll() rv = self.client.put(u'/v1/users/%s' % uid, data=json.dumps({'admin': False}), content_type='application/json') data = self.check_and_parse_response(rv) self.assertEquals(data, 'REPLY')
def test_create_admin(self): client = self.fake_client_set new_user = doubles.make(self.mox, doubles.User, id='NUID') (name, email, passw) = ('user-a', '*****@*****.**', 'bananas') client.identity_admin.users.list().AndReturn([self.user]) client.identity_admin.users.create( name=name, password=passw, email=email, enabled=True).AndReturn(new_user) # see doubles.py, near line 100 for role id and tenant id here client.identity_admin.roles.add_user_role( 'NUID', u'ADMIN_ROLE_ID', u'SYSTENANT_ID') users.user_to_view(new_user).AndReturn('new-user-dict') self.mox.ReplayAll() data = self._interact({"name": name, "email": email, "password": passw, "admin": True}) self.assertEquals(data, 'new-user-dict')
def test_create_user(self): client = self.fake_client_set (name, email, passw) = ('user-a', '*****@*****.**', 'bananas') fullname = "User Userovich" client.identity_admin.users.list().AndReturn([self.user]) client.identity_admin.users.create( name=name, password=passw, email=email, enabled=True).AndReturn('new-user') client.identity_admin.users.update('new-user', fullname=fullname) users.user_to_view('new-user').AndReturn('new-user-dict') self.mox.ReplayAll() data = self._interact({ "email": email, "password": passw, "fullname": fullname, "admin": False, }) self.assertEquals(data, 'new-user-dict')
def test_create_user(self): client = self.fake_client_set (name, email, passw) = ('user-a', '*****@*****.**', 'bananas') fullname = "User Userovich" client.identity_admin.users.list().AndReturn([self.user]) client.identity_admin.users.create(name=name, password=passw, email=email, enabled=True).AndReturn('new-user') client.identity_admin.users.update('new-user', fullname=fullname) users.user_to_view('new-user').AndReturn('new-user-dict') self.mox.ReplayAll() data = self._interact({ "email": email, "password": passw, "fullname": fullname, "admin": False, }) self.assertEquals(data, 'new-user-dict')
def accept_invite(code): data = parse_request_data(_ACCEPT_SCHEMA, _ACCEPT_REQUIRES) invite, user = _invite_and_user(code) data['enabled'] = True try: update_user_data(user, data) user = auth.admin_client_set().identity_admin.users.get(user.id) except osc_exc.NotFound: abort(404) InvitesDAO.complete_for_user(user.id) return make_json_response(user_to_view(user, invite), 200)
def test_create_admin(self): client = self.fake_client_set new_user = doubles.make(self.mox, doubles.User, id='NUID') (name, email, passw) = ('user-a', '*****@*****.**', 'bananas') client.identity_admin.users.list().AndReturn([self.user]) client.identity_admin.users.create(name=name, password=passw, email=email, enabled=True).AndReturn(new_user) # see doubles.py, near line 100 for role id and tenant id here client.identity_admin.roles.add_user_role('NUID', u'ADMIN_ROLE_ID', u'SYSTENANT_ID') users.user_to_view(new_user).AndReturn('new-user-dict') self.mox.ReplayAll() data = self._interact({ "name": name, "email": email, "password": passw, "admin": True }) self.assertEquals(data, 'new-user-dict')
def test_create_user_with_projects(self): client = self.fake_client_set (name, email, passw) = ('user-a', '*****@*****.**', 'bananas') client.identity_admin.users.list().AndReturn([self.user]) client.identity_admin.users.create( name=name, password=passw, email=email, enabled=True).AndReturn('new-user') users.member_role_id().AndReturn('member-role') client.identity_admin.roles.add_user_role( user='******', role='member-role', tenant='PID1') client.identity_admin.roles.add_user_role( user='******', role='member-role', tenant='PID2') users.user_to_view('new-user').AndReturn('new-user-dict') self.mox.ReplayAll() data = self._interact({ "name": name, "email": email, "password": passw, "projects": ['PID1', 'PID2'], "admin": False, }) self.assertEquals(data, 'new-user-dict')
def test_user_to_view_works(self): user = doubles.make(self.mox, doubles.User, id=u'42', name=u'iv', email=u'*****@*****.**', fullname=u'Example User', enabled=True) user.list_roles().AndReturn(self.roles) expected = { u'id': u'42', u'name': u'iv', u'href': u'/v1/users/42', u'email': u'*****@*****.**', u'fullname': 'Example User', u'admin': True, u'projects': [{ u'id': 'PID', u'name': 'ptest', u'href': '/v1/projects/PID' }, { u'id': 'PID2', u'name': 'other', u'href': '/v1/projects/PID2' }], u'completed-registration': True, u'links': { u'send-invite': '/v1/users/42/send-invite', u'ssh-keys': '/v1/users/42/ssh-keys/', } } self.mox.ReplayAll() with self.app.test_request_context(): self.install_fake_auth() data = users.user_to_view(user) self.assertEquals(data, expected)
def test_user_to_view_works(self): user = doubles.make(self.mox, doubles.User, id=u'42', name=u'iv', email=u'*****@*****.**', fullname=u'Example User', enabled=True) user.list_roles().AndReturn(self.roles) expected = { u'id': u'42', u'name': u'iv', u'href': u'/v1/users/42', u'email': u'*****@*****.**', u'fullname': 'Example User', u'admin': True, u'projects': [ { u'id': 'PID', u'name': 'ptest', u'href': '/v1/projects/PID' }, { u'id': 'PID2', u'name': 'other', u'href': '/v1/projects/PID2' } ], u'completed-registration': True, u'links': { u'send-invite': '/v1/users/42/send-invite', u'ssh-keys': '/v1/users/42/ssh-keys/', } } self.mox.ReplayAll() with self.app.test_request_context(): self.install_fake_auth() data = users.user_to_view(user) self.assertEquals(data, expected)
def get_user_by_code(code): invite, user = _invite_and_user(code) return make_json_response(user_to_view(user, invite))