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 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_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)