import json

from fjord.base.tests import (
    AnalyzerProfileFactory,
    LocalizingClient,
    TestCase,
    reverse,
)

from fjord.feedback.tests import ResponseFactory


class TestTriggerRuleMatchViewAPI(TestCase):
    client_class = LocalizingClient

    def test_not_logged_in(self):
        data = {}
        resp = self.client.post(
            reverse('triggerrule-match'),
            content_type='application/json',
            data=json.dumps(data)
        )
        assert resp.status_code == 403

    def test_empty_tr(self):
        feedback_responses = ResponseFactory.create_batch(5)

        jane = AnalyzerProfileFactory().user
        self.client_login_user(jane)

        data = {
            'locales': [],
            'products': [],
            'versions': [],
            'keywords': [],
            'url_exists': None
        }
        resp = self.client.post(
            reverse('triggerrule-match'),
            content_type='application/json',
            data=json.dumps(data)
        )
        assert resp.status_code == 200
        # Note: This matches everything because it's an empty rule.
        assert (
            [item['id'] for item in json.loads(resp.content)['results']] ==
            [fr.id for fr in reversed(feedback_responses)]
        )

    def test_invalid_data(self):
        jane = AnalyzerProfileFactory().user
        self.client_login_user(jane)

        resp = self.client.post(
            reverse('triggerrule-match'),
            content_type='application/json',
            data='[}'
        )
        assert resp.status_code == 400

    def test_locales(self):
        ResponseFactory(locale=u'fr')
        es_resp = ResponseFactory(locale=u'es')
        enus_resp = ResponseFactory(locale=u'en-US')

        jane = AnalyzerProfileFactory().user
        self.client_login_user(jane)

        # Test one locale
        data = {
            'locales': [u'en-US'],
            'products': [],
            'versions': [],
            'keywords': [],
            'url_exists': None
        }
        resp = self.client.post(
            reverse('triggerrule-match'),
            content_type='application/json',
            data=json.dumps(data)
        )
        assert resp.status_code == 200
        assert (
            [item['id'] for item in json.loads(resp.content)['results']] ==
            [enus_resp.id]
        )

        # Test two
        data['locales'] = [u'en-US', u'es']
        resp = self.client.post(
            reverse('triggerrule-match'),
            content_type='application/json',
            data=json.dumps(data)
        )
        assert resp.status_code == 200
        assert (
            [item['id'] for item in json.loads(resp.content)['results']] ==
            [enus_resp.id, es_resp.id]
        )

    def test_products(self):
        fx_resp = ResponseFactory(product=u'Firefox')
        fxa_resp = ResponseFactory(product=u'Firefox for Android')
        ResponseFactory(product=u'Loop')

        jane = AnalyzerProfileFactory().user
        self.client_login_user(jane)