Example #1
0
def get_records_dev(events: [Event]):
    """Get the records to add to the database for development"""
    categories = []
    for event in events:
        categories.append(Category(event_id=event.id, name='Web'))
        categories.append(Category(event_id=event.id, name='Forensics'))
        categories.append(Category(event_id=event.id, name='Steg'))
        categories.append(Category(event_id=event.id, name='Programming'))

    return categories
Example #2
0
def create_category(current_admin: Administrator):
    """Add a category """
    body = flask_rebar.get_validated_body()
    name = body["name"]
    event_id = body["event_id"]

    event = Event.query.filter_by(id=event_id).first()

    if event is None:
        raise errors.NotFound(f'Event with id "{event_id}" not found.')

    if not current_admin.is_admin_of_event(event_id):
        raise errors.Unauthorized(
            "You do not have the permission to administer this event.")

    category = Category.query.filter_by(name=name, event_id=event_id).first()

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

    category = Category(name=name, event_id=event_id)

    DB.session.add(category)
    DB.session.commit()

    return category
Example #3
0
 def _category_mock(self, category_mock: MagicMock):
     category_mock.side_effect = lambda *args, **kwargs: Category(
         *args, **kwargs)
Example #4
0
class TestCreateCategory:
    REQUEST_BODY = {"event_id": 0, "name": "New Category"}
    A_NEW_CATEGORY = Category(id=None,
                              event_id=REQUEST_BODY["event_id"],
                              name=REQUEST_BODY["name"])

    @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 _db_mock(self, db_mock: MagicMock):
        yield db_mock

    @fixture(autouse=True)
    def _event_mock(self, event_mock: MagicMock):
        yield event_mock

    @fixture(autouse=True)
    def _category_mock(self, category_mock: MagicMock):
        category_mock.side_effect = lambda *args, **kwargs: Category(
            *args, **kwargs)

    def test_given_non_existent_event_id_should_raise_not_found_error(
            self, event_mock: MagicMock):
        event_mock.query.filter_by.return_value.first.return_value = None

        with raises(errors.NotFound):
            inspect.unwrap(categories.create_category)(AN_ADMINISTRATOR)

    def test_given_already_used_name_should_raise_not_found_error(
            self, category_mock: MagicMock):
        category_mock.query.filter_by.return_value.first.return_value = A_CATEGORY

        with raises(errors.UnprocessableEntity):
            inspect.unwrap(categories.create_category)(AN_ADMINISTRATOR)

    def test_given_an_administrator_of_another_event_should_raise_unauthorized_error(
            self, category_mock: MagicMock):
        category_mock.query.filter_by.return_value.first.return_value = A_CATEGORY

        with raises(errors.Unauthorized):
            inspect.unwrap(categories.create_category)(ANOTHER_ADMINISTRATOR)

    def test_should_create_a_category(self, category_mock: MagicMock,
                                      db_mock: MagicMock):
        category_mock.query.filter_by.return_value.first.return_value = None

        inspect.unwrap(categories.create_category)(AN_ADMINISTRATOR)

        db_mock.session.add.assert_called_with(self.A_NEW_CATEGORY)
        db_mock.session.commit.assert_called_once()

    def test_should_return_category(self, category_mock: MagicMock,
                                    db_mock: MagicMock):
        category_mock.query.filter_by.return_value.first.return_value = None

        result = inspect.unwrap(categories.create_category)(AN_ADMINISTRATOR)

        assert result == self.A_NEW_CATEGORY
Example #5
0
    with local_patch("current_admin") as mock:
        yield mock


@fixture()
def require_admin_mock():
    with local_patch('require_admin') as mock:
        yield mock


A_EVENT = Event(id=0, name="Test Event", teams=True)
AN_ADMINISTRATOR = Administrator(id=0, is_platform_admin=True, user_id=0)
AN_ADMINISTRATOR.event_administrators = \
    [EventAdministrator(id=0, administrator_id=0, event_id=A_EVENT, event=A_EVENT)]
ANOTHER_ADMINISTRATOR = Administrator(id=1, is_platform_admin=False, user_id=1)
A_CATEGORY = Category(id=None, event_id=A_EVENT, name="Category")


class TestCreateCategory:
    REQUEST_BODY = {"event_id": 0, "name": "New Category"}
    A_NEW_CATEGORY = Category(id=None,
                              event_id=REQUEST_BODY["event_id"],
                              name=REQUEST_BODY["name"])

    @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 _db_mock(self, db_mock: MagicMock):
Example #6
0
        yield mock


@fixture
def current_user_mock():
    with local_patch("current_user") as mock:
        yield mock


A_EVENT = Event(id=0, name="Test Event", teams=True)
AN_ADMINISTRATOR = Administrator(id=0, is_platform_admin=True, user_id=0)
AN_ADMINISTRATOR.event_administrators = \
    [EventAdministrator(id=0, administrator_id=0, event_id=0, event=A_EVENT)]
ANOTHER_ADMINISTRATOR = Administrator(id=1, is_platform_admin=False, user_id=1)

A_CATEGORY = Category(id=0, event_id=A_EVENT.id, name="Category")
A_CHALLENGE = Challenge(id=0,
                        name="Challenge name",
                        points=100,
                        hidden=False,
                        description="My description",
                        category=A_CATEGORY,
                        category_id=A_CATEGORY.id)
A_FLAG = Flag(id=None,
              challenge_id=A_CHALLENGE.id,
              is_regex=False,
              value="JDIS-FLAG")
A_CHALLENGE.flags = [A_FLAG]


class TestGetChallenge:
Example #7
0
    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

    @fixture(autouse=True)
    def _event_mock(self, event_mock: MagicMock):
        event_mock.query.filter_by.return_value.first.return_value = 1
        yield event_mock

    @fixture(autouse=True)
    def _current_user_mock(self, current_user_mock: MagicMock):
Example #8
0
AN_EVENT = Event(id=AN_EVENT_ID,
                 name='',
                 teams=True,
                 is_visible=True,
                 is_open=True)
A_CLOSED_EVENT = Event(id=AN_EVENT_ID,
                       name='',
                       teams=True,
                       is_visible=True,
                       is_open=False)
AN_INVISIBLE_EVENT = Event(id=AN_EVENT_ID,
                           name='',
                           teams=True,
                           is_visible=False,
                           is_open=False)
A_CATEGORY_WITH_INVISIBLE_EVENT = Category(event_id=AN_INVISIBLE_EVENT,
                                           name="event invisible")
A_CATEGORY = Category(event_id=AN_EVENT, name="event invisible")
A_CHALLENGE_WITH_INVISIBLE_EVENT = Challenge(
    id=1,
    name='A chall',
    category_id=A_CATEGORY_WITH_INVISIBLE_EVENT.id,
    category=A_CATEGORY_WITH_INVISIBLE_EVENT)
A_CHALLENGE = Challenge(id=1,
                        name='A chall2',
                        category_id=A_CATEGORY_WITH_INVISIBLE_EVENT.id,
                        category=A_CATEGORY_WITH_INVISIBLE_EVENT)
AN_ADMINISTRATOR = Administrator(id=0, is_platform_admin=True, user_id=0)
ANOTHER_ADMINISTRATOR = Administrator(id=1, is_platform_admin=False, user_id=1)

A_ROLE = 'role'