Exemple #1
0
    def test_not_found(self, ctx, mock_switch_repository: SwitchRepository,
                       switch_manager: SwitchManager):
        mock_switch_repository.delete_switch = MagicMock(
            side_effect=SwitchNotFoundError)

        with raises(SwitchNotFoundError):
            switch_manager.delete(ctx, TEST_SWITCH_ID)
Exemple #2
0
    def test_switch_not_found(self, ctx,
                              mock_switch_repository: SwitchRepository,
                              switch_manager: SwitchManager):
        mock_switch_repository.search_switches_by = MagicMock(return_value=([],
                                                                            0))

        with raises(SwitchNotFoundError):
            switch_manager.get_by_id(ctx, TEST_SWITCH_ID)
Exemple #3
0
    def test_happy_path(self, ctx, mock_switch_repository: SwitchRepository,
                        switch_manager: SwitchManager):
        mock_switch_repository.delete_switch = MagicMock()

        switch_manager.delete(ctx, TEST_SWITCH_ID)

        mock_switch_repository.delete_switch.assert_called_once_with(
            ctx, TEST_SWITCH_ID)
Exemple #4
0
    def test_switch_not_found(self, ctx,
                              mock_switch_repository: SwitchRepository,
                              switch_manager: SwitchManager):
        req = MutationRequest(
            description='desc',
            ip_v4='157.159.123.123',
            community='ip',
        )
        mock_switch_repository.update_switch = MagicMock(
            side_effect=SwitchNotFoundError)

        with raises(SwitchNotFoundError):
            switch_manager.update(ctx, '2', req)
Exemple #5
0
    def test_happy_path(self, ctx, mock_switch_repository: SwitchRepository,
                        switch_manager: SwitchManager):
        req = MutationRequest(
            description='desc',
            ip_v4='157.159.123.123',
            community='ip',
        )
        mock_switch_repository.update_switch = MagicMock()

        switch_manager.update(ctx, '2', req)

        mock_switch_repository.update_switch.assert_called_once_with(
            ctx, switch_id='2', **asdict(req))
Exemple #6
0
    def test_invalid_mutation_request(self, ctx,
                                      mock_switch_repository: SwitchRepository,
                                      field: str, value,
                                      switch_manager: SwitchManager):
        req = MutationRequest(
            description='desc',
            ip_v4='157.159.123.123',
            community='ip',
        )
        req = MutationRequest(**{**asdict(req), **{field: value}})
        mock_switch_repository.update_switch = MagicMock()

        with raises(ValueError):
            switch_manager.update(ctx, '2', req)
Exemple #7
0
    def test_missing_required_field(self, ctx,
                                    mock_switch_repository: SwitchRepository,
                                    switch_manager: SwitchManager):
        req = MutationRequest(
            description='desc',
            ip_v4='157.159.123.123',
            community='ip',
        )
        req.community = None
        mock_switch_repository.update_switch = MagicMock()

        with raises(MissingRequiredField):
            switch_manager.update(ctx, '2', req)

        mock_switch_repository.update_switch.assert_not_called()
Exemple #8
0
    def test_happy_path(self, ctx, mock_switch_repository: SwitchRepository,
                        switch_manager: SwitchManager):
        req = MutationRequest(
            description='desc',
            ip_v4='157.159.123.123',
            community='ip',
        )
        mock_switch_repository.create_switch = MagicMock()

        switch_manager.create(ctx, req)

        mock_switch_repository.create_switch.assert_called_once_with(
            ctx,
            description=req.description,
            ip_v4=req.ip_v4,
            community=req.community)
Exemple #9
0
    def test_happy_path(self, ctx, mock_switch_repository: SwitchRepository,
                        sample_switch: Switch, switch_manager: SwitchManager):
        mock_switch_repository.search_switches_by = MagicMock(
            return_value=([sample_switch], 1))
        result = switch_manager.get_by_id(ctx, TEST_SWITCH_ID)

        assert sample_switch == result
        mock_switch_repository.search_switches_by.assert_called_once()
Exemple #10
0
    def test_happy_path(self, ctx, mock_switch_repository: SwitchRepository,
                        sample_switch: Switch, switch_manager: SwitchManager):
        mock_switch_repository.search_switches_by = MagicMock(
            return_value=([sample_switch], 1))
        result, count = switch_manager.search(ctx,
                                              limit=42,
                                              offset=2,
                                              terms='abc')

        assert [sample_switch] == result
        assert 1 == count
        mock_switch_repository.search_switches_by.assert_called_once_with(
            ctx, limit=42, offset=2, terms='abc')
Exemple #11
0
 def test_limit_negative(self, ctx, switch_manager: SwitchManager):
     with raises(IntMustBePositive):
         switch_manager.search(ctx, limit=-1)
Exemple #12
0
def switch_manager(mock_switch_repository, ):
    return SwitchManager(switch_repository=mock_switch_repository)
Exemple #13
0
def init(testing=True):
    """
    Initialize and wire together the dependency of the application.
    """
    if testing:
        configuration = TEST_CONFIGURATION
    else:
        configuration = CONFIGURATION

    Database.init_db(configuration.DATABASE, testing=testing)

    # Repositories:
    ping_repository = PingSQLRepository()
    member_sql_repository = MemberSQLRepository()
    network_object_sql_repository = NetworkObjectSQLRepository()
    device_sql_repository = DeviceSQLRepository()
    room_sql_repository = RoomSQLRepository()
    elk_repository = ElasticSearchRepository(configuration)
    money_repository = MoneySQLRepository()
    switch_network_manager = SwitchSNMPNetworkManager()
    account_sql_repository = AccountSQLRepository()
    product_sql_repository = ProductSQLRepository()
    payment_method_sql_repository = PaymentMethodSQLRepository()
    transaction_sql_repository = TransactionSQLRepository()
    account_type_sql_repository = AccountTypeSQLRepository()

    # Managers
    health_manager = HealthManager(ping_repository)
    switch_manager = SwitchManager(
        switch_repository=network_object_sql_repository, )
    port_manager = PortManager(port_repository=network_object_sql_repository, )
    device_manager = DeviceManager(
        device_repository=device_sql_repository,
        member_repository=member_sql_repository,
        room_repository=room_sql_repository,
        vlan_repository=network_object_sql_repository,
        ip_allocator=device_sql_repository,
    )
    member_manager = MemberManager(
        member_repository=member_sql_repository,
        money_repository=money_repository,
        membership_repository=member_sql_repository,
        logs_repository=elk_repository,
        configuration=configuration,
    )
    room_manager = RoomManager(room_repository=room_sql_repository, )
    account_manager = AccountManager(
        account_repository=account_sql_repository,
        member_repository=member_sql_repository,
    )
    product_manager = ProductManager(
        product_repository=product_sql_repository, )

    payment_method_manager = PaymentMethodManager(
        payment_method_repository=payment_method_sql_repository)
    transaction_manager = TransactionManager(
        transaction_repository=transaction_sql_repository, )
    account_type_manager = AccountTypeManager(
        account_type_repository=account_type_sql_repository)

    # HTTP Handlers:
    health_handler = HealthHandler(health_manager)
    transaction_handler = TransactionHandler(transaction_manager)
    member_handler = MemberHandler(member_manager)
    device_handler = DeviceHandler(device_manager)
    room_handler = RoomHandler(room_manager)
    switch_handler = SwitchHandler(switch_manager)
    port_handler = PortHandler(port_manager, switch_manager,
                               switch_network_manager)
    temporary_account_handler = TemporaryAccountHandler()
    account_type_handler = AccountTypeHandler(account_type_manager)
    payment_method_handler = PaymentMethodHandler(payment_method_manager)
    account_handler = AccountHandler(account_manager)
    product_handler = ProductHandler(product_manager)

    # Connexion will use this function to authenticate and fetch the information of the user.
    if os.environ.get('TOKENINFO_FUNC') is None:
        os.environ[
            'TOKENINFO_FUNC'] = 'src.interface_adapter.http_api.auth.token_info'

    app = connexion.FlaskApp(__name__, specification_dir='openapi')
    app.add_api(
        'swagger.yaml',
        # resolver=RestyResolver('src.interface_adapter.http_api'),
        resolver=ADHResolver({
            'health': health_handler,
            'transaction': transaction_handler,
            'member': member_handler,
            'device': device_handler,
            'room': room_handler,
            'switch': switch_handler,
            'port': port_handler,
            'temporary_account': temporary_account_handler,
            'account_type': account_type_handler,
            'payment_method': payment_method_handler,
            'account': account_handler,
            'product': product_handler,
        }),
        validate_responses=True,
        strict_validation=True,
        pythonic_params=True,
        auth_all_paths=True,
    )
    app.app.config.update(configuration.API_CONF)

    return app