Ejemplo n.º 1
0
 def test_refresh(self, mock_box_refresh, mock_drive_refresh,
                  mock_mendeley_refresh):
     fake_authorized_box_account = BoxAccountFactory(
         date_last_refreshed=datetime.datetime.utcnow())
     fake_authorized_drive_account = GoogleDriveAccountFactory(
         date_last_refreshed=datetime.datetime.utcnow())
     fake_authorized_mendeley_account = MendeleyAccountFactory(
         date_last_refreshed=datetime.datetime.utcnow())
     fake_unauthorized_box_account = BoxAccountFactory(
         date_last_refreshed=datetime.datetime.utcnow() -
         datetime.timedelta(days=4))
     fake_unauthorized_drive_account = GoogleDriveAccountFactory(
         date_last_refreshed=datetime.datetime.utcnow() -
         datetime.timedelta(days=4))
     fake_unauthorized_mendeley_account = MendeleyAccountFactory(
         date_last_refreshed=datetime.datetime.utcnow() -
         datetime.timedelta(days=4))
     for addon in self.addons:
         Provider = look_up_provider(addon)
         main(delta=relativedelta(days=3),
              Provider=Provider,
              rate_limit=(5, 1),
              dry_run=False)
     assert_equal(1, mock_box_refresh.call_count)
     assert_equal(1, mock_drive_refresh.call_count)
     assert_equal(1, mock_mendeley_refresh.call_count)
Ejemplo n.º 2
0
    def test_set_auth_wrong_user(self):
        external_account = MendeleyAccountFactory()
        self.user.external_accounts.append(external_account)
        self.user.save()

        with assert_raises(PermissionsError):
            self.node_settings.set_auth(external_account=external_account,
                                        user=UserFactory())
Ejemplo n.º 3
0
    def test_clear_auth(self):
        self.node_settings.external_account = MendeleyAccountFactory()
        self.node_settings.mendeley_list_id = 'something'
        self.node_settings.user_settings = self.user_settings
        self.node_settings.save()

        self.node_settings.clear_auth()

        assert_is_none(self.node_settings.external_account)
        assert_is_none(self.node_settings.mendeley_list_id)
        assert_is_none(self.node_settings.user_settings)
Ejemplo n.º 4
0
    def test_deauthorize(self):
        self.node_settings.external_account = MendeleyAccountFactory()
        self.node_settings.mendeley_list_id = 'something'
        self.node_settings.user_settings = self.user_settings
        self.node_settings.save()

        assert_true(self.node_settings.mendeley_list_id)
        self.node_settings.deauthorize(auth=Auth(self.user))
        self.node_settings.save()
        assert_is(self.node_settings.user_settings, None)
        assert_is(self.node_settings.mendeley_list_id, None)

        last_log = self.node.logs[-1]
        assert_equal(last_log.action, 'mendeley_node_deauthorized')
        params = last_log.params
        assert_in('node', params)
        assert_in('project', params)
Ejemplo n.º 5
0
 def setUp(self):
     super(MendeleyViewsTestCase, self).setUp()
     self.account = MendeleyAccountFactory()
     self.user = AuthUserFactory(external_accounts=[self.account])
     self.account.display_name = self.user.fullname
     self.account.save()
     self.user_addon = MendeleyUserSettingsFactory(owner=self.user, external_account=self.account)
     self.project = ProjectFactory(creator=self.user)
     self.node_addon = MendeleyNodeSettingsFactory(owner=self.project)
     self.node_addon.set_auth(external_account=self.account, user=self.user)
     #self.user_addon.grant_oauth_access(self.node_addon, self.account, metadata={'lists': 'list'})
     self.node = MockNode()
     self.node.addon = self.node_addon
     self.id_patcher = mock.patch('website.addons.mendeley.model.Mendeley.client_id')
     self.secret_patcher = mock.patch('website.addons.mendeley.model.Mendeley.client_secret')
     self.id_patcher.__get__ = mock.Mock(return_value='1234567890asdf')
     self.secret_patcher.__get__ = mock.Mock(return_value='1234567890asdf')
     self.id_patcher.start()
     self.secret_patcher.start()
Ejemplo n.º 6
0
    def test_set_target_folder(self):
        folder_id = 'fake-folder-id'
        folder_name = 'fake-folder-name'

        external_account = MendeleyAccountFactory()
        self.user.external_accounts.append(external_account)
        self.user.save()

        self.node_settings.set_auth(
            external_account=external_account,
            user=self.user,
        )

        assert_is_none(self.node_settings.mendeley_list_id)

        self.node_settings.set_target_folder(
            folder_id,
            folder_name,
            auth=Auth(user=self.user),
        )

        # instance was updated
        assert_equal(
            self.node_settings.mendeley_list_id,
            'fake-folder-id',
        )

        # user_settings was updated
        # TODO: the call to grant_oauth_access should be mocked
        assert_true(
            self.user_settings.verify_oauth_access(
                node=self.node,
                external_account=external_account,
                metadata={'folder': 'fake-folder-id'}))

        log = self.node.logs[-1]
        assert_equal(log.action, 'mendeley_folder_selected')
        assert_equal(log.params['folder_id'], folder_id)
        assert_equal(log.params['folder_name'], folder_name)
Ejemplo n.º 7
0
    def test_set_auth(self):
        external_account = MendeleyAccountFactory()
        self.user.external_accounts.append(external_account)
        self.user.save()

        # this should be reset after the call
        self.node_settings.mendeley_list_id = 'anything'

        self.node_settings.set_auth(external_account=external_account,
                                    user=self.user)

        # this instance is updated
        assert_equal(self.node_settings.external_account, external_account)
        assert_equal(self.node_settings.user_settings, self.user_settings)
        assert_is_none(self.node_settings.mendeley_list_id)

        # user_settings was updated
        # TODO: The call to grant_oauth_access in set_auth should be mocked
        assert_true(
            self.user_settings.verify_oauth_access(
                node=self.node,
                external_account=external_account,
            ))
Ejemplo n.º 8
0
class MendeleyViewsTestCase(OsfTestCase):

    def setUp(self):
        super(MendeleyViewsTestCase, self).setUp()
        self.account = MendeleyAccountFactory()
        self.user = AuthUserFactory(external_accounts=[self.account])
        self.account.display_name = self.user.fullname
        self.account.save()
        self.user_addon = MendeleyUserSettingsFactory(owner=self.user, external_account=self.account)
        self.project = ProjectFactory(creator=self.user)
        self.node_addon = MendeleyNodeSettingsFactory(owner=self.project)
        self.node_addon.set_auth(external_account=self.account, user=self.user)
        #self.user_addon.grant_oauth_access(self.node_addon, self.account, metadata={'lists': 'list'})
        self.node = MockNode()
        self.node.addon = self.node_addon
        self.id_patcher = mock.patch('website.addons.mendeley.model.Mendeley.client_id')
        self.secret_patcher = mock.patch('website.addons.mendeley.model.Mendeley.client_secret')
        self.id_patcher.__get__ = mock.Mock(return_value='1234567890asdf')
        self.secret_patcher.__get__ = mock.Mock(return_value='1234567890asdf')
        self.id_patcher.start()
        self.secret_patcher.start()

    def tearDown(self):
        self.id_patcher.stop()
        self.secret_patcher.stop()

    def test_serialize_settings_authorizer(self):
        #"""dict: a serialized version of user-specific addon settings"""
        res = self.app.get(
            self.project.api_url_for('mendeley_get_config'),
            auth=self.user.auth,
        )
        result = res.json['result']
        assert_true(result['nodeHasAuth'])
        assert_true(result['userHasAuth'])
        assert_true(result['userIsOwner'])
        assert_equal(result['folder'], {'name': ''})
        assert_equal(result['ownerName'], self.user.fullname)
        assert_true(result['urls']['auth'])
        assert_true(result['urls']['config'])
        assert_true(result['urls']['deauthorize'])
        assert_true(result['urls']['folders'])
        assert_true(result['urls']['importAuth'])
        assert_true(result['urls']['settings'])

    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 = self.app.get(
            self.project.api_url_for('mendeley_get_config'),
            auth=non_authorizing_user.auth,
        )
        result = res.json['result']
        assert_true(result['nodeHasAuth'])
        assert_false(result['userHasAuth'])
        assert_false(result['userIsOwner'])
        assert_equal(result['folder'], {'name': ''})
        assert_equal(result['ownerName'], self.user.fullname)
        assert_true(result['urls']['auth'])
        assert_true(result['urls']['config'])
        assert_true(result['urls']['deauthorize'])
        assert_true(result['urls']['folders'])
        assert_true(result['urls']['importAuth'])
        assert_true(result['urls']['settings'])

    def test_set_auth(self):

        res = self.app.put_json(
            self.project.api_url_for('mendeley_add_user_auth'),
            {
                'external_account_id': self.account._id,
            },
            auth=self.user.auth,
        )

        assert_equal(
            res.status_code,
            200
        )

        assert_true(res.json['result']['userHasAuth'])

        assert_equal(
            self.node_addon.user_settings,
            self.user_addon
        )
        assert_equal(
            self.node_addon.external_account,
            self.account
        )

    def test_remove_user_auth(self):
        self.node_addon.set_auth(self.account, self.user)

        res = self.app.delete_json(
            self.project.api_url_for('mendeley_remove_user_auth'),
            {
                'external_account_id': self.account._id,
            },
            auth=self.user.auth,
        )

        assert_equal(
            res.status_code,
            200
        )

        self.node_addon.reload()

        assert_is_none(self.node_addon.user_settings)
        assert_is_none(self.node_addon.external_account)

    @mock.patch('website.addons.mendeley.model.Mendeley._folder_metadata')
    def test_set_config_owner(self, mock_metadata):
        mock_metadata.return_value = MockFolder(name='Fake Folder')
        # Settings config updates node settings
        self.node_addon.associated_user_settings = []
        self.node_addon.save()
        res = self.app.put_json(
            self.project.api_url_for('mendeley_set_config'),
            {
                'external_account_id': self.account._id,
                'external_list_id': 'list',
            },
            auth=self.user.auth,
        )
        self.node_addon.reload()
        assert_equal(self.user_addon, self.node_addon.user_settings)
        serializer = MendeleySerializer(node_settings=self.node_addon, user_settings=self.user_addon)
        expected = {
            'result': serializer.serialized_node_settings
        }
        assert_equal(res.json, expected)

    @mock.patch('website.addons.mendeley.model.Mendeley._folder_metadata')
    def test_set_config_not_owner(self, mock_metadata):
        mock_metadata.return_value = MockFolder(name='Fake Folder')
        user = AuthUserFactory()
        user.add_addon('mendeley')
        self.project.add_contributor(user)
        self.project.save()
        res = self.app.put_json(
            self.project.api_url_for('mendeley_set_config'),
            {
                'external_account_id': self.account._id,
                'external_list_id': 'list',
            },
            auth=user.auth,
        )
        self.node_addon.reload()
        assert_equal(self.user_addon, self.node_addon.user_settings)
        serializer = MendeleySerializer(node_settings=self.node_addon, user_settings=None)
        expected = {
            'result': serializer.serialized_node_settings
        }
        assert_equal(res.json, expected)

    def test_mendeley_widget_view_complete(self):
        # JSON: everything a widget needs
        assert_false(self.node_addon.complete)
        assert_equal(self.node_addon.mendeley_list_id, None)
        self.node_addon.set_target_folder('ROOT-ID', 'ROOT', auth=Auth(user=self.user))
        res = views.mendeley_widget(node_addon=self.node_addon,
                                    project=self.project,
                                    node=self.node,
                                    nid=self.node_addon._id,
                                    pid=self.project._id,
                                    auth=self.user.auth)
        assert_true(res['complete'])
        assert_equal(res['list_id'], 'ROOT-ID')

    def test_widget_view_incomplete(self):
        # JSON: tell the widget when it hasn't been configured
        assert_false(self.node_addon.complete)
        assert_equal(self.node_addon.mendeley_list_id, None)
        res = views.mendeley_widget(node_addon=self.node_addon,
                                    project=self.project,
                                    node=self.node,
                                    nid=self.node_addon._id,
                                    pid=self.project._id,
                                    auth=self.user.auth)
        assert_false(res['complete'])
        assert_is_none(res['list_id'])

    @httpretty.activate
    def test_mendeley_citation_list_root(self):

        httpretty.register_uri(
            httpretty.GET,
            urlparse.urljoin(API_URL, 'folders'),
            body=mock_responses['folders'],
            content_type='application/json'
        )

        res = self.app.get(
            self.project.api_url_for('mendeley_citation_list'),
            auth=self.user.auth
        )
        root = res.json['contents'][0]
        assert_equal(root['kind'], 'folder')
        assert_equal(root['id'], 'ROOT')
        assert_equal(root['parent_list_id'], '__')

    @httpretty.activate
    def test_mendeley_citation_list_non_root(self):

        httpretty.register_uri(
            httpretty.GET,
            urlparse.urljoin(API_URL, 'folders'),
            body=mock_responses['folders'],
            content_type='application/json'
        )

        httpretty.register_uri(
            httpretty.GET,
            urlparse.urljoin(API_URL, 'documents'),
            body=mock_responses['documents'],
            content_type='application/json'
        )

        res = self.app.get(
            self.project.api_url_for('mendeley_citation_list', mendeley_list_id='ROOT'),
            auth=self.user.auth
        )

        children = res.json['contents']
        assert_equal(len(children), 7)
        assert_equal(children[0]['kind'], 'folder')
        assert_equal(children[1]['kind'], 'file')
        assert_true(children[1].get('csl') is not None)

    @httpretty.activate
    def test_mendeley_citation_list_non_linked_or_child_non_authorizer(self):

        non_authorizing_user = AuthUserFactory()
        self.project.add_contributor(non_authorizing_user, save=True)

        self.node_addon.mendeley_list_id = 'e843da05-8818-47c2-8c37-41eebfc4fe3f'
        self.node_addon.save()

        httpretty.register_uri(
            httpretty.GET,
            urlparse.urljoin(API_URL, 'folders'),
            body=mock_responses['folders'],
            content_type='application/json'
        )

        httpretty.register_uri(
            httpretty.GET,
            urlparse.urljoin(API_URL, 'documents'),
            body=mock_responses['documents'],
            content_type='application/json'
        )

        res = self.app.get(
            self.project.api_url_for('mendeley_citation_list', mendeley_list_id='ROOT'),
            auth=non_authorizing_user.auth,
            expect_errors=True
        )
        assert_equal(res.status_code, 403)
Ejemplo n.º 9
0
class MendeleyViewsTestCase(OsfTestCase):
    def setUp(self):
        super(MendeleyViewsTestCase, self).setUp()
        self.account = MendeleyAccountFactory()
        self.user = AuthUserFactory(external_accounts=[self.account])
        self.account.display_name = self.user.fullname
        self.account.save()
        self.user_addon = MendeleyUserSettingsFactory(owner=self.user, external_account=self.account)
        self.project = ProjectFactory(creator=self.user)
        self.node_addon = MendeleyNodeSettingsFactory(owner=self.project)
        self.node_addon.set_auth(external_account=self.account, user=self.user)
        self.provider = MendeleyCitationsProvider()
        # self.user_addon.grant_oauth_access(self.node_addon, self.account, metadata={'lists': 'list'})
        self.node = MockNode()
        self.node.addon = self.node_addon
        self.id_patcher = mock.patch("website.addons.mendeley.model.Mendeley.client_id")
        self.secret_patcher = mock.patch("website.addons.mendeley.model.Mendeley.client_secret")
        self.id_patcher.__get__ = mock.Mock(return_value="1234567890asdf")
        self.secret_patcher.__get__ = mock.Mock(return_value="1234567890asdf")
        self.id_patcher.start()
        self.secret_patcher.start()

    def tearDown(self):
        self.id_patcher.stop()
        self.secret_patcher.stop()

    @mock.patch("website.addons.mendeley.model.Mendeley.client", new_callable=mock.PropertyMock)
    def test_check_mendeley_credentials(self, mock_client):
        mock_client.side_effect = HTTPError(403)
        assert_false(self.provider.check_credentials(self.node_addon))

        mock_client.side_effect = HTTPError(402)
        with assert_raises(HTTPError):
            self.provider.check_credentials(self.node_addon)

    @mock.patch("website.addons.mendeley.views.MendeleyCitationsProvider.check_credentials")
    def test_serialize_settings_authorizer(self, mock_credentials):
        # """dict: a serialized version of user-specific addon settings"""
        mock_credentials.return_value = True
        res = self.app.get(self.project.api_url_for("mendeley_get_config"), auth=self.user.auth)
        result = res.json["result"]
        assert_true(result["nodeHasAuth"])
        assert_true(result["userHasAuth"])
        assert_true(result["userIsOwner"])
        assert_true(result["validCredentials"])
        assert_equal(result["folder"], {"name": ""})
        assert_equal(result["ownerName"], self.user.fullname)
        assert_true(result["urls"]["auth"])
        assert_true(result["urls"]["config"])
        assert_true(result["urls"]["deauthorize"])
        assert_true(result["urls"]["folders"])
        assert_true(result["urls"]["importAuth"])
        assert_true(result["urls"]["settings"])

    @mock.patch("website.addons.mendeley.views.MendeleyCitationsProvider.check_credentials")
    def test_serialize_settings_non_authorizer(self, mock_credentials):
        # """dict: a serialized version of user-specific addon settings"""
        mock_credentials.return_value = True
        non_authorizing_user = AuthUserFactory()
        self.project.add_contributor(non_authorizing_user, save=True)
        res = self.app.get(self.project.api_url_for("mendeley_get_config"), auth=non_authorizing_user.auth)
        result = res.json["result"]
        assert_true(result["nodeHasAuth"])
        assert_false(result["userHasAuth"])
        assert_false(result["userIsOwner"])
        assert_true(result["validCredentials"])
        assert_equal(result["folder"], {"name": ""})
        assert_equal(result["ownerName"], self.user.fullname)
        assert_true(result["urls"]["auth"])
        assert_true(result["urls"]["config"])
        assert_true(result["urls"]["deauthorize"])
        assert_true(result["urls"]["folders"])
        assert_true(result["urls"]["importAuth"])
        assert_true(result["urls"]["settings"])

    @mock.patch("website.addons.mendeley.provider.MendeleyCitationsProvider.check_credentials")
    def test_set_auth(self, mock_credentials):

        mock_credentials.return_value = True
        res = self.app.put_json(
            self.project.api_url_for("mendeley_add_user_auth"),
            {"external_account_id": self.account._id},
            auth=self.user.auth,
        )

        assert_equal(res.status_code, 200)

        assert_true(res.json["result"]["userHasAuth"])

        assert_equal(self.node_addon.user_settings, self.user_addon)
        assert_equal(self.node_addon.external_account, self.account)

    def test_remove_user_auth(self):
        self.node_addon.set_auth(self.account, self.user)

        res = self.app.delete_json(
            self.project.api_url_for("mendeley_remove_user_auth"),
            {"external_account_id": self.account._id},
            auth=self.user.auth,
        )

        assert_equal(res.status_code, 200)

        self.node_addon.reload()

        assert_is_none(self.node_addon.user_settings)
        assert_is_none(self.node_addon.external_account)

    @mock.patch("website.addons.mendeley.model.Mendeley._folder_metadata")
    def test_set_config_owner(self, mock_metadata):
        mock_metadata.return_value = MockFolder(name="Fake Folder")
        # Settings config updates node settings
        self.node_addon.associated_user_settings = []
        self.node_addon.save()
        res = self.app.put_json(
            self.project.api_url_for("mendeley_set_config"),
            {"external_account_id": self.account._id, "external_list_id": "list"},
            auth=self.user.auth,
        )
        self.node_addon.reload()
        assert_equal(self.user_addon, self.node_addon.user_settings)
        serializer = MendeleySerializer(node_settings=self.node_addon, user_settings=self.user_addon)
        expected = {"result": serializer.serialized_node_settings}
        assert_equal(res.json, expected)

    @mock.patch("website.addons.mendeley.model.Mendeley._folder_metadata")
    def test_set_config_not_owner(self, mock_metadata):
        mock_metadata.return_value = MockFolder(name="Fake Folder")
        user = AuthUserFactory()
        user.add_addon("mendeley")
        self.project.add_contributor(user)
        self.project.save()
        res = self.app.put_json(
            self.project.api_url_for("mendeley_set_config"),
            {"external_account_id": self.account._id, "external_list_id": "list"},
            auth=user.auth,
        )
        self.node_addon.reload()
        assert_equal(self.user_addon, self.node_addon.user_settings)
        serializer = MendeleySerializer(node_settings=self.node_addon, user_settings=None)
        expected = {"result": serializer.serialized_node_settings}
        assert_equal(res.json, expected)

    def test_mendeley_widget_view_complete(self):
        # JSON: everything a widget needs
        assert_false(self.node_addon.complete)
        assert_equal(self.node_addon.mendeley_list_id, None)
        self.node_addon.set_target_folder("ROOT-ID", "ROOT", auth=Auth(user=self.user))
        url = self.project.api_url_for("mendeley_widget")
        res = self.app.get(url, auth=self.user.auth).json

        assert_true(res["complete"])
        assert_equal(res["list_id"], "ROOT-ID")

    def test_widget_view_incomplete(self):
        # JSON: tell the widget when it hasn't been configured
        assert_false(self.node_addon.complete)
        assert_equal(self.node_addon.mendeley_list_id, None)
        url = self.project.api_url_for("mendeley_widget")
        res = self.app.get(url, auth=self.user.auth).json

        assert_false(res["complete"])
        assert_is_none(res["list_id"])

    @httpretty.activate
    def test_mendeley_citation_list_root(self):

        httpretty.register_uri(
            httpretty.GET,
            urlparse.urljoin(API_URL, "folders"),
            body=mock_responses["folders"],
            content_type="application/json",
        )

        res = self.app.get(self.project.api_url_for("mendeley_citation_list"), auth=self.user.auth)
        root = res.json["contents"][0]
        assert_equal(root["kind"], "folder")
        assert_equal(root["id"], "ROOT")
        assert_equal(root["parent_list_id"], "__")

    @httpretty.activate
    def test_mendeley_citation_list_non_root(self):

        httpretty.register_uri(
            httpretty.GET,
            urlparse.urljoin(API_URL, "folders"),
            body=mock_responses["folders"],
            content_type="application/json",
        )

        httpretty.register_uri(
            httpretty.GET,
            urlparse.urljoin(API_URL, "documents"),
            body=mock_responses["documents"],
            content_type="application/json",
        )

        res = self.app.get(
            self.project.api_url_for("mendeley_citation_list", mendeley_list_id="ROOT"), auth=self.user.auth
        )

        children = res.json["contents"]
        assert_equal(len(children), 7)
        assert_equal(children[0]["kind"], "folder")
        assert_equal(children[1]["kind"], "file")
        assert_true(children[1].get("csl") is not None)

    @httpretty.activate
    def test_mendeley_citation_list_non_linked_or_child_non_authorizer(self):

        non_authorizing_user = AuthUserFactory()
        self.project.add_contributor(non_authorizing_user, save=True)

        self.node_addon.mendeley_list_id = "e843da05-8818-47c2-8c37-41eebfc4fe3f"
        self.node_addon.save()

        httpretty.register_uri(
            httpretty.GET,
            urlparse.urljoin(API_URL, "folders"),
            body=mock_responses["folders"],
            content_type="application/json",
        )

        httpretty.register_uri(
            httpretty.GET,
            urlparse.urljoin(API_URL, "documents"),
            body=mock_responses["documents"],
            content_type="application/json",
        )

        res = self.app.get(
            self.project.api_url_for("mendeley_citation_list", mendeley_list_id="ROOT"),
            auth=non_authorizing_user.auth,
            expect_errors=True,
        )
        assert_equal(res.status_code, 403)