def test_parse_query__everything(self):
        service = Service(self.model)

        parsed_query = service._parse_query(
            field_one=['foo', 'bar', 'foobar'], field_two=42, field_three=[],
            field_four=['hello', 'world'], field_five='helloworld',
            case_sensitive=False
        )

        expected_query = Q()

        field_one_query = Q()
        field_one_query |= (Q(**{'field_one__iexact': 'foo'}))
        field_one_query |= (Q(**{'field_one__iexact': 'bar'}))
        field_one_query |= (Q(**{'field_one__iexact': 'foobar'}))
        expected_query &= field_one_query

        field_two_query = (Q(**{'field_two': 42}))
        expected_query &= field_two_query

        field_four_query = Q()
        field_four_query |= Q(**{'field_four__iexact': 'hello'})
        field_four_query |= Q(**{'field_four__iexact': 'world'})
        expected_query &= field_four_query

        field_five_query = Q(**{'field_five__iexact': 'helloworld'})
        expected_query &= field_five_query

        self.assertQEqual(parsed_query, expected_query)
    def test_parse_query__everything(self):
        service = Service(self.model)

        parsed_query = service._parse_query(field_one=['foo', 'bar', 'foobar'],
                                            field_two=42,
                                            field_three=[],
                                            field_four=['hello', 'world'],
                                            field_five='helloworld',
                                            case_sensitive=False)

        expected_query = Q()

        field_one_query = Q()
        field_one_query |= (Q(**{'field_one__iexact': 'foo'}))
        field_one_query |= (Q(**{'field_one__iexact': 'bar'}))
        field_one_query |= (Q(**{'field_one__iexact': 'foobar'}))
        expected_query &= field_one_query

        field_two_query = (Q(**{'field_two': 42}))
        expected_query &= field_two_query

        field_four_query = Q()
        field_four_query |= Q(**{'field_four__iexact': 'hello'})
        field_four_query |= Q(**{'field_four__iexact': 'world'})
        expected_query &= field_four_query

        field_five_query = Q(**{'field_five__iexact': 'helloworld'})
        expected_query &= field_five_query

        self.assertQEqual(parsed_query, expected_query)
    def test_parse_query__empty_or(self):
        service = Service(self.model)

        parsed_query = service._parse_query(field=[])

        expected_query = Q()

        self.assertQEqual(parsed_query, expected_query)
    def test_parse_query__empty_or(self):
        service = Service(self.model)

        parsed_query = service._parse_query(field=[])

        expected_query = Q()

        self.assertQEqual(parsed_query, expected_query)
    def test_remove_models(self, mock_list_models):
        mock_listed_models = MagicMock()
        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        service.remove_models(case_sensitive=False, foo='bar')

        service.list_models.assert_called_once_with(case_sensitive=False, foo='bar')
        mock_listed_models.delete.assert_called_once_with()
    def test_parse_query__case_insensitive(self):
        service = Service(self.model)

        parsed_query = service._parse_query(field='foo', case_sensitive=False)

        expected_query = Q()
        expected_query &= Q(**{'field__iexact': 'foo'})

        self.assertQEqual(parsed_query, expected_query)
    def test_parse_query__single_field(self):
        service = Service(self.model)

        parsed_query = service._parse_query(field='foo')

        expected_query = Q()
        expected_query &= Q(**{'field': 'foo'})

        self.assertQEqual(parsed_query, expected_query)
    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)
    def test_parse_query__single_field(self):
        service = Service(self.model)

        parsed_query = service._parse_query(field='foo')

        expected_query = Q()
        expected_query &= Q(**{'field': 'foo'})

        self.assertQEqual(parsed_query, expected_query)
    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)
Exemple #11
0
    def test_parse_query__case_insensitive_non_string(self):
        service = Service(self.model)

        parsed_query = service._parse_query(field=42, case_sensitive=False)

        expected_query = Q()
        expected_query &= Q(**{'field': 42})

        self.assertQEqual(parsed_query, expected_query)
Exemple #12
0
 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 test_update_models_success(self, mock_list_models):
        mock_listed_models = MagicMock()
        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        service.update_models({'filter': 'foo'}, {'update': 'bar'}, case_sensitive=False)

        service.list_models.assert_called_once_with(case_sensitive=False, filter='foo')
        mock_listed_models.update.assert_called_once_with(update='bar')
    def test_get_model__object_does_not_exist(self, mock_parse_query):
        service = Service(self.model)
        service._parse_query.return_value = 'foobar_query'
        service.model.objects.get.side_effect = ObjectDoesNotExist

        returned_model = service.get_model(case_sensitive=True, foo='foo', bar='bar')

        service._parse_query.assert_called_once_with(case_sensitive=True, foo='foo', bar='bar')
        service.model.objects.get.assert_called_once_with(service._parse_query.return_value)
        self.assertIsNone(returned_model)
Exemple #15
0
    def test_parse_query__and(self):
        service = Service(self.model)

        parsed_query = service._parse_query(field_one='foo', field_two='bar')

        expected_query = Q()
        expected_query &= Q(**{'field_one': 'foo'})
        expected_query &= Q(**{'field_two': 'bar'})

        self.assertQEqual(parsed_query, expected_query)
Exemple #16
0
    def test_remove_models(self, mock_list_models):
        mock_listed_models = MagicMock()
        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        service.remove_models(case_sensitive=False, foo='bar')

        service.list_models.assert_called_once_with(case_sensitive=False,
                                                    foo='bar')
        mock_listed_models.delete.assert_called_once_with()
    def test_list_models__validation_error(self, mock_parse_query):
        service = Service(self.model)
        service.model.objects.filter.side_effect = ValidationError('mock validation error')
        service._parse_query.return_value = 'foobar_query'

        with self.assertRaises(AttributeError):
            service.list_models(case_sensitive=False, foo='foo', bar='bar')

        service._parse_query.assert_called_once_with(case_sensitive=False, foo='foo', bar='bar')
        service.model.objects.filter.assert_called_once_with('foobar_query')
    def test_get_model__field_error(self, mock_parse_query):
        service = Service(self.model)
        service._parse_query.return_value = 'foobar_query'
        service.model.objects.get.side_effect = FieldError

        with self.assertRaises(AttributeError):
            service.get_model(case_sensitive=True, foo='foo', bar='bar')

        service._parse_query.assert_called_once_with(case_sensitive=True, foo='foo', bar='bar')
        service.model.objects.get.assert_called_once_with(service._parse_query.return_value)
    def test_parse_query__case_insensitive_non_string(self):
        service = Service(self.model)

        parsed_query = service._parse_query(
            field=42, case_sensitive=False
        )

        expected_query = Q()
        expected_query &= Q(**{'field': 42})

        self.assertQEqual(parsed_query, expected_query)
    def test_list_models_success(self, mock_parse_query):
        mock_models = MagicMock()
        service = Service(self.model)
        service.model.objects.filter.return_value = mock_models
        service._parse_query.return_value = 'foobar_query'

        returned_models = service.list_models(case_sensitive=False, foo='foo', bar='bar')

        service._parse_query.assert_called_once_with(case_sensitive=False, foo='foo', bar='bar')
        service.model.objects.filter.assert_called_once_with('foobar_query')
        self.assertEqual(returned_models, mock_models)
    def test_get_model_success(self, mock_parse_query):
        mock_model = MagicMock()
        service = Service(self.model)
        service._parse_query.return_value = 'foobar_query'
        service.model.objects.get.return_value = mock_model

        returned_model = service.get_model(case_sensitive=True, foo='foo', bar='bar')

        service._parse_query.assert_called_once_with(case_sensitive=True, foo='foo', bar='bar')
        service.model.objects.get.assert_called_once_with(service._parse_query.return_value)
        self.assertEqual(returned_model, mock_model)
Exemple #22
0
    def test_update_models_success(self, mock_list_models):
        mock_listed_models = MagicMock()
        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        service.update_models({'filter': 'foo'}, {'update': 'bar'},
                              case_sensitive=False)

        service.list_models.assert_called_once_with(case_sensitive=False,
                                                    filter='foo')
        mock_listed_models.update.assert_called_once_with(update='bar')
    def test_parse_query__case_insensitive(self):
        service = Service(self.model)

        parsed_query = service._parse_query(
            field='foo', case_sensitive=False
        )

        expected_query = Q()
        expected_query &= Q(**{'field__iexact': 'foo'})

        self.assertQEqual(parsed_query, expected_query)
Exemple #24
0
    def test_list_models__field_error(self, mock_parse_query):
        service = Service(self.model)
        service.model.objects.filter.side_effect = FieldError
        service._parse_query.return_value = 'foobar_query'

        with self.assertRaises(AttributeError):
            service.list_models(case_sensitive=False, foo='foo', bar='bar')

        service._parse_query.assert_called_once_with(case_sensitive=False,
                                                     foo='foo',
                                                     bar='bar')
        service.model.objects.filter.assert_called_once_with('foobar_query')
Exemple #25
0
    def test_parse_query__or(self):
        service = Service(self.model)

        parsed_query = service._parse_query(field=['foo', 'bar'])

        expected_query = Q()
        or_query = Q()
        or_query |= Q(**{'field': 'foo'})
        or_query |= Q(**{'field': 'bar'})
        expected_query &= or_query

        self.assertQEqual(parsed_query, expected_query)
    def test_parse_query__and(self):
        service = Service(self.model)

        parsed_query = service._parse_query(
            field_one='foo', field_two='bar'
        )

        expected_query = Q()
        expected_query &= Q(**{'field_one': 'foo'})
        expected_query &= Q(**{'field_two': 'bar'})

        self.assertQEqual(parsed_query, expected_query)
    def test_count_models(self, mock_list_models):
        mock_listed_models = MagicMock()
        mock_listed_models.count.return_value = 42
        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        returned_count = service.count_models(foo='bar')

        service.list_models.assert_called_once_with(case_sensitive=True, foo='bar')
        mock_listed_models.count.assert_called_once_with()

        self.assertEqual(returned_count, 42)
    def test_get_latest__field_error(self, mock_list_models):
        mock_listed_models = MagicMock()
        mock_listed_models.latest.side_effect = FieldError

        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        with self.assertRaises(AttributeError):
            service.get_latest({'filter': 'foo'}, latest_by_field='some_field')

        service.list_models.assert_called_once_with(case_sensitive=True, filter='foo')
        mock_listed_models.latest.assert_called_once_with('some_field')
    def test_remove_model__object_does_not_exist(self, mock_get_model):
        mock_model = MagicMock()
        mock_model.pk = 42

        service = Service(self.model)
        service.get_model.return_value = mock_model
        service.model.objects.get.side_effect = ObjectDoesNotExist

        service.remove_model(foo='bar')

        service.get_model.assert_called_once_with(case_sensitive=True, foo='bar')
        service.model.objects.get.assert_called_once_with(pk=42)
    def test_get_latest_success(self, mock_list_models):
        mock_model = MagicMock()
        mock_listed_models = MagicMock()
        mock_listed_models.latest.return_value = mock_model
        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        returned_model = service.get_latest({'filter': 'foo'}, latest_by_field='some_field')

        service.list_models.assert_called_once_with(case_sensitive=True, filter='foo')
        mock_listed_models.latest.assert_called_once_with('some_field')
        self.assertEqual(returned_model, mock_model)
    def test_get_latest__object_does_not_exist(self, mock_list_models):
        mock_listed_models = MagicMock()
        mock_listed_models.latest.side_effect = ObjectDoesNotExist

        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        returned_model = service.get_latest({'filter': 'foo'}, latest_by_field='some_field')

        service.list_models.assert_called_once_with(case_sensitive=True, filter='foo')
        mock_listed_models.latest.assert_called_once_with('some_field')
        self.assertIsNone(returned_model)
    def test_remove_model__attribute_error(self, mock_get_model):
        mock_model = MagicMock()
        mock_model.pk = 42

        service = Service(self.model)
        service.get_model.return_value = mock_model
        service.model.objects.get.side_effect = AttributeError

        service.remove_model(foo='bar')

        service.get_model.assert_called_once_with(case_sensitive=True, foo='bar')
        service.model.objects.get.assert_called_once_with(pk=42)
    def test_update_models_fail(self, mock_list_models):
        mock_listed_models = MagicMock()
        mock_listed_models.update.side_effect = FieldDoesNotExist

        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        with self.assertRaises(AttributeError):
            service.update_models({'filter': 'foo'}, {'update': 'bar'}, case_sensitive=False)

        service.list_models.assert_called_once_with(case_sensitive=False, filter='foo')
        mock_listed_models.update.assert_called_once_with(update='bar')
Exemple #34
0
    def test_get_latest__field_error(self, mock_list_models):
        mock_listed_models = MagicMock()
        mock_listed_models.latest.side_effect = FieldError

        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        with self.assertRaises(AttributeError):
            service.get_latest({'filter': 'foo'}, latest_by_field='some_field')

        service.list_models.assert_called_once_with(case_sensitive=True,
                                                    filter='foo')
        mock_listed_models.latest.assert_called_once_with('some_field')
Exemple #35
0
    def test_create_model_fail(self):
        mock_created_model = MagicMock()
        mock_created_model.save.side_effect = IntegrityError

        service = Service(self.model)
        service.model.create.return_value = mock_created_model

        with self.assertRaises(AttributeError):
            service.create_model(foo='bar')

        service.model.create.assert_called_once_with(foo='bar')
        mock_created_model.clean.assert_called_once_with()
        mock_created_model.save.assert_called_once_with()
Exemple #36
0
    def test_count_models(self, mock_list_models):
        mock_listed_models = MagicMock()
        mock_listed_models.count.return_value = 42
        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        returned_count = service.count_models(foo='bar')

        service.list_models.assert_called_once_with(case_sensitive=True,
                                                    foo='bar')
        mock_listed_models.count.assert_called_once_with()

        self.assertEqual(returned_count, 42)
Exemple #37
0
    def test_remove_model__object_does_not_exist(self, mock_get_model):
        mock_model = MagicMock()
        mock_model.pk = 42

        service = Service(self.model)
        service.get_model.return_value = mock_model
        service.model.objects.get.side_effect = ObjectDoesNotExist

        service.remove_model(foo='bar')

        service.get_model.assert_called_once_with(case_sensitive=True,
                                                  foo='bar')
        service.model.objects.get.assert_called_once_with(pk=42)
    def test_create_model_fail(self):
        mock_created_model = MagicMock()
        mock_created_model.save.side_effect = IntegrityError

        service = Service(self.model)
        service.model.create.return_value = mock_created_model

        with self.assertRaises(AttributeError):
            service.create_model(foo='bar')

        service.model.create.assert_called_once_with(foo='bar')
        mock_created_model.clean.assert_called_once_with()
        mock_created_model.save.assert_called_once_with()
Exemple #39
0
    def test_remove_model__attribute_error(self, mock_get_model):
        mock_model = MagicMock()
        mock_model.pk = 42

        service = Service(self.model)
        service.get_model.return_value = mock_model
        service.model.objects.get.side_effect = AttributeError

        service.remove_model(foo='bar')

        service.get_model.assert_called_once_with(case_sensitive=True,
                                                  foo='bar')
        service.model.objects.get.assert_called_once_with(pk=42)
    def test_remove_model_success(self, mock_get_model):
        mock_model = MagicMock()
        mock_model.pk = 42
        mock_gotten_model = MagicMock()
        service = Service(self.model)
        service.get_model.return_value = mock_model
        service.model.objects.get.return_value = mock_gotten_model

        service.remove_model(foo='bar')

        service.get_model.assert_called_once_with(case_sensitive=True, foo='bar')
        service.model.objects.get.assert_called_once_with(pk=42)
        mock_gotten_model.delete.assert_called_once_with()
    def test_create_model_success(self):
        mock_created_model = MagicMock()

        service = Service(self.model)
        service.model.create.return_value = mock_created_model

        created_model = service.create_model(foo='bar')

        service.model.create.assert_called_once_with(foo='bar')
        mock_created_model.clean.assert_called_once_with()
        mock_created_model.save.assert_called_once_with()

        self.assertEqual(created_model, mock_created_model)
Exemple #42
0
    def test_get_model__field_error(self, mock_parse_query):
        service = Service(self.model)
        service._parse_query.return_value = 'foobar_query'
        service.model.objects.get.side_effect = FieldError

        with self.assertRaises(AttributeError):
            service.get_model(case_sensitive=True, foo='foo', bar='bar')

        service._parse_query.assert_called_once_with(case_sensitive=True,
                                                     foo='foo',
                                                     bar='bar')
        service.model.objects.get.assert_called_once_with(
            service._parse_query.return_value)
Exemple #43
0
    def test_create_model_success(self):
        mock_created_model = MagicMock()

        service = Service(self.model)
        service.model.create.return_value = mock_created_model

        created_model = service.create_model(foo='bar')

        service.model.create.assert_called_once_with(foo='bar')
        mock_created_model.clean.assert_called_once_with()
        mock_created_model.save.assert_called_once_with()

        self.assertEqual(created_model, mock_created_model)
    def test_parse_query__or(self):
        service = Service(self.model)

        parsed_query = service._parse_query(
            field=['foo', 'bar']
        )

        expected_query = Q()
        or_query = Q()
        or_query |= Q(**{'field': 'foo'})
        or_query |= Q(**{'field': 'bar'})
        expected_query &= or_query

        self.assertQEqual(parsed_query, expected_query)
Exemple #45
0
    def test_get_latest_success(self, mock_list_models):
        mock_model = MagicMock()
        mock_listed_models = MagicMock()
        mock_listed_models.latest.return_value = mock_model
        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        returned_model = service.get_latest({'filter': 'foo'},
                                            latest_by_field='some_field')

        service.list_models.assert_called_once_with(case_sensitive=True,
                                                    filter='foo')
        mock_listed_models.latest.assert_called_once_with('some_field')
        self.assertEqual(returned_model, mock_model)
Exemple #46
0
    def test_remove_model_success(self, mock_get_model):
        mock_model = MagicMock()
        mock_model.pk = 42
        mock_gotten_model = MagicMock()
        service = Service(self.model)
        service.get_model.return_value = mock_model
        service.model.objects.get.return_value = mock_gotten_model

        service.remove_model(foo='bar')

        service.get_model.assert_called_once_with(case_sensitive=True,
                                                  foo='bar')
        service.model.objects.get.assert_called_once_with(pk=42)
        mock_gotten_model.delete.assert_called_once_with()
Exemple #47
0
    def test_get_latest__object_does_not_exist(self, mock_list_models):
        mock_listed_models = MagicMock()
        mock_listed_models.latest.side_effect = ObjectDoesNotExist

        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        returned_model = service.get_latest({'filter': 'foo'},
                                            latest_by_field='some_field')

        service.list_models.assert_called_once_with(case_sensitive=True,
                                                    filter='foo')
        mock_listed_models.latest.assert_called_once_with('some_field')
        self.assertIsNone(returned_model)
Exemple #48
0
    def test_update_models_fail(self, mock_list_models):
        mock_listed_models = MagicMock()
        mock_listed_models.update.side_effect = FieldDoesNotExist

        service = Service(self.model)
        service.list_models.return_value = mock_listed_models

        with self.assertRaises(AttributeError):
            service.update_models({'filter': 'foo'}, {'update': 'bar'},
                                  case_sensitive=False)

        service.list_models.assert_called_once_with(case_sensitive=False,
                                                    filter='foo')
        mock_listed_models.update.assert_called_once_with(update='bar')
Exemple #49
0
    def test_list_models_success(self, mock_parse_query):
        mock_models = MagicMock()
        service = Service(self.model)
        service.model.objects.filter.return_value = mock_models
        service._parse_query.return_value = 'foobar_query'

        returned_models = service.list_models(case_sensitive=False,
                                              foo='foo',
                                              bar='bar')

        service._parse_query.assert_called_once_with(case_sensitive=False,
                                                     foo='foo',
                                                     bar='bar')
        service.model.objects.filter.assert_called_once_with('foobar_query')
        self.assertEqual(returned_models, mock_models)
Exemple #50
0
    def test_get_model__object_does_not_exist(self, mock_parse_query):
        service = Service(self.model)
        service._parse_query.return_value = 'foobar_query'
        service.model.objects.get.side_effect = ObjectDoesNotExist

        returned_model = service.get_model(case_sensitive=True,
                                           foo='foo',
                                           bar='bar')

        service._parse_query.assert_called_once_with(case_sensitive=True,
                                                     foo='foo',
                                                     bar='bar')
        service.model.objects.get.assert_called_once_with(
            service._parse_query.return_value)
        self.assertIsNone(returned_model)
    def test_update_model_success(self, mock_get_model):
        mock_model = MagicMock()
        mock_model.pk.return_value = 1

        mock_filter_object = MagicMock()

        service = Service(self.model)
        service.get_model.return_value = mock_model
        service.model.objects.filter.return_value = mock_filter_object

        service.update_model({'filter': 'foo'}, {'update': 'bar'}, case_sensitive=False)

        service.get_model.assert_called_once_with(case_sensitive=False, filter='foo')
        service.model.objects.filter.assert_called_once_with(pk=mock_model.pk)
        mock_filter_object.update.assert_called_once_with(update='bar')
Exemple #52
0
    def test_get_model_success(self, mock_parse_query):
        mock_model = MagicMock()
        service = Service(self.model)
        service._parse_query.return_value = 'foobar_query'
        service.model.objects.get.return_value = mock_model

        returned_model = service.get_model(case_sensitive=True,
                                           foo='foo',
                                           bar='bar')

        service._parse_query.assert_called_once_with(case_sensitive=True,
                                                     foo='foo',
                                                     bar='bar')
        service.model.objects.get.assert_called_once_with(
            service._parse_query.return_value)
        self.assertEqual(returned_model, mock_model)
    def test_update_model_fail(self, mock_get_model):
        mock_model = MagicMock()
        mock_model.pk.return_value = 1

        mock_filter_object = MagicMock()
        mock_filter_object.update.side_effect = FieldDoesNotExist

        service = Service(self.model)
        service.get_model.return_value = mock_model
        service.model.objects.filter.return_value = mock_filter_object

        with self.assertRaises(AttributeError):
            service.update_model({'filter': 'foo'}, {'update': 'bar'}, case_sensitive=False)

        service.get_model.assert_called_once_with(case_sensitive=False, filter='foo')
        service.model.objects.filter.assert_called_once_with(pk=mock_model.pk)
        mock_filter_object.update.assert_called_once_with(update='bar')
Exemple #54
0
    def test_parse_query__and_or(self):
        service = Service(self.model)

        parsed_query = service._parse_query(field_one=['foo', 'bar', 'foobar'],
                                            field_two=42)

        expected_query = Q()
        field_one_query = Q()
        field_one_query |= (Q(**{'field_one': 'foo'}))
        field_one_query |= (Q(**{'field_one': 'bar'}))
        field_one_query |= (Q(**{'field_one': 'foobar'}))
        expected_query &= field_one_query

        field_two_query = (Q(**{'field_two': 42}))
        expected_query &= field_two_query

        self.assertQEqual(parsed_query, expected_query)
Exemple #55
0
    def test_update_model_success(self, mock_get_model):
        mock_model = MagicMock()
        mock_model.pk.return_value = 1

        mock_filter_object = MagicMock()

        service = Service(self.model)
        service.get_model.return_value = mock_model
        service.model.objects.filter.return_value = mock_filter_object

        service.update_model({'filter': 'foo'}, {'update': 'bar'},
                             case_sensitive=False)

        service.get_model.assert_called_once_with(case_sensitive=False,
                                                  filter='foo')
        service.model.objects.filter.assert_called_once_with(pk=mock_model.pk)
        mock_filter_object.update.assert_called_once_with(update='bar')
    def test_parse_query__and_or(self):
        service = Service(self.model)

        parsed_query = service._parse_query(
            field_one=['foo', 'bar', 'foobar'], field_two=42
        )

        expected_query = Q()
        field_one_query = Q()
        field_one_query |= (Q(**{'field_one': 'foo'}))
        field_one_query |= (Q(**{'field_one': 'bar'}))
        field_one_query |= (Q(**{'field_one': 'foobar'}))
        expected_query &= field_one_query

        field_two_query = (Q(**{'field_two': 42}))
        expected_query &= field_two_query

        self.assertQEqual(parsed_query, expected_query)
Exemple #57
0
    def test_update_model_fail(self, mock_get_model):
        mock_model = MagicMock()
        mock_model.pk.return_value = 1

        mock_filter_object = MagicMock()
        mock_filter_object.update.side_effect = FieldDoesNotExist

        service = Service(self.model)
        service.get_model.return_value = mock_model
        service.model.objects.filter.return_value = mock_filter_object

        with self.assertRaises(AttributeError):
            service.update_model({'filter': 'foo'}, {'update': 'bar'},
                                 case_sensitive=False)

        service.get_model.assert_called_once_with(case_sensitive=False,
                                                  filter='foo')
        service.model.objects.filter.assert_called_once_with(pk=mock_model.pk)
        mock_filter_object.update.assert_called_once_with(update='bar')
Exemple #58
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))