<commit_before># -*- coding: utf-8 -*-

from nose.tools import *  # noqa

import responses

from tests.base import OsfTestCase
from tests.factories import AuthUserFactory, ProjectFactory

import json
import urlparse

from website.addons.mendeley.tests.factories import (
    MendeleyAccountFactory, MendeleyUserSettingsFactory,
    MendeleyNodeSettingsFactory
)

from website.util import api_url_for
from website.addons.mendeley import utils


API_URL = 'https://api.mendeley.com'

FOLDER_LIST_JSON = [
    {
        "id": "68624820-2f4c-438d-ae54-ae2bc431cee3",
        "name": "API Related Papers",
        "created": "2014-04-08T10:11:40.000Z",
    },
    {
        "id": "1cb47377-e3a1-40dd-bd82-11aff83a46eb",
        "name": "MapReduce",
        "created": "2014-07-02T13:19:36.000Z",
    },
]


class MendeleyViewsTestCase(OsfTestCase):

    def setUp(self):
        super(MendeleyViewsTestCase, self).setUp()
        self.account = MendeleyAccountFactory()
        self.user = AuthUserFactory(external_accounts=[self.account])
        self.user_addon = MendeleyUserSettingsFactory(owner=self.user)
        self.project = ProjectFactory(creator=self.user)
        self.node_addon = MendeleyNodeSettingsFactory(owner=self.project, external_account=self.account)
        self.node_addon.grant_oauth_access(self.user, self.account, metadata={'lists': 'list'})

    def test_user_folders(self):
        """JSON: a list of user's Mendeley folders"""
        res = self.app.get(
            api_url_for('list_mendeley_accounts_user'),
            auth=self.user.auth,
        )
        expected = {
            'accounts': [
                utils.serialize_account(each)
                for each in self.user.external_accounts
                if each.provider == 'mendeley'
            ]
        }<commit_msg>WIP views tests<commit_after># -*- coding: utf-8 -*-

from nose.tools import *  # noqa

import responses

from tests.base import OsfTestCase
from tests.factories import AuthUserFactory, ProjectFactory

import json
import urlparse

from website.addons.mendeley.tests.factories import (
    MendeleyAccountFactory, MendeleyUserSettingsFactory,
    MendeleyNodeSettingsFactory
)

from website.util import api_url_for
from website.addons.mendeley import utils
from website.addons.mendeley.views import serialize_settings, serialize_urls

API_URL = 'https://api.mendeley.com'

FOLDER_LIST_JSON = [
    {
        "id": "68624820-2f4c-438d-ae54-ae2bc431cee3",
        "name": "API Related Papers",
        "created": "2014-04-08T10:11:40.000Z",
    },
    {
        "id": "1cb47377-e3a1-40dd-bd82-11aff83a46eb",
        "name": "MapReduce",
        "created": "2014-07-02T13:19:36.000Z",
    },
]


class MendeleyViewsTestCase(OsfTestCase):

    def setUp(self):
        super(MendeleyViewsTestCase, self).setUp()
        self.account = MendeleyAccountFactory()
        self.user = AuthUserFactory(external_accounts=[self.account])
        self.user_addon = MendeleyUserSettingsFactory(owner=self.user)
        self.project = ProjectFactory(creator=self.user)
        self.node_addon = MendeleyNodeSettingsFactory(owner=self.project, external_account=self.account)
        self.node_addon.grant_oauth_access(self.user, self.account, metadata={'lists': 'list'})

    def test_serialize_settings_authorizer(self):
        """dict: a serialized version of user-specific addon settings"""
        res = serialize_settings(self.node_addon, self.user.auth)        
        expected = {
            'nodeHasAuth': True,
            'userIsOwner': True,
            'userHasAuth': True,
            'urls': serialize_urls(self.node_addon, self.user),
            'userAccountId': filter(lambda a: a.provider == 'mendeley', user.external_accounts)[0]._id,
            'folder': '',
            'ownerName': self.user.fullname            
        }
        assert_equal(res, expected)


    def test_serialize_settings_non_authorizer(self):
        """dict: a serialized version of user-specific addon settings"""
        non_authorizing_user = AuthUserFactory()
        self.project.add_contributor(non_authorizing_user, save=True)    
        res = serialize_settings(self.node_addon, non_authorizing_user.auth)
        

    def test_user_folders(self):
        """JSON: a list of user's Mendeley folders"""
        res = self.app.get(
            api_url_for('list_mendeley_accounts_user'),
            auth=self.user.auth,
        )
        expected = {
            'accounts': [
                utils.serialize_account(each)
                for each in self.user.external_accounts
                if each.provider == 'mendeley'
            ]
        }
{'fraction_non_alphanumeric': 0.10989247311827957, 'fraction_numerical': 0.031612903225806455, 'mean_word_length': 2.8279835390946504, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7649387', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1459, 'n_words': 252}