def test_migration(self): BoxUserSettings.remove() user = UserFactory() node = ProjectFactory(creator=user) account = BoxAccountFactory() user.external_accounts = [account] user.add_addon('box', auth=Auth(user)) user_addon = user.get_addon('box') user_addon.save() node.add_addon('box', auth=Auth(user)) node_addon = node.get_addon('box') node_addon.foreign_user_settings = user_addon node_addon.folder_id = 'abcdef0' node_addon.folder_path = '/' node_addon.folder_name = '/ (Full Box)' 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 Box)')
class TestNodeSettings(OsfTestCase): def setUp(self): super(TestNodeSettings, self).setUp() self.user = UserFactory() self.project = ProjectFactory(creator=self.user) self.user.add_addon('s3') self.project.add_addon('s3', auth=Auth(self.user)) self.user_settings = self.user.get_addon('s3') self.node_settings = self.project.get_addon('s3') self.user_settings.access_key = 'We-Will-Rock-You' self.user_settings.secret_key = 'Idontknowanyqueensongs' self.user_settings.save() self.node_settings.bucket = 'Sheer-Heart-Attack' self.node_settings.user_settings = self.user_settings self.node_settings.save() def test_complete_true(self): assert_true(self.node_settings.has_auth) assert_true(self.node_settings.complete) def test_complete_false(self): self.node_settings.bucket = None assert_true(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_complete_auth_false(self): self.node_settings.user_settings = None assert_false(self.node_settings.has_auth) assert_false(self.node_settings.complete)
class TestCallbacks(OsfTestCase): def setUp(self): super(TestCallbacks, self).setUp() self.user = UserFactory() self.user.add_addon('twofactor') self.user_settings = self.user.get_addon('twofactor') def test_add_to_user(self): assert_equal(self.user_settings.totp_drift, 0) assert_is_not_none(self.user_settings.totp_secret) assert_false(self.user_settings.is_confirmed) def test_remove_from_unconfirmed_user(self): # drift defaults to 0. Change it so we can test it was changed back. self.user_settings.totp_drift = 1 self.user_settings.save() self.user.delete_addon('twofactor') assert_equal(self.user_settings.totp_drift, 0) assert_is_none(self.user_settings.totp_secret) assert_false(self.user_settings.is_confirmed) def test_remove_from_confirmed_user(self): # drift defaults to 0. Change it so we can test it was changed back. self.user_settings.totp_drift = 1 self.user_settings.is_confirmed = True self.user_settings.save() self.user.delete_addon('twofactor') assert_equal(self.user_settings.totp_drift, 0) assert_is_none(self.user_settings.totp_secret) assert_false(self.user_settings.is_confirmed)
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)')
class TestCore(OsfTestCase): @mock.patch('website.addons.twofactor.models.push_status_message') def setUp(self, mocked): super(TestCore, self).setUp() self.user = UserFactory() self.user.set_password('badpassword') self.user.save() self.user.add_addon('twofactor') self.user_settings = self.user.get_addon('twofactor') self.user_settings.is_confirmed = True self.user_settings.save() def test_login_valid(self): res = login(username=self.user.username, password='******', two_factor=_valid_code(self.user_settings.totp_secret)) assert_true(isinstance(res, BaseResponse)) assert_equal(res.status_code, 302) def test_login_invalid_code(self): with assert_raises(TwoFactorValidationError): login(username=self.user.username, password='******', two_factor='000000') def test_login_valid_code_invalid_password(self): with assert_raises(PasswordIncorrectError): login(username=self.user.username, password='******', two_factor=_valid_code(self.user_settings.totp_secret))
class TestGithubNodeSettings(OsfTestCase): def setUp(self): OsfTestCase.setUp(self) self.user = UserFactory() self.user.add_addon('github') self.user_settings = self.user.get_addon('github') self.external_account = GitHubAccountFactory() self.user_settings.owner.external_accounts.append( self.external_account) self.user_settings.owner.save() self.node_settings = GitHubNodeSettingsFactory( user_settings=self.user_settings) @mock.patch('website.addons.github.api.GitHubClient.delete_hook') def test_delete_hook(self, mock_delete_hook): self.node_settings.hook_id = 'hook' self.node_settings.save() args = ( self.node_settings.user, self.node_settings.repo, self.node_settings.hook_id, ) res = self.node_settings.delete_hook() assert_true(res) mock_delete_hook.assert_called_with(*args) @mock.patch('website.addons.github.api.GitHubClient.delete_hook') def test_delete_hook_no_hook(self, mock_delete_hook): res = self.node_settings.delete_hook() assert_false(res) assert_false(mock_delete_hook.called) @mock.patch('website.addons.github.api.GitHubClient.delete_hook') def test_delete_hook_not_found(self, mock_delete_hook): self.node_settings.hook_id = 'hook' self.node_settings.save() mock_delete_hook.side_effect = NotFoundError args = ( self.node_settings.user, self.node_settings.repo, self.node_settings.hook_id, ) res = self.node_settings.delete_hook() assert_false(res) mock_delete_hook.assert_called_with(*args) @mock.patch('website.addons.github.api.GitHubClient.delete_hook') def test_delete_hook_error(self, mock_delete_hook): self.node_settings.hook_id = 'hook' self.node_settings.save() mock_delete_hook.side_effect = GitHubError(mock.Mock()) args = ( self.node_settings.user, self.node_settings.repo, self.node_settings.hook_id, ) res = self.node_settings.delete_hook() assert_false(res) mock_delete_hook.assert_called_with(*args)
class TestGithubNodeSettings(OsfTestCase): def setUp(self): OsfTestCase.setUp(self) self.user = UserFactory() self.user.add_addon('github') self.user_settings = self.user.get_addon('github') self.external_account = GitHubAccountFactory() self.user_settings.owner.external_accounts.append(self.external_account) self.user_settings.owner.save() self.node_settings = GitHubNodeSettingsFactory(user_settings=self.user_settings) @mock.patch('website.addons.github.api.GitHubClient.delete_hook') def test_delete_hook(self, mock_delete_hook): self.node_settings.hook_id = 'hook' self.node_settings.save() args = ( self.node_settings.user, self.node_settings.repo, self.node_settings.hook_id, ) res = self.node_settings.delete_hook() assert_true(res) mock_delete_hook.assert_called_with(*args) @mock.patch('website.addons.github.api.GitHubClient.delete_hook') def test_delete_hook_no_hook(self, mock_delete_hook): res = self.node_settings.delete_hook() assert_false(res) assert_false(mock_delete_hook.called) @mock.patch('website.addons.github.api.GitHubClient.delete_hook') def test_delete_hook_not_found(self, mock_delete_hook): self.node_settings.hook_id = 'hook' self.node_settings.save() mock_delete_hook.side_effect = NotFoundError args = ( self.node_settings.user, self.node_settings.repo, self.node_settings.hook_id, ) res = self.node_settings.delete_hook() assert_false(res) mock_delete_hook.assert_called_with(*args) @mock.patch('website.addons.github.api.GitHubClient.delete_hook') def test_delete_hook_error(self, mock_delete_hook): self.node_settings.hook_id = 'hook' self.node_settings.save() mock_delete_hook.side_effect = GitHubError(mock.Mock()) args = ( self.node_settings.user, self.node_settings.repo, self.node_settings.hook_id, ) res = self.node_settings.delete_hook() assert_false(res) mock_delete_hook.assert_called_with(*args)
def test_to_json_noauthorizing_authed_user(self): user = UserFactory() user.add_addon('github') user_settings = user.get_addon('github') oauth_settings = AddonGitHubOauthSettings(oauth_access_token='foobar') oauth_settings.github_user_id = 'testuser' oauth_settings.save() user_settings.oauth_settings = self.oauth_settings user_settings.save() self.node_settings.to_json(user)
def test_must_be_authorizer_false(self): # Setup self.project.add_addon('github', auth=None) node_settings = self.project.get_addon('github') user2 = UserFactory() user2.add_addon('github') user_settings = user2.get_addon('github') node_settings.user_settings = user_settings # Test with assert_raises(HTTPError): self.decorated()
class TestCore(OsfTestCase): def setUp(self): self.user = UserFactory() self.user.add_addon('menbib') self.user.save() self.settings = self.user.get_addon('menbib') self.settings.access_token = '12345' self.settings.refresh_token = 'abcde' self.settings.save() def test_get_addon_returns_menbib_user_settings(self): result = self.user.get_addon('menbib') assert_true(isinstance(result, AddonMenbibUserSettings))
class TestCore(OsfTestCase): def setUp(self): super(TestCore, self).setUp() self.user = UserFactory() self.user.add_addon('box') self.user.save() self.settings = self.user.get_addon('box') self.settings.save() def test_get_addon_returns_box_user_settings(self): result = self.user.get_addon('box') assert_true(isinstance(result, BoxUserSettings))
def test_migration_no_account(self): BoxUserSettings.remove() user = UserFactory() node = ProjectFactory(creator=user) user.add_addon('box', auth=Auth(user)) user_addon = user.get_addon('box') user_addon.save() node.add_addon('box', auth=Auth(user)) node_addon = node.get_addon('box') node_addon.foreign_user_settings = user_addon node_addon.save() do_migration() # Would raise exception if fail
def test_migration_no_account(self): GoogleDriveUserSettings.remove() user = UserFactory() node = ProjectFactory(creator=user) 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.save() do_migration() # Would raise exception if fail
class TestCore(unittest.TestCase): def setUp(self): super(TestCore, self).setUp() self.user = UserFactory() self.user.add_addon('dropbox') self.user.save() self.settings = self.user.get_addon('dropbox') self.settings.access_token = '12345' self.settings.save() def test_get_addon_returns_dropbox_user_settings(self): result = self.user.get_addon('dropbox') assert_true(isinstance(result, UserSettings))
def test_migration_no_project(self): user = UserFactory() user.add_addon('googledrive') user_addon = user.get_addon('googledrive') user_addon.oauth_settings = GoogleDriveOAuthSettingsFactory() user_addon.save() do_migration([user_addon]) user_addon.reload() assert_is_none(user_addon.oauth_settings) assert_equal(len(user.external_accounts), 1) account = user.external_accounts[0] assert_equal(account.provider, 'googledrive') assert_equal(account.oauth_key, 'abcdef1')
class TestCallbacks(OsfTestCase): @mock.patch('website.addons.twofactor.models.push_status_message') def setUp(self, mocked): super(TestCallbacks, self).setUp() self.user = UserFactory() self.user.add_addon('twofactor') self.user_settings = self.user.get_addon('twofactor') self.on_add_called = mocked.called def test_add_to_user(self): assert_equal(self.user_settings.totp_drift, 0) assert_is_not_none(self.user_settings.totp_secret) assert_false(self.user_settings.is_confirmed) assert_true(self.on_add_called) @mock.patch('website.addons.twofactor.models.push_status_message') def test_remove_from_unconfirmed_user(self, mocked): # drift defaults to 0. Change it so we can test it was changed back. self.user_settings.totp_drift = 1 self.user_settings.save() self.user.delete_addon('twofactor') assert_equal(self.user_settings.totp_drift, 0) assert_is_none(self.user_settings.totp_secret) assert_false(self.user_settings.is_confirmed) assert_false(mocked.called) @mock.patch('website.addons.twofactor.models.push_status_message') def test_remove_from_confirmed_user(self, mocked): # drift defaults to 0. Change it so we can test it was changed back. self.user_settings.totp_drift = 1 self.user_settings.is_confirmed = True self.user_settings.save() self.user.delete_addon('twofactor') assert_equal(self.user_settings.totp_drift, 0) assert_is_none(self.user_settings.totp_secret) assert_false(self.user_settings.is_confirmed) assert_true(mocked.called)
def test_migration_no_project(self): user = UserFactory() api_token = 'api-token-2345' user.add_addon('dataverse') user_addon = user.get_addon('dataverse') user_addon.api_token = api_token user_addon.save() do_migration([user_addon], dry=False) user_addon.reload() assert_is_none(user_addon.api_token) assert_equal(len(user_addon.external_accounts), 1) account = user_addon.external_accounts[0] assert_equal(account.provider, 'dataverse') assert_equal(account.oauth_key, 'dataverse.harvard.edu') assert_equal(account.oauth_secret, api_token)
def test_migration_includes_project(self): user = UserFactory() project = ProjectFactory(creator=user) api_token = 'api-token-2345' user.add_addon('dataverse', auth=Auth(user)) user_addon = user.get_addon('dataverse') user_addon.api_token = api_token user_addon.save() project.add_addon('dataverse', auth=Auth(user)) node_addon = project.get_addon('dataverse') node_addon.user_settings = user_addon node_addon.save() do_migration([user_addon], dry=False) user_addon.reload() node_addon.reload() account = user_addon.external_accounts[0] assert_equal(account, node_addon.external_account)
def test_migration_removes_targets(self): GoogleDriveUserSettings.remove() user = UserFactory() project = ProjectFactory(creator=user) user.add_addon('googledrive', auth=Auth(user)) user_addon = user.get_addon('googledrive') user_addon.oauth_settings = GoogleDriveOAuthSettingsFactory() user_addon.save() project.add_addon('googledrive', auth=Auth(user)) node_addon = project.get_addon('googledrive') node_addon.foreign_user_settings = user_addon node_addon.save() assert_equal(get_targets().count(), 1) do_migration([user_addon]) user_addon.reload() assert_equal(get_targets().count(), 0)
def test_migration_multiple_users(self): user1 = UserFactory() user2 = UserFactory() api_token = 'api-token-2345' user1.add_addon('dataverse') user1_addon = user1.get_addon('dataverse') user1_addon.api_token = api_token user1_addon.save() user2.add_addon('dataverse') user2_addon = user2.get_addon('dataverse') user2_addon.api_token = api_token user2_addon.save() do_migration([user1_addon, user2_addon], dry=False) user1_addon.reload() user2_addon.reload() assert_equal( user1_addon.external_accounts[0], user2_addon.external_accounts[0], )
def test_migration_multiple_users(self): user1 = UserFactory() user2 = UserFactory() oauth_settings = GoogleDriveOAuthSettingsFactory() user1.add_addon('googledrive') user1_addon = user1.get_addon('googledrive') user1_addon.oauth_settings = oauth_settings user1_addon.save() user2.add_addon('googledrive') user2_addon = user2.get_addon('googledrive') user2_addon.oauth_settings = oauth_settings user2_addon.save() do_migration([user1_addon, user2_addon]) user1_addon.reload() user2_addon.reload() assert_equal( user1.external_accounts[0], user2.external_accounts[0], )
class TestMendeleyNodeSettingsModel(OsfTestCase): def setUp(self): self.user = UserFactory() self.user.add_addon('mendeley') self.user.save() self.user_settings = self.user.get_addon('mendeley') self.project = ProjectFactory() self.node_settings = MendeleyNodeSettingsFactory( user_settings=self.user_settings, owner=self.project ) def test_fields(self): node_settings = AddonMendeleyNodeSettings(user_settings=self.user_settings) node_settings.save() assert_true(node_settings.user_settings) assert_equal(node_settings.user_settings.owner, self.user) assert_true(hasattr(node_settings, 'folder')) # def test_shorturl(self): # node_settings = AddonMendeleyNodeSettings(user_settings=self.user_settings) # print self.user # print node_settings.short_url # assert_equal(node_settings.short_url, '/'.join([self.user])) # def test_has_auth(self): # None # def test_to_json(self): node_settings = self.node_settings user = self.user result = node_settings.to_json(user) assert_equal(result['addon_short_name'], 'mendeley')
class TestMenbibNodeSettingsModel(OsfTestCase): def setUp(self): self.user = UserFactory() self.user.add_addon('menbib') self.user.save() self.user_settings = self.user.get_addon('menbib') self.project = ProjectFactory() self.node_settings = MenbibNodeSettingsFactory( user_settings=self.user_settings, owner=self.project ) def test_fields(self): node_settings = AddonMenbibNodeSettings(user_settings=self.user_settings) node_settings.save() assert_true(node_settings.user_settings) assert_equal(node_settings.user_settings.owner, self.user) assert_true(hasattr(node_settings, 'folder')) def test_folder_defaults_to_none(self): node_settings = AddonMenbibNodeSettings(user_settings=self.user_settings) node_settings.save() assert_is_none(node_settings.folder) def test_has_auth(self): settings = AddonMenbibNodeSettings(user_settings=self.user_settings) settings.save() assert_false(settings.has_auth) settings.user_settings.access_token = '123abc' settings.user_settings.save() assert_true(settings.has_auth) def test_to_json(self): settings = self.node_settings user = UserFactory() result = settings.to_json(user) assert_equal(result['addon_short_name'], 'menbib') def test_delete(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder) old_logs = self.project.logs self.node_settings.delete() self.node_settings.save() assert_is(self.node_settings.user_settings, None) assert_is(self.node_settings.folder, None) assert_true(self.node_settings.deleted) assert_equal(self.project.logs, old_logs) def test_deauthorize(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder) 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.folder, None) last_log = self.project.logs[-1] assert_equal(last_log.action, 'menbib_node_deauthorized') params = last_log.params assert_in('node', params) assert_in('project', params) assert_in('folder', params) def test_set_folder(self): folder_name = 'cos_folder' self.node_settings.set_folder(folder_name, auth=Auth(self.user)) self.node_settings.save() # Folder was set assert_equal(self.node_settings.folder, folder_name) # Log was saved last_log = self.project.logs[-1] assert_equal(last_log.action, 'menbib_folder_selected') def test_set_user_auth(self): node_settings = MenbibNodeSettingsFactory() user_settings = MenbibUserSettingsFactory() 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, 'menbib_node_authorized') log_params = last_log.params #assert_equal(log_params['folder'], node_settings.folder) assert_equal(log_params['node'], node_settings.owner._primary_key) assert_equal(last_log.user, user_settings.owner)
class TestUserSettingsModel(OsfTestCase): TOTP_SECRET = 'b8f85986068f8079aa9d' TOTP_SECRET_B32 = 'XD4FTBQGR6AHTKU5' def setUp(self): super(TestUserSettingsModel, self).setUp() self.user = UserFactory() self.user.add_addon('twofactor') self.user_settings = self.user.get_addon('twofactor') self.user_settings.totp_secret = self.TOTP_SECRET self.user_settings.save() def tearDown(self): super(TestUserSettingsModel, self).tearDown() self.user.__class__.remove_one(self.user) def test_b32(self): assert_equal(self.user_settings.totp_secret_b32, self.TOTP_SECRET_B32) def test_otpauth_url(self): url = urlparse(self.user_settings.otpauth_url) assert_equal(url.scheme, 'otpauth') assert_equal(url.netloc, 'totp') assert_equal(url.path, '/OSF:{}'.format(self.user.username)) assert_equal( parse_qs(url.query), {'secret': [self.TOTP_SECRET_B32]} ) def test_json(self): url = 'otpauth://totp/OSF:{}?secret=' + self.TOTP_SECRET_B32 settings = self.user_settings.to_json(user=None) assert_equal( settings, { 'is_enabled': True, 'addon_full_name': 'Two-factor Authentication', 'addon_short_name': 'twofactor', 'drift': 0, 'is_confirmed': False, 'nodes': [], 'secret': self.TOTP_SECRET_B32, 'has_auth': False, } ) def test_verify_valid_code(self): assert_true( self.user_settings.verify_code(_valid_code(self.TOTP_SECRET)) ) def test_verify_valid_core_drift(self): # use a code from 30 seconds in the future assert_true( self.user_settings.verify_code( _valid_code(self.TOTP_SECRET, drift=1) ) ) # make sure drift is updated. assert_equal(self.user_settings.totp_drift, 1) # use a code from 60 seconds in the future assert_true( self.user_settings.verify_code( _valid_code(self.TOTP_SECRET, drift=2) ) ) # make sure drift is updated. assert_equal(self.user_settings.totp_drift, 2) # use the current code (which is now 2 periods away from the drift) assert_false( self.user_settings.verify_code(_valid_code(self.TOTP_SECRET)) )
class TestBoxNodeSettingsModel(OsfTestCase): def setUp(self): super(TestBoxNodeSettingsModel, self).setUp() self.user = UserFactory() self.user.add_addon('box') self.user.save() self.oauth = BoxOAuthSettings(user_id='not sleep', access_token='seems legit') self.oauth.save() self.user_settings = self.user.get_addon('box') self.user_settings.oauth_settings = self.oauth self.user_settings.save() self.project = ProjectFactory() self.node_settings = BoxNodeSettingsFactory( user_settings=self.user_settings, folder_id='1234567890', owner=self.project) def test_complete_true(self): assert_true(self.node_settings.has_auth) assert_true(self.node_settings.complete) def test_complete_false(self): self.node_settings.folder_id = None assert_true(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_complete_auth_false(self): self.node_settings.user_settings = None assert_false(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_fields(self): node_settings = BoxNodeSettings(user_settings=self.user_settings) node_settings.save() assert_true(node_settings.user_settings) assert_equal(node_settings.user_settings.owner, self.user) assert_true(hasattr(node_settings, 'folder_id')) assert_true(hasattr(node_settings, 'user_settings')) def test_folder_defaults_to_none(self): node_settings = BoxNodeSettings(user_settings=self.user_settings) node_settings.save() assert_is_none(node_settings.folder_id) def test_has_auth(self): self.user_settings.access_token = None settings = BoxNodeSettings(user_settings=self.user_settings) settings.save() assert_false(settings.has_auth) settings.user_settings.access_token = '123abc' settings.user_settings.save() assert_true(settings.has_auth) def test_to_json(self): settings = self.node_settings user = UserFactory() result = settings.to_json(user) assert_equal(result['addon_short_name'], 'box') def test_delete(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder_id) old_logs = self.project.logs self.node_settings.delete() self.node_settings.save() assert_is(self.node_settings.user_settings, None) assert_is(self.node_settings.folder_id, None) assert_true(self.node_settings.deleted) assert_equal(self.project.logs, old_logs) def test_deauthorize(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder_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.folder_id, None) last_log = self.project.logs[-1] assert_equal(last_log.action, 'box_node_deauthorized') params = last_log.params assert_in('node', params) assert_in('project', params) assert_in('folder_id', params) def test_set_folder(self): folder_id = '1234567890' self.node_settings.set_folder(folder_id, auth=Auth(self.user)) self.node_settings.save() # Folder was set assert_equal(self.node_settings.folder_id, folder_id) # Log was saved last_log = self.project.logs[-1] assert_equal(last_log.action, 'box_folder_selected') def test_set_user_auth(self): node_settings = BoxNodeSettingsFactory() user_settings = BoxUserSettingsFactory() 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 test_serialize_credentials(self): self.user_settings.access_token = 'secret' self.user_settings.save() credentials = self.node_settings.serialize_waterbutler_credentials() expected = {'token': self.node_settings.user_settings.access_token} assert_equal(credentials, expected) def test_serialize_credentials_not_authorized(self): self.node_settings.user_settings = None self.node_settings.save() with assert_raises(exceptions.AddonError): self.node_settings.serialize_waterbutler_credentials() def test_serialize_settings(self): settings = self.node_settings.serialize_waterbutler_settings() expected = {'folder': self.node_settings.folder_id} assert_equal(settings, expected) def test_serialize_settings_not_configured(self): self.node_settings.folder_id = None self.node_settings.save() with assert_raises(exceptions.AddonError): self.node_settings.serialize_waterbutler_settings() def test_create_log(self): action = 'file_added' path = 'pizza.nii' nlog = len(self.project.logs) self.node_settings.create_waterbutler_log( auth=Auth(user=self.user), action=action, metadata={'path': path}, ) self.project.reload() assert_equal(len(self.project.logs), nlog + 1) assert_equal( self.project.logs[-1].action, 'box_{0}'.format(action), ) assert_equal( self.project.logs[-1].params['path'], os.path.join(self.node_settings.folder_id, path), )
class TestDropboxNodeSettingsModel(OsfTestCase): def setUp(self): super(TestDropboxNodeSettingsModel, self).setUp() self.user = UserFactory() self.user.add_addon('dropbox') self.user.save() self.user_settings = self.user.get_addon('dropbox') self.project = ProjectFactory() self.node_settings = DropboxNodeSettingsFactory( user_settings=self.user_settings, owner=self.project) def test_complete_true(self): self.node_settings.user_settings.access_token = 'seems legit' assert_true(self.node_settings.has_auth) assert_true(self.node_settings.complete) def test_complete_false(self): self.node_settings.user_settings.access_token = 'seems legit' self.node_settings.folder = None assert_true(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_complete_auth_false(self): self.node_settings.user_settings = None assert_false(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_fields(self): node_settings = DropboxNodeSettings(user_settings=self.user_settings) node_settings.save() assert_true(node_settings.user_settings) assert_equal(node_settings.user_settings.owner, self.user) assert_true(hasattr(node_settings, 'folder')) assert_true(hasattr(node_settings, 'registration_data')) def test_folder_defaults_to_none(self): node_settings = DropboxNodeSettings(user_settings=self.user_settings) node_settings.save() assert_is_none(node_settings.folder) def test_has_auth(self): settings = DropboxNodeSettings(user_settings=self.user_settings) settings.save() assert_false(settings.has_auth) settings.user_settings.access_token = '123abc' settings.user_settings.save() assert_true(settings.has_auth) def test_to_json(self): settings = self.node_settings user = UserFactory() result = settings.to_json(user) assert_equal(result['addon_short_name'], 'dropbox') def test_delete(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder) old_logs = self.project.logs self.node_settings.delete() self.node_settings.save() assert_is(self.node_settings.user_settings, None) assert_is(self.node_settings.folder, None) assert_true(self.node_settings.deleted) assert_equal(self.project.logs, old_logs) def test_deauthorize(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder) 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.folder, None) last_log = self.project.logs[-1] assert_equal(last_log.action, 'dropbox_node_deauthorized') params = last_log.params assert_in('node', params) assert_in('project', params) assert_in('folder', params) def test_set_folder(self): folder_name = 'queen/freddie' self.node_settings.set_folder(folder_name, auth=Auth(self.user)) self.node_settings.save() # Folder was set assert_equal(self.node_settings.folder, folder_name) # Log was saved last_log = self.project.logs[-1] assert_equal(last_log.action, 'dropbox_folder_selected') def test_set_user_auth(self): node_settings = DropboxNodeSettingsFactory() user_settings = DropboxUserSettingsFactory() 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, 'dropbox_node_authorized') log_params = last_log.params assert_equal(log_params['folder'], node_settings.folder) assert_equal(log_params['node'], node_settings.owner._primary_key) assert_equal(last_log.user, user_settings.owner) def test_serialize_credentials(self): self.user_settings.access_token = 'secret' self.user_settings.save() credentials = self.node_settings.serialize_waterbutler_credentials() expected = {'token': self.node_settings.user_settings.access_token} assert_equal(credentials, expected) def test_serialize_credentials_not_authorized(self): self.node_settings.user_settings = None self.node_settings.save() with assert_raises(exceptions.AddonError): self.node_settings.serialize_waterbutler_credentials() def test_serialize_settings(self): settings = self.node_settings.serialize_waterbutler_settings() expected = {'folder': self.node_settings.folder} assert_equal(settings, expected) def test_serialize_settings_not_configured(self): self.node_settings.folder = None self.node_settings.save() with assert_raises(exceptions.AddonError): self.node_settings.serialize_waterbutler_settings() def test_create_log(self): action = 'file_added' path = 'pizza.nii' self.node_settings.folder = '/SomeOddPath' self.node_settings.save() nlog = len(self.project.logs) self.node_settings.create_waterbutler_log( auth=Auth(user=self.user), action=action, metadata={'path': path}, ) self.project.reload() assert_equal(len(self.project.logs), nlog + 1) assert_equal( self.project.logs[-1].action, 'dropbox_{0}'.format(action), ) assert_equal( self.project.logs[-1].params['path'], path, ) @mock.patch('website.archiver.tasks.archive') def test_does_not_get_copied_to_registrations(self, mock_archive): registration = self.project.register_node( schema=None, auth=Auth(user=self.project.creator), template='Template1', data='hodor') assert_false(registration.has_addon('dropbox'))
class TestDropboxNodeSettingsModel(OsfTestCase): def setUp(self): super(TestDropboxNodeSettingsModel, self).setUp() self.user = UserFactory() self.user.add_addon('dropbox') self.user.save() self.user_settings = self.user.get_addon('dropbox') self.project = ProjectFactory() self.node_settings = DropboxNodeSettingsFactory( user_settings=self.user_settings, owner=self.project ) def test_fields(self): node_settings = DropboxNodeSettings(user_settings=self.user_settings) node_settings.save() assert_true(node_settings.user_settings) assert_equal(node_settings.user_settings.owner, self.user) assert_true(hasattr(node_settings, 'folder')) assert_true(hasattr(node_settings, 'registration_data')) def test_folder_defaults_to_none(self): node_settings = DropboxNodeSettings(user_settings=self.user_settings) node_settings.save() assert_is_none(node_settings.folder) def test_has_auth(self): settings = DropboxNodeSettings(user_settings=self.user_settings) settings.save() assert_false(settings.has_auth) settings.user_settings.access_token = '123abc' settings.user_settings.save() assert_true(settings.has_auth) def test_to_json(self): settings = self.node_settings user = UserFactory() result = settings.to_json(user) assert_equal(result['addon_short_name'], 'dropbox') def test_delete(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder) old_logs = self.project.logs self.node_settings.delete() self.node_settings.save() assert_is(self.node_settings.user_settings, None) assert_is(self.node_settings.folder, None) assert_true(self.node_settings.deleted) assert_equal(self.project.logs, old_logs) def test_deauthorize(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder) 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.folder, None) last_log = self.project.logs[-1] assert_equal(last_log.action, 'dropbox_node_deauthorized') params = last_log.params assert_in('node', params) assert_in('project', params) assert_in('folder', params) def test_set_folder(self): folder_name = 'queen/freddie' self.node_settings.set_folder(folder_name, auth=Auth(self.user)) self.node_settings.save() # Folder was set assert_equal(self.node_settings.folder, folder_name) # Log was saved last_log = self.project.logs[-1] assert_equal(last_log.action, 'dropbox_folder_selected') def test_set_user_auth(self): node_settings = DropboxNodeSettingsFactory() user_settings = DropboxUserSettingsFactory() 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, 'dropbox_node_authorized') log_params = last_log.params assert_equal(log_params['folder'], node_settings.folder) assert_equal(log_params['node'], node_settings.owner._primary_key) assert_equal(last_log.user, user_settings.owner) def test_serialize_credentials(self): self.user_settings.access_token = 'secret' self.user_settings.save() credentials = self.node_settings.serialize_waterbutler_credentials() expected = {'token': self.node_settings.user_settings.access_token} assert_equal(credentials, expected) def test_serialize_credentials_not_authorized(self): self.node_settings.user_settings = None self.node_settings.save() with assert_raises(exceptions.AddonError): self.node_settings.serialize_waterbutler_credentials() def test_serialize_settings(self): settings = self.node_settings.serialize_waterbutler_settings() expected = {'folder': self.node_settings.folder} assert_equal(settings, expected) def test_serialize_settings_not_configured(self): self.node_settings.folder = None self.node_settings.save() with assert_raises(exceptions.AddonError): self.node_settings.serialize_waterbutler_settings() def test_create_log(self): action = 'file_added' path = 'pizza.nii' nlog = len(self.project.logs) self.node_settings.create_waterbutler_log( auth=Auth(user=self.user), action=action, metadata={'path': path}, ) self.project.reload() assert_equal(len(self.project.logs), nlog + 1) assert_equal( self.project.logs[-1].action, 'dropbox_{0}'.format(action), ) assert_equal( self.project.logs[-1].params['path'], os.path.join(self.node_settings.folder, path), ) def test_does_not_get_copied_to_registrations(self): registration = self.project.register_node( schema=None, auth=Auth(user=self.project.creator), template='Template1', data='hodor' ) assert_false(registration.has_addon('dropbox'))
class TestCore(OsfTestCase): @mock.patch('website.addons.twofactor.models.push_status_message') def setUp(self, mocked): super(TestCore, self).setUp() self.user = UserFactory() self.user.save() self.user.add_addon('twofactor') self.user_settings = self.user.get_addon('twofactor') self.user_settings.is_confirmed = True self.user_settings.save() def test_authenticate_two_factor_returns_correct_response(self): response = authenticate_two_factor(self.user) assert_true(isinstance(response, BaseResponse)) assert_equal(response.location, web_url_for('two_factor')) assert_equal(response.status_code, 302) def test_authenticate_two_factor_with_next_url(self): fake_session = sessions.Session(data={'next_url': '/someendpoint/'}) sessions.set_session(fake_session) response = authenticate_two_factor(self.user) assert_true(isinstance(response, BaseResponse)) assert_equal( response.location, u'{0}?next=%2Fsomeendpoint%2F'.format(web_url_for('two_factor'))) assert_equal(response.status_code, 302) def test_verify_two_factor_with_invalid_code(self): with assert_raises(TwoFactorValidationError): verify_two_factor(self.user._id, 1234567) def test_verify_two_factor_with_valid_code(self): fake_session = sessions.Session( data={ 'two_factor_auth': { 'auth_user_username': self.user.username, 'auth_user_id': self.user._primary_key, 'auth_user_fullname': self.user.fullname, } }) sessions.set_session(fake_session) response = verify_two_factor( self.user._id, _valid_code(self.user_settings.totp_secret)) assert_true(isinstance(response, BaseResponse)) assert_equal(response.location, u'/dashboard/') assert_equal(response.status_code, 302) def test_verify_two_factor_with_valid_code_and_next_url(self): fake_session = sessions.Session( data={ 'two_factor_auth': { 'auth_user_username': self.user.username, 'auth_user_id': self.user._primary_key, 'auth_user_fullname': self.user.fullname, }, 'next_url': '/someendpoint/' }) sessions.set_session(fake_session) response = verify_two_factor( self.user._id, _valid_code(self.user_settings.totp_secret)) assert_true(isinstance(response, BaseResponse)) assert_equal(response.location, u'/someendpoint/') assert_equal(response.status_code, 302) def test_user_requires_two_factor_verification_returns_true_if_confirmed( self): response = user_requires_two_factor_verification(self.user) assert_true(response) def test_user_requires_two_factor_verification_returns_false_if_not_confirmed( self): self.user_settings.is_confirmed = False response = user_requires_two_factor_verification(self.user) assert_false(response)
class TestDropboxNodeSettingsModel(OsfTestCase): def setUp(self): super(TestDropboxNodeSettingsModel, self).setUp() self.user = UserFactory() self.user.add_addon('dropbox') self.user.save() self.user_settings = self.user.get_addon('dropbox') self.project = ProjectFactory() self.node_settings = DropboxNodeSettingsFactory( user_settings=self.user_settings, owner=self.project) def test_fields(self): node_settings = DropboxNodeSettings(user_settings=self.user_settings) node_settings.save() assert_true(node_settings.user_settings) assert_equal(node_settings.user_settings.owner, self.user) assert_true(hasattr(node_settings, 'folder')) assert_true(hasattr(node_settings, 'registration_data')) def test_folder_defaults_to_none(self): node_settings = DropboxNodeSettings(user_settings=self.user_settings) node_settings.save() assert_is_none(node_settings.folder) def test_has_auth(self): settings = DropboxNodeSettings(user_settings=self.user_settings) settings.save() assert_false(settings.has_auth) settings.user_settings.access_token = '123abc' settings.user_settings.save() assert_true(settings.has_auth) def test_to_json(self): settings = self.node_settings user = UserFactory() result = settings.to_json(user) assert_equal(result['addon_short_name'], 'dropbox') def test_delete(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder) old_logs = self.project.logs self.node_settings.delete() self.node_settings.save() assert_is(self.node_settings.user_settings, None) assert_is(self.node_settings.folder, None) assert_true(self.node_settings.deleted) assert_equal(self.project.logs, old_logs) def test_deauthorize(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder) 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.folder, None) last_log = self.project.logs[-1] assert_equal(last_log.action, 'dropbox_node_deauthorized') params = last_log.params assert_in('node', params) assert_in('project', params) assert_in('folder', params) def test_set_folder(self): folder_name = 'queen/freddie' self.node_settings.set_folder(folder_name, auth=Auth(self.user)) self.node_settings.save() # Folder was set assert_equal(self.node_settings.folder, folder_name) # Log was saved last_log = self.project.logs[-1] assert_equal(last_log.action, 'dropbox_folder_selected') def test_set_user_auth(self): node_settings = DropboxNodeSettingsFactory() user_settings = DropboxUserSettingsFactory() 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, 'dropbox_node_authorized') log_params = last_log.params assert_equal(log_params['folder'], node_settings.folder) assert_equal(log_params['node'], node_settings.owner._primary_key) assert_equal(last_log.user, user_settings.owner)
class TestDropboxNodeSettingsModel(OsfTestCase): def setUp(self): super(TestDropboxNodeSettingsModel, self).setUp() self.user = UserFactory() self.user.add_addon("dropbox") self.user.save() self.user_settings = self.user.get_addon("dropbox") self.project = ProjectFactory() self.node_settings = DropboxNodeSettingsFactory(user_settings=self.user_settings, owner=self.project) def test_complete_true(self): self.node_settings.user_settings.access_token = "seems legit" assert_true(self.node_settings.has_auth) assert_true(self.node_settings.complete) def test_complete_false(self): self.node_settings.user_settings.access_token = "seems legit" self.node_settings.folder = None assert_true(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_complete_auth_false(self): self.node_settings.user_settings = None assert_false(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_fields(self): node_settings = DropboxNodeSettings(user_settings=self.user_settings) node_settings.save() assert_true(node_settings.user_settings) assert_equal(node_settings.user_settings.owner, self.user) assert_true(hasattr(node_settings, "folder")) assert_true(hasattr(node_settings, "registration_data")) def test_folder_defaults_to_none(self): node_settings = DropboxNodeSettings(user_settings=self.user_settings) node_settings.save() assert_is_none(node_settings.folder) def test_has_auth(self): settings = DropboxNodeSettings(user_settings=self.user_settings) settings.save() assert_false(settings.has_auth) settings.user_settings.access_token = "123abc" settings.user_settings.save() assert_true(settings.has_auth) def test_to_json(self): settings = self.node_settings user = UserFactory() result = settings.to_json(user) assert_equal(result["addon_short_name"], "dropbox") def test_delete(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder) old_logs = self.project.logs self.node_settings.delete() self.node_settings.save() assert_is(self.node_settings.user_settings, None) assert_is(self.node_settings.folder, None) assert_true(self.node_settings.deleted) assert_equal(self.project.logs, old_logs) def test_deauthorize(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder) 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.folder, None) last_log = self.project.logs[-1] assert_equal(last_log.action, "dropbox_node_deauthorized") params = last_log.params assert_in("node", params) assert_in("project", params) assert_in("folder", params) def test_set_folder(self): folder_name = "queen/freddie" self.node_settings.set_folder(folder_name, auth=Auth(self.user)) self.node_settings.save() # Folder was set assert_equal(self.node_settings.folder, folder_name) # Log was saved last_log = self.project.logs[-1] assert_equal(last_log.action, "dropbox_folder_selected") def test_set_user_auth(self): node_settings = DropboxNodeSettingsFactory() user_settings = DropboxUserSettingsFactory() 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, "dropbox_node_authorized") log_params = last_log.params assert_equal(log_params["folder"], node_settings.folder) assert_equal(log_params["node"], node_settings.owner._primary_key) assert_equal(last_log.user, user_settings.owner) def test_serialize_credentials(self): self.user_settings.access_token = "secret" self.user_settings.save() credentials = self.node_settings.serialize_waterbutler_credentials() expected = {"token": self.node_settings.user_settings.access_token} assert_equal(credentials, expected) def test_serialize_credentials_not_authorized(self): self.node_settings.user_settings = None self.node_settings.save() with assert_raises(exceptions.AddonError): self.node_settings.serialize_waterbutler_credentials() def test_serialize_settings(self): settings = self.node_settings.serialize_waterbutler_settings() expected = {"folder": self.node_settings.folder} assert_equal(settings, expected) def test_serialize_settings_not_configured(self): self.node_settings.folder = None self.node_settings.save() with assert_raises(exceptions.AddonError): self.node_settings.serialize_waterbutler_settings() def test_create_log(self): action = "file_added" path = "pizza.nii" self.node_settings.folder = "/SomeOddPath" self.node_settings.save() nlog = len(self.project.logs) self.node_settings.create_waterbutler_log(auth=Auth(user=self.user), action=action, metadata={"path": path}) self.project.reload() assert_equal(len(self.project.logs), nlog + 1) assert_equal(self.project.logs[-1].action, "dropbox_{0}".format(action)) assert_equal(self.project.logs[-1].params["path"], path) @mock.patch("website.archiver.tasks.archive") def test_does_not_get_copied_to_registrations(self, mock_archive): registration = self.project.register_node( schema=None, auth=Auth(user=self.project.creator), template="Template1", data="hodor" ) assert_false(registration.has_addon("dropbox"))
class TestGoogleDriveNodeSettingsModel(OsfTestCase): def setUp(self): super(TestGoogleDriveNodeSettingsModel, self).setUp() self.user = UserFactory() self.user.add_addon('googledrive') self.user.save() self.user_settings = self.user.get_addon('googledrive') oauth_settings = GoogleDriveOAuthSettingsFactory() oauth_settings.save() self.user_settings.oauth_settings = oauth_settings self.user_settings.save() self.project = ProjectFactory() self.node_settings = GoogleDriveNodeSettingsFactory( user_settings=self.user_settings, owner=self.project, ) def test_fields(self): node_settings = GoogleDriveNodeSettings(user_settings=self.user_settings) node_settings.save() assert_true(node_settings.user_settings) assert_true(hasattr(node_settings, 'folder_id')) assert_true(hasattr(node_settings, 'folder_path')) assert_true(hasattr(node_settings, 'folder_name')) assert_equal(node_settings.user_settings.owner, self.user) def test_complete_true(self): assert_true(self.node_settings.has_auth) assert_true(self.node_settings.complete) def test_complete_false(self): self.node_settings.folder_id = None assert_true(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_complete_auth_false(self): self.node_settings.user_settings = None assert_false(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_folder_defaults_to_none(self): node_settings = GoogleDriveNodeSettings(user_settings=self.user_settings) node_settings.save() assert_is_none(node_settings.folder_id) assert_is_none(node_settings.folder_path) def test_has_auth(self): settings = GoogleDriveNodeSettings(user_settings=self.user_settings) settings.user_settings.access_token = None settings.save() assert_false(settings.has_auth) settings.user_settings.access_token = '123abc' settings.user_settings.save() assert_true(settings.has_auth) # TODO use this test if delete function is used in googledrive/model # def test_delete(self): # assert_true(self.node_settings.user_settings) # assert_true(self.node_settings.folder) # old_logs = self.project.logs # self.node_settings.delete() # self.node_settings.save() # assert_is(self.node_settings.user_settings, None) # assert_is(self.node_settings.folder, None) # assert_true(self.node_settings.deleted) # assert_equal(self.project.logs, old_logs) def test_deauthorize(self): assert_true(self.node_settings.folder_id) assert_true(self.node_settings.user_settings) self.node_settings.deauthorize(auth=Auth(self.user)) self.node_settings.save() assert_is(self.node_settings.folder_id, None) assert_is(self.node_settings.user_settings, None) last_log = self.project.logs[-1] params = last_log.params assert_in('node', params) assert_in('folder', params) assert_in('project', params) assert_equal(last_log.action, 'googledrive_node_deauthorized') def test_set_folder(self): folder_name = { 'id': '1234', 'name': 'freddie', 'path': 'queen/freddie', } self.node_settings.set_folder(folder_name, auth=Auth(self.user)) self.node_settings.save() # Folder was set assert_equal(self.node_settings.folder_name, folder_name['name']) # Log was saved last_log = self.project.logs[-1] assert_equal(last_log.action, 'googledrive_folder_selected') def test_set_user_auth(self): node_settings = GoogleDriveNodeSettingsFactory() user_settings = GoogleDriveUserSettingsFactory() 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] log_params = last_log.params assert_equal(last_log.user, user_settings.owner) assert_equal(log_params['folder'], node_settings.folder_path) assert_equal(last_log.action, 'googledrive_node_authorized') assert_equal(log_params['node'], node_settings.owner._primary_key) def test_serialize_credentials(self): self.user_settings.access_token = 'secret' self.user_settings.save() credentials = self.node_settings.serialize_waterbutler_credentials() expected = {'token': self.node_settings.user_settings.access_token} assert_equal(credentials, expected) def test_serialize_credentials_not_authorized(self): self.node_settings.user_settings = None self.node_settings.save() with assert_raises(exceptions.AddonError): self.node_settings.serialize_waterbutler_credentials() def test_serialize_settings(self): self.node_settings.folder_path = 'camera uploads/pizza.nii' self.node_settings.save() settings = self.node_settings.serialize_waterbutler_settings() expected = { 'folder': { 'id': '12345', 'name': 'pizza.nii', 'path': 'camera uploads/pizza.nii', } } assert_equal(settings, expected) def test_serialize_settings_not_configured(self): self.node_settings.folder_id = None self.node_settings.save() with assert_raises(exceptions.AddonError): self.node_settings.serialize_waterbutler_settings() def test_create_log(self): action = 'file_added' path = '12345/camera uploads/pizza.nii' nlog = len(self.project.logs) self.node_settings.create_waterbutler_log( auth=Auth(user=self.user), action=action, metadata={'path': path}, ) self.project.reload() assert_equal(len(self.project.logs), nlog + 1) assert_equal( self.project.logs[-1].action, 'googledrive_{0}'.format(action), ) assert_equal(self.project.logs[-1].params['path'], path)
class TestCore(OsfTestCase): @mock.patch('website.addons.twofactor.models.push_status_message') def setUp(self, mocked): super(TestCore, self).setUp() self.user = UserFactory() self.user.save() self.user.add_addon('twofactor') self.user_settings = self.user.get_addon('twofactor') self.user_settings.is_confirmed = True self.user_settings.save() def test_authenticate_two_factor_returns_correct_response(self): response = authenticate_two_factor(self.user) assert_true(isinstance(response, BaseResponse)) assert_equal(response.location, web_url_for('two_factor')) assert_equal(response.status_code, 302) def test_authenticate_two_factor_with_next_url(self): fake_session = sessions.Session(data={'next_url': '/someendpoint/'}) sessions.set_session(fake_session) response = authenticate_two_factor(self.user) assert_true(isinstance(response, BaseResponse)) assert_equal(response.location, u'{0}?next=%2Fsomeendpoint%2F'.format(web_url_for('two_factor')) ) assert_equal(response.status_code, 302) def test_verify_two_factor_with_invalid_code(self): with assert_raises(TwoFactorValidationError): verify_two_factor(self.user._id, 1234567) def test_verify_two_factor_with_valid_code(self): fake_session = sessions.Session(data={ 'two_factor_auth':{ 'auth_user_username': self.user.username, 'auth_user_id': self.user._primary_key, 'auth_user_fullname': self.user.fullname, } }) sessions.set_session(fake_session) response = verify_two_factor(self.user._id, _valid_code(self.user_settings.totp_secret) ) assert_true(isinstance(response, BaseResponse)) assert_equal(response.location, u'/dashboard/') assert_equal(response.status_code, 302) def test_verify_two_factor_with_valid_code_and_next_url(self): fake_session = sessions.Session(data={ 'two_factor_auth':{ 'auth_user_username': self.user.username, 'auth_user_id': self.user._primary_key, 'auth_user_fullname': self.user.fullname, }, 'next_url': '/someendpoint/' }) sessions.set_session(fake_session) response = verify_two_factor(self.user._id, _valid_code(self.user_settings.totp_secret) ) assert_true(isinstance(response, BaseResponse)) assert_equal(response.location, u'/someendpoint/') assert_equal(response.status_code, 302) def test_user_requires_two_factor_verification_returns_true_if_confirmed(self): response = user_requires_two_factor_verification(self.user) assert_true(response) def test_user_requires_two_factor_verification_returns_false_if_not_confirmed(self): self.user_settings.is_confirmed = False response = user_requires_two_factor_verification(self.user) assert_false(response)
class TestAddonGithubNodeSettings(OsfTestCase): def setUp(self): OsfTestCase.setUp(self) self.user = UserFactory() self.user.add_addon('github') self.user_settings = self.user.get_addon('github') self.oauth_settings = AddonGitHubOauthSettings(oauth_access_token='foobar') self.oauth_settings.github_user_id = 'testuser' self.oauth_settings.save() self.user_settings.oauth_settings = self.oauth_settings self.user_settings.save() self.node_settings = AddonGitHubNodeSettings( owner=ProjectFactory(), user='******', repo='openpokemon', user_settings=self.user_settings, ) self.node_settings.save() def test_complete_true(self): assert_true(self.node_settings.has_auth) assert_true(self.node_settings.complete) def test_complete_false(self): self.node_settings.user = None assert_true(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_complete_repo_false(self): self.node_settings.repo = None assert_true(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_complete_auth_false(self): self.node_settings.user_settings = None assert_false(self.node_settings.has_auth) assert_false(self.node_settings.complete) @mock.patch('website.addons.github.api.GitHub.delete_hook') def test_delete_hook(self, mock_delete_hook): self.node_settings.hook_id = 'hook' self.node_settings.save() args = ( self.node_settings.user, self.node_settings.repo, self.node_settings.hook_id, ) res = self.node_settings.delete_hook() assert_true(res) mock_delete_hook.assert_called_with(*args) @mock.patch('website.addons.github.api.GitHub.delete_hook') def test_delete_hook_no_hook(self, mock_delete_hook): res = self.node_settings.delete_hook() assert_false(res) assert_false(mock_delete_hook.called) @mock.patch('website.addons.github.api.GitHub.delete_hook') def test_delete_hook_not_found(self, mock_delete_hook): self.node_settings.hook_id = 'hook' self.node_settings.save() mock_delete_hook.side_effect = NotFoundError args = ( self.node_settings.user, self.node_settings.repo, self.node_settings.hook_id, ) res = self.node_settings.delete_hook() assert_false(res) mock_delete_hook.assert_called_with(*args) @mock.patch('website.addons.github.api.GitHub.delete_hook') def test_delete_hook_error(self, mock_delete_hook): self.node_settings.hook_id = 'hook' self.node_settings.save() mock_delete_hook.side_effect = GitHubError(mock.Mock()) args = ( self.node_settings.user, self.node_settings.repo, self.node_settings.hook_id, ) res = self.node_settings.delete_hook() assert_false(res) mock_delete_hook.assert_called_with(*args) def test_to_json_noauthorizing_authed_user(self): user = UserFactory() user.add_addon('github') user_settings = user.get_addon('github') oauth_settings = AddonGitHubOauthSettings(oauth_access_token='foobar') oauth_settings.github_user_id = 'testuser' oauth_settings.save() user_settings.oauth_settings = self.oauth_settings user_settings.save() self.node_settings.to_json(user)
class TestBoxNodeSettingsModel(OsfTestCase): def setUp(self): super(TestBoxNodeSettingsModel, self).setUp() self.user = UserFactory() self.user.add_addon('box') self.user.save() self.oauth = BoxOAuthSettings(user_id='not sleep', access_token='seems legit') self.oauth.save() self.user_settings = self.user.get_addon('box') self.user_settings.oauth_settings = self.oauth self.user_settings.save() self.project = ProjectFactory() self.node_settings = BoxNodeSettingsFactory( user_settings=self.user_settings, folder_id='1234567890', owner=self.project ) def test_complete_true(self): assert_true(self.node_settings.has_auth) assert_true(self.node_settings.complete) def test_complete_false(self): self.node_settings.folder_id = None assert_true(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_complete_auth_false(self): self.node_settings.user_settings = None assert_false(self.node_settings.has_auth) assert_false(self.node_settings.complete) def test_fields(self): node_settings = BoxNodeSettings(user_settings=self.user_settings) node_settings.save() assert_true(node_settings.user_settings) assert_equal(node_settings.user_settings.owner, self.user) assert_true(hasattr(node_settings, 'folder_id')) assert_true(hasattr(node_settings, 'user_settings')) def test_folder_defaults_to_none(self): node_settings = BoxNodeSettings(user_settings=self.user_settings) node_settings.save() assert_is_none(node_settings.folder_id) def test_has_auth(self): self.user_settings.access_token = None settings = BoxNodeSettings(user_settings=self.user_settings) settings.save() assert_false(settings.has_auth) settings.user_settings.access_token = '123abc' settings.user_settings.save() assert_true(settings.has_auth) def test_to_json(self): settings = self.node_settings user = UserFactory() result = settings.to_json(user) assert_equal(result['addon_short_name'], 'box') def test_delete(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder_id) old_logs = self.project.logs self.node_settings.delete() self.node_settings.save() assert_is(self.node_settings.user_settings, None) assert_is(self.node_settings.folder_id, None) assert_true(self.node_settings.deleted) assert_equal(self.project.logs, old_logs) def test_deauthorize(self): assert_true(self.node_settings.user_settings) assert_true(self.node_settings.folder_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.folder_id, None) last_log = self.project.logs[-1] assert_equal(last_log.action, 'box_node_deauthorized') params = last_log.params assert_in('node', params) assert_in('project', params) assert_in('folder_id', params) @mock.patch("website.addons.box.model.BoxNodeSettings._update_folder_data") def test_set_folder(self, mock_update_folder): folder_id = '1234567890' self.node_settings.set_folder(folder_id, auth=Auth(self.user)) self.node_settings.save() # Folder was set assert_equal(self.node_settings.folder_id, folder_id) # Log was saved last_log = self.project.logs[-1] assert_equal(last_log.action, 'box_folder_selected') def test_set_user_auth(self): node_settings = BoxNodeSettingsFactory() user_settings = BoxUserSettingsFactory() 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 test_serialize_credentials(self): self.user_settings.access_token = 'secret' self.user_settings.save() credentials = self.node_settings.serialize_waterbutler_credentials() expected = {'token': self.node_settings.user_settings.access_token} assert_equal(credentials, expected) def test_serialize_credentials_not_authorized(self): self.node_settings.user_settings = None self.node_settings.save() with assert_raises(exceptions.AddonError): self.node_settings.serialize_waterbutler_credentials() def test_serialize_settings(self): settings = self.node_settings.serialize_waterbutler_settings() expected = {'folder': self.node_settings.folder_id} assert_equal(settings, expected) def test_serialize_settings_not_configured(self): self.node_settings.folder_id = None self.node_settings.save() with assert_raises(exceptions.AddonError): self.node_settings.serialize_waterbutler_settings() @mock.patch('website.addons.box.model.BoxUserSettings.fetch_access_token') def test_serialize_waterbutler_credentials_reraises_box_client_exception_as_http_error(self, mock_fetch_access_token): mock_fetch_access_token.side_effect = BoxClientException(status_code=400, message='Oops') with assert_raises(HTTPError): self.node_settings.serialize_waterbutler_credentials() def test_create_log(self): action = 'file_added' path = 'pizza.nii' nlog = len(self.project.logs) self.node_settings.create_waterbutler_log( auth=Auth(user=self.user), action=action, metadata={'path': path, 'materialized': path}, ) self.project.reload() assert_equal(len(self.project.logs), nlog + 1) assert_equal( self.project.logs[-1].action, 'box_{0}'.format(action), ) assert_equal( self.project.logs[-1].params['path'], path )