def test_create_team(client, db, admin_user, admin_token, webhook_backends): assert Team.get_by_name('foo') is None assert db.query(AuditLog.id).first() is None r = client.post('/api/team', data={'team': 'foo'}, headers={'Authorization': admin_token}) assert r.status_code == 201, r.json team = Team.get_by_name('foo') audit = db.query(AuditLog).first() assert team is not None assert audit is not None assert audit.user.id == admin_user.id assert audit.action_name == 'CREATE_TEAM' assert json.loads(audit.action_data) == { 'team_id': team.id, 'team_name': 'foo', 'team_desc': team.team_desc, } assert audit.rollback_to is None for result in webhook_backends: assert result['action_data']['team_id'] == team.id assert result['action_data']['team_name'] == 'foo' assert result['action_name'] == 'CREATE_TEAM'
def test_create_team_failed(client, db, test_token): assert Team.get_by_name('foo') is None assert db.query(AuditLog.id).first() is None r = client.post('/api/team', data={'team': 'foo'}, headers={'Authorization': test_token}) assert r.status_code == 400, r.json assert Team.get_by_name('foo') is None assert db.query(AuditLog.id).first() is None
def _create(self, ignore_existed=False): team_name = request.values['team'].strip() application_name = request.values['application'].strip() validate_fields(application_schema, {'name': application_name, 'team_name': team_name}) team = Team.get_by_name(team_name) if team is None: abort(400, 'team "%s" does not exist' % team_name) require_team_admin_or_site_admin(team) application = Application.get_by_name(application_name) if application is not None: if ignore_existed: return api_response() raise ApplicationExistedError( 'application: {} has existed, application is globally ' 'unique'.format(application)) try: application = Application.create(application_name, team.id) except NameOccupiedError: abort(400, 'The application name {0} has been occupied.'.format( application_name)) audit_log.emit( audit_log.types.CREATE_APPLICATION, application=application, team=team) return api_response()
def test_delete_team(team_foo, team_bar, user_foo): Application.create('biu', team_bar.id) TeamAdmin.ensure(team_foo.id, user_foo.id) # fill cache assert Team.get_by_name(team_foo.team_name) is team_foo assert Team.get_by_name(team_bar.team_name) is team_bar assert TeamAdmin.get_user_ids(team_foo.id) == [user_foo.id] Team.delete(team_foo.id) with raises(TeamNotEmptyError): Team.delete(team_bar.id) assert Team.get_by_name(team_foo.team_name) is None assert Team.get_by_name(team_bar.team_name) is not None assert Team.get(team_foo.id) is None assert Team.get(team_bar.id) is not None assert TeamAdmin.get_user_ids(team_foo.id) == [] Team.delete(team_foo.id) assert Team.get(team_foo.id) is None
def _find_target(self, name): if self.target_type is AuditLog.TYPE_SITE: return 0 if self.target_type is AuditLog.TYPE_TEAM: team = Team.get_by_name(name) if team is not None: return team.id if self.target_type is AuditLog.TYPE_APPLICATION: application = Application.get_by_name(name) if application is not None: return application.id
def test_archive_team(db, team_foo, user_foo, user_bar): team_foo.grant_admin(user_foo.id) assert team_foo.is_active is True application = Application.create('biu', team_foo.id) assert application.is_active is True with raises(TeamNotEmptyError): team_foo.archive() application.archive() team_foo.archive() assert Team.get_by_name(team_foo.team_name) is None assert db.query(TeamAdmin.id).filter_by( team_id=team_foo.id, user_id=user_foo.id).first() is not None
def post(self): """Creates a new team. :form team: The name of creating team. :<header Authorization: Huskar Token (See :ref:`token`) :status 400: The name is invalid. :status 200: The team with specified name exists. :status 201: The team with specified name is created successfully. """ g.auth.require_admin('only admin can add team') team_name = request.form['team'].strip() team = Team.get_by_name(team_name) if team is not None: return api_response(), 200 try: team = Team.create(team_name) except NameOccupiedError: abort(400, 'Team %s has been archived.' % team_name) audit_log.emit(audit_log.types.CREATE_TEAM, team=team) return api_response(), 201
def _get_team_or_404(self, team_name): team = Team.get_by_name(team_name) if team is None: abort(404, 'team %s does not exist' % team_name) return team
def test_get_team_by_name(db, team_foo): team = Team.get_by_name(team_foo.team_name) assert team is team_foo team = Team.get_by_name(team_foo.team_name + '1s') assert team is None
def test_unarchive(db, team_foo, user_foo, user_bar): team_foo.archive() assert Team.get_by_name(team_foo.team_name) is None team_foo.unarchive() assert Team.get_by_name(team_foo.team_name) is not None
def ensure_team(self): return Team.get_by_name(self.department.team_name) or Team.create( self.department.team_name, self.department.team_desc)