# -*- coding: utf-8 -*-
import mock
import pytest
import urlparse
from framework.auth import Auth
from nose.tools import (assert_equal, assert_true, assert_false)
from addons.base.tests import views
from addons.base.tests.utils import MockLibrary, MockFolder
from addons.zotero.models import Zotero
from addons.zotero.provider import ZoteroCitationsProvider
from addons.zotero.serializer import ZoteroSerializer

from addons.zotero.tests.utils import ZoteroTestCase, mock_responses
from tests.base import OsfTestCase

API_URL = 'https://api.zotero.org'
pytestmark = pytest.mark.django_db

class TestAuthViews(ZoteroTestCase, views.OAuthAddonAuthViewsTestCaseMixin, OsfTestCase):

    @mock.patch('osf.models.external.OAuth1Session.fetch_request_token')
    def test_oauth_start(self, mock_token):
        mock_token.return_value = {
            'oauth_token': 'token',
            'oauth_secret': 'secret',
        }
        super(TestAuthViews, self).test_oauth_start()


class TestConfigViews(ZoteroTestCase, views.OAuthCitationAddonConfigViewsTestCaseMixin, OsfTestCase):
    folder = MockFolder()
    library = MockLibrary()
    Serializer = ZoteroSerializer
    client = Zotero
    citationsProvider = ZoteroCitationsProvider
    foldersApiUrl = None
    documentsApiUrl = None
    mockResponses = mock_responses

    def setUp(self):
        super(TestConfigViews, self).setUp()
        self.foldersApiUrl = urlparse.urljoin(API_URL, 'users/{}/collections'
            .format(self.external_account.provider_id))
        self.documentsApiUrl = urlparse.urljoin(API_URL, 'users/{}/items'
            .format(self.external_account.provider_id))

    def test_widget_view_incomplete_library_set_only(self):
        # JSON: everything a widget needs
        # When library is set in zotero, folder is cleared.
        self.citationsProvider().set_config(
            self.node_settings,
            self.user,
            self.folder.json['id'],
            self.folder.name,
            Auth(self.user),
            self.library.json['id'],
            self.library.name
        )
        assert_false(self.node_settings.complete)
        assert_equal(self.node_settings.list_id, None)
        assert_equal(self.node_settings.library_id, 'Fake Library Key')
        url = self.project.api_url_for('{0}_widget'.format(self.ADDON_SHORT_NAME))
        res = self.app.get(url, auth=self.user.auth).json

        assert_false(res['complete'])
        assert_equal(res['list_id'], None)
        assert_equal(res['library_id'], 'Fake Library Key')

    def test_widget_view_complete(self):
        # JSON: everything a widget needs
        # Library must be set, then folder.
        self.citationsProvider().set_config(
            self.node_settings,
            self.user,
            self.folder.json['id'],
            self.folder.name,
            Auth(self.user),
            self.library.json['id'],
            self.library.name
        )
        self.citationsProvider().set_config(
            self.node_settings,
            self.user,
            self.folder.json['id'],
            self.folder.name,
            Auth(self.user),
        )
        assert_true(self.node_settings.complete)
        assert_equal(self.node_settings.list_id, 'Fake Key')
        assert_equal(self.node_settings.library_id, 'Fake Library Key')
        url = self.project.api_url_for('{0}_widget'.format(self.ADDON_SHORT_NAME))
        res = self.app.get(url, auth=self.user.auth).json
        assert_true(res['complete'])
        assert_equal(res['list_id'], 'Fake Key')
        assert_equal(res['library_id'], 'Fake Library Key')

@@ -2258,35 +2258,35 @@
 y')%0A        
-url
+res
  = self.
 project.api_
@@ -2277,130 +2277,81 @@
 elf.
-project.api_url_for('%7B0%7D_widget'.format(self.ADDON_SHORT_NAME))%0A        res = self.app.get(url, auth=self.user.auth).json%0A
+citationsProvider().widget(self.project.get_addon(self.ADDON_SHORT_NAME))
 %0A   
@@ -2616,16 +2616,43 @@
 folder.%0A
+        # Sets library key%0A
         
@@ -2907,32 +2907,54 @@
 .name%0A        )%0A
+        # Sets folder%0A
         self.cit
@@ -3336,19 +3336,19 @@
     
-url
+res
  = self.
 proj
@@ -3347,129 +3347,81 @@
 elf.
-project.api_url_for('%7B0%7D_widget'.format(self.ADDON_SHORT_NAME))%0A        res = self.app.get(url, auth=self.user.auth).json
+citationsProvider().widget(self.project.get_addon(self.ADDON_SHORT_NAME))
 %0A   

Use widget method on citationsProvider instead of api_url_for.