import unittest
import copy

from django.test import TestCase
from django.core.urlresolvers import reverse

from account.factories import AccountFactory, DEFAULT_PASSWORD


class TestAccounts(TestCase):
    def setUp(self):
        self.account = AccountFactory.create()
        self.user = self.account.user

    def test_logout_without_logged_in(self):
        response = self.client.get(reverse('account_logout'))
        self.assertRedirects(response, '{0}?next={1}'.format(reverse('account_login'),
                                                             reverse('account_logout')))

    def test_logout_with_logged_in(self):
        session = self.client.login(username=self.user.username, password=DEFAULT_PASSWORD)
        response = self.client.get(reverse('account_logout'))
        self.assertRedirects(response, reverse('index'))

    @unittest.skip('Skip until coverage process to be added to project')
    def test_call_view_denies_anonymous(self):
        response = self.client.get('/url/to/view', follow=True)
        self.assertRedirects(response, '/login/')
        response = self.client.post('/url/to/view', follow=True)
        self.assertRedirects(response, '/login/')  # check what is the best for these three options...
        self.assertRedirects(response, reverse('login'))
        self.assertRedirects(response, 'link.views.login')

    @unittest.skip('Skip until coverage process to be added to project')
    def test_call_view_loads(self):
        self.client.login(username='user', password='test')  # defined in fixture or with factory in setUp()
        response = self.client.get('/url/to/view')
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'conversation.html')

    @unittest.skip('Skip until coverage process to be added to project')
    def test_call_view_fails_blank(self):
        self.client.login(username='user', password='test')
        response = self.client.post('/url/to/view', {})  # blank data dictionary
        self.assertFormError(response, 'form', 'some_field', 'This field is required.')
        # etc. ...

    @unittest.skip('Skip until coverage process to be added to project')
    def test_call_view_fails_invalid(self):
        # as above, but with invalid rather than blank data in dictionary
        self.assertTrue(True)

    @unittest.skip('Skip until coverage process to be added to project')
    def test_call_view_success_valid(self):
        self.client.login(username='user', password='test')
        response = self.client.post('/url/to/view', {})  # same again, but with valid data, then
        self.assertRedirects(response, '/contact/1/calls/')

@@ -12,20 +12,8 @@
 test
-%0Aimport copy
 %0A%0Afr
@@ -628,18 +628,8 @@
     
- session =
  sel
@@ -827,461 +827,1476 @@
     
-@unittest.skip('Skip until coverage process to be added to project')%0A    def test_call_view_denies_anonymous(self):%0A        response = self.client.get('/url/to/view', follow=True)%0A        self.assertRedirects(response, '/login/')%0A        response = self.client.post('/url/to/view', follow=True)%0A        self.assertRedirects(response, '/login/')  # check what is the best for these three options...%0A        self.assertRedirects(response, reverse('login')
+def test_redirect_home_logged_in(self):%0A        self.client.login(username=self.user.username, password=DEFAULT_PASSWORD)%0A        response = self.client.get(reverse('index'))%0A        self.assertRedirects(response, reverse('home'))%0A%0A    def test_login_with_username_success(self):%0A        response = self.client.post(reverse('account_login'), data=%7B'identity': self.user.username,%0A                                                                    'password': DEFAULT_PASSWORD%7D)%0A        self.assertRedirects(response, reverse('home'))%0A%0A    def test_login_with_email_success(self):%0A        response = self.client.post(reverse('account_login'), data=%7B'identity': self.user.email,%0A                                                                    'password': DEFAULT_PASSWORD%7D)%0A        self.assertRedirects(response, reverse('home'))%0A%0A    def test_login_with_keep_connected(self):%0A        response = self.client.post(reverse('account_login'), data=%7B'identity': self.user.username,%0A                                                                    'password': DEFAULT_PASSWORD,%0A                                                                    'keep_connected': True%7D)%0A        self.assertRedirects(response, reverse('home'))%0A%0A    def test_login_fail_user_not_exists(self):%0A        response = self.client.post(reverse('account_login'), data=%7B'identity': 'invaliduser',%0A                                                                    'password': 'invalidpassword'%7D
 )%0A  
@@ -2308,33 +2308,36 @@
  self.assert
-Redirects
+TemplateUsed
 (response, '
@@ -2340,24 +2340,44 @@
 e, '
-l
 in
-k.views.login
+dex.html', 'Authentication failed.
 ')%0A%0A
@@ -2477,32 +2477,32 @@
 ew_loads(self):%0A
+
         self.cli
@@ -2548,57 +2548,8 @@
 st')
-  # defined in fixture or with factory in setUp()
 %0A   

Add test to verify login process