Пример #1
0
    def test_delete(self, mock_requests):
        user_settings = BoxUserSettingsFactory()
        assert_true(user_settings.has_auth)
        user_settings.delete()
        user_settings.save()

        assert_false(user_settings.user_id)
        assert_true(user_settings.deleted)
        assert_false(user_settings.access_token)
        mock_requests.post.assert_called_once()
Пример #2
0
    def test_has_auth(self):
        oauth_settings = BoxOAuthSettings(user_id='foo', username='******')
        oauth_settings.save()

        user_settings = BoxUserSettingsFactory(oauth_settings=oauth_settings)

        assert_false(user_settings.has_auth)
        user_settings.access_token = '12345'
        user_settings.save()
        assert_true(user_settings.has_auth)
Пример #3
0
    def test_delete(self, mock_requests):
        user_settings = BoxUserSettingsFactory()
        assert_true(user_settings.has_auth)
        user_settings.delete()
        user_settings.save()

        assert_false(user_settings.user_id)
        assert_true(user_settings.deleted)
        assert_false(user_settings.access_token)
        mock_requests.post.assert_called_once()
Пример #4
0
    def test_has_auth(self):
        oauth_settings = BoxOAuthSettings(user_id='foo', username='******')
        oauth_settings.save()

        user_settings = BoxUserSettingsFactory(oauth_settings=oauth_settings)

        assert_false(user_settings.has_auth)
        user_settings.access_token = '12345'
        user_settings.save()
        assert_true(user_settings.has_auth)
Пример #5
0
    def test_clear(self, mock_requests):
        node_settings = BoxNodeSettingsFactory()
        user_settings = BoxUserSettingsFactory()
        node_settings.user_settings = user_settings
        node_settings.save()

        assert_true(user_settings.access_token)
        user_settings.clear()
        user_settings.save()

        assert_false(user_settings.user_id)
        assert_false(user_settings.access_token)
        mock_requests.post.assert_called_once()
Пример #6
0
    def test_clear_clears_associated_node_settings(self, mock_requests):
        node_settings = BoxNodeSettingsFactory()
        user_settings = BoxUserSettingsFactory()
        node_settings.user_settings = user_settings
        node_settings.save()

        user_settings.clear()
        user_settings.save()

        # Node settings no longer associated with user settings
        assert_is(node_settings.folder_id, None)
        assert_is(node_settings.user_settings, None)
        mock_requests.post.assert_called_once()
Пример #7
0
    def test_clear(self, mock_requests):
        node_settings = BoxNodeSettingsFactory()
        user_settings = BoxUserSettingsFactory()
        node_settings.user_settings = user_settings
        node_settings.save()

        assert_true(user_settings.access_token)
        user_settings.clear()
        user_settings.save()

        assert_false(user_settings.user_id)
        assert_false(user_settings.access_token)
        mock_requests.post.assert_called_once()
Пример #8
0
    def test_clear_clears_associated_node_settings(self, mock_requests):
        node_settings = BoxNodeSettingsFactory()
        user_settings = BoxUserSettingsFactory()
        node_settings.user_settings = user_settings
        node_settings.save()

        user_settings.clear()
        user_settings.save()

        # Node settings no longer associated with user settings
        assert_is(node_settings.folder_id, None)
        assert_is(node_settings.user_settings, None)
        mock_requests.post.assert_called_once()
Пример #9
0
    def test_clear_wo_oauth_settings(self, mock_requests):
        user_settings = BoxUserSettingsFactory()
        user_settings.oauth_settings = None
        user_settings.save()
        node_settings = BoxNodeSettingsFactory()
        node_settings.user_settings = user_settings
        node_settings.save()

        assert_false(user_settings.oauth_settings)
        user_settings.clear()
        user_settings.save()

        assert_false(user_settings.user_id)
        assert_false(user_settings.access_token)
        assert_false(mock_requests.post.called)
Пример #10
0
    def test_clear_wo_oauth_settings(self, mock_requests):
        user_settings = BoxUserSettingsFactory()
        user_settings.oauth_settings = None
        user_settings.save()
        node_settings = BoxNodeSettingsFactory()
        node_settings.user_settings = user_settings
        node_settings.save()

        assert_false(user_settings.oauth_settings)
        user_settings.clear()
        user_settings.save()

        assert_false(user_settings.user_id)
        assert_false(user_settings.access_token)
        assert_false(mock_requests.post.called)
Пример #11
0
    def test_set_user_auth(self):
        node_settings = BoxNodeSettingsFactory()
        user_settings = BoxUserSettingsFactory()
        external_account = BoxAccountFactory()

        user_settings.owner.external_accounts.append(external_account)
        user_settings.save()

        node_settings.external_account = external_account
        node_settings.set_user_auth(user_settings)
        node_settings.save()

        assert_true(node_settings.has_auth)
        assert_equal(node_settings.user_settings, user_settings)
        # A log was saved
        last_log = node_settings.owner.logs[-1]
        assert_equal(last_log.action, 'box_node_authorized')
        log_params = last_log.params
        assert_equal(log_params['folder_id'], node_settings.folder_id)
        assert_equal(log_params['node'], node_settings.owner._primary_key)
        assert_equal(last_log.user, user_settings.owner)
Пример #12
0
    def test_set_user_auth(self):
        node_settings = BoxNodeSettingsFactory()
        user_settings = BoxUserSettingsFactory()
        external_account = BoxAccountFactory()

        user_settings.owner.external_accounts.append(external_account)
        user_settings.save()

        node_settings.external_account = external_account
        node_settings.set_user_auth(user_settings)
        node_settings.save()

        assert_true(node_settings.has_auth)
        assert_equal(node_settings.user_settings, user_settings)
        # A log was saved
        last_log = node_settings.owner.logs[-1]
        assert_equal(last_log.action, 'box_node_authorized')
        log_params = last_log.params
        assert_equal(log_params['folder_id'], node_settings.folder_id)
        assert_equal(log_params['node'], node_settings.owner._primary_key)
        assert_equal(last_log.user, user_settings.owner)
class TestBoxMigration(OsfTestCase):
    # Note: BoxUserSettings.user_settings has to be changed to foreign_user_settings (model and mongo). See migration instructions

    def setUp(self):
        super(TestBoxMigration, self).setUp()
        self.user1 = UserFactory()
        self.user1_settings = BoxUserSettingsFactory(owner=self.user1)
        self.user1_settings.oauth_settings = BoxOAuthSettingsFactory()
        self.user1_settings.save()

    def tearDown(self):
        super(TestBoxMigration, self).tearDown()
        BoxUserSettings.remove()
        ExternalAccount.remove()

    def test_migration_no_project(self):
        self.old_oauth = self.user1_settings.oauth_settings
        do_migration([self.user1_settings])
        self.user1_settings.reload()

        assert_is_none(self.user1_settings.oauth_settings)
        assert_equal(len(self.user1.external_accounts), 1)

        account = self.user1_settings.owner.external_accounts[0]
        assert_is_none(self.user1_settings.oauth_settings)
        assert_equal(account.provider, "box")
        assert_equal(account.oauth_key, self.old_oauth.access_token)

    def test_migration_removes_targets(self):
        project = ProjectFactory(creator=self.user1)

        project.add_addon("box", auth=Auth(self.user1))
        node_addon = project.get_addon("box")
        node_addon.foreign_user_settings = self.user1_settings
        node_addon.save()

        assert_equal(get_targets().count(), 1)

        do_migration([self.user1_settings])
        self.user1_settings.reload()

        assert_equal(get_targets().count(), 0)

    def test_migration_multiple_users(self):
        self.user2 = UserFactory()
        self.user2_settings = BoxUserSettingsFactory(owner=self.user2)
        self.user2_settings.oauth_settings = self.user1_settings.oauth_settings
        self.user2_settings.save()

        do_migration([self.user1_settings, self.user2_settings])
        self.user1_settings.reload()
        self.user2_settings.reload()

        assert_equal(self.user1_settings.owner.external_accounts[0], self.user2_settings.owner.external_accounts[0])

    def test_get_targets(self):
        self.user2 = UserFactory()
        self.user2_settings = BoxUserSettingsFactory(owner=self.user2)
        self.user2_settings.oauth_settings = None
        self.user2_settings.save()
        addons = [self.user2_settings, self.user1_settings]
        for addon in addons:
            addon.save()
        targets = get_targets()
        assert_equal(targets.count(), 1)
        assert_equal(targets[0]._id, addons[-1]._id)
class TestBoxMigration(OsfTestCase):
    # Note: BoxUserSettings.user_settings has to be changed to foreign_user_settings (model and mongo). See migration instructions

    def setUp(self):
        super(TestBoxMigration, self).setUp()
        self.user1 = UserFactory()
        self.user1_settings = BoxUserSettingsFactory(owner=self.user1)
        self.user1_settings.oauth_settings = BoxOAuthSettingsFactory()
        self.user1_settings.save()

    def tearDown(self):
        super(TestBoxMigration, self).tearDown()
        BoxUserSettings.remove()
        ExternalAccount.remove()

    def test_migration_no_project(self):
        self.old_oauth = self.user1_settings.oauth_settings
        do_migration([self.user1_settings])
        self.user1_settings.reload()

        assert_is_none(self.user1_settings.oauth_settings)
        assert_equal(len(self.user1.external_accounts), 1)

        account = self.user1_settings.owner.external_accounts[0]
        assert_is_none(self.user1_settings.oauth_settings)
        assert_equal(account.provider, 'box')
        assert_equal(account.oauth_key, self.old_oauth.access_token)

    def test_migration_removes_targets(self):
        project = ProjectFactory(creator=self.user1)

        project.add_addon('box', auth=Auth(self.user1))
        node_addon = project.get_addon('box')
        node_addon.foreign_user_settings = self.user1_settings
        node_addon.save()

        assert_equal(get_targets().count(), 1)

        do_migration([self.user1_settings])
        self.user1_settings.reload()

        assert_equal(get_targets().count(), 0)

    def test_migration_multiple_users(self):
        self.user2 = UserFactory()
        self.user2_settings = BoxUserSettingsFactory(owner=self.user2)
        self.user2_settings.oauth_settings = self.user1_settings.oauth_settings
        self.user2_settings.save()

        do_migration([self.user1_settings, self.user2_settings])
        self.user1_settings.reload()
        self.user2_settings.reload()

        assert_equal(
            self.user1_settings.owner.external_accounts[0],
            self.user2_settings.owner.external_accounts[0],
        )

    def test_get_targets(self):
        self.user2 = UserFactory()
        self.user2_settings = BoxUserSettingsFactory(owner=self.user2)
        self.user2_settings.oauth_settings = None
        self.user2_settings.save()
        addons = [
            self.user2_settings,
            self.user1_settings,
        ]
        for addon in addons:
            addon.save()
        targets = get_targets()
        assert_equal(targets.count(), 1)
        assert_equal(targets[0]._id, addons[-1]._id)