def test_multiple_saves(self): white_team = Team(name="White Team", color="White") self.session.add(white_team) blue_team = Team(name="Blue", color="Blue") self.session.add(blue_team) self.session.commit() assert len(self.session.query(Team).all()) == 2
def home(): results = Team.get_all_rounds_results() team_data = {} # We start at one because that's how javascript expects the team_data current_index = 1 for name in sorted(results['scores'].keys()): scores = results['scores'][name] rgb_color = results['rgb_colors'][name] team_data[current_index] = { "label": name, "data": scores, "color": rgb_color } current_index += 1 team_labels = [] team_scores = [] scores_colors = [] for blue_team in Team.get_all_blue_teams(): team_labels.append(blue_team.name) team_scores.append(blue_team.current_score) scores_colors.append(blue_team.rgb_color) return render_template('scoreboard.html', round_labels=results['rounds'], team_data=team_data, team_labels=team_labels, team_scores=team_scores, scores_colors=scores_colors)
def test_rank(self): team_1 = Team(name="Blue Team 1", color="Blue") self.session.add(team_1) service_1 = Service(name="Example Service 1", team=team_1, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service_1) check_1 = Check(service=service_1, result=True, output='Good output') check_2 = Check(service=service_1, result=True, output='Good output') self.session.add(check_1) self.session.add(check_2) self.session.commit() team_2 = Team(name="Blue Team 2", color="Blue") self.session.add(team_2) service_1 = Service(name="Example Service 1", team=team_2, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service_1) check_1 = Check(service=service_1, result=True, output='Good output') check_2 = Check(service=service_1, result=True, output='Good output') self.session.add(check_1) self.session.add(check_2) self.session.commit() team_3 = Team(name="Blue Team 3", color="Blue") self.session.add(team_3) service_1 = Service(name="Example Service 1", team=team_3, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service_1) check_1 = Check(service=service_1, result=True, output='Good output') check_2 = Check(service=service_1, result=False, output='Good output') self.session.add(check_1) self.session.add(check_2) self.session.commit() assert team_1.services[0].rank == 1 assert team_2.services[0].rank == 1 assert team_3.services[0].rank == 3
def create_default_user(self): team1 = Team(name="Team 1", color="White") self.session.add(team1) user1 = User(username='******', password='******', team=team1) self.session.add(user1) self.session.commit() return user1
def get_table_columns(): blue_teams = Team.get_all_blue_teams() columns = [] columns.append({"title": "", "data": ""}) for team in blue_teams: columns.append({"title": team.name, "data": team.name}) return columns
def overview_get_data(): columns = get_table_columns() data = [] blue_teams = Team.get_all_blue_teams() if len(blue_teams) > 0: current_score_row_data = {'': 'Current Score'} current_place_row_data = {'': 'Current Place'} for blue_team in blue_teams: current_score_row_data[blue_team.name] = blue_team.current_score current_place_row_data[blue_team.name] = blue_team.place data.append(current_score_row_data) data.append(current_place_row_data) for service in blue_teams[0].services: service_row_data = {'': service.name} for blue_team in blue_teams: service = session.query(Service).filter( Service.name == service.name).filter( Service.team == blue_team).first() service_text = service.host if str(service.port) != '0': service_text += ':' + str(service.port) service_data = { 'result': str(service.last_check_result()), 'host_info': service_text } service_row_data[blue_team.name] = service_data data.append(service_row_data) return json.dumps({'columns': columns, 'data': data}) else: return '{}'
class TestIntegration(object): def test_overall(self): blue_teams = Team.get_all_blue_teams() assert len(blue_teams) == 10, \ "Incorrect number of blue teams" def test_round_num(self): assert Round.get_last_round_num() == NUM_OVERALL_ROUNDS, \ "Expecting only {0} of rounds to have run...".format(NUM_OVERALL_ROUNDS) @pytest.mark.parametrize("blue_team", Team.get_all_blue_teams()) def test_blue_teams(self, blue_team): assert len(blue_team.services) == NUM_TESTBED_SERVICES, \ "Invalid number of services enabled per team {0}".format(blue_team.name) assert blue_team.current_score == (SERVICE_TOTAL_POINTS_PER_ROUND * NUM_OVERALL_ROUNDS), \ "Invalid number of overall points per team {0}".format(blue_team.name) @pytest.mark.parametrize("service", session.query(Service).all()) def test_services(self, service): assert service.last_check_result() is True, \ "{0} service failed on {1}".format(service.name, service.team.name) assert service.percent_earned == 100 @pytest.mark.parametrize("check", session.query(Check).all()) def test_checks(self, check): assert check.result is True, \ "{0} on round {1} failed for team {2}\nReason: {3}\nOutput: {4}".format(check.service.name, check.round.number, check.service.team.name, check.reason, check.output)
def populate_sample_data(session): team = Team(name="Blue Team 1", color="Blue") session.add(team) service = Service(name="Example Service 1", team=team, check_name="ICMP IPv4 Check", host='127.0.0.1') session.add(service) round_1 = Round(number=1) session.add(round_1) check_1 = Check(service=service, result=True, output='Good output', round=round_1) session.add(check_1) round_2 = Round(number=2) session.add(round_2) check_2 = Check(service=service, result=False, output='Bad output', round=round_2) session.add(check_2) session.commit() return team
def get_table_columns(): blue_teams = Team.get_all_blue_teams() columns = [] columns.append({'title': '', 'data': ''}) for team in blue_teams: columns.append({'title': team.name, 'data': team.name}) return columns
def test_blue_team_user(self): team = Team(name="Blue Team", color="Blue") self.db.save(team) user = User(username='******', password='******', team=team) self.db.save(user) user.is_red_team is False user.is_white_team is False user.is_blue_team is True
def manage(): if current_user.is_white_team: users = session.query(User).with_entities(User.id, User.username).all() teams = session.query(Team).with_entities(Team.id, Team.name).all() blue_teams = Team.get_all_blue_teams() return render_template('admin/manage.html', users=sorted(users, key=itemgetter(0)), teams=teams, blue_teams=blue_teams) else: return redirect(url_for('auth.unauthorized'))
def scoreboard_get_bar_data(): results = Team.get_all_rounds_results() team_data = {} team_labels = [] team_scores = [] scores_colors = [] for blue_team in Team.get_all_blue_teams(): team_labels.append(blue_team.name) team_scores.append(blue_team.current_score) scores_colors.append(blue_team.rgb_color) team_data['labels'] = team_labels team_data['scores'] = team_scores team_data['colors'] = scores_colors return jsonify(team_data)
def test_save_and_destroy(self): self.db.connect() self.db.setup() obj = Team(name="White Team", color="White") self.db.save(obj) assert len(Team.query.all()) == 1 self.db.destroy() with pytest.raises(OperationalError): Team.query.all()
def test_init_redteam(self): team = Team(name="Red Team", color="Red") assert team.name == "Red Team" assert team.color == "Red" assert team.id is None assert team.current_score == 0 assert team.is_red_team is True assert team.is_white_team is False assert team.is_blue_team is False
def progress(): if current_user.is_white_team: teams = Team.query.with_entities(Team.id, Team.name).all() blue_teams = Team.get_all_blue_teams() return render_template('admin/progress.html', teams=teams, blue_teams=blue_teams) else: return redirect(url_for('auth.unauthorized'))
def generate_sample_model_tree(model, session): # Team team = Team(name="Team 1", color="Blue") session.add(team) session.commit() if model == 'Team': return team # Users user = User(username="******" + str(random.randrange(10000)), password="******", team=team) session.add(user) session.commit() if model == 'User': return user # Services service = Service(name="ICMP IPv4", team=team, check_name="ICMP IPv4 Check", host='127.0.0.1') session.add(service) session.commit() if model == 'Service': return service # Environments environment = Environment(service=service, matching_regex='*') session.add(environment) session.commit() if model == 'Environment': return environment # Properties property_obj = Property(name="testproperty", value="testvalue", environment=environment) session.add(property_obj) session.commit() if model == 'Property': return property_obj # Rounds round_obj = Round(number=1) session.add(round_obj) session.commit() if model == 'Round': return round_obj # Checks check = Check(round=round_obj, service=service) session.add(check) session.commit() if model == 'Check': return check
def test_red_team_user(self): team = Team(name="Red Team", color="Red") self.session.add(team) self.session.commit() user = User(username='******', password='******', team=team) self.session.add(user) self.session.commit() user.is_red_team is True user.is_white_team is False user.is_blue_team is False
def test_get_array_of_scores(self): populate_sample_data(self.session) results = Team.get_all_rounds_results() assert 'rounds' in results assert results['rounds'] == ['Round 0', 'Round 1', 'Round 2'] assert 'rgb_colors' in results assert 'Blue Team 1' in results['rgb_colors'] assert results['rgb_colors']['Blue Team 1'].startswith('rgba') assert 'scores' in results assert results['scores'] == {'Blue Team 1': [0, 100, 100]}
def setup(self): super(TestGoodSetup, self).setup() competition = Competition(self.setup) competition.save(self.session) self.blue_teams = Team.get_all_blue_teams() self.blue_team = self.blue_teams[0] self.service = self.blue_team.services[0] self.account = self.service.accounts[0] self.environment = self.service.environments[0] self.property = self.environment.properties[0]
def test_blue_team_user(self): team = Team(name="Blue Team", color="Blue") self.session.add(team) self.session.commit() user = User(username="******", password="******", team=team) self.session.add(user) self.session.commit() user.is_red_team is False user.is_white_team is False user.is_blue_team is True
def service(id): if current_user.is_white_team: service = session.query(Service).get(id) blue_teams = Team.get_all_blue_teams() if service is None: return redirect(url_for('auth.unauthorized')) return render_template('admin/service.html', service=service, blue_teams=blue_teams) else: return redirect(url_for('auth.unauthorized'))
def unauthorized_admin_test(self, path): red_team = Team(name="Red Team", color="Red") self.session.add(red_team) user = User(username="******", password="******", team=red_team) self.session.add(user) self.session.commit() self.client.login('testuser_red', 'testpass_red') resp = self.client.get(path) assert resp.status_code == 302 assert 'unauthorized' in str(resp.data)
def team(id): if current_user.is_white_team: team = Team.query.get(id) blue_teams = Team.get_all_blue_teams() if team is None: return redirect(url_for('auth.unauthorized')) return render_template('admin/team.html', team=team, blue_teams=blue_teams) else: return redirect(url_for('auth.unauthorized'))
def settings(): if current_user.is_white_team: about_page_content = Setting.get_setting('about_page_content').value welcome_page_content = Setting.get_setting( 'welcome_page_content').value blue_teams = Team.get_all_blue_teams() return render_template('admin/settings.html', blue_teams=blue_teams, about_page_content=about_page_content, welcome_page_content=welcome_page_content) else: return redirect(url_for('auth.unauthorized'))
def get_queue_stats(): finished_queue_facts = [] queues_facts = {} all_services = session.query(Service).all() for service in all_services: if service.worker_queue not in queues_facts: queues_facts[service.worker_queue] = { "name": service.worker_queue, "workers": [], "services_running": {} } if service.team.name not in queues_facts[ service.worker_queue]['services_running']: queues_facts[service.worker_queue]['services_running'][ service.team.name] = [] queues_facts[service.worker_queue]['services_running'][ service.team.name].append(service.name) for queue_name, queue_facts in queues_facts.items(): # If all of the services are listed for this specific worker, let's just alias it as 'All' queue_services_total_running = 0 for team_name, team_services in queues_facts[ service.worker_queue]['services_running'].items(): queue_services_total_running += len(team_services) if queue_services_total_running == len(all_services): queues_facts[service.worker_queue]['services_running'] = 'All' else: blue_teams = Team.get_all_blue_teams() for blue_team in blue_teams: if blue_team.name in queue_facts[ 'services_running'] and len( blue_team.services) == len( queue_facts['services_running'][ blue_team.name]): # Summarize it for each team if the worker runs all services queues_facts[service.worker_queue]['services_running'][ blue_team.name] = 'ALL' for queue_name, queue_facts in queues_facts.items(): # Get which workers are assigned to which queues active_queues = celery_app.control.inspect().active_queues() # If we don't have any queues, we also have no workers if active_queues is not None: for worker_name, queues in active_queues.items(): if queue_name in [k['name'] for k in queues]: queue_facts['workers'].append(worker_name) for queue_name, queue_facts in queues_facts.items(): finished_queue_facts.append(queue_facts) return finished_queue_facts
def permissions(): if current_user.is_white_team: blue_teams = Team.get_all_blue_teams() return render_template( 'admin/permissions.html', blue_teams=blue_teams, blue_team_update_hostname=Setting.get_setting('blue_team_update_hostname').value, blue_team_update_port=Setting.get_setting('blue_team_update_port').value, blue_team_update_account_usernames=Setting.get_setting('blue_team_update_account_usernames').value, blue_team_update_account_passwords=Setting.get_setting('blue_team_update_account_passwords').value, ) else: return redirect(url_for('auth.unauthorized'))
def admin_add_team(): if current_user.is_white_team: if 'name' in request.form and 'color' in request.form: team_obj = Team(html.escape(request.form['name']), html.escape(request.form['color'])) db.save(team_obj) flash('Team successfully added.', 'success') return redirect(url_for('admin.manage')) else: flash('Error: Team name or color not defined.', 'danger') return redirect(url_for('admin.manage')) else: return {'status': 'Unauthorized'}, 403
def scoreboard_get_line_data(): results = Team.get_all_rounds_results() team_data = {"team": [], "rounds": results["rounds"]} if "team_names" in results.keys(): for team_name in results["team_names"]: scores = results["scores"][team_name] rgb_color = results["rgb_colors"][team_name] team_data["team"].append({ "name": team_name, "scores": scores, "color": rgb_color }) return jsonify(team_data)
def test_place(self): team_1 = Team(name="Blue Team 1", color="Blue") self.db.save(team_1) service_1 = Service(name="Example Service 1", team=team_1, check_name="ICMP IPv4 Check", ip_address='127.0.0.1') self.db.save(service_1) check_1 = Check(service=service_1, result=True, output='Good output') check_2 = Check(service=service_1, result=True, output='Good output') self.db.save(check_1) self.db.save(check_2) team_2 = Team(name="Blue Team 2", color="Blue") self.db.save(team_2) service_1 = Service(name="Example Service 1", team=team_2, check_name="ICMP IPv4 Check", ip_address='127.0.0.1') self.db.save(service_1) check_1 = Check(service=service_1, result=False, output='Good output') check_2 = Check(service=service_1, result=False, output='Good output') self.db.save(check_1) self.db.save(check_2) team_3 = Team(name="Blue Team 3", color="Blue") self.db.save(team_3) service_1 = Service(name="Example Service 1", team=team_3, check_name="ICMP IPv4 Check", ip_address='127.0.0.1') self.db.save(service_1) check_1 = Check(service=service_1, result=True, output='Good output') check_2 = Check(service=service_1, result=False, output='Good output') self.db.save(check_1) self.db.save(check_2) assert team_1.place == 1 assert team_2.place == 3 assert team_3.place == 2
def scoreboard_get_line_data(): results = Team.get_all_rounds_results() team_data = {'team': [], 'rounds': results['rounds']} if 'team_names' in results.keys(): for team_name in results['team_names']: scores = results['scores'][team_name] rgb_color = results['rgb_colors'][team_name] team_data['team'].append({ "name": team_name, "scores": scores, "color": rgb_color }) return jsonify(team_data)