def convert_user_to_organization(user_obj, admin_user): if user_obj.robot: raise DataModelException("Cannot convert a robot into an organization") with db_transaction(): # Change the user to an organization and disable this account for login. user_obj.organization = True user_obj.password_hash = None user_obj.save() # Clear any federated auth pointing to this user. FederatedLogin.delete().where( FederatedLogin.user == user_obj).execute() # Delete any user-specific permissions on repositories. (RepositoryPermission.delete().where( RepositoryPermission.user == user_obj).execute()) # Create a team for the owners owners_team = team.create_team("owners", user_obj, "admin") # Add the user who will admin the org to the owners team team.add_user_to_team(admin_user, owners_team) return user_obj
def create_organization(name, email, creating_user, email_required=True, is_possible_abuser=False): with db_transaction(): try: # Create the org new_org = user.create_user_noverify( name, email, email_required=email_required, is_possible_abuser=is_possible_abuser) new_org.organization = True new_org.save() # Create a team for the owners owners_team = team.create_team("owners", new_org, "admin") # Add the user who created the org to the owners team team.add_user_to_team(creating_user, owners_team) return new_org except InvalidUsernameException as iue: raise InvalidOrganizationException(iue.message)
def test_remove_user_from_team(initialized_db): first_user = get_user("devtable") second_user = get_user("randomuser") # Create new org: devtable should be in the admin owners team new_org = create_organization("testorg", "testorg" + "@example.com", first_user) admin_teams = list(__get_user_admin_teams("testorg", "devtable")) # Add user to another admin team new_team = create_team("testteam", new_org, "admin", description="test another admin team") assert add_user_to_team(second_user, new_team) # Cannot remove themselves from their only admin team with pytest.raises(DataModelException): remove_user_from_team("testorg", "testteam", "randomuser", "randomuser") # Another admin should be able to remove_user_from_team("testorg", "testteam", "randomuser", "devtable")
def run_invite_flow(orgname): # Create an org owned by `devtable`. org = create_organization(orgname, orgname + "@example.com", first_user) # Create another team and add `devtable` to it. Since `devtable` is already # in the org, it should be done directly. other_team = create_team("otherteam", org, "admin") invite = add_or_invite_to_team(first_user, other_team, user_obj=first_user) assert invite is None assert is_in_team(other_team, first_user) # Try to add `newuser` to the team, which should require an invite. invite = add_or_invite_to_team(first_user, other_team, user_obj=second_user) assert invite is not None assert not is_in_team(other_team, second_user) # Accept the invite. confirm_team_invite(invite.invite_token, second_user) assert is_in_team(other_team, second_user)
def test_delete_robot(initialized_db): # Create a robot account. user = create_user_noverify('foobar', '*****@*****.**', email_required=False) robot, _ = create_robot('foo', user) # Add some notifications and other rows pointing to the robot. create_notification('repo_push', robot) team = create_team('someteam', get_organization('buynlarge'), 'member') add_user_to_team(robot, team) # Ensure the robot exists. assert lookup_robot(robot.username).id == robot.id # Delete the robot. delete_robot(robot.username) # Ensure it is gone. with pytest.raises(InvalidRobotException): lookup_robot(robot.username)
def test_remove_team(initialized_db): first_user = get_user("devtable") # Create new org: devtable should be in the admin owners team new_org = create_organization("testorg", "testorg" + "@example.com", first_user) admin_teams = list(__get_user_admin_teams("testorg", "devtable")) assert len(admin_teams) == 1 and admin_teams[0].name == "owners" # Create new admin team without adding the devtable to the team: # devtable should be able to delete the new admin team new_team = create_team("testteam", new_org, "admin", description="test second admin team") admin_teams = list(__get_user_admin_teams("testorg", "devtable")) assert len(admin_teams) == 1 and admin_teams[0].name != "testteam" # Removing the only team which devtable is the admin to should fail with pytest.raises(DataModelException): remove_team("testorg", "owners", "devtable") # Removing the other admin team should succeed, since devtable is already admin in another team remove_team("testorg", "testteam", "devtable")