# coding: utf-8
from __future__ import unicode_literals, print_function, division, absolute_import

from django_digest.test import DigestAuth, BasicAuth
from rest_framework import authentication

from onadata.apps.api.tests.viewsets.test_abstract_viewset import \
    TestAbstractViewSet
from onadata.apps.api.viewsets.connect_viewset import ConnectViewSet
from onadata.libs.authentication import DigestAuthentication


class TestConnectViewSet(TestAbstractViewSet):
    def setUp(self):
        super(self.__class__, self).setUp()
        self.view = ConnectViewSet.as_view({
            "get": "list",
        })
        self.data = {
            'id': 1,
            'username': 'bob',
            'name': 'Bob',
            'email': 'bob@columbia.edu',
            'city': 'Bobville',
            'country': 'US',
            'organization': 'Bob Inc.',
            'website': 'bob.com',
            'twitter': 'boberama',
            'gravatar': self.user.profile.gravatar,
            'require_auth': False,
            'api_token': self.user.auth_token.key,
            'temp_token': self.client.session.session_key,
            'metadata': {},
        }

    def test_get_profile(self):
        request = self.factory.get('/', **self.extra)
        request.session = self.client.session

        response = self.view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, self.data)

    def test_user_list_with_digest(self):
        view = ConnectViewSet.as_view(
            {'get': 'list'},
            authentication_classes=(DigestAuthentication,))
        request = self.factory.head('/')

        auth = DigestAuth('bob', 'bob')
        response = view(request)
        self.assertTrue(response.has_header('WWW-Authenticate'))
        self.assertTrue(
            response['WWW-Authenticate'].startswith('Digest nonce='))
        request = self.factory.get('/')
        request.META.update(auth(request.META, response))
        request.session = self.client.session

        response = view(request)
        self.assertEqual(response.status_code, 401)
        self.assertEqual(response.data['detail'],
                         "Invalid username/password")
        auth = DigestAuth('bob', 'bobbob')
        request.META.update(auth(request.META, response))
        request.session = self.client.session

        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, self.data)

    def test_user_list_with_basic_and_digest(self):
        view = ConnectViewSet.as_view(
            {'get': 'list'},
            authentication_classes=(
                DigestAuthentication,
                authentication.BasicAuthentication
            ))
        request = self.factory.get('/')
        auth = BasicAuth('bob', 'bob')
        request.META.update(auth(request.META))
        request.session = self.client.session

        response = view(request)
        self.assertEqual(response.status_code, 401)
        self.assertEqual(response.data['detail'],
                         "Invalid username/password.")
        auth = BasicAuth('bob', 'bobbob')

        # redo the request
        request = self.factory.get('/')
        request.META.update(auth(request.META))
        request.session = self.client.session

        response = view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, self.data)

@@ -608,16 +608,335 @@
      %7D)%0A
+%0A        # PostgreSQL behaves differently than SQLite. After each test, table is%0A        # truncated but PostgreSQL does not reset its sequences but SQLite does.%0A        # %60self.user_profile_data()%60 does contain the real value of user's id.%0A        # let's use it.%0A        user_profile_data = self.user_profile_data()%0A%0A
         
@@ -967,17 +967,39 @@
   'id': 
-1
+user_profile_data%5B'id'%5D
 ,%0A      
@@ -1020,251 +1020,402 @@
 e': 
-'bob',%0A            'name': 'Bob',%0A            'email': 'bob@columbia.edu',%0A            'city': 'Bobville',%0A            'country': 'US',%0A            'organization': 'Bob Inc.',%0A            'website': 'bob.com',%0A            'twitter': 'boberama'
+user_profile_data%5B'username'%5D,%0A            'name': user_profile_data%5B'name'%5D,%0A            'email': user_profile_data%5B'email'%5D,%0A            'city': user_profile_data%5B'city'%5D,%0A            'country': user_profile_data%5B'country'%5D,%0A            'organization': user_profile_data%5B'organization'%5D,%0A            'website': user_profile_data%5B'website'%5D,%0A            'twitter': user_profile_data%5B'twitter'%5D
 ,%0A  
@@ -1440,34 +1440,35 @@
 r': 
-self.
 user
-.
+_
 profile
-.
+_data%5B'
 gravatar
 ,%0A  
@@ -1463,16 +1463,18 @@
 gravatar
+'%5D
 ,%0A      

Use PK from DB instead of hard coded PK