Beispiel #1
0
    def test_none(self):
        mock_null_model = MagicMock()
        service = Service(self.model)
        service.model.objects.none.return_value = mock_null_model

        returned_null_model = service.none()

        service.model.objects.none.assert_called_once_with()
        self.assertEqual(returned_null_model, mock_null_model)
Beispiel #2
0
    def test_none(self):
        mock_null_model = MagicMock()
        service = Service(self.model)
        service.model.objects.none.return_value = mock_null_model

        returned_null_model = service.none()

        service.model.objects.none.assert_called_once_with()
        self.assertEqual(returned_null_model, mock_null_model)
Beispiel #3
0
class ServiceIntegrationTestCase(TestCase):
    """
    Unit testing class for the Service class.
    """
    def setUp(self):
        """
        Initialize testing data.
        """
        self.location_service = LocationService()
        self.service = Service(Target)
        self.service.remove_models()
        self.target_generator = self._generate_attacks()
        self.generator_count = 0

    def _generate_attacks(self):
        while True:
            self.generator_count += 1
            location = self.location_service.create_model(city='FooCity', state='FooState', country='FooCountry', latitude=45, longitude=-72)
            yield self.service.create_model(
                location=location,
                ip='127.0.0.{0}'.format(self.generator_count),
            )

    def test_init(self):
        """
        Test the init method.

        :raise AssertionError: If the test fails.
        """
        self.assertEqual(Target, self.service.model)

    def test_create_model(self):
        """
        Test the create method.

        :raise AssertionError: If the test fails.
        """
        target = next(self.target_generator)

        self.assertTrue(Target, type(target))
        self.assertEqual(45, target.location.latitude)
        self.assertEqual(-72, target.location.longitude)
        self.assertEqual('FooCity', target.location.city)
        self.assertEqual('FooState', target.location.state)
        self.assertEqual('FooCountry', target.location.country)
        self.assertEqual('127.0.0.{0}'.format(self.generator_count), target.ip)

        try:
            self.service.create_model()
        except AttributeError:
            pass
        else:
            raise AssertionError()

    def test_get_model(self):
        """
        Test the get model method.

        :raise AssertionError: If the test fails.
        """
        target_one = next(self.target_generator)
        target_two = next(self.target_generator)
        target_three = next(self.target_generator)

        self.assertEqual(target_two.ip, self.service.get_model(location=target_two.location,
                                                               ip=target_two.ip).ip)
        self.assertEqual(target_two.ip, self.service.get_model(location=[target_two.location,
                                                                         target_three.location],
                                                               ip=target_two.ip).ip)
        self.assertEqual(target_three.ip, self.service.get_model(location=[target_three.location],
                                                                 location__longitude=[
                                                                     target_one.location.longitude,
                                                                     target_three.location.longitude
                                                                 ],
                                                                 ip=target_three.ip).ip)
        self.assertEqual(target_one.ip, self.service.get_model(location=target_one.location,
                                                               location__longitude=[
                                                                   target_two.location.longitude,
                                                                   target_one.location.longitude
                                                               ],
                                                               ip=target_one.ip).ip)
        self.assertEqual(target_one.ip, self.service.get_model(case_sensitive=False,
                                                               location=target_one.location,
                                                               location__longitude=[
                                                                   target_two.location.longitude,
                                                                   target_one.location.longitude
                                                               ],
                                                               ip=target_one.ip.upper()).ip)

        self.assertRaises(AttributeError, lambda: self.service.get_model(ip=[target_one.ip, target_two.ip]))
        self.assertRaises(AttributeError, lambda: self.service.get_model(foo='bar'))

    def test_list_models(self):
        """
        Test the list models method.

        :raise AssertionError: If the test fails.
        """
        target_one = next(self.target_generator)
        target_two = next(self.target_generator)
        target_three = next(self.target_generator)

        self.assertListEqual([target_one, target_two, target_three],
                             list(self.service.list_models(ip=[target_one.ip,
                                                               target_two.ip,
                                                               target_three.ip])))

        self.assertListEqual([target_one],
                             list(self.service.list_models(ip=target_one.ip)))

        self.assertListEqual([target_two],
                             list(self.service.list_models(ip=[target_two.ip],
                                                           location=target_two.location)))

        self.assertListEqual([target_one, target_two],
                             list(self.service.list_models(ip=[target_one.ip, target_two.ip],
                                                           location=[target_one.location, target_two.location])))

        self.assertListEqual([target_one, target_two],
                             list(self.service.list_models(case_sensitive=False,
                                                           ip=[target_one.ip.upper(), target_two.ip.upper()],
                                                           location=[target_one.location,
                                                                     target_two.location])))

        self.assertRaises(AttributeError, lambda: self.service.list_models(foo='bar'))

    def test_update_model(self):
        """
        Test the update model method.

        :raise AssertionError: If the test fails.
        """
        target_one = next(self.target_generator)
        target_two = next(self.target_generator)
        target_three = next(self.target_generator)

        new_location = self.location_service.create_model(city='FOO', state='BAR', country='FOOBAR', latitude=1, longitude=1)
        new_location_two = self.location_service.create_model(city='BAR', state='FOO', country='BARFOO', latitude=2, longitude=2)

        self.service.update_model(filter_args={'ip': target_one.ip},
                                  update_args={'location': new_location})
        self.service.update_model(case_sensitive=False,
                                  filter_args={'ip': target_two.ip.upper(),
                                               'location': [target_one.location, target_two.location]},
                                  update_args={'location': new_location_two})

        self.assertEqual('FooCity', target_one.location.city)
        self.assertEqual(45, target_two.location.latitude)
        self.assertEqual('FooCity', target_two.location.city)

        target_one = self.service.get_model(ip=target_one.ip)
        target_two = self.service.get_model(ip=target_two.ip)

        self.assertEqual(new_location.city, target_one.location.city)
        self.assertEqual(2, target_two.location.latitude)
        self.assertEqual(new_location_two.city, target_two.location.city)

        self.assertRaises(AttributeError, lambda: self.service.update_model(filter_args={'ip': [target_one.ip,
                                                                                                target_three.ip]},
                                                                            update_args={}))
        self.assertRaises(AttributeError, lambda: self.service.update_model(filter_args={'ip': target_three.ip},
                                                                            update_args={'foo': 'bar'}))
        self.assertRaises(AttributeError, lambda: self.service.update_model(filter_args={},
                                                                            update_args={}))

    def test_update_models(self):
        """
        Test the update models method.

        :raise AssertionError: If the test fails.
        """
        target_one = next(self.target_generator)
        target_two = next(self.target_generator)
        target_three = next(self.target_generator)

        new_location = self.location_service.create_model(city='barCity', state='barState', country='barCountry', latitude=3, longitude=3)
        self.service.update_models(filter_args={'ip': [target_one.ip,
                                                       target_two.ip,
                                                       target_three.ip]},
                                   update_args={'location': new_location})

        self.assertEqual('FooCity', target_one.location.city)
        self.assertEqual('FooCity', target_two.location.city)
        self.assertEqual('FooCity', target_three.location.city)

        target_one = self.service.get_model(ip=target_one.ip)
        target_two = self.service.get_model(ip=target_two.ip)
        target_three = self.service.get_model(ip=target_three.ip)

        self.assertEqual(new_location, target_one.location)
        self.assertEqual(new_location, target_two.location)
        self.assertEqual(new_location, target_three.location)

        new_location_two = self.location_service.create_model(city='foofoo', state='barbar', country='foobarfoobar', latitude=4, longitude=4)
        self.service.update_models(case_sensitive=False,
                                   filter_args={'ip': [target_two.ip,
                                                       target_three.ip],
                                                'location': new_location},
                                   update_args={'location': new_location_two})

        target_one = self.service.get_model(ip=target_one.ip)
        target_two = self.service.get_model(ip=target_two.ip)
        target_three = self.service.get_model(ip=target_three.ip)

        self.assertEqual(new_location, target_one.location)
        self.assertEqual(new_location_two, target_two.location)
        self.assertEqual(new_location_two, target_three.location)

        self.assertRaises(AttributeError, lambda: self.service.update_models(filter_args={'ip': target_three.ip},
                                                                             update_args={'foo': 'bar'}))
        self.assertRaises(AttributeError, lambda: self.service.update_models(filter_args={'foo': 'bar'},
                                                                             update_args={}))

    def test_get_latest(self):
        """
        Test the get latest method.

        :raise AssertionError: If the test fails.
        """
        target_one = next(self.target_generator)
        target_two = next(self.target_generator)
        target_three = next(self.target_generator)

        self.assertEqual(target_three, self.service.get_latest(filter_args={}, latest_by_field='ip'))
        self.assertEqual(target_two, self.service.get_latest(filter_args={'ip': [target_one.ip,
                                                                                 target_two.ip]},
                                                             latest_by_field='ip'))
        self.assertEqual(target_one, self.service.get_latest(filter_args={'ip': target_one.ip},
                                                             latest_by_field='ip'))
        self.assertEqual(target_three, self.service.get_latest(case_sensitive=False,
                                                               filter_args={'ip': [target_one.ip.upper(),
                                                                                   target_two.ip.upper(),
                                                                                   target_three.ip.upper()],
                                                                            'location': [
                                                                                target_one.location,
                                                                                target_two.location,
                                                                                target_three.location
                                                                            ]},
                                                               latest_by_field='ip'))

        self.assertIsNone(self.service.get_latest({'ip': 'foo'}, 'ip'))
        self.assertRaises(AttributeError, lambda: self.service.get_latest(filter_args={'foo': 'bar'},
                                                                          latest_by_field='ip'))
        self.assertRaises(AttributeError, lambda: self.service.get_latest(filter_args={},
                                                                          latest_by_field='foo'))

    def test_count_models(self):
        """
        Test the count models method.

        :raise AssertionError: If the test fails.
        """
        target_one = next(self.target_generator)
        target_two = next(self.target_generator)
        target_three = next(self.target_generator)

        self.assertEqual(3, self.service.count_models(ip=[target_one.ip,
                                                          target_two.ip,
                                                          target_three.ip]))

        self.assertEqual(2, self.service.count_models(case_sensitive=False,
                                                      ip=[target_two.ip.upper(),
                                                          target_three.ip.upper()],
                                                      location=[target_two.location,
                                                                target_three.location]))

        self.assertEqual(1, self.service.count_models(ip=[target_one.ip],
                                                      location=target_one.location))

        self.assertEqual(1, self.service.count_models(ip=target_one.ip))

        self.assertRaises(AttributeError, lambda: self.service.count_models(foo='bar'))

    def test_remove_model(self):
        """
        Test the remove model method.

        :raise AssertionError: If the test fails.
        """
        target_one = next(self.target_generator)
        target_two = next(self.target_generator)
        target_three = next(self.target_generator)
        target_four = next(self.target_generator)

        self.service.remove_model(ip=target_one.ip)
        self.service.remove_model(case_sensitive=False, ip=target_two.ip.upper(), location=target_two.location)

        self.assertIsNone(self.service.get_model(ip=target_one.ip))
        self.assertIsNone(self.service.get_model(ip=target_two.ip))

        self.assertRaises(AttributeError,
                          lambda: self.service.remove_model(ip=[target_three.ip, target_four.ip]))

        try:
            self.service.remove_model(ip=target_one.ip)
        except:
            raise AssertionError()

    def test_remove_models(self):
        """
        Test the remove models method.

        :raise AssertionError: If the test fails.
        """
        target_one = next(self.target_generator)
        target_two = next(self.target_generator)
        target_three = next(self.target_generator)
        target_four = next(self.target_generator)

        self.service.remove_models(ip=[target_one.ip, target_two.ip])
        self.service.remove_models(case_sensitive=False,
                                   ip=[target_three.ip.upper(), target_four.ip.upper()],
                                   location=target_three.location)
        self.service.remove_models(ip=target_four.ip)

        self.assertIsNone(self.service.get_model(ip=target_one.ip))
        self.assertIsNone(self.service.get_model(ip=target_two.ip))
        self.assertIsNone(self.service.get_model(ip=target_three.ip))
        self.assertIsNone(self.service.get_model(ip=target_four.ip))

        try:
            self.service.remove_models(ip=[target_one.ip,
                                           target_two.ip,
                                           target_three.ip,
                                           target_four.ip])
        except:
            raise AssertionError()

    def test_none(self):
        """
        Test the none method.

        :raise AssertionError: If the test fails
        """
        none = self.service.none()
        self.assertEqual(0, len(none))
        self.assertEqual(QuerySet, type(none))