def register_administrator(): """Register a new user""" body = flask_rebar.get_validated_body() email = body["email"] username = body["username"] password = body["password"] # Validate user uniqueness constraint. user = User.query.filter_by(email=email).first() if user is not None: administrator = user.get_administrator() if administrator is not None: raise errors.UnprocessableEntity("An administrator with that email already exists") user = User.query.filter_by(username=username).first() if user is not None: administrator = user.get_administrator() if administrator: raise errors.UnprocessableEntity("An administrator with that username already exists for this event") user = User(email=email, username=username) user.set_password(password) administrator = Administrator(is_platform_admin=False, user=user) DB.session.add(administrator) DB.session.commit() return administrator, 201
def register_participant(event: Event): """Register a new user""" body = flask_rebar.get_validated_body() email = body["email"] username = body["username"] password = body["password"] if not username: raise errors.UnprocessableEntity("Please choose a username") # Validate user uniqueness constraint. user = User.query.filter_by(email=email).first() if user is not None: participant = user.get_participant() if user is not None and participant and participant.event_id == event.id: raise errors.UnprocessableEntity("A participant with that email already exists for this event") user = User.query.filter_by(username=username).first() if user is not None: participant = user.get_participant() if user is not None and participant and participant.event_id == event.id: raise errors.UnprocessableEntity("A participant with that username already exists for this event") user = User(email=email, username=username) user.set_password(password) participant = Participant(event_id=event.id, user=user) DB.session.add(participant) if not event.teams: # means that its a solo event, need to create a team with the participant in it. team = Team(event_id=event.id, name=user.username, members=[TeamMember(participant=participant, captain=True)]) DB.session.add(team) DB.session.commit() login_user(participant.user, remember=True) return participant, 201
yield mock @fixture(autouse=True) def current_participant_mock(): with patch('JDISCTF.permission_wrappers.require_participant', lambda x: x): with local_patch('Participant') as mock: importlib.reload(sys.modules['JDISCTF.api.teams']) yield mock importlib.reload(sys.modules['JDISCTF.api.teams']) A_EVENT = Event(id=0, name="Test Event", teams=True) A_USER = User(id=0, username="******", email="*****@*****.**") A_USER.set_password("test") A_PARTICIPANT = Participant(id=0, user_id=A_USER.id, event_id=A_EVENT.id) A_USER_WITHOUT_TEAM = User(id=1, username="******", email="*****@*****.**") A_USER_WITHOUT_TEAM.set_password("test2") A_PARTICIPANT_WITHOUT_TEAM = Participant(id=1, user_id=A_USER_WITHOUT_TEAM.id, event_id=A_EVENT.id) A_USER_NOT_CAPTAIN = User(id=2, username="******", email="*****@*****.**") A_USER_NOT_CAPTAIN.set_password("test3") A_PARTICIPANT_NOT_CAPTAIN = Participant(id=2,
return patch('JDISCTF.api.admin.auth.' + module) @fixture def flask_rebar_mock(): with local_patch('flask_rebar') as mock: yield mock @fixture def user_mock(): with local_patch('User') as mock: yield mock A_USER = User(id=0, email="an_email", password_hash="a_password") AN_ADMINISTRATOR = Administrator(id=0, is_platform_admin=False, user_id=0) class TestLogin: @fixture(autouse=True) def _user_mock(self, user_mock: MagicMock): a_user_mock = MagicMock() a_user_mock.id = A_USER.id a_user_mock.email = A_USER.email a_user_mock.password_hash = A_USER.password_hash a_user_mock.check_password.return_value = True user_mock.query.filter_by.return_value.first.return_value = a_user_mock yield user_mock @fixture(autouse=True)
def get_records_dev(events: [Event]): """Get the records to add to the database for development""" users = [] for _ in events: user1 = User(email='*****@*****.**', username='******') user1.set_password('test') users.append(user1) user2 = User(email='*****@*****.**', username='******') user2.set_password('test') users.append(user2) user3 = User(email='*****@*****.**', username='******') user3.set_password('test') users.append(user3) user4 = User(email='*****@*****.**', username='******') user4.set_password('test') users.append(user4) user = User(email='*****@*****.**', username='******') user.set_password('admin') users.append(user) user = User(email='*****@*****.**', username='******') user.set_password('admin2') users.append(user) user = User(email='*****@*****.**', username='******') user.set_password('admin3') users.append(user) user = User(email='*****@*****.**', username='******') user.set_password('admin4') users.append(user) return users
def current_user_mock(): with local_patch('current_user') as mock: yield mock @fixture def submission_mock(): with local_patch('Submission') as mock: yield mock EVENT_ID = 1 A_TEAM = Team(id=1, event_id=EVENT_ID, name='Team 1') REQUEST_BODY = {"team_id": 1, "flag": "JDIS"} A_EVENT = Event(id=EVENT_ID, name="Test Event", teams=True) A_USER = User(id=0, username="******", email="*****@*****.**") A_CHALLENGE = Challenge(id=1, category_id=1, name='Challenge', description='Description', points=100, hidden=False) A_CATEGORY = Category(id=1, event_id=1, name='Category') class TestGetAllChallengesForEvent: @fixture(autouse=True) def _submission_mock(self, submission_mock: MagicMock): submission_mock.query.filter.return_value.exists.return_value.label.return_value = 1 yield submission_mock