<s> from nose . tools import * <EOL> from scripts . box . migrate_to_external_account import do_migration , get_targets <EOL> from framework . auth import Auth <EOL> from tests . base import OsfTestCase <EOL> from tests . factories import ProjectFactory , UserFactory <EOL> from website . oauth . models import ExternalAccount <EOL> from website . addons . box . model import BoxUserSettings , BoxOAuthSettings <EOL> from website . addons . box . tests . factories import BoxOAuthSettingsFactory , BoxUserSettingsFactory <EOL> class TestBoxMigration ( OsfTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestBoxMigration , self ) . setUp ( ) <EOL> self . user1 = UserFactory ( ) <EOL> self . user1_settings = BoxUserSettingsFactory ( owner = self . user1 ) <EOL> self . user1_settings . oauth_settings = BoxOAuthSettingsFactory ( ) <EOL> self . user1_settings . save ( ) <EOL> def tearDown ( self ) : <EOL> super ( TestBoxMigration , self ) . tearDown ( ) <EOL> BoxUserSettings . remove ( ) <EOL> ExternalAccount . remove ( ) <EOL> def test_migration_no_project ( self ) : <EOL> self . old_oauth = self . user1_settings . oauth_settings <EOL> do_migration ( [ self . user1_settings ] ) <EOL> self . user1_settings . reload ( ) <EOL> assert_is_none ( self . user1_settings . oauth_settings ) <EOL> assert_equal ( len ( self . user1 . external_accounts ) , <NUM_LIT:1> ) <EOL> account = self . user1_settings . owner . external_accounts [ <NUM_LIT:0> ] <EOL> assert_is_none ( self . user1_settings . oauth_settings ) <EOL> assert_equal ( account . provider , '<STR_LIT>' ) <EOL> assert_equal ( account . oauth_key , self . old_oauth . access_token ) <EOL> def test_migration_removes_targets ( self ) : <EOL> project = ProjectFactory ( creator = self . user1 ) <EOL> project . add_addon ( '<STR_LIT>' , auth = Auth ( self . user1 ) ) <EOL> node_addon = project . get_addon ( '<STR_LIT>' ) <EOL> node_addon . foreign_user_settings = self . user1_settings <EOL> node_addon . save ( ) <EOL> assert_equal ( get_targets ( ) . count ( ) , <NUM_LIT:1> ) <EOL> do_migration ( [ self . user1_settings ] ) <EOL> self . user1_settings . reload ( ) <EOL> assert_equal ( get_targets ( ) . count ( ) , <NUM_LIT:0> ) <EOL> def test_migration_multiple_users ( self ) : <EOL> self . user2 = UserFactory ( ) <EOL> self . user2_settings = BoxUserSettingsFactory ( owner = self . user2 ) <EOL> self . user2_settings . oauth_settings = self . user1_settings . oauth_settings <EOL> self . user2_settings . save ( ) <EOL> do_migration ( [ self . user1_settings , self . user2_settings ] ) <EOL> self . user1_settings . reload ( ) <EOL> self . user2_settings . reload ( ) <EOL> assert_equal ( <EOL> self . user1_settings . owner . external_accounts [ <NUM_LIT:0> ] , <EOL> self . user2_settings . owner . external_accounts [ <NUM_LIT:0> ] , <EOL> ) <EOL> def test_get_targets ( self ) : <EOL> self . user2 = UserFactory ( ) <EOL> self . user2_settings = BoxUserSettingsFactory ( owner = self . user2 ) <EOL> self . user2_settings . oauth_settings = None <EOL> self . user2_settings . save ( ) <EOL> addons = [ <EOL> self . user2_settings , <EOL> self . user1_settings , <EOL> ] <EOL> for addon in addons : <EOL> addon . save ( ) <EOL> targets = get_targets ( ) <EOL> assert_equal ( targets . count ( ) , <NUM_LIT:1> ) <EOL> assert_equal ( targets [ <NUM_LIT:0> ] . _id , addons [ - <NUM_LIT:1> ] . _id ) </s>
