def test_get_targets(self):
     now = datetime.datetime.utcnow()
     records = [
         BoxAccountFactory(expires_at=now + datetime.timedelta(days=4)),
         BoxAccountFactory(expires_at=now + datetime.timedelta(days=2)),
         GoogleDriveAccountFactory(expires_at=now +
                                   datetime.timedelta(days=4)),
         GoogleDriveAccountFactory(expires_at=now +
                                   datetime.timedelta(days=2)),
         MendeleyAccountFactory(expires_at=now +
                                datetime.timedelta(days=4)),
         MendeleyAccountFactory(expires_at=now +
                                datetime.timedelta(days=2)),
     ]
     box_targets = list(
         get_targets(delta=relativedelta(days=-3), addon_short_name='box'))
     drive_targets = list(
         get_targets(delta=relativedelta(days=-3),
                     addon_short_name='googledrive'))
     mendeley_targets = list(
         get_targets(delta=relativedelta(days=-3),
                     addon_short_name='mendeley'))
     assert_equal(records[1]._id, box_targets[0]._id)
     assert_not_in(records[0], box_targets)
     assert_equal(records[3]._id, drive_targets[0]._id)
     assert_not_in(records[2], drive_targets)
     assert_equal(records[5]._id, mendeley_targets[0]._id)
     assert_not_in(records[4], mendeley_targets)
Beispiel #2
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)
Beispiel #3
0
    def test_one_user_with_multiple_addons(self):
        results = AddonSnapshot().get_events()
        github_res = [
            res for res in results if res['provider']['name'] == 'github'
        ][0]
        googledrive_res = [
            res for res in results if res['provider']['name'] == 'googledrive'
        ][0]
        assert_equal(github_res['users']['total'], 1)
        assert_equal(googledrive_res['users']['total'], 0)

        self.user.add_addon('googledrive')
        oauth_settings = GoogleDriveAccountFactory()
        oauth_settings.save()
        self.user.external_accounts.append(oauth_settings)
        self.user.save()
        results = AddonSnapshot().get_events()
        github_res = [
            res for res in results if res['provider']['name'] == 'github'
        ][0]
        googledrive_res = [
            res for res in results if res['provider']['name'] == 'googledrive'
        ][0]
        assert_equal(github_res['users']['total'], 1)
        assert_equal(googledrive_res['users']['total'], 1)
Beispiel #4
0
 def test_fetch_access_token_with_token_not_expired(self):
     external_account = GoogleDriveAccountFactory()
     self.user.external_accounts.append(external_account)
     external_account.expires_at = datetime.utcnow() + relativedelta.relativedelta(minutes=6)
     external_account.oauth_key = 'fake-token'
     external_account.save()
     self.node_settings.set_auth(external_account, self.user)
     assert_equal(self.node_settings.fetch_access_token(), 'fake-token')
Beispiel #5
0
    def setUp(self):
        super(TestGoogleDriveUserSettings, self).setUp()
        self.node = ProjectFactory()
        self.user = self.node.creator

        self.external_account = GoogleDriveAccountFactory()

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

        self.user_settings = self.user.get_or_add_addon('googledrive')
    def test_migration(self):
        GoogleDriveUserSettings.remove()

        user = UserFactory()
        node = ProjectFactory(creator=user)
        account = GoogleDriveAccountFactory()

        user.external_accounts = [account]

        user.add_addon('googledrive', auth=Auth(user))
        user_addon = user.get_addon('googledrive')
        user_addon.save()


        node.add_addon('googledrive', auth=Auth(user))
        node_addon = node.get_addon('googledrive')
        node_addon.foreign_user_settings = user_addon
        node_addon.folder_id = 'abcdef0'
        node_addon.folder_path = '/'
        node_addon.save()

        assert_equal(node_addon.external_account, None)
        assert_equal(node_addon.folder_id, 'abcdef0')

        do_migration()
        node_addon.reload()

        assert_equal(node_addon.external_account, account)
        assert_equal(node_addon.folder_id, 'abcdef0')
        assert_equal(node_addon.folder_path, '/')
        assert_equal(node_addon.folder_name, '/ (Full Google Drive)')
Beispiel #7
0
 def test_serialize_credentials(self):
     external_account = GoogleDriveAccountFactory()
     self.user.external_accounts.append(external_account)
     self.node_settings.set_auth(external_account, self.user)
     credentials = self.node_settings.serialize_waterbutler_credentials()
     expected = {'token': self.node_settings.fetch_access_token()}
     assert_equal(credentials, expected)
Beispiel #8
0
    def test_one_user_with_multiple_addons(self):
        results = AddonSnapshot().get_events()
        github_res = [res for res in results if res['provider']['name'] == 'github'][0]
        googledrive_res = [res for res in results if res['provider']['name'] == 'googledrive'][0]
        assert_equal(github_res['users']['total'], 1)
        assert_equal(googledrive_res['users']['total'], 0)

        self.user.add_addon('googledrive')
        oauth_settings = GoogleDriveAccountFactory()
        oauth_settings.save()
        self.user.external_accounts.append(oauth_settings)
        self.user.save()
        results = AddonSnapshot().get_events()
        github_res = [res for res in results if res['provider']['name'] == 'github'][0]
        googledrive_res = [res for res in results if res['provider']['name'] == 'googledrive'][0]
        assert_equal(github_res['users']['total'], 1)
        assert_equal(googledrive_res['users']['total'], 1)
Beispiel #9
0
    def test_one_user_with_multiple_addons(self):
        results = AddonSnapshot().get_events()
        github_res = [res for res in results if res["provider"]["name"] == "github"][0]
        googledrive_res = [res for res in results if res["provider"]["name"] == "googledrive"][0]
        assert_equal(github_res["users"]["enabled"], 1)
        assert_equal(googledrive_res["users"]["enabled"], 0)

        self.user.add_addon("googledrive")
        oauth_settings = GoogleDriveAccountFactory()
        oauth_settings.save()
        self.user.external_accounts.append(oauth_settings)
        self.user.save()
        results = AddonSnapshot().get_events()
        github_res = [res for res in results if res["provider"]["name"] == "github"][0]
        googledrive_res = [res for res in results if res["provider"]["name"] == "googledrive"][0]
        assert_equal(github_res["users"]["enabled"], 1)
        assert_equal(googledrive_res["users"]["enabled"], 1)
Beispiel #10
0
    def test_set_auth_wrong_user(self):
        external_account = GoogleDriveAccountFactory()
        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())
Beispiel #11
0
 def test_list_googledrive_accounts_returns_accounts_multiple(self):
     external_account = GoogleDriveAccountFactory()
     self.user.external_accounts.append(
         external_account)  # self.account is already present
     self.user.save()
     url = api_url_for('list_googledrive_user_accounts')
     res = self.app.get(url)
     assert_equal(res.status_code, 200)
     assert_equal(len(res.json['accounts']), 2)
Beispiel #12
0
    def setUp(self):
        super(TestGoogleDriveUserSettings, self).setUp()
        self.node = ProjectFactory()
        self.user = self.node.creator

        self.external_account = GoogleDriveAccountFactory()

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

        self.user_settings = self.user.get_or_add_addon('googledrive')
Beispiel #13
0
    def test_has_auth_true(self):
        external_account = GoogleDriveAccountFactory()
        self.user.external_accounts.append(external_account)

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

        self.node_settings.folder_id = None
        assert_true(self.node_settings.has_auth)

        self.node_settings.folder_id = 'totally fake ID'
        assert_true(self.node_settings.has_auth)
Beispiel #14
0
    def test_clear_auth(self):
        self.node_settings.external_account = GoogleDriveAccountFactory()
        self.node_settings.folder_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.folder_id)
        assert_is_none(self.node_settings.user_settings)
        assert_is_none(self.node_settings.folder_path)
        assert_is_none(self.node_settings.folder_name)
Beispiel #15
0
 def setUp(self):
     super(TestGoogleDriveConfigViews, self).setUp()
     self.account = GoogleDriveAccountFactory()
     self.user = AuthUserFactory(external_accounts=[self.account])
     self.user_settings = self.user.get_or_add_addon('googledrive')
     self.project = ProjectFactory(creator=self.user)
     self.project.add_addon('googledrive', Auth(self.user))
     self.node_settings = self.project.get_addon('googledrive')
     self.node_settings.user_settings = self.user_settings
     self.node_settings.save()
     self.user_settings.save()
     # Log user in
     self.app.authenticate(*self.user.auth)
Beispiel #16
0
 def test_fetch_access_token_with_token_not_expired(self):
     external_account = GoogleDriveAccountFactory()
     self.user.external_accounts.append(external_account)
     external_account.expires_at = datetime.utcnow(
     ) + relativedelta.relativedelta(minutes=6)
     external_account.oauth_key = 'fake-token'
     external_account.save()
     self.node_settings.set_auth(external_account, self.user)
     assert_equal(self.node_settings.fetch_access_token(), 'fake-token')
Beispiel #17
0
    def test_verify_oauth_access_no_metadata(self):
        self.user_settings.grant_oauth_access(
            node=self.node,
            external_account=self.external_account,
        )
        self.user_settings.save()

        account_has_access = self.user_settings.verify_oauth_access(
            node=self.node, external_account=self.external_account)

        factory_account_has_access = self.user_settings.verify_oauth_access(
            node=self.node, external_account=GoogleDriveAccountFactory())

        assert_true(account_has_access)
        assert_false(factory_account_has_access)
Beispiel #18
0
    def test_one_node_with_multiple_addons(self):
        results = AddonSnapshot().get_events()
        github_res = [res for res in results if res["provider"]["name"] == "github"][0]
        googledrive_res = [res for res in results if res["provider"]["name"] == "googledrive"][0]
        assert_equal(github_res["nodes"]["total"], 1)
        assert_equal(googledrive_res["nodes"]["total"], 0)

        self.user.add_addon("googledrive")
        user_addon = self.user.get_addon("googledrive")
        oauth_settings = GoogleDriveAccountFactory()
        oauth_settings.save()
        self.user.external_accounts.append(oauth_settings)
        self.user.save()
        self.node.add_addon("googledrive", Auth(self.user))
        node_addon = self.node.get_addon("googledrive")
        node_addon.user = self.user.fullname
        node_addon.user_settings = user_addon
        node_addon.external_account = oauth_settings
        node_addon.save()
        results = AddonSnapshot().get_events()
        github_res = [res for res in results if res["provider"]["name"] == "github"][0]
        googledrive_res = [res for res in results if res["provider"]["name"] == "googledrive"][0]
        assert_equal(github_res["nodes"]["total"], 1)
        assert_equal(googledrive_res["nodes"]["total"], 1)
 def test_refresh(self, mock_box_refresh, mock_drive_refresh,
                  mock_mendeley_refresh):
     fake_box_account = BoxAccountFactory(
         expires_at=datetime.datetime.utcnow())
     fake_drive_account = GoogleDriveAccountFactory(
         expires_at=datetime.datetime.utcnow())
     fake_mendeley_account = MendeleyAccountFactory(
         expires_at=datetime.datetime.utcnow())
     for addon in self.addons:
         Provider = look_up_provider(addon)
         main(delta=relativedelta(days=-3),
              Provider=Provider,
              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)
Beispiel #20
0
    def test_has_auth_false(self):
        external_account = GoogleDriveAccountFactory()

        assert_false(self.node_settings.has_auth)

        # both external_account and user_settings must be set to have auth
        self.node_settings.external_account = external_account
        assert_false(self.node_settings.has_auth)

        self.node_settings.external_account = None
        self.node_settings.user_settings = self.user_settings
        assert_false(self.node_settings.has_auth)

        # set_auth must be called to have auth
        self.node_settings.external_account = external_account
        self.node_settings.user_settings = self.user_settings
        assert_false(self.node_settings.has_auth)
Beispiel #21
0
    def test_set_target_folder(self):

        folder = {
            'id': 'fake-folder-id',
            'name': 'fake-folder-name',
            'path': 'fake_path'
        }

        external_account = GoogleDriveAccountFactory()
        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.folder_id)

        self.node_settings.set_target_folder(
            folder,
            auth=Auth(user=self.user),
        )

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

        has_access = self.user_settings.verify_oauth_access(
            node=self.node,
            external_account=external_account,
            metadata={'folder': 'fake-folder-id'})

        # user_settings was updated
        assert_true(has_access)

        log = self.node.logs[-1]
        assert_equal(log.action, 'googledrive_folder_selected')
        assert_equal(log.params['folder'], folder['path'])
Beispiel #22
0
    def test_set_auth(self):
        external_account = GoogleDriveAccountFactory()
        self.user.external_accounts.append(external_account)
        self.user.save()

        # this should be reset after the call
        self.node_settings.folder_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.folder_id)

        set_auth_gives_access = self.user_settings.verify_oauth_access(
            node=self.node,
            external_account=external_account,
        )

        assert_true(set_auth_gives_access)
Beispiel #23
0
    def test_fetch_access_token_with_token_expired(self, mock_refresh):
        external_account = GoogleDriveAccountFactory()
        self.user.external_accounts.append(external_account)
        external_account.expires_at = datetime.utcnow(
        ) + relativedelta.relativedelta(minutes=4)
        external_account.oauth_key = 'fake-token'
        external_account.refresh_token = 'refresh-fake-token'
        external_account.save()

        fake_token = {
            'access_token': 'new-access-token',
            'refresh_token': 'new-refresh-token',
            'expires_at': 1234.5
        }
        mock_refresh.return_value = fake_token
        self.node_settings.set_auth(external_account, self.user)
        self.node_settings.fetch_access_token()
        mock_refresh.assert_called_once()
        assert_equal(external_account.oauth_key, 'new-access-token')
        assert_equal(external_account.refresh_token, 'new-refresh-token')
        assert_equal(external_account.expires_at,
                     datetime.utcfromtimestamp(1234.5))
Beispiel #24
0
    def test_fetch_access_token_with_token_expired(self, mock_refresh):
        external_account = GoogleDriveAccountFactory()
        self.user.external_accounts.append(external_account)
        external_account.expires_at = datetime.utcnow() + relativedelta.relativedelta(minutes=4)
        external_account.oauth_key = 'fake-token'
        external_account.refresh_token = 'refresh-fake-token'
        external_account.save()

        fake_token = {
            'access_token': 'new-access-token',
            'refresh_token': 'new-refresh-token',
            'expires_at': 1234.5
        }
        mock_refresh.return_value = fake_token
        self.node_settings.set_auth(external_account, self.user)
        self.node_settings.fetch_access_token()
        mock_refresh.assert_called_once()
        assert_equal(external_account.oauth_key, 'new-access-token')
        assert_equal(external_account.refresh_token, 'new-refresh-token')
        assert_equal(external_account.expires_at, datetime.utcfromtimestamp(1234.5))
Beispiel #25
0
 def test_serialize_credentials_not_authorized(self):
     external_account = GoogleDriveAccountFactory()
     self.node_settings.external_account = external_account
     with assert_raises(exceptions.AddonError):
         self.node_settings.serialize_waterbutler_credentials()
Beispiel #26
0
class TestGoogleDriveUserSettings(OsfTestCase):
    def setUp(self):
        super(TestGoogleDriveUserSettings, self).setUp()
        self.node = ProjectFactory()
        self.user = self.node.creator

        self.external_account = GoogleDriveAccountFactory()

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

        self.user_settings = self.user.get_or_add_addon('googledrive')

    def tearDown(self):
        super(TestGoogleDriveUserSettings, self).tearDown()
        self.user_settings.remove()
        self.external_account.remove()
        self.node.remove()
        self.user.remove()

    def test_grant_oauth_access_no_metadata(self):
        self.user_settings.grant_oauth_access(
            node=self.node,
            external_account=self.external_account,
        )
        self.user_settings.save()

        assert_equal(
            self.user_settings.oauth_grants,
            {self.node._id: {
                self.external_account._id: {}
            }},
        )

    def test_grant_oauth_access_metadata(self):
        self.user_settings.grant_oauth_access(
            node=self.node,
            external_account=self.external_account,
            metadata={'folder': 'fake_folder_id'})
        self.user_settings.save()

        assert_equal(
            self.user_settings.oauth_grants, {
                self.node._id: {
                    self.external_account._id: {
                        'folder': 'fake_folder_id'
                    }
                },
            })

    def test_verify_oauth_access_no_metadata(self):
        self.user_settings.grant_oauth_access(
            node=self.node,
            external_account=self.external_account,
        )
        self.user_settings.save()

        account_has_access = self.user_settings.verify_oauth_access(
            node=self.node, external_account=self.external_account)

        factory_account_has_access = self.user_settings.verify_oauth_access(
            node=self.node, external_account=GoogleDriveAccountFactory())

        assert_true(account_has_access)
        assert_false(factory_account_has_access)

    def test_verify_oauth_access_metadata(self):
        self.user_settings.grant_oauth_access(
            node=self.node,
            external_account=self.external_account,
            metadata={'folder': 'fake_folder_id'})
        self.user_settings.save()

        correct_meta_access = self.user_settings.verify_oauth_access(
            node=self.node,
            external_account=self.external_account,
            metadata={'folder': 'fake_folder_id'})

        incorrect_meta_no_access = self.user_settings.verify_oauth_access(
            node=self.node,
            external_account=self.external_account,
            metadata={'folder': 'another_folder_id'})

        assert_true(correct_meta_access)
        assert_false(incorrect_meta_no_access)
Beispiel #27
0
class TestGoogleDriveUserSettings(OsfTestCase):
    def setUp(self):
        super(TestGoogleDriveUserSettings, self).setUp()
        self.node = ProjectFactory()
        self.user = self.node.creator

        self.external_account = GoogleDriveAccountFactory()

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

        self.user_settings = self.user.get_or_add_addon('googledrive')

    def tearDown(self):
        super(TestGoogleDriveUserSettings, self).tearDown()
        self.user_settings.remove()
        self.external_account.remove()
        self.node.remove()
        self.user.remove()

    def test_grant_oauth_access_no_metadata(self):
        self.user_settings.grant_oauth_access(
            node=self.node,
            external_account=self.external_account,
        )
        self.user_settings.save()

        assert_equal(
            self.user_settings.oauth_grants,
            {self.node._id: {self.external_account._id: {}}},
        )

    def test_grant_oauth_access_metadata(self):
        self.user_settings.grant_oauth_access(
            node=self.node,
            external_account=self.external_account,
            metadata={'folder': 'fake_folder_id'}
        )
        self.user_settings.save()

        assert_equal(
            self.user_settings.oauth_grants,
            {
                self.node._id: {
                    self.external_account._id: {'folder': 'fake_folder_id'}
                },
            }
        )

    def test_verify_oauth_access_no_metadata(self):
        self.user_settings.grant_oauth_access(
            node=self.node,
            external_account=self.external_account,
        )
        self.user_settings.save()

        account_has_access = self.user_settings.verify_oauth_access(
            node=self.node,
            external_account=self.external_account
        )

        factory_account_has_access = self.user_settings.verify_oauth_access(
            node=self.node,
            external_account=GoogleDriveAccountFactory()
        )

        assert_true(account_has_access)
        assert_false(factory_account_has_access)

    def test_verify_oauth_access_metadata(self):
        self.user_settings.grant_oauth_access(
            node=self.node,
            external_account=self.external_account,
            metadata={'folder': 'fake_folder_id'}
        )
        self.user_settings.save()

        correct_meta_access = self.user_settings.verify_oauth_access(
            node=self.node,
            external_account=self.external_account,
            metadata={'folder': 'fake_folder_id'}
        )

        incorrect_meta_no_access = self.user_settings.verify_oauth_access(
            node=self.node,
            external_account=self.external_account,
            metadata={'folder': 'another_folder_id'}
        )

        assert_true(correct_meta_access)
        assert_false(incorrect_meta_no_access)
Beispiel #28
0
 def test_serialize_credentials_not_authorized(self, mock_refresh):
     mock_refresh.return_value = True
     external_account = GoogleDriveAccountFactory()
     self.node_settings.external_account = external_account
     with assert_raises(exceptions.AddonError):
         self.node_settings.serialize_waterbutler_credentials()