from django.test import TestCase
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
import json

from projects.models import Project
from bookmarks.models import Bookmark


class TestBookmarks(TestCase):
    fixtures = ['eric', 'test_data']

    def setUp(self):
        self.project = Project.objects.get(pk=1)
        self.client.login(username='eric', password='test')
        self.user = User.objects.get(pk=1)
        self.project.users.add(self.user)

    def tearDown(self):
        pass

    def __add_bookmark(self):
        post_data = {
            "project": self.project.slug,
            "version": 'latest',
            "page": "",
            "url": "",
        }

        return self.client.post(
            reverse('bookmarks_add'),
            data=json.dumps(post_data),
            content_type="application/json"
        )

    def test_add_bookmark(self):
        response = self.__add_bookmark()
        self.assertEqual(response.status_code, 201)

        bookmark = Bookmark.objects.get(pk=1)
        self.assertEqual(Bookmark.objects.count(), 1)
        self.assertEqual(bookmark.user, self.user)
        self.assertEqual(bookmark.project.slug, self.project.slug)

    def test_delete_bookmark(self):
        response = self.__add_bookmark()
        self.assertEqual(response.status_code, 201)

        response = self.client.post(
            reverse('bookmark_remove', kwargs={'bookmark_pk': '1'})
        )

        self.assertEqual(response.status_code, 302)
        self.assertEqual(Bookmark.objects.count(), 0)

@@ -944,32 +944,102 @@
 _add_bookmark()%0A
+        self.assertEqual(json.loads(response.content)%5B'added'%5D, True)%0A
         self.ass
@@ -1363,32 +1363,102 @@
 _add_bookmark()%0A
+        self.assertEqual(json.loads(response.content)%5B'added'%5D, True)%0A
         self.ass
@@ -1633,39 +1633,53 @@
 sert
-Equal(response.status_code, 302
+Redirects(response, reverse('user_bookmarks')
 )%0A  

test to ensure adding bookmark returns correct json in response