from __future__ import absolute_import
import json
import random

from unittest import TestCase
from django.test.client import RequestFactory
from django.core.urlresolvers import reverse
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import SuspiciousOperation, PermissionDenied

from OWR.users.factory import UserFactory

from .models import OpenHardwareLike
from .views import like_set
from .factory import OpenHardwareFactory


class OpenHardwareViewTests(TestCase):
    BATCH_NUMBER = 10
    def setUp(self):
        self.factory = RequestFactory()
        self.user  = UserFactory()
        self.admin = UserFactory(is_superuser=True, is_staff=True)
        self.ohs = OpenHardwareFactory.create_batch(self.BATCH_NUMBER)

    def test_like_add_not_logged(self):
        count_start = OpenHardwareLike.objects.count()

        exception = False
        response = None

        # this is not logged
        request = self.factory.post(reverse('oh:like_set'), data=json.dumps({'id': 1}), content_type='text/javascript')
        request.user = AnonymousUser()
        try:
            response = like_set(request)
        except PermissionDenied as e:
            exception = True
            # TODO: test error parameters
            #js = json.loads(response.body)

        count_final = OpenHardwareLike.objects.count()

        self.assertEqual(exception, True, 'An exception must raise if the user is not logged')
        self.assertEqual(count_start, count_final)

    def test_like_add_logged(self):
        idx = random.randrange(0, self.BATCH_NUMBER)
        count_start = OpenHardwareLike.objects.count()

        # this is logged
        request = self.factory.post(reverse('oh:like_set'), data=json.dumps({'id': self.ohs[idx].pk, 'action': 'set'}), content_type='text/javascript')
        request.user = self.user
        response = like_set(request)

        count_final = OpenHardwareLike.objects.count()

        self.assertEqual(response.status_code, 200)
        self.assertEqual(count_start + 1, count_final)

    def test_like_with_malformed_json(self):
        count_start = OpenHardwareLike.objects.count()
        exception = False

        # this is logged
        request = self.factory.post(reverse('oh:like_set'), data=json.dumps({'id': 1, 'action': 'foobar'}), content_type='text/javascript')
        request.user = self.user

        try:
            response = like_set(request)
        except SuspiciousOperation as e:
            exception = True
            # TODO: test error parameters
            #js = json.loads(response.body)

        count_final = OpenHardwareLike.objects.count()

        self.assertEqual(exception, True)
        self.assertEqual(count_start, count_final)


    def test_remove_like(self):
        deal_to_test = self.ohs[0]
        OpenHardwareLike.objects.create(oh=deal_to_test, user=self.user)
        count_start = OpenHardwareLike.objects.count()

        # this is logged
        request = self.factory.post(reverse('oh:like_set'), data=json.dumps({'id': deal_to_test.pk, 'action': 'unset'}), content_type='text/javascript')
        request.user = self.user
        response = like_set(request)

        count_final = OpenHardwareLike.objects.count()

        self.assertEqual(response.status_code, 200)
        self.assertEqual(count_start - 1, count_final)

        self.assertEqual(json.loads(response.content), {'action': 'unset', 'status': 'ok'})

@@ -63,38 +63,8 @@
 om%0A%0A
-from unittest import TestCase%0A
 from
@@ -105,16 +105,16 @@
 Factory%0A
+
 from dja
@@ -275,16 +275,57 @@
 onDenied
+%0Afrom django.test import Client, TestCase
 %0A%0Afrom O
@@ -530,16 +530,16 @@
 ER = 10%0A
-
     def 
@@ -543,32 +543,63 @@
 ef setUp(self):%0A
+        self.client = Client()%0A
         self.fac
@@ -3488,8 +3488,663 @@
  'ok'%7D)%0A
+%0A    def test_index(self):%0A        '''We check for stuffs should be in home page.'''%0A        response = self.client.get(reverse('home'))%0A%0A        self.assertEqual(response.status_code, 200)%0A%0A        self.assertTrue(self.client.login(username=self.user.username, password='password'))%0A        response = self.client.get(reverse('home'))%0A        self.assertEqual(response.status_code, 200)%0A%0A    def test_admin(self):%0A        self.assertTrue(self.client.login(username=self.admin.username, password='password'))%0A%0A        url = reverse('admin:oh_openhardware_add')%0A%0A        response = self.client.get(url)%0A%0A        self.assertEqual(response.status_code, 200)%0A

Add basic tests about home page.