<commit_before>import json
from io import BytesIO

from django.conf import settings
from django.core.cache import cache
from django.test.utils import override_settings

import mock
from nose import SkipTest
from nose.tools import eq_
from pyquery import PyQuery as pq
from test_utils import RequestFactory

import amo
import amo.tests
from amo.urlresolvers import reverse

from mkt.site.urls import template_plus_xframe
from mkt.webapps.models import Webapp


class Test403(amo.tests.TestCase):
    fixtures = ['base/users', 'webapps/337141-steamcube']

    def setUp(self):
        assert self.client.login(username='<EMAIL>',
                                 password='password')

    def _test_403(self, url):
        res = self.client.get(url, follow=True)
        eq_(res.status_code, 403)
        self.assertTemplateUsed(res, 'site/403.html')

    def test_403_admin(self):
        self._test_403('/admin')

    def test_403_devhub(self):
        assert self.client.login(username='<EMAIL>',
                                 password='password')
        app = Webapp.objects.get(pk=337141)
        self._test_403(app.get_dev_url('edit'))

    def test_403_reviewer(self):
        self._test_403('/reviewers')


class Test404(amo.tests.TestCase):
    fixtures = ['webapps/337141-steamcube']

    def _test_404(self, url):
        r = self.client.get(url, follow=True)
        eq_(r.status_code, 404)
        self.assertTemplateUsed(r, 'site/404.html')
        return r

    def test_404(self):
        self._test_404('/xxx')

    def test_404_devhub(self):
        self._test_404('/developers/xxx')

    def test_404_consumer_legacy(self):
        self._test_404('/xxx')

    def test_404_consumer(self):
        self._test_404('/xxx')

    def test_404_api(self):
        res = self.client.get('/api/this-should-never-work/')
        eq_(res.status_code, 404)
        eq_(res.content, '')


class TestManifest(amo.tests.TestCase):

    def setUp(self):
        self.url = reverse('manifest.webapp')

    @mock.patch('mkt.carriers.carriers.CARRIERS', {'boop': 'boop'})
    @mock.patch.object(settings, 'WEBAPP_MANIFEST_NAME', 'Firefox Marketplace')
    @mock.patch('mkt.site.views.get_carrier')
    def test_manifest(self, mock_get_carrier):
        mock_get_carrier.return_value = 'boop'
        response = self.client.get(reverse('manifest.webapp'))
        eq_(response.status_code, 200)
        eq_(response['Content-Type'], 'application/x-web-app-manifest+json')
        content = json.loads(response.content)
        eq_(content['name'], 'Firefox Marketplace')
        url = reverse('manifest.webapp')
        assert 'en-US' not in url and 'firefox' not in url
        eq_(content['launch_path'], '/?carrier=boop')

    @mock.patch('mkt.carriers.carriers.CARRIERS', [])
    def test_manifest_no_carrier(self):
        response = self.client.get(self.url)
        eq_(response.status_code, 200)
        content = json.loads(response.content)
        assert 'launch_path' not in content

    @mock.patch.object(settings, 'WEBAPP_MANIFEST_NAME', 'Mozilla Fruitstand')
    def test_manifest_name(self):
        response = self.client.get(self.url)
        eq_(response.status_code, 200)
        content = json.loads(response.content)
        eq_(content['name'], 'Mozilla Fruitstand')

    @mock.patch.object(settings, 'USE_APPCACHE', True)
    def test_manifest_appcache(self):
        response = self.client.get(self.url)
        eq_(response.status_code, 200)
        content = json.loads(response.content)
        eq_(content['appcache_path'], reverse('django_appcache.manifest'))

    @mock.patch.object(settings, 'USE_APPCACHE', False)
    def test_manifest_no_appcache(self):
        response = self.client.get(self.url)
        eq_(response.status_code, 200)
        content = json.loads(response.content)
        assert 'appcache_path' not in content, 'Unexpected: `appcache_path`'

    def test_manifest_orientation(self):
        response = self.client.get(self.url)
        eq_(response.status_code, 200)
        content = json.loads(response.content)
        eq_(content['orientation'], ['portrait-primary'])

    def test_manifest_etag(self):
        resp = self.client.get(self.url)
        etag = resp.get('Etag')
        assert etag, 'Missing ETag'

        # Trigger a change to the manifest by changing the name.
        with self.settings(WEBAPP_MANIFEST_NAME='Mozilla Fruitstand'):
            resp = self.client.get(self.url)
            assert resp.get('Etag'), 'Missing ETag'
            self.assertNotEqual(etag, resp.get('Etag'))

    def test_conditional_get_manifest(self):
        resp = self.client.get(self.url)
        etag = resp.get('Etag')

        resp = self.client.get(self.url, HTTP_IF_NONE_MATCH=str(etag))
        eq_(resp.content, '')
        eq_(resp.status_code, 304)


class TestPackageMinifest(amo.tests.TestCase):

    def setUp(self):
        self.url = reverse('minifest.webapp')

    @mock.patch.object(settings, 'WEBAPP_MANIFEST_NAME', 'Firefox Marketplace')
    def test_minifest(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        content = json.loads(res.content)
        eq_(content['name'], 'Firefox Marketplace')
        assert 'package_path' in content
        assert res.get('Etag'), 'Missing ETag'

    def test_minifest_etag(self):
        res = self.client.get(self.url)
        etag = res.get('Etag')

        # Trigger a change to the minifest by changing the name.
        with self.settings(WEBAPP_MANIFEST_NAME='Mozilla Fruitstand'):
            res = self.client.get(self.url)
            assert etag, 'Missing ETag'
            self.assertNotEqual(etag, res.get('Etag'))
<commit_msg>fix packaged zip tests (bug 863508)<commit_after>import json
from io import BytesIO

from django.conf import settings
from django.core.cache import cache
from django.test.utils import override_settings

import mock
from nose import SkipTest
from nose.tools import eq_
from pyquery import PyQuery as pq
from test_utils import RequestFactory

import amo
import amo.tests
from amo.urlresolvers import reverse

from mkt.site.views import get_package_path
from mkt.site.urls import template_plus_xframe
from mkt.webapps.models import Webapp


class Test403(amo.tests.TestCase):
    fixtures = ['base/users', 'webapps/337141-steamcube']

    def setUp(self):
        assert self.client.login(username='<EMAIL>',
                                 password='password')

    def _test_403(self, url):
        res = self.client.get(url, follow=True)
        eq_(res.status_code, 403)
        self.assertTemplateUsed(res, 'site/403.html')

    def test_403_admin(self):
        self._test_403('/admin')

    def test_403_devhub(self):
        assert self.client.login(username='<EMAIL>',
                                 password='password')
        app = Webapp.objects.get(pk=337141)
        self._test_403(app.get_dev_url('edit'))

    def test_403_reviewer(self):
        self._test_403('/reviewers')


class Test404(amo.tests.TestCase):
    fixtures = ['webapps/337141-steamcube']

    def _test_404(self, url):
        r = self.client.get(url, follow=True)
        eq_(r.status_code, 404)
        self.assertTemplateUsed(r, 'site/404.html')
        return r

    def test_404(self):
        self._test_404('/xxx')

    def test_404_devhub(self):
        self._test_404('/developers/xxx')

    def test_404_consumer_legacy(self):
        self._test_404('/xxx')

    def test_404_consumer(self):
        self._test_404('/xxx')

    def test_404_api(self):
        res = self.client.get('/api/this-should-never-work/')
        eq_(res.status_code, 404)
        eq_(res.content, '')


class TestManifest(amo.tests.TestCase):

    def setUp(self):
        self.url = reverse('manifest.webapp')

    @mock.patch('mkt.carriers.carriers.CARRIERS', {'boop': 'boop'})
    @mock.patch.object(settings, 'WEBAPP_MANIFEST_NAME', 'Firefox Marketplace')
    @mock.patch('mkt.site.views.get_carrier')
    def test_manifest(self, mock_get_carrier):
        mock_get_carrier.return_value = 'boop'
        response = self.client.get(reverse('manifest.webapp'))
        eq_(response.status_code, 200)
        eq_(response['Content-Type'], 'application/x-web-app-manifest+json')
        content = json.loads(response.content)
        eq_(content['name'], 'Firefox Marketplace')
        url = reverse('manifest.webapp')
        assert 'en-US' not in url and 'firefox' not in url
        eq_(content['launch_path'], '/?carrier=boop')

    @mock.patch('mkt.carriers.carriers.CARRIERS', [])
    def test_manifest_no_carrier(self):
        response = self.client.get(self.url)
        eq_(response.status_code, 200)
        content = json.loads(response.content)
        assert 'launch_path' not in content

    @mock.patch.object(settings, 'WEBAPP_MANIFEST_NAME', 'Mozilla Fruitstand')
    def test_manifest_name(self):
        response = self.client.get(self.url)
        eq_(response.status_code, 200)
        content = json.loads(response.content)
        eq_(content['name'], 'Mozilla Fruitstand')

    @mock.patch.object(settings, 'USE_APPCACHE', True)
    def test_manifest_appcache(self):
        response = self.client.get(self.url)
        eq_(response.status_code, 200)
        content = json.loads(response.content)
        eq_(content['appcache_path'], reverse('django_appcache.manifest'))

    @mock.patch.object(settings, 'USE_APPCACHE', False)
    def test_manifest_no_appcache(self):
        response = self.client.get(self.url)
        eq_(response.status_code, 200)
        content = json.loads(response.content)
        assert 'appcache_path' not in content, 'Unexpected: `appcache_path`'

    def test_manifest_orientation(self):
        response = self.client.get(self.url)
        eq_(response.status_code, 200)
        content = json.loads(response.content)
        eq_(content['orientation'], ['portrait-primary'])

    def test_manifest_etag(self):
        resp = self.client.get(self.url)
        etag = resp.get('Etag')
        assert etag, 'Missing ETag'

        # Trigger a change to the manifest by changing the name.
        with self.settings(WEBAPP_MANIFEST_NAME='Mozilla Fruitstand'):
            resp = self.client.get(self.url)
            assert resp.get('Etag'), 'Missing ETag'
            self.assertNotEqual(etag, resp.get('Etag'))

    def test_conditional_get_manifest(self):
        resp = self.client.get(self.url)
        etag = resp.get('Etag')

        resp = self.client.get(self.url, HTTP_IF_NONE_MATCH=str(etag))
        eq_(resp.content, '')
        eq_(resp.status_code, 304)


class TestPackageMinifest(amo.tests.TestCase):

    def setUp(self):
        self.url = reverse('minifest.webapp')
        get_package_path_patch = mock.patch('mkt.site.views.get_package_path')
        self.get_package_path = get_package_path_patch.start()
        self.get_package_path.return_value = get_package_path(signed=False)
        self.addCleanup(get_package_path_patch.stop)

    @mock.patch.object(settings, 'WEBAPP_MANIFEST_NAME', 'Firefox Marketplace')
    def test_minifest(self):
        res = self.client.get(self.url)
        eq_(res.status_code, 200)
        content = json.loads(res.content)
        eq_(content['name'], 'Firefox Marketplace')
        assert 'package_path' in content
        assert res.get('Etag'), 'Missing ETag'

    def test_minifest_etag(self):
        res = self.client.get(self.url)
        etag = res.get('Etag')

        # Trigger a change to the minifest by changing the name.
        with self.settings(WEBAPP_MANIFEST_NAME='Mozilla Fruitstand'):
            res = self.client.get(self.url)
            assert etag, 'Missing ETag'
            self.assertNotEqual(etag, res.get('Etag'))

{'fraction_non_alphanumeric': 0.12277261488617955, 'fraction_numerical': 0.019950549919004176, 'mean_word_length': 3.011627906976744, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5690367', 'n_tokens_mistral': 3974, 'n_tokens_neox': 3695, 'n_words': 688}