Esempio n. 1
0
File: teams.py Progetto: JDIS/flaggr
def create_team(current_participant: Participant):
    """Create a team for a given event."""
    body = flask_rebar.get_validated_body()
    team_name = body["team_name"]

    if not team_name:
        raise errors.UnprocessableEntity("Please choose a team name")

    team = current_participant.get_team()

    if team is not None:
        raise errors.UnprocessableEntity(
            "You cannot create a team if you already are in a team.")

    team = Team.query.filter_by(name=team_name).first()

    if team is not None:
        raise errors.UnprocessableEntity(
            "A team with that name already exists.")

    team = Team(name=team_name,
                event_id=current_participant.event_id,
                members=[
                    TeamMember(participant_id=current_participant.id,
                               captain=True)
                ])

    DB.session.add(team)
    DB.session.commit()
    return team
Esempio n. 2
0
class TestCreateTeam:
    REQUEST_BODY = {"team_name": "Test Team"}

    A_NEW_TEAM = Team(id=None,
                      name=REQUEST_BODY["team_name"],
                      event_id=A_PARTICIPANT.event_id,
                      members=[
                          TeamMember(
                              participant_id=A_PARTICIPANT_WITHOUT_TEAM.id,
                              captain=True)
                      ])

    @fixture(autouse=True)
    def _rebar_mock(self, rebar_mock: MagicMock):
        rebar_mock.get_validated_body.return_value = self.REQUEST_BODY
        yield rebar_mock

    @fixture(autouse=True)
    def _team_mock(self, team_mock: MagicMock):
        team_mock.side_effect = lambda *args, **kwargs: Team(*args, **kwargs)

    @fixture(autouse=True)
    def _db_mock(self, db_mock: MagicMock):
        yield db_mock

    def test_given_a_user_already_in_a_team_should_raise_unprocessable_entity_error(
            self, current_participant_mock: MagicMock):
        current_participant_mock.get_team.return_value = A_TEAM

        with raises(errors.UnprocessableEntity):
            teams.create_team(current_participant_mock)

    def test_given_an_already_existing_team_name_should_raise_unprocessable_entity_error(
            self, team_mock: MagicMock):
        team_mock.query.filter_by.return_value.first.return_value = A_TEAM

        with raises(errors.UnprocessableEntity):
            teams.create_team(A_PARTICIPANT)

    def test_should_create_a_team(self, db_mock: MagicMock,
                                  team_mock: MagicMock):
        team_mock.query.filter_by.return_value.first.return_value = None

        teams.create_team(A_PARTICIPANT)

        db_mock.session.add.assert_called_with(self.A_NEW_TEAM)

        db_mock.session.commit.assert_called_once()

    def test_should_return_created_team(self, team_mock: MagicMock):
        team_mock.query.filter_by.return_value.first.return_value = None
        team = teams.create_team(A_PARTICIPANT)
        assert team == self.A_NEW_TEAM
Esempio n. 3
0
File: auth.py Progetto: JDIS/flaggr
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
Esempio n. 4
0
 def _team_mock(self, team_mock: MagicMock):
     team_mock.side_effect = lambda *args, **kwargs: Team(*args, **kwargs)
Esempio n. 5
0
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,
                                        user_id=A_USER_NOT_CAPTAIN.id,
                                        event_id=A_EVENT.id)

A_TEAM = Team(id=0,
              name="Test Team",
              event_id=A_EVENT.id,
              members=[
                  TeamMember(participant_id=A_PARTICIPANT.id, captain=True),
                  TeamMember(participant_id=A_PARTICIPANT_NOT_CAPTAIN.id,
                             captain=False)
              ])


class TestCurrentTeam:
    def test_given_a_user_without_a_team(self,
                                         current_participant_mock: MagicMock):
        current_participant_mock.get_team.return_value = None
        result = teams.current_team(current_participant_mock)

        assert result is None

    def test_given_a_user_with_a_team(self,
                                      current_participant_mock: MagicMock):
Esempio n. 6
0

@fixture
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):