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

from django.test import TestCase
from django.contrib.auth.models import Permission, User
from django.contrib.auth import authenticate, login
from rankingempresas.models import *
from django.test import Client


# Create your tests here.

class SomeViewsTest(TestCase):
    def setUp(self):
        user = User.objects.create_user("prueba", "prueba@prueba.com", 123123)
        company = Company.objects.create(name=u'compañia de prueba', address="calle falsa 123", telephone="56456156",
                                         email="prueba@compañiadeprueba.com")
    def test_login(self):
        c = Client()
        response = self.client.get('/students/login/')
        self.assertEqual(response.status_code, 200)
        response = c.post('/students/login/', {'username': 'prueba'})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['error'], "No has introducido usuario/contraseña")
        response = c.post('/students/login/', {'username': 'prueba', 'password':"123123"})
        #if login sucessfull redirecto to ranking
        self.assertEqual(response.status_code, 302)

    def test_company(self):
        c = Client()
        response = self.client.get('/company/1/view/')
        self.assertEqual(response.status_code, 200)
        self.assertEquals(response.context['name'], u'compañia de prueba')
        self.assertEquals(response.context['address'], "calle falsa 123")
        self.assertEquals(response.context['telephone'], "56456156")
        self.assertEquals(response.context['email'], u'prueba@compañiadeprueba.com')

    def test_vote(self):
        c = Client()
        response = self.client.get('/company/1/score/add/')
        self.assertEqual(response.status_code, 200)
        response = c.post('/company/1/score/add/', {'mark': 20, 'comment':'Estuvo de luho premoh!'})
        self.assertEqual(response.status_code, 200)
        self.assertEquals(response.context['error'], "Debes estar autenticado!")
        response = c.post('/students/login/', {'username': 'prueba', 'password':"123123"})
        self.assertEqual(response.status_code, 302)
        #with minus than 1
        response = c.post('/company/1/score/add/', {'mark': '-5', 'comment':'Estuvo de luho premoh!'})
        self.assertEqual(response.status_code, 200)
        self.assertEquals(response.context['error'], "La nota debe ser entre 1 y 10")
        #with more than 10
        response = c.post('/company/1/score/add/', {'mark': 20, 'comment':'Estuvo de luho premoh!'})
        self.assertEqual(response.status_code, 200)
        self.assertEquals(response.context['error'], "La nota debe ser entre 1 y 10")
        response = c.post('/company/1/score/add/', {'mark': 'hola', 'comment':'Estuvo de luho premoh!'})
        #with a string
        self.assertEqual(response.status_code, 200)
        self.assertEquals(response.context['error'], "La nota debe ser un entero")
        #score good
        response = c.post('/company/1/score/add/', {'mark': '10', 'comment':'Estuvo de luho premoh!'})
        self.assertEqual(response.status_code, 302)
        #try another vote
        response = c.post('/company/1/score/add/', {'mark': '10', 'comment':'Estuvo de luho premoh!'})
        self.assertEqual(response.status_code, 200)
        self.assertEquals(response.context['error'], "Ya has votado")

    def test_ranking(self):
        c = Client()
        response = c.post('/ranking/', {'mark': '10', 'comment':'Estuvo de luho premoh!'})
        self.assertEqual(response.status_code, 200)
        company = response.context['companies'][0]
        self.assertEquals(company.name,  u'compañia de prueba')
        self.assertEquals(company.total_score,  0)
        self.assertEquals(company.number_votes,  0)
        response = c.post('/students/login/', {'username': 'prueba', 'password':"123123"})
        self.assertEqual(response.status_code, 302)
        response = c.post('/company/1/score/add/', {'mark': '10', 'comment':'Estuvo de luho premoh!'})
        self.assertEqual(response.status_code, 302)
        response = c.post('/ranking/', {'mark': '10', 'comment':'Estuvo de luho premoh!'})
        self.assertEqual(response.status_code, 200)
        company = response.context['companies'][0]
        self.assertEquals(company.name,  u'compañia de prueba')
        self.assertEquals(company.total_score,  10)
        self.assertEquals(company.number_votes,  1)
