<commit_before>from nose.tools import *

from scripts.box.migrate_to_external_account import do_migration, get_targets

from framework.auth import Auth

from tests.base import OsfTestCase
from tests.factories import ProjectFactory, UserFactory

from website.addons.box.model import BoxUserSettings
from website.addons.box.tests.factories import BoxOAuthSettingsFactory


class TestBoxMigration(OsfTestCase):
    # Note: BoxUserSettings.user_settings has to be changed to foreign_user_settings (model and mongo). See migration instructions

    def test_migration_no_project(self):

        user = UserFactory()

        user.add_addon('box')
        user_addon = user.get_addon('box')
        user_addon.oauth_settings = BoxOAuthSettingsFactory()
        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, 'box')
        assert_equal(account.oauth_key, 'abcdef1')

    def test_migration_removes_targets(self):
        BoxUserSettings.remove()

        user = UserFactory()
        project = ProjectFactory(creator=user)

        user.add_addon('box', auth=Auth(user))
        user_addon = user.get_addon('box')
        user_addon.oauth_settings = BoxOAuthSettingsFactory()
        user_addon.save()

        project.add_addon('box', auth=Auth(user))
        node_addon = project.get_addon('box')
        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()
        oauth_settings = BoxOAuthSettingsFactory()

        user1.add_addon('box')
        user1_addon = user1.get_addon('box')
        user1_addon.oauth_settings = oauth_settings
        user1_addon.save()

        user2.add_addon('box')
        user2_addon = user2.get_addon('box')
        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],
        )

    def test_get_targets(self):
        BoxUserSettings.remove()
        addons = [
            BoxUserSettings(),
            BoxUserSettings(oauth_settings=BoxOAuthSettingsFactory()),
        ]
        for addon in addons:
            addon.save()
        targets = get_targets()
        assert_equal(targets.count(), 1)
        assert_equal(targets[0]._id, addons[-1]._id)
<commit_msg>Fix and update tests<commit_after>from nose.tools import *

from scripts.box.migrate_to_external_account import do_migration, get_targets

from framework.auth import Auth

from tests.base import OsfTestCase
from tests.factories import ProjectFactory, UserFactory

from website.oauth.models import ExternalAccount
from website.addons.box.model import BoxUserSettings, BoxOAuthSettings
from website.addons.box.tests.factories import BoxOAuthSettingsFactory, BoxUserSettingsFactory


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)

{'fraction_non_alphanumeric': 0.09171548117154811, 'fraction_numerical': 0.012217573221757322, 'mean_word_length': 3.1270718232044197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3745110', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1806, 'n_words': 280}