"""This module contains tests about the ``translation.views`` module."""

import tempfile
from mock import patch

import polib

from django.core.urlresolvers import reverse

from mtp.apps.common.tests.test_cases import MTPTestCase
from mtp.apps.translation.export_views.views import POExportView

from . import constants


class POExportViewTestCase(MTPTestCase):
    """Test the `POExportView` class."""

    @classmethod
    def setUpClass(cls):
        """Set up class."""

        super().setUpClass()

        cls.language_code = 'fr'

        # We create a test po file
        po_content = constants.CORRECT_POFILE

        with tempfile.NamedTemporaryFile(suffix='.po') as tmp_file:
            tmp_file.write(po_content)
            tmp_file.flush()

            cls.po_file = polib.pofile(tmp_file.name)

    def test_view_works(self):
        """Test the view works."""

        url = reverse('translation:export-interface', kwargs={
            'language_code_slug': self.language_code,
        })

        self.login_as_admin()

        # We can't use po files created with `makemessages` command.
        # So we mock the `POExportView` to return the po file created earlier
        with patch.object(POExportView, 'get_po_file', return_value=self.po_file):
            response = self.client.get(url)

        self.assertEqual(response.status_code, 200)
