Exemplo n.º 1
0
        def _test_delete(self, test_user):
            # Delete openid from user
            # Create another user to test deleting OpenID that doesn't belong to them
            new_user = fixture_add_user(username="******")
            openid = OpenIDUserURL()
            openid.openid_url = "http://realfake.myopenid.com/"
            openid.user_id = new_user.id
            openid.save()

            # Try and delete OpenID url that isn't the users
            template.clear_test_template_context()
            res = openid_plugin_app.post("/edit/openid/delete/", {"openid": "http://realfake.myopenid.com/"})
            context = template.TEMPLATE_TEST_CONTEXT["mediagoblin/plugins/openid/delete.html"]
            form = context["form"]
            assert form.openid.errors == [u"That OpenID is not registered to this account."]

            # Delete OpenID
            # Kind of weird to POST to delete/finish
            template.clear_test_template_context()
            res = openid_plugin_app.post("/edit/openid/delete/finish/", {"openid": u"http://add.myopenid.com"})
            res.follow()

            # Correct place?
            assert urlparse.urlsplit(res.location)[2] == "/edit/account/"
            assert "mediagoblin/edit/edit_account.html" in template.TEMPLATE_TEST_CONTEXT

            # OpenID deleted?
            new_openid = mg_globals.database.OpenIDUserURL.query.filter_by(
                openid_url=u"http://add.myopenid.com"
            ).first()
            assert not new_openid
Exemplo n.º 2
0
        def _test_delete(self, test_user):
            # Delete openid from user
            # Create another user to test deleting OpenID that doesn't belong to them
            new_user = fixture_add_user(username='******')
            openid = OpenIDUserURL()
            openid.openid_url = 'http://realfake.myopenid.com/'
            openid.user_id = new_user.id
            openid.save()

            # Try and delete OpenID url that isn't the users
            template.clear_test_template_context()
            res = openid_plugin_app.post(
                '/edit/openid/delete/', {
                    'openid': 'http://realfake.myopenid.com/'})
            context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/openid/delete.html']
            form = context['form']
            assert form.openid.errors == [u'That OpenID is not registered to this account.']

            # Delete OpenID
            # Kind of weird to POST to delete/finish
            template.clear_test_template_context()
            res = openid_plugin_app.post(
                '/edit/openid/delete/finish/', {
                    'openid': u'http://add.myopenid.com'})
            res.follow()

            # Correct place?
            assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
            assert 'mediagoblin/edit/edit_account.html' in template.TEMPLATE_TEST_CONTEXT

            # OpenID deleted?
            new_openid = mg_globals.database.OpenIDUserURL.query.filter_by(
                openid_url=u'http://add.myopenid.com').first()
            assert not new_openid
Exemplo n.º 3
0
def finish_edit(request):
    """Finishes the process of adding an openid url to a user"""
    response = _finish_verification(request)

    if not response:
        # Verification failed, redirect to add openid page.
        return redirect(request, 'mediagoblin.plugins.openid.edit')

    # Verification was successfull
    query = OpenIDUserURL.query.filter_by(
        openid_url=response.identity_url, ).first()
    user_exists = query.user if query else None

    if user_exists:
        # user exists with that openid url, redirect back to edit page
        messages.add_message(
            request, messages.WARNING,
            _('Sorry, an account is already registered to that OpenID.'))
        return redirect(request, 'mediagoblin.plugins.openid.edit')

    else:
        # Save openid to user
        user = User.query.filter_by(id=request.session['user_id']).first()

        new_entry = OpenIDUserURL()
        new_entry.openid_url = response.identity_url
        new_entry.user_id = user.id
        new_entry.save()

        messages.add_message(request, messages.SUCCESS,
                             _('Your OpenID url was saved successfully.'))

        return redirect(request, 'mediagoblin.edit.account')
Exemplo n.º 4
0
def finish_edit(request):
    """Finishes the process of adding an openid url to a user"""
    response = _finish_verification(request)

    if not response:
        # Verification failed, redirect to add openid page.
        return redirect(request, 'mediagoblin.plugins.openid.edit')

    # Verification was successfull
    query = OpenIDUserURL.query.filter_by(
        openid_url=response.identity_url,
        ).first()
    user_exists = query.user if query else None

    if user_exists:
        # user exists with that openid url, redirect back to edit page
        messages.add_message(
            request,
            messages.WARNING,
            _('Sorry, an account is already registered to that OpenID.'))
        return redirect(request, 'mediagoblin.plugins.openid.edit')

    else:
        # Save openid to user
        user = User.query.filter_by(
            id=request.session['user_id']
            ).first()

        new_entry = OpenIDUserURL()
        new_entry.openid_url = response.identity_url
        new_entry.user_id = user.id
        new_entry.save()

        messages.add_message(
            request,
            messages.SUCCESS,
            _('Your OpenID url was saved successfully.'))

        return redirect(request, 'mediagoblin.edit.account')
Exemplo n.º 5
0
def create_user(register_form):
    if 'openid' in register_form:
        username = register_form.username.data
        user = User.query.filter(
            or_(
                LocalUser.username == username,
                LocalUser.email == username,
            )).first()

        if not user:
            user = create_basic_user(register_form)

        new_entry = OpenIDUserURL()
        new_entry.openid_url = register_form.openid.data
        new_entry.user_id = user.id
        new_entry.save()

        return user
Exemplo n.º 6
0
    def test_add_delete(self, openid_plugin_app):
        """Test adding and deleting openids"""
        # Add user
        test_user = fixture_add_user(password="")
        openid = OpenIDUserURL()
        openid.openid_url = "http://real.myopenid.com"
        openid.user_id = test_user.id
        openid.save()

        # Log user in
        template.clear_test_template_context()
        self._setup(openid_plugin_app)

        @mock.patch("mediagoblin.plugins.openid.views._finish_verification", self._finish_verification)
        @mock.patch("mediagoblin.plugins.openid.views._start_verification", self._start_verification)
        def _login_user():
            openid_plugin_app.post("/auth/openid/login/finish/", {"openid": u"http://real.myopenid.com"})

        _login_user()

        # Try and delete only OpenID url
        template.clear_test_template_context()
        res = openid_plugin_app.post("/edit/openid/delete/", {"openid": "http://real.myopenid.com"})
        assert "mediagoblin/plugins/openid/delete.html" in template.TEMPLATE_TEST_CONTEXT

        # Add OpenID to user
        # Empty form
        template.clear_test_template_context()
        res = openid_plugin_app.post("/edit/openid/", {})
        context = template.TEMPLATE_TEST_CONTEXT["mediagoblin/plugins/openid/add.html"]
        form = context["form"]
        assert form.openid.errors == [u"This field is required."]

        # Try with a bad url
        template.clear_test_template_context()
        openid_plugin_app.post("/edit/openid/", {"openid": u"not_a_url.com"})
        context = template.TEMPLATE_TEST_CONTEXT["mediagoblin/plugins/openid/add.html"]
        form = context["form"]
        assert form.openid.errors == [u"Please enter a valid url."]

        # Try with a url that's already registered
        template.clear_test_template_context()
        openid_plugin_app.post("/edit/openid/", {"openid": "http://real.myopenid.com"})
        context = template.TEMPLATE_TEST_CONTEXT["mediagoblin/plugins/openid/add.html"]
        form = context["form"]
        assert form.openid.errors == [u"Sorry, an account is already registered to that OpenID."]

        # Test adding openid to account
        # Need to clear_test_template_context before calling _setup
        template.clear_test_template_context()
        self._setup(openid_plugin_app, edit=True)

        # Need to remove openid_url from db because it was added at setup
        openid = OpenIDUserURL.query.filter_by(openid_url=u"http://add.myopenid.com")
        openid.delete()

        @mock.patch("mediagoblin.plugins.openid.views._finish_verification", self._finish_verification)
        @mock.patch("mediagoblin.plugins.openid.views._start_verification", self._start_verification)
        def _test_add():
            # Successful add
            template.clear_test_template_context()
            res = openid_plugin_app.post("/edit/openid/", {"openid": u"http://add.myopenid.com"})
            res.follow()

            # Correct place?
            assert urlparse.urlsplit(res.location)[2] == "/edit/account/"
            assert "mediagoblin/edit/edit_account.html" in template.TEMPLATE_TEST_CONTEXT

            # OpenID Added?
            new_openid = mg_globals.database.OpenIDUserURL.query.filter_by(
                openid_url=u"http://add.myopenid.com"
            ).first()
            assert new_openid

        _test_add()

        # Test deleting openid from account
        # Need to clear_test_template_context before calling _setup
        template.clear_test_template_context()
        self._setup(openid_plugin_app, delete=True)

        # Need to add OpenID back to user because it was deleted during
        # patch
        openid = OpenIDUserURL()
        openid.openid_url = "http://add.myopenid.com"
        openid.user_id = test_user.id
        openid.save()

        @mock.patch("mediagoblin.plugins.openid.views._finish_verification", self._finish_verification)
        @mock.patch("mediagoblin.plugins.openid.views._start_verification", self._start_verification)
        def _test_delete(self, test_user):
            # Delete openid from user
            # Create another user to test deleting OpenID that doesn't belong to them
            new_user = fixture_add_user(username="******")
            openid = OpenIDUserURL()
            openid.openid_url = "http://realfake.myopenid.com/"
            openid.user_id = new_user.id
            openid.save()

            # Try and delete OpenID url that isn't the users
            template.clear_test_template_context()
            res = openid_plugin_app.post("/edit/openid/delete/", {"openid": "http://realfake.myopenid.com/"})
            context = template.TEMPLATE_TEST_CONTEXT["mediagoblin/plugins/openid/delete.html"]
            form = context["form"]
            assert form.openid.errors == [u"That OpenID is not registered to this account."]

            # Delete OpenID
            # Kind of weird to POST to delete/finish
            template.clear_test_template_context()
            res = openid_plugin_app.post("/edit/openid/delete/finish/", {"openid": u"http://add.myopenid.com"})
            res.follow()

            # Correct place?
            assert urlparse.urlsplit(res.location)[2] == "/edit/account/"
            assert "mediagoblin/edit/edit_account.html" in template.TEMPLATE_TEST_CONTEXT

            # OpenID deleted?
            new_openid = mg_globals.database.OpenIDUserURL.query.filter_by(
                openid_url=u"http://add.myopenid.com"
            ).first()
            assert not new_openid

        _test_delete(self, test_user)
Exemplo n.º 7
0
    def test_add_delete(self, openid_plugin_app):
        """Test adding and deleting openids"""
        # Add user
        test_user = fixture_add_user(password='', privileges=[u'active'])
        openid = OpenIDUserURL()
        openid.openid_url = 'http://real.myopenid.com'
        openid.user_id = test_user.id
        openid.save()

        # Log user in
        template.clear_test_template_context()
        self._setup(openid_plugin_app)

        @mock.patch('mediagoblin.plugins.openid.views._finish_verification', self._finish_verification)
        @mock.patch('mediagoblin.plugins.openid.views._start_verification', self._start_verification)
        def _login_user():
            openid_plugin_app.post(
                '/auth/openid/login/finish/', {
                    'openid': u'http://real.myopenid.com'})

        _login_user()

        # Try and delete only OpenID url
        template.clear_test_template_context()
        res = openid_plugin_app.post(
            '/edit/openid/delete/', {
                'openid': 'http://real.myopenid.com'})
        assert 'mediagoblin/plugins/openid/delete.html' in template.TEMPLATE_TEST_CONTEXT

        # Add OpenID to user
        # Empty form
        template.clear_test_template_context()
        res = openid_plugin_app.post(
            '/edit/openid/', {})
        context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/openid/add.html']
        form = context['form']
        assert form.openid.errors == [u'This field is required.']

        # Try with a bad url
        template.clear_test_template_context()
        openid_plugin_app.post(
            '/edit/openid/', {
                'openid': u'not_a_url.com'})
        context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/openid/add.html']
        form = context['form']
        assert form.openid.errors == [u'Please enter a valid url.']

        # Try with a url that's already registered
        template.clear_test_template_context()
        openid_plugin_app.post(
            '/edit/openid/', {
                'openid': 'http://real.myopenid.com'})
        context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/openid/add.html']
        form = context['form']
        assert form.openid.errors == [u'Sorry, an account is already registered to that OpenID.']

        # Test adding openid to account
        # Need to clear_test_template_context before calling _setup
        template.clear_test_template_context()
        self._setup(openid_plugin_app, edit=True)

        # Need to remove openid_url from db because it was added at setup
        openid = OpenIDUserURL.query.filter_by(
            openid_url=u'http://add.myopenid.com')
        openid.delete()

        @mock.patch('mediagoblin.plugins.openid.views._finish_verification', self._finish_verification)
        @mock.patch('mediagoblin.plugins.openid.views._start_verification', self._start_verification)
        def _test_add():
            # Successful add
            template.clear_test_template_context()
            res = openid_plugin_app.post(
                '/edit/openid/', {
                    'openid': u'http://add.myopenid.com'})
            res.follow()

            # Correct place?
            assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
            assert 'mediagoblin/edit/edit_account.html' in template.TEMPLATE_TEST_CONTEXT

            # OpenID Added?
            new_openid = mg_globals.database.OpenIDUserURL.query.filter_by(
                openid_url=u'http://add.myopenid.com').first()
            assert new_openid

        _test_add()

        # Test deleting openid from account
        # Need to clear_test_template_context before calling _setup
        template.clear_test_template_context()
        self._setup(openid_plugin_app, delete=True)

        # Need to add OpenID back to user because it was deleted during
        # patch
        openid = OpenIDUserURL()
        openid.openid_url = 'http://add.myopenid.com'
        openid.user_id = test_user.id
        openid.save()

        @mock.patch('mediagoblin.plugins.openid.views._finish_verification', self._finish_verification)
        @mock.patch('mediagoblin.plugins.openid.views._start_verification', self._start_verification)
        def _test_delete(self, test_user):
            # Delete openid from user
            # Create another user to test deleting OpenID that doesn't belong to them
            new_user = fixture_add_user(username='******')
            openid = OpenIDUserURL()
            openid.openid_url = 'http://realfake.myopenid.com/'
            openid.user_id = new_user.id
            openid.save()

            # Try and delete OpenID url that isn't the users
            template.clear_test_template_context()
            res = openid_plugin_app.post(
                '/edit/openid/delete/', {
                    'openid': 'http://realfake.myopenid.com/'})
            context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/openid/delete.html']
            form = context['form']
            assert form.openid.errors == [u'That OpenID is not registered to this account.']

            # Delete OpenID
            # Kind of weird to POST to delete/finish
            template.clear_test_template_context()
            res = openid_plugin_app.post(
                '/edit/openid/delete/finish/', {
                    'openid': u'http://add.myopenid.com'})
            res.follow()

            # Correct place?
            assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
            assert 'mediagoblin/edit/edit_account.html' in template.TEMPLATE_TEST_CONTEXT

            # OpenID deleted?
            new_openid = mg_globals.database.OpenIDUserURL.query.filter_by(
                openid_url=u'http://add.myopenid.com').first()
            assert not new_openid

        _test_delete(self, test_user)