def test_api_update_repo(self, updates, expected, backend): repo_name = UPDATE_REPO_NAME repo = fixture.create_repo(repo_name, repo_type=backend.alias) if updates.get('group'): fixture.create_repo_group(updates['group']) expected_api_data = repo.get_api_data(include_secrets=True) if expected is SAME_AS_UPDATES: expected_api_data.update(updates) else: expected_api_data.update(expected) id_, params = build_data(self.apikey, 'update_repo', repoid=repo_name, **updates) response = api_call(self.app, params) if updates.get('name'): repo_name = updates['name'] if updates.get('group'): repo_name = '/'.join([updates['group'], repo_name]) try: expected = { 'msg': 'updated repo ID:%s %s' % (repo.repo_id, repo_name), 'repository': jsonify(expected_api_data) } assert_ok(id_, expected, given=response.body) finally: fixture.destroy_repo(repo_name) if updates.get('group'): fixture.destroy_repo_group(updates['group'])
def test_api_update_user_group_regular_user(self, changing_attr, updates, user_util): user_group = user_util.create_user_group() group_name = user_group.users_group_name expected_api_data = user_group.get_api_data() expected_api_data.update(updates) # grant permission to this user user = UserModel().get_by_username(self.TEST_USER_LOGIN) user_util.grant_user_permission_to_user_group(user_group, user, 'usergroup.admin') id_, params = build_data(self.apikey_regular, 'update_user_group', usergroupid=group_name, **updates) response = api_call(self.app, params) expected = { 'msg': 'updated user group ID:%s %s' % (user_group.users_group_id, user_group.users_group_name), 'user_group': jsonify(expected_api_data) } assert_ok(id_, expected, given=response.body)
def test_api_create_user_group_regular_user(self): group_name = 'some_new_group' usr = UserModel().get_by_username(self.TEST_USER_LOGIN) usr.inherit_default_permissions = False Session().add(usr) UserModel().grant_perm(self.TEST_USER_LOGIN, 'hg.usergroup.create.true') Session().commit() id_, params = build_data(self.apikey_regular, 'create_user_group', group_name=group_name) response = api_call(self.app, params) expected = { 'msg': 'created new user group `%s`' % (group_name, ), 'user_group': jsonify(UserGroupModel().get_by_name(group_name).get_api_data()) } try: assert_ok(id_, expected, given=response.body) finally: self.fixture.destroy_user_group(group_name) UserModel().revoke_perm(self.TEST_USER_LOGIN, 'hg.usergroup.create.true') usr = UserModel().get_by_username(self.TEST_USER_LOGIN) usr.inherit_default_permissions = True Session().add(usr) Session().commit()
def test_api_get_users(self): id_, params = build_data(self.apikey, 'get_users', ) response = api_call(self.app, params) ret_all = [] _users = User.query().filter(User.username != User.DEFAULT_USER) \ .order_by(User.username).all() for usr in _users: ret = usr.get_api_data(include_secrets=True) ret_all.append(jsonify(ret)) expected = ret_all assert_ok(id_, expected, given=response.body)
def test_api_get_repos(self): id_, params = build_data(self.apikey, 'get_repos') response = api_call(self.app, params) result = [] for repo in RepoModel().get_all(): result.append(repo.get_api_data(include_secrets=True)) ret = jsonify(result) expected = ret assert_ok(id_, expected, given=response.body)
def test_api_update_user_when_exception_happens(self): usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) ret = jsonify(usr.get_api_data(include_secrets=True)) id_, params = build_data(self.apikey, 'update_user', userid=usr.user_id) response = api_call(self.app, params) ret = 'failed to update user `%s`' % (usr.user_id, ) expected = ret assert_error(id_, expected, given=response.body)
def test_api_update_user_by_user_id(self): usr = UserModel().get_by_username(TEST_USER_ADMIN_LOGIN) ret = jsonify(usr.get_api_data(include_secrets=True)) id_, params = build_data(self.apikey, 'update_user', userid=usr.user_id) response = api_call(self.app, params) ret = { 'msg': 'updated user ID:%s %s' % (usr.user_id, TEST_USER_ADMIN_LOGIN), 'user': ret } expected = ret assert_ok(id_, expected, given=response.body)
def test_api_create_user_group(self): group_name = 'some_new_group' id_, params = build_data(self.apikey, 'create_user_group', group_name=group_name) response = api_call(self.app, params) ret = { 'msg': 'created new user group `%s`' % (group_name, ), 'user_group': jsonify(UserGroupModel().get_by_name(group_name).get_api_data()) } expected = ret assert_ok(id_, expected, given=response.body) self.fixture.destroy_user_group(group_name)
def test_api_get_repos_non_admin(self): id_, params = build_data(self.apikey_regular, 'get_repos') response = api_call(self.app, params) user = User.get_by_username(self.TEST_USER_LOGIN) allowed_repos = user.AuthUser.permissions['repositories'] result = [] for repo in RepoModel().get_all(): perm = allowed_repos[repo.repo_name] if perm in ['repository.read', 'repository.write', 'repository.admin']: result.append(repo.get_api_data()) ret = jsonify(result) expected = ret assert_ok(id_, expected, given=response.body)
def test_api_update_user(self, name, expected, user_util): usr = user_util.create_user() kw = {name: expected, 'userid': usr.user_id} id_, params = build_data(self.apikey, 'update_user', **kw) response = api_call(self.app, params) ret = { 'msg': 'updated user ID:%s %s' % (usr.user_id, usr.username), 'user': jsonify(UserModel().get_by_username( usr.username).get_api_data(include_secrets=True)) } expected = ret assert_ok(id_, expected, given=response.body)
def test_api_update_repo_fork_of_field(self, backend): master_repo = backend.create_repo() repo = backend.create_repo() updates = {'fork_of': master_repo.repo_name} expected_api_data = repo.get_api_data(include_secrets=True) expected_api_data.update(updates) id_, params = build_data(self.apikey, 'update_repo', repoid=repo.repo_name, **updates) response = api_call(self.app, params) expected = { 'msg': 'updated repo ID:%s %s' % (repo.repo_id, repo.repo_name), 'repository': jsonify(expected_api_data) } assert_ok(id_, expected, given=response.body) result = response.json['result']['repository'] assert result['fork_of'] == master_repo.repo_name
def test_api_create_user_without_password(self): username = '******' email = username + "@foo.com" id_, params = build_data( self.apikey, 'create_user', username=username, email=email) response = api_call(self.app, params) usr = UserModel().get_by_username(username) ret = { 'msg': 'created new user `%s`' % (username,), 'user': jsonify(usr.get_api_data(include_secrets=True)), } try: expected = ret assert_ok(id_, expected, given=response.body) finally: fixture.destroy_user(usr.user_id)
def test_api_update_user_group(self, changing_attr, updates, user_util): user_group = user_util.create_user_group() group_name = user_group.users_group_name expected_api_data = user_group.get_api_data() expected_api_data.update(updates) id_, params = build_data(self.apikey, 'update_user_group', usergroupid=group_name, **updates) response = api_call(self.app, params) expected = { 'msg': 'updated user group ID:%s %s' % (user_group.users_group_id, user_group.users_group_name), 'user_group': jsonify(expected_api_data) } assert_ok(id_, expected, given=response.body)