def test_users(self): team = generate_sample_model_tree('Team', self.db) user_1 = User(username="******", password="******", team=team) user_2 = User(username="******", password="******", team=team) self.db.save(user_1) self.db.save(user_2) assert team.users == [user_1, user_2]
def test_username_unique(self): team = generate_sample_model_tree('Team', self.db) user1 = User(username="******", password="******", team=team) self.db.save(user1) with pytest.raises(IntegrityError): user2 = User(username="******", password="******", team=team) self.db.save(user2)
def test_users(self): team = generate_sample_model_tree('Team', self.session) user_1 = User(username="******", password="******", team=team) user_2 = User(username="******", password="******", team=team) self.session.add(user_1) self.session.add(user_2) self.session.commit() assert team.users == [user_1, user_2]
def test_username_unique(self): team = generate_sample_model_tree("Team", self.session) user1 = User(username="******", password="******", team=team) self.session.add(user1) self.session.commit() with pytest.raises(IntegrityError): user2 = User(username="******", password="******", team=team) self.session.add(user2) self.session.commit()
def test_basic_user(self): team = generate_sample_model_tree('Team', self.db) user = User(username="******", password="******", team=team) self.db.save(user) assert user.id is 1 assert user.username == "testuser" assert user.password == User.generate_hash('testpass') assert user.team is team assert user.team_id is team.id assert user.get_id() == 1
def test_basic_user(self): team = generate_sample_model_tree("Team", self.session) user = User(username="******", password="******", team=team) self.session.add(user) self.session.commit() assert user.id == 1 assert user.username == "testuser" assert user.password == User.generate_hash("testpass") assert user.team is team assert user.team_id is team.id assert user.get_id() == 1
def test_init_service(self): user = User(username="******", password="******") assert user.id is None assert user.username == "testuser" assert user.password == User.generate_hash("testpass") assert type(user.password) is str assert user.team is None assert user.team_id is None assert user.is_authenticated is None assert user.is_active is True assert user.is_anonymous is False assert user.get_username == "testuser" assert user.get_id() is None
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 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 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_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 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 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 save(self, db_session): for team_dict in self['teams']: logger.info("Creating {0} Team: {1}".format( team_dict['color'], team_dict['name'])) team_obj = Team(name=team_dict['name'], color=team_dict['color']) db_session.add(team_obj) for user_dict in team_dict['users']: logger.info("\tCreating User {0}:{1}".format( user_dict['username'], user_dict['password'])) db_session.add( User(username=user_dict['username'], password=user_dict['password'], team=team_obj)) if 'services' in team_dict: for service_dict in team_dict['services']: logger.info("\tCreating {0} Service".format( service_dict['name'])) service_obj = Service( name=service_dict['name'], team=team_obj, check_name=service_dict['check_name'], host=service_dict['host'], port=service_dict['port'], points=service_dict['points']) if 'worker_queue' in service_dict: service_obj.worker_queue = service_dict['worker_queue'] db_session.add(service_obj) if 'accounts' in service_dict: for account_dict in service_dict['accounts']: db_session.add( Account(username=account_dict['username'], password=account_dict['password'], service=service_obj)) for environment_dict in service_dict['environments']: environment_obj = Environment( service=service_obj, matching_content=environment_dict[ 'matching_content']) db_session.add(environment_obj) if 'properties' in environment_dict: for property_dict in environment_dict[ 'properties']: db_session.add( Property(environment=environment_obj, name=property_dict['name'], value=property_dict['value'])) db_session.commit()
def admin_add_user(): if current_user.is_white_team: if 'username' in request.form and 'password' in request.form and 'team_id' in request.form: team_obj = Team.query.filter( Team.id == request.form['team_id']).one() user_obj = User(username=html.escape(request.form['username']), password=html.escape(request.form['password']), team=team_obj) db.save(user_obj) flash('User successfully added.', 'success') return redirect(url_for('admin.manage')) else: flash('Error: Username, Password, or Team ID not specified.', 'danger') return redirect(url_for('admin.manage')) else: return {'status': 'Unauthorized'}, 403
def login(): if current_user.is_authenticated: flash('You are already logged in.', 'info') return redirect(url_for("welcome.home")) form = LoginForm() if form.errors: flash(form.errors, 'danger') return render_template('login.html', form=form) if form.validate_on_submit(): username = request.form.get('username') password = request.form.get('password') try: user = db.session.query(User).filter(User.username == username).one() except NoResultFound: flash('Invalid username or password. Please try again.', 'danger') return render_template('login.html', form=form) except OperationalError: flash("Error 'OperationError' received!. Try restarting the db service.", 'danger') return render_template('login.html', form=form) if user: if User.generate_hash(password, user.password) == user.password: user.authenticated = True db.save(user) login_user(user, remember=True) if user.is_white_team: return redirect(request.values.get('next') or url_for("admin.status")) elif user.is_blue_team: return redirect(request.values.get('next') or url_for("services.home")) else: return redirect(request.values.get('next') or url_for("overview.home")) else: flash('Invalid username or password. Please try again.', 'danger') return render_template('login.html', form=form) else: flash('Invalid username or password. Please try again.', 'danger') return render_template('login.html', form=form) return render_template('login.html', form=form)
def admin_add_user(): if current_user.is_white_team: if ("username" in request.form and "password" in request.form and "team_id" in request.form): team_obj = (session.query(Team).filter( Team.id == request.form["team_id"]).one()) user_obj = User( username=html.escape(request.form["username"]), password=html.escape(request.form["password"]), team=team_obj, ) session.add(user_obj) session.commit() flash("User successfully added.", "success") return redirect(url_for("admin.manage")) else: flash("Error: Username, Password, or Team ID not specified.", "danger") return redirect(url_for("admin.manage")) else: return {"status": "Unauthorized"}, 403
def create_default_user(self): team1 = Team(name="Team 1", color="White") self.db.save(team1) user1 = User(username='******', password='******', team=team1) self.db.save(user1) return user1