def update_team(team_id, data): team = team_service.get_team_for_update(team_id) if not team: raise NotFoundError('Team {} does not exist'.format(team_id)) if len([tm for tm in team.team_members if tm.user_id == current_user.id and tm.is_team_lead is True]) == 0: raise UnauthorisedError('Only team leads can edit a team') update_team_information(team, data) result = update_team_leads_and_members(team, data) update_permissions(team, data) create_team = data.get('createTeam', False) saved = False if create_team: validation_result = TeamValidator(team, current_user).validate_all() if len([e for e in validation_result.errors]) > 0: raise ValidationError([e for e in validation_result.errors]) team.status = 'completed' team_service.save(team) saved = True send_team_lead_notification_emails(team_id) send_team_member_notification_emails(team_id) elif team.status == 'completed': validation_result = TeamValidator(team, current_user).validate_all() if len([e for e in validation_result.errors]) > 0: raise ValidationError([e for e in validation_result.errors]) team_service.save(team) saved = True new_team_leads = result.get('new_team_leads', []) new_team_members = result.get('new_team_members', []) removed_team_members = result.get('removed_team_members', []) if len(new_team_leads) > 0: send_team_lead_notification_emails(team_id, new_team_leads) if len(new_team_members) > 0: send_team_member_notification_emails(team_id, new_team_members) if len(removed_team_members) > 0: send_removed_team_member_notification_emails(team_id, removed_team_members) elif team.status == 'created': validation_result = TeamValidator(team, current_user).validate_all() team_member_errors = [e for e in validation_result.errors if str(e.get('id')).startswith('TM')] if team_member_errors: raise ValidationError(team_member_errors) team_service.save(team) if saved: publish_tasks.team.delay( publish_tasks.compress_team(team), 'updated' ) return get_team(team_id)
def test_team_fails_validation_with_no_team_lead(self, users, user, teams, team, team_members): team_members[0].is_team_lead = False result = TeamValidator(team, user, ['teamtest.gov.au']).validate_all() assert len(result.errors) == 2 assert any(error['id'] == 'TM002' for error in result.errors) assert any(error['id'] == 'TM003' for error in result.errors)
def test_team_fails_validation_with_bad_email_address( self, users, user, email_address): team = Team(name='Marketplace', email_address=email_address) errors = TeamValidator(team, user).validate_basics() assert len(errors) >= 1 assert any(error['id'] == 'T002' for error in errors)
def test_team_passes_validation_with_name_and_email(self, users, user): team = Team(email_address='*****@*****.**', name='Marketplace') errors = TeamValidator(team, user, ['teamtest.gov.au']).validate_basics() assert len(errors) == 0
def test_team_fails_validation_when_member_has_different_email_domain( self, users, user, teams, team, team_members): buyer = users.pop() buyer.agency_id = 11 errors = TeamValidator(team, user).validate_team_members() assert len(errors) == 1 assert all(error['id'] in ['TM006'] for error in errors)
def test_team_fails_validation_when_member_is_not_a_buyer( self, users, user, teams, team, team_members): seller = users.pop() seller.role = 'supplier' errors = TeamValidator(team, user).validate_team_members() assert len(errors) == 1 assert all(error['id'] in ['TM005'] for error in errors)
def test_team_fails_validation_when_member_is_in_another_team( self, users, user, teams, team, team_members): db.session.add(TeamMember(team_id=2, user_id=5, is_team_lead=True)) errors = TeamValidator(team, user).validate_team_members() assert len(errors) == 1 assert all(error['id'] in ['TM004'] for error in errors)
def test_team_fails_validation_when_email_domain_is_different_to_user_domain( self, users, user): team = Team(name='Marketplace', email_address='*****@*****.**') errors = TeamValidator(team, user).validate_basics() assert len(errors) == 1 assert any(error['id'] == 'T003' for error in errors)
def test_team_passes_validation_with_name_only(self, users, user): team = Team(name='Marketplace') errors = TeamValidator(team, user).validate_basics() assert len(errors) == 0
def test_team_fails_validation_with_null_name(self, users, user): team = Team(name=None) errors = TeamValidator(team, user).validate_basics() assert len(errors) == 1 assert any(error['id'] == 'T001' for error in errors)
def test_team_fails_validation_with_empty_team_members( self, users, user, teams, team): errors = TeamValidator(team, user).validate_team_members() assert len(errors) == 1 assert all(error['id'] in ['TM001'] for error in errors)