def test_invalid_limit(self, ctx, device_manager: DeviceManager): # When... with raises(IntMustBePositive): device_manager.search(ctx, limit=-1, offset=1, username=TEST_USERNAME, terms='blabla')
def test_happy_path(self, ctx, mock_device_repository: MagicMock, device_manager: DeviceManager): # When... device_manager.delete(ctx, TEST_MAC_ADDRESS1) # Expect... mock_device_repository.delete_device.assert_called_once_with( ctx, mac_address=TEST_MAC_ADDRESS1)
def test_happy_path(self, ctx, mock_device_repository: MagicMock, sample_device, device_manager: DeviceManager): mock_device_repository.search_device_by = MagicMock( return_value=([sample_device], 1)) # When... device_manager.get_by_mac_address(ctx, TEST_MAC_ADDRESS1) # Expect... mock_device_repository.search_device_by.assert_called_once_with( ctx, mac_address=TEST_MAC_ADDRESS1)
def test_device_not_found(self, ctx, mock_device_repository: MagicMock, device_manager: DeviceManager): mock_device_repository.search_device_by = MagicMock(return_value=([], 0)) # When... with raises(DeviceNotFoundError): device_manager.get_by_mac_address(ctx, TEST_MAC_ADDRESS1) # Expect... mock_device_repository.search_device_by.assert_called_once_with( ctx, mac_address=TEST_MAC_ADDRESS1)
def test_device_not_found(self, ctx, mock_device_repository: MagicMock, device_manager: DeviceManager): # Given mock_device_repository.delete_device = MagicMock( side_effect=DeviceNotFoundError) # When... with raises(DeviceNotFoundError): device_manager.delete(ctx, TEST_MAC_ADDRESS1) # Expect... mock_device_repository.delete_device.assert_called_once_with( ctx, mac_address=TEST_MAC_ADDRESS1)
def test_invalid_mac(self, ctx, mock_device_repository: MagicMock, mock_member_repository: MagicMock, mock_room_repository: MagicMock, mock_ip_allocator: MagicMock, sample_member: Member, sample_room: Room, device_manager: DeviceManager): ipv4 = '192.0.0.1' ipv6 = 'fe80::1' # Given... # That the owner exists: mock_member_repository.search_member_by = MagicMock( return_value=([sample_member], 1)) # That the device does not exist in the DB: mock_device_repository.search_device_by = MagicMock(return_value=([], 0)) # That the owner has a room (needed to get the ip range to allocate the IP): mock_room_repository.search_room_by = MagicMock( return_value=([sample_room], 1)) mock_ip_allocator.allocate_ip_v4 = MagicMock(return_value=ipv4) mock_ip_allocator.allocate_ip_v6 = MagicMock(return_value=ipv6) # When... with raises(InvalidMACAddress): device_manager.update_or_create( ctx, mac_address=TEST_MAC_ADDRESS1, req=MutationRequest( mac_address='invalid mac', owner_username=TEST_USERNAME, connection_type=DeviceType.Wired, ip_v4_address=None, ip_v6_address=None, ), ) # Expect... mock_device_repository.create_device.assert_not_called()
def device_manager( mock_device_repository: DeviceRepository, mock_member_repository: MemberRepository, mock_room_repository: RoomRepository, mock_vlan_repository: VLANRepository, mock_ip_allocator: IPAllocator, ): return DeviceManager( device_repository=mock_device_repository, member_repository=mock_member_repository, room_repository=mock_room_repository, vlan_repository=mock_vlan_repository, ip_allocator=mock_ip_allocator, )
def test_allocation_failed(self, ctx, mock_device_repository: MagicMock, mock_member_repository: MagicMock, mock_room_repository: MagicMock, sample_member: Member, sample_device: Device, sample_room: Room, mock_ip_allocator: MagicMock, device_manager: DeviceManager): # Given... mock_ip_allocator.allocate_ip_v4 = MagicMock( side_effect=NoMoreIPAvailableException) # That the owner exists: mock_member_repository.search_member_by = MagicMock( return_value=([sample_member], 1)) # That the device exists in the DB: mock_room_repository.search_room_by = MagicMock( return_value=([sample_room], 1)) # That the device exists in the DB: mock_device_repository.search_device_by = MagicMock( return_value=([sample_device], 1)) # When... with raises(NoMoreIPAvailableException): device_manager.update_or_create( ctx, mac_address=sample_device.mac_address, req=MutationRequest( owner_username=TEST_USERNAME, connection_type=DeviceType.Wired, mac_address=TEST_MAC_ADDRESS1, ip_v4_address=None, ip_v6_address=None, ), )
def test_invalid_device_type(self, ctx, mock_member_repository: MagicMock, mock_device_repository: MagicMock, device_manager: DeviceManager): # Given... mock_member_repository.search_member_by = MagicMock(return_value=([], 0)) # When...Unnamed with raises(ValueError): device_manager.update_or_create( ctx, mac_address=TEST_MAC_ADDRESS1, req=MutationRequest( owner_username='******', connection_type='invalid', mac_address=TEST_MAC_ADDRESS1, ip_v4_address=None, ip_v6_address=None, ), ) # Expect... mock_device_repository.create_device.assert_not_called() mock_device_repository.update_device.assert_not_called()
def test_bad_username(self, ctx, mock_member_repository: MagicMock, mock_device_repository: MagicMock, device_manager: DeviceManager): # Given... mock_member_repository.search_member_by = MagicMock(return_value=([], 0)) # When... with raises(MemberNotFoundError): device_manager.update_or_create( ctx, mac_address=TEST_MAC_ADDRESS1, req=MutationRequest( owner_username='******', connection_type=DeviceType.Wired, mac_address=TEST_MAC_ADDRESS1, ip_v4_address=None, ip_v6_address=None, ), ) # Expect... mock_device_repository.create_device.assert_not_called() mock_device_repository.update_device.assert_not_called()
def test_happy_path(self, ctx, mock_device_repository: MagicMock, sample_device: Device, device_manager: DeviceManager): # Given... terms = 'blah blah blah' mock_device_repository.search_device_by = MagicMock( return_value=([sample_device], 1)) # When... result, count = device_manager.search(ctx, limit=10, offset=1, username=TEST_USERNAME, terms=terms) # Expect... assert [sample_device] == result assert 1 == count mock_device_repository.search_device_by.assert_called_once_with( ctx, limit=10, offset=1, username=TEST_USERNAME, terms=terms)
def test_create_no_room(self, ctx, mock_device_repository: MagicMock, mock_member_repository: MagicMock, mock_room_repository: MagicMock, sample_member: Member, device_manager: DeviceManager): # Given... # That the owner has no a room. mock_room_repository.search_room_by = MagicMock(return_value=([], 0)) # That the owner exists: mock_member_repository.search_member_by = MagicMock( return_value=([sample_member], 1)) # That the device does not exist in the DB: mock_device_repository.search_device_by = MagicMock(return_value=([], 0)) # When... created = device_manager.update_or_create( ctx, mac_address=TEST_MAC_ADDRESS1, req=MutationRequest( owner_username=TEST_USERNAME, connection_type=DeviceType.Wired, mac_address=TEST_MAC_ADDRESS1, ip_v4_address=None, ip_v6_address=None, ), ) # Expect... assert created is True mock_device_repository.create_device.assert_called_once_with( ctx, mac_address=TEST_MAC_ADDRESS1, owner_username=TEST_USERNAME, connection_type=DeviceType.Wired, ip_v4_address=None, ip_v6_address=None)
def test_update_happy_path(self, ctx, mock_device_repository: MagicMock, mock_member_repository: MagicMock, sample_member: Member, sample_device: Device, device_manager: DeviceManager): # Given... # That the owner exists: mock_member_repository.search_member_by = MagicMock( return_value=([sample_member], 1)) # That the device exists in the DB: mock_device_repository.search_device_by = MagicMock( return_value=([sample_device], 1)) # When... created = device_manager.update_or_create( ctx, mac_address=sample_device.mac_address, req=MutationRequest( owner_username=TEST_USERNAME, connection_type=DeviceType.Wireless, mac_address=sample_device.mac_address, ip_v4_address=None, ip_v6_address=None, ), ) # Expect... assert created is False mock_device_repository.update_device.assert_called_once_with( ctx, sample_device.mac_address, mac_address=sample_device.mac_address, owner_username=TEST_USERNAME, connection_type=DeviceType.Wireless, ip_v4_address=None, ip_v6_address=None)
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