Пример #1
0
    def test_should_delete_team_member(self, db_mock: MagicMock,
                                       team_member_mock: MagicMock):
        # Multiple call needs to return different things
        team_member_mock.query.filter_by.return_value.first.side_effect = \
            [TeamMember(participant_id=A_PARTICIPANT.id, captain=True), self.A_TEAM_MEMBER]

        teams.kick_team_member(A_PARTICIPANT)

        db_mock.session.delete.assert_called_with(
            TeamMember(participant_id=self.A_TEAM_MEMBER.participant_id,
                       team_id=self.A_TEAM_MEMBER.team_id))
        db_mock.session.commit.assert_called_once()
Пример #2
0
def accept_team_request(current_participant: Participant):
    """Accepts a team request. Only captains can accept a request."""
    body = flask_rebar.get_validated_body()
    participant_id = body["participant_id"]

    current_member = TeamMember.query.filter_by(
        participant_id=current_participant.id).first()

    if not current_member or not current_member.captain:
        raise errors.Unauthorized(
            "You don't have the rights to accept this request.")

    # Remove TeamRequest and add the new member
    team_request = TeamRequest.query.filter_by(
        team_id=current_member.team_id, participant_id=participant_id).first()

    if team_request is None:
        raise errors.UnprocessableEntity("The request doesn't exist.")

    new_member = TeamMember(participant_id=participant_id,
                            team_id=current_member.team_id)

    DB.session.delete(team_request)
    DB.session.add(new_member)
    DB.session.commit()

    return ""
Пример #3
0
    def test_given_a_non_captain_should_raise_unauthorized_error(
            self, team_member_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = \
            TeamMember(participant_id=A_PARTICIPANT_NOT_CAPTAIN.id, captain=False)

        with raises(errors.Unauthorized):
            teams.change_role(A_PARTICIPANT)
Пример #4
0
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
Пример #5
0
    def test_given_an_invalid_user_id_should_raise_unprocessable_entity_error(
            self, team_member_mock: MagicMock, team_request_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = TeamMember(
            participant_id=A_PARTICIPANT.id, captain=True)
        team_request_mock.query.filter_by.return_value.first.return_value = None

        with raises(errors.UnprocessableEntity):
            teams.decline_team_request(A_PARTICIPANT)
Пример #6
0
    def test_should_commit_changes(self, db_mock: MagicMock,
                                   team_member_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = TeamMember(
            participant_id=A_PARTICIPANT.id, captain=True)

        teams.change_role(A_PARTICIPANT)

        db_mock.session.commit.assert_called_once()
Пример #7
0
class TestAcceptTeamRequest:
    A_TEAM_REQUEST = TeamRequest(team_id=A_TEAM.members[0].team_id,
                                 participant_id=A_PARTICIPANT_WITHOUT_TEAM.id)
    A_TEAM_MEMBER = TeamMember(team_id=A_TEAM.members[0].team_id,
                               participant_id=A_PARTICIPANT_WITHOUT_TEAM.id)
    REQUEST_BODY = {"participant_id": A_TEAM_REQUEST.participant_id}

    @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_member_mock(self, team_member_mock):
        team_member_mock.side_effect = lambda *args, **kwargs: TeamMember(
            *args, **kwargs)

    def test_given_a_user_without_team_should_raise_unauthorized_error(
            self, team_member_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = None

        with raises(errors.Unauthorized):
            teams.accept_team_request(A_PARTICIPANT)

    def test_given_a_non_captain_should_raise_unauthorized_error(
            self, team_member_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = A_TEAM.members[
            1]

        with raises(errors.Unauthorized):
            teams.accept_team_request(A_PARTICIPANT)

    def test_given_an_invalid_user_id_should_raise_unprocessable_entity_error(
            self, team_member_mock: MagicMock, team_request_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = A_TEAM.members[
            0]
        team_request_mock.query.filter_by.return_value.first.return_value = None

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

    def test_should_delete_team_request_and_add_user_to_team(
            self, db_mock: MagicMock, team_member_mock: MagicMock,
            team_request_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = A_TEAM.members[
            0]
        team_request_mock.query.filter_by.return_value.first.return_value = self.A_TEAM_REQUEST

        teams.accept_team_request(A_PARTICIPANT)

        db_mock.session.delete.assert_called_with(
            TeamRequest(participant_id=self.A_TEAM_REQUEST.participant_id,
                        team_id=self.A_TEAM_REQUEST.team_id))
        db_mock.session.add.assert_called_with(
            TeamMember(participant_id=self.A_TEAM_MEMBER.participant_id,
                       team_id=self.A_TEAM_MEMBER.team_id))
        db_mock.session.commit.assert_called_once()
Пример #8
0
    def test_given_the_current_participant_id_should_raise_unprocessable_entity_error(
            self, team_member_mock: MagicMock, _rebar_mock: MagicMock):
        _rebar_mock.get_validated_body.return_value = {
            "participant_id": A_PARTICIPANT.id
        }
        team_member_mock.query.filter_by.return_value.first.return_value = TeamMember(
            participant_id=A_PARTICIPANT.id, captain=True)

        with raises(errors.UnprocessableEntity):
            teams.kick_team_member(A_PARTICIPANT)
Пример #9
0
    def test_should_delete_current_team_member(self, db_mock: MagicMock,
                                               team_member_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = self.A_TEAM_MEMBER

        teams.leave_team(A_PARTICIPANT)

        db_mock.session.delete.assert_called_with(
            TeamMember(team_id=self.A_TEAM_MEMBER.team_id,
                       participant_id=self.A_TEAM_MEMBER.participant_id))
        db_mock.session.commit.assert_called_once()
Пример #10
0
def get_records_dev(teams: [Team], participants: [Participant]):
    """Get the records to add to the database for development"""
    members = []
    captain = True
    for team, participant in zip(teams, participants):
        members.append(
            TeamMember(team_id=team.id,
                       participant_id=participant.id,
                       captain=captain))
        captain = not captain

    return members
Пример #11
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
Пример #12
0
    def test_should_delete_team_request(self, db_mock: MagicMock,
                                        team_member_mock: MagicMock,
                                        team_request_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = TeamMember(
            participant_id=A_PARTICIPANT.id, captain=True)
        team_request_mock.query.filter_by.return_value.first.return_value = self.A_TEAM_REQUEST

        teams.decline_team_request(A_PARTICIPANT)

        db_mock.session.delete.assert_called_with(
            TeamRequest(participant_id=self.A_TEAM_REQUEST.participant_id,
                        team_id=self.A_TEAM_REQUEST.team_id))
        db_mock.session.commit.assert_called_once()
Пример #13
0
class TestKickTeamMember:
    A_TEAM_MEMBER = TeamMember(team_id=A_TEAM.id,
                               participant_id=A_PARTICIPANT_NOT_CAPTAIN.id)
    REQUEST_BODY = {"participant_id": A_TEAM_MEMBER.participant_id}

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

    def test_given_a_user_without_team_should_raise_unauthorized_error(
            self, team_member_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = None

        with raises(errors.Unauthorized):
            teams.kick_team_member(A_PARTICIPANT)

    def test_given_a_non_captain_should_raise_unauthorized_error(
            self, team_member_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = \
            TeamMember(participant_id=A_PARTICIPANT_NOT_CAPTAIN.id, captain=False)

        with raises(errors.Unauthorized):
            teams.kick_team_member(A_PARTICIPANT)

    def test_given_the_current_participant_id_should_raise_unprocessable_entity_error(
            self, team_member_mock: MagicMock, _rebar_mock: MagicMock):
        _rebar_mock.get_validated_body.return_value = {
            "participant_id": A_PARTICIPANT.id
        }
        team_member_mock.query.filter_by.return_value.first.return_value = TeamMember(
            participant_id=A_PARTICIPANT.id, captain=True)

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

    def test_should_delete_team_member(self, db_mock: MagicMock,
                                       team_member_mock: MagicMock):
        # Multiple call needs to return different things
        team_member_mock.query.filter_by.return_value.first.side_effect = \
            [TeamMember(participant_id=A_PARTICIPANT.id, captain=True), self.A_TEAM_MEMBER]

        teams.kick_team_member(A_PARTICIPANT)

        db_mock.session.delete.assert_called_with(
            TeamMember(participant_id=self.A_TEAM_MEMBER.participant_id,
                       team_id=self.A_TEAM_MEMBER.team_id))
        db_mock.session.commit.assert_called_once()
Пример #14
0
    def test_should_delete_team_request_and_add_user_to_team(
            self, db_mock: MagicMock, team_member_mock: MagicMock,
            team_request_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = A_TEAM.members[
            0]
        team_request_mock.query.filter_by.return_value.first.return_value = self.A_TEAM_REQUEST

        teams.accept_team_request(A_PARTICIPANT)

        db_mock.session.delete.assert_called_with(
            TeamRequest(participant_id=self.A_TEAM_REQUEST.participant_id,
                        team_id=self.A_TEAM_REQUEST.team_id))
        db_mock.session.add.assert_called_with(
            TeamMember(participant_id=self.A_TEAM_MEMBER.participant_id,
                       team_id=self.A_TEAM_MEMBER.team_id))
        db_mock.session.commit.assert_called_once()
Пример #15
0
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
Пример #16
0
class TestLeaveTeam:
    A_TEAM_MEMBER = TeamMember(team_id=A_TEAM.id,
                               participant_id=A_PARTICIPANT.id)

    def test_given_a_user_without_team_should_raise_unprocessable_entity_error(
            self, team_member_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = None

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

    def test_should_delete_current_team_member(self, db_mock: MagicMock,
                                               team_member_mock: MagicMock):
        team_member_mock.query.filter_by.return_value.first.return_value = self.A_TEAM_MEMBER

        teams.leave_team(A_PARTICIPANT)

        db_mock.session.delete.assert_called_with(
            TeamMember(team_id=self.A_TEAM_MEMBER.team_id,
                       participant_id=self.A_TEAM_MEMBER.participant_id))
        db_mock.session.commit.assert_called_once()
Пример #17
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):
Пример #18
0
 def _team_member_mock(self, team_member_mock):
     team_member_mock.side_effect = lambda *args, **kwargs: TeamMember(
         *args, **kwargs)