# -*- coding: utf-8 -*-

from django.test import TestCase
from django.test.client import Client
from django.test.utils import override_settings
from django.contrib.auth.models import User
from django.conf import settings
from django.core.urlresolvers import reverse

from models import Structure, UserProfile


@override_settings(LOGIN_URL='/login/')
class LoginTestCase(TestCase):
    def test_login(self):
        response = self.client.get('/')
        _next = settings.FORCE_SCRIPT_NAME + '/'
        self.assertRedirects(response, '/login/?next=' + _next)


class StructureTest(TestCase):
    def test_basic(self):
        s = Structure(name=u"Mercantour")
        self.assertEqual(unicode(s), u"Mercantour")


class UserProfileTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user('Joe', email='temporary@yopmail.com', password='Bar')
        self.user.set_password('Bar')
        self.user.save()

    def test_profile(self):
        self.assertTrue(isinstance(self.user.profile, UserProfile))
        self.assertEqual(self.user.profile.structure.name, settings.DEFAULT_STRUCTURE_NAME)
        self.assertEqual(self.user.profile.language, settings.LANGUAGE_CODE)
        
    def test_language(self):
        c = Client()
        success = c.login(username=u"Joe", password=u"Bar")
        self.assertTrue(success)
        response = c.get(reverse('home'))
        self.assertEqual(200, response.status_code)
        self.assertTrue(u"déconnecter" in response.content)
        
        # Change user lang
        self.assertNotEqual(settings.LANGUAGE_CODE, u"en")
        userprofile = UserProfile.objects.get(user=self.user)
        userprofile.language = u"en"
        userprofile.save()
        self.assertEqual(self.user.profile.language, u"en")
        # No effect if no logout
        response = c.get(reverse('home'))
        self.assertTrue(u"déconnecter" in response.content)
        
        c.logout()
        response = c.get(reverse('home'))
        self.assertEqual(response.status_code, 302)
        c.login(username=u"Joe", password=u"Bar")
        response = c.get(reverse('home'))

        self.assertEqual(c.session['django_language'], u"en")
        self.assertFalse(u"déconnecter" in response.content)

    def test_admin(self):
        self.assertFalse(self.user.is_staff)
        c = Client()
        success = c.login(username=u"Joe", password=u"Bar")
        self.assertTrue(success)
        
        response = c.get(reverse('home'))
        self.assertFalse(u"Administration" in response.content)
        
        self.user.is_staff = True
        self.user.save()
        response = c.get(reverse('home'))
        self.assertTrue(u"Administration" in response.content)

@@ -258,16 +258,66 @@
  reverse
+%0Afrom django.utils.translation import gettext as _
 %0A%0Afrom m
@@ -1507,38 +1507,35 @@
 .assertTrue(
-u%22d%C3%A9connecter%22
+_(%22Logout%22)
  in response
@@ -1924,30 +1924,27 @@
 ertTrue(
-u%22d%C3%A9connecter%22
+_(%22Logout%22)
  in resp
@@ -2263,22 +2263,19 @@
 lse(
-u%22d%C3%A9connecter%22
+_(%22Logout%22)
  in 
@@ -2521,32 +2521,41 @@
 everse('home'))%0A
+        %0A
         self.ass
@@ -2563,33 +2563,26 @@
 rtFalse(
-u
+_(
 %22Admin
-istration
 %22
+)
  in resp
@@ -2733,25 +2733,18 @@
 rue(
-u
+_(
 %22Admin
-istration
 %22
+)
  in 

Fix unicode errors, use translations