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()
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)
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()
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()
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)
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 setUp(self): super(TestClientHelpers, self).setUp() self.user_settings = BoxUserSettingsFactory() self.node_settings = BoxNodeSettingsFactory(user_settings=self.user_settings) self.user = self.user_settings.owner self.node = self.node_settings.owner
def setUp(self): super(TestNodeSettingsCallbacks, self).setUp() # Create node settings with auth self.user_settings = BoxUserSettingsFactory(access_token='123abc') self.node_settings = BoxNodeSettingsFactory( user_settings=self.user_settings, ) self.project = self.node_settings.owner self.user = self.user_settings.owner
def setUp(self): super(TestNodeSettingsCallbacks, self).setUp() # Create node settings with auth self.user_settings = BoxUserSettingsFactory(access_token='123abc') self.node_settings = BoxNodeSettingsFactory( user_settings=self.user_settings, ) self.external_account = BoxAccountFactory() self.user_settings.owner.external_accounts.append( self.external_account) self.node_settings.external_account = self.external_account self.project = self.node_settings.owner self.user = self.user_settings.owner self.user_settings.grant_oauth_access( node=self.project, external_account=self.external_account, )
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)
def test_clear_auth(self): node_settings = BoxNodeSettingsFactory() node_settings.external_account = BoxAccountFactory() node_settings.user_settings = BoxUserSettingsFactory() node_settings.save() node_settings.clear_auth() assert_is_none(node_settings.external_account) assert_is_none(node_settings.folder_id) assert_is_none(node_settings.user_settings)
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)
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)
def setUp(self): super(TestNodeSettingsCallbacks, self).setUp() # Create node settings with auth self.user_settings = BoxUserSettingsFactory(access_token='123abc') self.node_settings = BoxNodeSettingsFactory( user_settings=self.user_settings, ) self.external_account = BoxAccountFactory() self.user_settings.owner.external_accounts.append(self.external_account) self.node_settings.external_account = self.external_account self.project = self.node_settings.owner self.user = self.user_settings.owner self.user_settings.grant_oauth_access( node=self.project, external_account=self.external_account, )
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()
class TestNodeSettingsCallbacks(OsfTestCase): def setUp(self): super(TestNodeSettingsCallbacks, self).setUp() # Create node settings with auth self.user_settings = BoxUserSettingsFactory(access_token='123abc') self.node_settings = BoxNodeSettingsFactory( user_settings=self.user_settings, ) self.external_account = BoxAccountFactory() self.user_settings.owner.external_accounts.append(self.external_account) self.node_settings.external_account = self.external_account self.project = self.node_settings.owner self.user = self.user_settings.owner self.user_settings.grant_oauth_access( node=self.project, external_account=self.external_account, ) def test_after_fork_by_authorized_box_user(self): fork = ProjectFactory() clone, message = self.node_settings.after_fork( node=self.project, fork=fork, user=self.user_settings.owner ) print(message) assert_equal(clone.user_settings, self.user_settings) def test_after_fork_by_unauthorized_box_user(self): fork = ProjectFactory() user = UserFactory() clone, message = self.node_settings.after_fork( node=self.project, fork=fork, user=user, save=True ) # need request context for url_for assert_is(clone.user_settings, None) def test_before_fork(self): node = ProjectFactory() message = self.node_settings.before_fork(node, self.user) assert_true(message) def test_before_remove_contributor_message(self): message = self.node_settings.before_remove_contributor( self.project, self.user) assert_true(message) assert_in(self.user.fullname, message) assert_in(self.project.project_or_component, message) def test_after_remove_authorized_box_user_not_self(self): message = self.node_settings.after_remove_contributor( self.project, self.user_settings.owner) self.node_settings.save() assert_is_none(self.node_settings.user_settings) assert_true(message) assert_in("You can re-authenticate", message) def test_after_remove_authorized_box_user_self(self): auth = Auth(user=self.user_settings.owner) message = self.node_settings.after_remove_contributor( self.project, self.user_settings.owner, auth) self.node_settings.save() assert_is_none(self.node_settings.user_settings) assert_true(message) assert_not_in("You can re-authenticate", message) def test_after_delete(self): self.project.remove_node(Auth(user=self.project.creator)) # Ensure that changes to node settings have been saved self.node_settings.reload() assert_is_none(self.node_settings.user_settings) assert_is_none(self.node_settings.folder_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)
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 TestNodeSettingsCallbacks(OsfTestCase): def setUp(self): super(TestNodeSettingsCallbacks, self).setUp() # Create node settings with auth self.user_settings = BoxUserSettingsFactory(access_token='123abc') self.node_settings = BoxNodeSettingsFactory( user_settings=self.user_settings, ) self.external_account = BoxAccountFactory() self.user_settings.owner.external_accounts.append( self.external_account) self.node_settings.external_account = self.external_account self.project = self.node_settings.owner self.user = self.user_settings.owner self.user_settings.grant_oauth_access( node=self.project, external_account=self.external_account, ) def test_after_fork_by_authorized_box_user(self): fork = ProjectFactory() clone, message = self.node_settings.after_fork( node=self.project, fork=fork, user=self.user_settings.owner) print(message) assert_equal(clone.user_settings, self.user_settings) def test_after_fork_by_unauthorized_box_user(self): fork = ProjectFactory() user = UserFactory() clone, message = self.node_settings.after_fork(node=self.project, fork=fork, user=user, save=True) # need request context for url_for assert_is(clone.user_settings, None) def test_before_fork(self): node = ProjectFactory() message = self.node_settings.before_fork(node, self.user) assert_true(message) def test_before_remove_contributor_message(self): message = self.node_settings.before_remove_contributor( self.project, self.user) assert_true(message) assert_in(self.user.fullname, message) assert_in(self.project.project_or_component, message) def test_after_remove_authorized_box_user_not_self(self): message = self.node_settings.after_remove_contributor( self.project, self.user_settings.owner) self.node_settings.save() assert_is_none(self.node_settings.user_settings) assert_true(message) assert_in("You can re-authenticate", message) def test_after_remove_authorized_box_user_self(self): auth = Auth(user=self.user_settings.owner) message = self.node_settings.after_remove_contributor( self.project, self.user_settings.owner, auth) self.node_settings.save() assert_is_none(self.node_settings.user_settings) assert_true(message) assert_not_in("You can re-authenticate", message) def test_after_delete(self): self.project.remove_node(Auth(user=self.project.creator)) # Ensure that changes to node settings have been saved self.node_settings.reload() assert_is_none(self.node_settings.user_settings) assert_is_none(self.node_settings.folder_id)