<commit_before>import json

from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.test.client import Client

from bulbs.campaigns.models import Campaign
from bulbs.special_coverage.models import SpecialCoverage

from bulbs.utils.test import BaseAPITestCase, JsonEncoder


class SpecialCoverageApiTestCase(BaseAPITestCase):

    def setUp(self):
        # set up client
        super(SpecialCoverageApiTestCase, self).setUp()

        # do client stuff
        self.client = Client()
        self.client.login(username="admin", password="<PASSWORD>")

        # set up a test special coverage
        self.campaign = Campaign.objects.create(
            sponsor_name="<NAME>"
        )
        self.special_coverage = SpecialCoverage.objects.create(
            name="<NAME>",
            slug="jackz-linkz-coveragez",
            description="Stuff about jerky.",
            query={},
            videos=[],
            campaign=self.campaign
        )

        self.special_coverage.save()

    def test_special_coverage_detail(self):
        """Test retrieving a single special coverage object via URL."""

        endpoint = reverse(
            "special-coverage-detail",
            kwargs={"pk": self.special_coverage.id})
        response = self.client.get(endpoint)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data["id"], self.special_coverage.id)

    def test_special_coverage_detail_permissions(self):
        """Ensure there is no unauthorized access to special coverage cms endpoints."""

        # create regular user
        regular_user_name = "regularuser"
        regular_user_pass = "<PASSWORD>"
        get_user_model().objects.create_user(
            regular_user_name,
            "<EMAIL>",
            regular_user_pass
        )
        self.client.login(username=regular_user_name, password=regular_user_pass)

        endpoint = reverse(
            "special-coverage-detail",
            kwargs={"pk": self.special_coverage.id})
        response = self.client.get(endpoint)

        self.assertEqual(response.status_code, 403)

    def test_query_dict_resolves_to_json_object_when_retrieving(self):
        """Check that query string does not resolve to a string when received by
        the frontend."""

        self.special_coverage.query = {"included_ids": [1, 2, 3]}
        self.special_coverage.save()

        endpoint = reverse(
            "special-coverage-detail",
            kwargs={"pk": self.special_coverage.id})
        response = self.client.get(endpoint)

        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.data["query"], dict)

    def test_query_string_resolves_to_dict_when_saving(self):
        """Check that query string resolves back into a string when returned to the
        backend."""

        data_special_coverage = {
            "name": "something",
            "query": {
                "included_ids": [1, 2, 3]
            }
        }

        # post new listing
        self.client.post(
            reverse("special-coverage-list"),
            json.dumps(data_special_coverage, cls=JsonEncoder),
            content_type="application/json"
        )

        self.assertIsInstance(SpecialCoverage.objects.all()[0].query, dict)

    def test_query_string_filters_null_values_when_saving(self):
        """<NAME> comments on tests. I however, do not."""

        data = {
            "name": "<NAME>",
            "query": {
                "pinned_ids": [1, 2, 3, None],
                "excluded_ids": [4, 5, 6, None],
                "groups": ["fun guys", None],
                "included_ids": [7, 8, 9, None]
            }
        }

        resp = self.client.post(
            reverse("special-coverage-list"),
            json.dumps(data, cls=JsonEncoder),
            content_type="application/json"
        )
        query = resp.data.get("query")
        pinned_ids = query.get("pinned_ids")
        excluded_ids = query.get("excluded_ids")
        groups = query.get("groups")
        included_ids = query.get("included_ids")
        self.assertEqual(pinned_ids, [1, 2, 3])
        self.assertEqual(excluded_ids, [4, 5, 6])
        self.assertEqual(groups, ["fun guys"])<commit_msg>Fix broken test exposed by SpecialCoverage fix<commit_after>import json

from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.test.client import Client

from bulbs.campaigns.models import Campaign
from bulbs.special_coverage.models import SpecialCoverage

from bulbs.utils.test import BaseAPITestCase, JsonEncoder


class SpecialCoverageApiTestCase(BaseAPITestCase):

    def setUp(self):
        # set up client
        super(SpecialCoverageApiTestCase, self).setUp()

        # do client stuff
        self.client = Client()
        self.client.login(username="admin", password="<PASSWORD>")

        # set up a test special coverage
        self.campaign = Campaign.objects.create(
            sponsor_name="<NAME>"
        )
        self.special_coverage = SpecialCoverage.objects.create(
            name="<NAME>",
            slug="jackz-linkz-coveragez",
            description="Stuff about jerky.",
            query={},
            videos=[],
            campaign=self.campaign
        )

        self.special_coverage.save()

    def test_special_coverage_detail(self):
        """Test retrieving a single special coverage object via URL."""

        endpoint = reverse(
            "special-coverage-detail",
            kwargs={"pk": self.special_coverage.id})
        response = self.client.get(endpoint)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data["id"], self.special_coverage.id)

    def test_special_coverage_detail_permissions(self):
        """Ensure there is no unauthorized access to special coverage cms endpoints."""

        # create regular user
        regular_user_name = "regularuser"
        regular_user_pass = "<PASSWORD>"
        get_user_model().objects.create_user(
            regular_user_name,
            "<EMAIL>",
            regular_user_pass
        )
        self.client.login(username=regular_user_name, password=regular_user_pass)

        endpoint = reverse(
            "special-coverage-detail",
            kwargs={"pk": self.special_coverage.id})
        response = self.client.get(endpoint)

        self.assertEqual(response.status_code, 403)

    def test_query_dict_resolves_to_json_object_when_retrieving(self):
        """Check that query string does not resolve to a string when received by
        the frontend."""

        self.special_coverage.query = {"included_ids": [1, 2, 3]}
        self.special_coverage.save()

        endpoint = reverse(
            "special-coverage-detail",
            kwargs={"pk": self.special_coverage.id})
        response = self.client.get(endpoint)

        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.data["query"], dict)

    def test_query_string_resolves_to_dict_when_saving(self):
        """Check that query string resolves back into a string when returned to the
        backend."""

        data_special_coverage = {
            "name": "something",
            "query": {
                "included_ids": [1, 2, 3]
            }
        }

        # post new listing
        self.client.post(
            reverse("special-coverage-list"),
            json.dumps(data_special_coverage, cls=JsonEncoder),
            content_type="application/json"
        )

        self.assertIsInstance(SpecialCoverage.objects.all()[0].query, dict)

    def test_query_string_filters_null_values_when_saving(self):
        """<NAME> comments on tests. I however, do not."""

        group = {
            "conditions": [
                {
                    "values": [{
                        "value": 'test',
                        "label": 'test'
                    }],
                    "type": "all",
                    "field": "tag"
                },
            ],
        }
        data = {
            "name": "<NAME>",
            "query": {
                "pinned_ids": [1, 2, 3, None],
                "excluded_ids": [4, 5, 6, None],
                "groups": [group, None],
                "included_ids": [7, 8, 9, None]
            }
        }

        resp = self.client.post(
            reverse("special-coverage-list"),
            json.dumps(data, cls=JsonEncoder),
            content_type="application/json"
        )
        query = resp.data.get("query")
        pinned_ids = query.get("pinned_ids")
        excluded_ids = query.get("excluded_ids")
        groups = query.get("groups")
        included_ids = query.get("included_ids")
        self.assertEqual(pinned_ids, [1, 2, 3])
        self.assertEqual(excluded_ids, [4, 5, 6])
        self.assertEqual(groups, [group])
{'fraction_non_alphanumeric': 0.1104019706639794, 'fraction_numerical': 0.006942111745605196, 'mean_word_length': 2.1865858009275776, 'pattern_counts': {'":': 32, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5441577', 'n_tokens_mistral': 2571, 'n_tokens_neox': 2435, 'n_words': 510}