Example #1
0
    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 do_migration(records, dry=True):
    count, inval_cred_handled = 0, 0

    for raw_user_settings in records:

        # False if missing, None if field exists
        access_token = raw_user_settings.get('oauth_access_token', False)
        token_type = raw_user_settings.get('oauth_token_type', False)
        github_user_name = raw_user_settings.get('github_user', False)

        if access_token and token_type and github_user_name:
            if not dry:
                gh = GitHub(access_token, token_type)
                try:
                    github_user = gh.user()
                except github3.models.GitHubError:
                    AddonGitHubUserSettings._storage[0].store.update(
                        {'_id': raw_user_settings['_id']}, {
                            '$unset': {
                                "oauth_access_token": True,
                                "oauth_token_type": True,
                                "github_user": True,
                            },
                        })
                    inval_cred_handled += 1
                    print('invalidated credentials handled record: {}'.format(
                        raw_user_settings['_id']))
                    continue

                oauth_settings = AddonGitHubOauthSettings()
                oauth_settings.github_user_id = str(github_user.id)
                oauth_settings.save()
                oauth_settings.oauth_access_token = access_token
                oauth_settings.oauth_token_type = token_type
                oauth_settings.github_user_name = github_user_name
                oauth_settings.save()

                AddonGitHubUserSettings._storage[0].store.update(
                    {'_id': raw_user_settings['_id']}, {
                        '$unset': {
                            'oauth_access_token': True,
                            'oauth_token_type': True,
                            'github_user': True,
                        },
                        '$set': {
                            'oauth_settings': oauth_settings.github_user_id,
                        }
                    })

                AddonGitHubOauthSettings._storage[0].store.update(
                    {'github_user_id': oauth_settings.github_user_id}, {
                        '$push': {
                            '__backrefs.accessed.addongithubusersettings.oauth_settings':
                            raw_user_settings['_id'],
                        }
                    })
                print('Finished migrating AddonGithubUserSettings record: {}'.
                      format(raw_user_settings['_id']))
            count += 1
        # Old fields have not yet been unset
        elif None in set([access_token, token_type, github_user_name]):
            if not dry:
                AddonGitHubUserSettings._storage[0].store.update(
                    {'_id': raw_user_settings['_id']}, {
                        '$unset': {
                            'oauth_access_token': True,
                            'oauth_token_type': True,
                            'github_user': True,
                        },
                    })
                print('Unset oauth_access_token and oauth_token_type: {0}'.
                      format(raw_user_settings['_id']))
            count += 1

    return count, inval_cred_handled
def do_migration(records, dry=True):
    count, inval_cred_handled = 0, 0

    for raw_user_settings in records:

        # False if missing, None if field exists
        access_token = raw_user_settings.get('oauth_access_token', False)
        token_type = raw_user_settings.get('oauth_token_type', False)
        github_user_name = raw_user_settings.get('github_user', False)

        if access_token and token_type and github_user_name:
            if not dry:
                gh = GitHub(access_token, token_type)
                try:
                    github_user = gh.user()
                except github3.models.GitHubError:
                    AddonGitHubUserSettings._storage[0].store.update(
                        {'_id': raw_user_settings['_id']},
                        {
                            '$unset': {
                                "oauth_access_token" : True,
                                "oauth_token_type" : True,
                                "github_user" : True,
                            },
                        }
                    )
                    inval_cred_handled += 1
                    print('invalidated credentials handled record: {}'.format(raw_user_settings['_id']))
                    continue


                oauth_settings = AddonGitHubOauthSettings()
                oauth_settings.github_user_id = str(github_user.id)
                oauth_settings.save()
                oauth_settings.oauth_access_token = access_token
                oauth_settings.oauth_token_type = token_type
                oauth_settings.github_user_name = github_user_name
                oauth_settings.save()

                AddonGitHubUserSettings._storage[0].store.update(
                    {'_id': raw_user_settings['_id']},
                    {
                        '$unset': {
                            'oauth_access_token': True,
                            'oauth_token_type': True,
                            'github_user': True,
                        },
                        '$set': {
                            'oauth_settings': oauth_settings.github_user_id,
                        }
                    }
                )

                AddonGitHubOauthSettings._storage[0].store.update(
                    {'github_user_id': oauth_settings.github_user_id},
                    {
                        '$push': {
                            '__backrefs.accessed.addongithubusersettings.oauth_settings': raw_user_settings['_id'],
                        }
                    }
                )
                print('Finished migrating AddonGithubUserSettings record: {}'.format(raw_user_settings['_id']))
            count += 1
        # Old fields have not yet been unset
        elif None in set([access_token, token_type, github_user_name]):
            if not dry:
                AddonGitHubUserSettings._storage[0].store.update(
                    {'_id': raw_user_settings['_id']},
                    {
                        '$unset': {
                            'oauth_access_token': True,
                            'oauth_token_type': True,
                            'github_user': True,
                        },
                    }
                )
                print('Unset oauth_access_token and oauth_token_type: {0}'.format(raw_user_settings['_id']))
            count += 1

    return count, inval_cred_handled