Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Commit

Permalink
Merge pull request #229 from hasgeek/current_auth
Browse files Browse the repository at this point in the history
Use coaster's current_auth
  • Loading branch information
jace authored Feb 24, 2018
2 parents 6716fa1 + 43c5bc9 commit 5b532f5
Show file tree
Hide file tree
Showing 25 changed files with 254 additions and 287 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ before_script:
- psql -c 'create database lastuser_test_app;' -U postgres
script:
- ./runtests.sh
after_success:
- coveralls
notifications:
email: false
slack:
Expand Down
4 changes: 2 additions & 2 deletions features/steps/failed_login.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from flask import g
from behave import when, then, given
from coaster.auth import current_auth
from lastuserapp import app


Expand All @@ -16,7 +16,7 @@ def when_login_form_submit(context):
context.test_user['form.id'] = "passwordlogin"
with app.test_client() as c:
c.post('/login', data=context.test_user, follow_redirects=True)
context.user = g.user
context.user = current_auth.user


@then("we do not log the user in")
Expand Down
6 changes: 2 additions & 4 deletions features/steps/failed_registration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from flask import g
from behave import given, when, then
from lastuser_core.models import User
from lastuserapp import app


@given('a new user trying to register with a used username')
Expand All @@ -19,7 +17,7 @@ def given_new_user(context):
for k, v in context.test_user.iteritems():
context.browser.find_element_by_name(k).send_keys(v)

register_form = context.browser.find_element_by_id('register')
register_form = context.browser.find_element_by_id('form-register')
register_form.submit()


Expand All @@ -32,7 +30,7 @@ def when_form_submit(context):
for k, v in context.test_user.iteritems():
context.browser.find_element_by_name(k).send_keys(v)

register_form = context.browser.find_element_by_id('register')
register_form = context.browser.find_element_by_id('form-register')
register_form.submit()
# page will have error message
alert = context.browser.find_elements_by_xpath("//*[contains(text(), 'This username is taken')]")
Expand Down
4 changes: 2 additions & 2 deletions features/steps/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def given_existing_user(context):
for k, v in context.test_user.iteritems():
context.browser.find_element_by_name(k).send_keys(v)

register_form = context.browser.find_element_by_id('register')
register_form = context.browser.find_element_by_id('form-register')
register_form.submit()


Expand All @@ -27,7 +27,7 @@ def when_login_form_submit(context):
'username': context.test_user['username'],
'password': context.test_user['password']
}
wait = ui.WebDriverWait(context.browser, 2)
wait = ui.WebDriverWait(context.browser, 30)

context.browser.visit('/login')
assert context.browser.find_element_by_name('csrf_token').is_enabled()
Expand Down
2 changes: 1 addition & 1 deletion features/steps/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def when_form_submit(context):
for k, v in context.test_user.iteritems():
context.browser.find_element_by_name(k).send_keys(v)

register_form = context.browser.find_element_by_id('register')
register_form = context.browser.find_element_by_id('form-register')
register_form.submit()


Expand Down
2 changes: 2 additions & 0 deletions lastuser_oauth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

class LastuserOAuthBlueprint(Blueprint):
def init_app(self, app):
from .views.helpers import LoginManager
self.serializer = JSONWebSignatureSerializer(
app.config.get('LASTUSER_SECRET_KEY') or app.config['SECRET_KEY'])
app.login_manager = LoginManager()


lastuser_oauth = LastuserOAuthBlueprint('lastuser_oauth', __name__,
Expand Down
19 changes: 10 additions & 9 deletions lastuser_oauth/views/account.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
from flask import abort, url_for, flash, redirect, g, session, render_template, request
from flask import abort, url_for, flash, redirect, session, render_template, request

from coaster.utils import valid_username
from coaster.auth import current_auth
from coaster.views import get_next_url
from baseframe import _
from baseframe.signals import exception_catchall
Expand Down Expand Up @@ -48,7 +49,7 @@ def login_service_callback(service):
msg = _(u"{service} login failed: {error}").format(service=provider.title, error=unicode(e))
exception_catchall.send(e, message=msg)
flash(msg, category='danger')
if g.user:
if current_auth.is_authenticated:
return redirect(get_next_url(referrer=False))
else:
return redirect(url_for('.login'))
Expand Down Expand Up @@ -115,9 +116,9 @@ def login_service_postcallback(service, userdata):
db.session.add(extid)

if user is None:
if g.user:
if current_auth.is_authenticated:
# Attach this id to currently logged-in user
user = g.user
user = current_auth.user
extid.user = user
else:
# Register a new user
Expand All @@ -128,7 +129,7 @@ def login_service_postcallback(service, userdata):
# Set a username for this user if it's available
user.username = userdata['username']
else: # This id is attached to a user
if g.user and g.user != user:
if current_auth.is_authenticated and current_auth.user != user:
# Woah! Account merger handler required
# Always confirm with user before doing an account merger
session['merge_buid'] = user.buid
Expand Down Expand Up @@ -161,15 +162,15 @@ def login_service_postcallback(service, userdata):
if not user.fullname and userdata.get('fullname'):
user.fullname = userdata['fullname']

if not g.user: # If a user isn't already logged in, login now.
if not current_auth.is_authenticated: # If a user isn't already logged in, login now.
login_internal(user)
flash(_(u"You have logged in via {service}").format(service=login_registry[service].title), 'success')
next_url = get_next_url(session=True)

db.session.commit()

# Finally: set a login method cookie and send user on their way
if not g.user.is_profile_complete():
if not current_auth.user.is_profile_complete():
login_next = url_for('.profile_new', next=next_url)
else:
login_next = next_url
Expand All @@ -192,7 +193,7 @@ def profile_merge():
form = ProfileMergeForm()
if form.validate_on_submit():
if 'merge' in request.form:
new_user = merge_users(g.user, other_user)
new_user = merge_users(current_auth.user, other_user)
login_internal(new_user)
flash(_("Your accounts have been merged"), 'success')
session.pop('merge_buid', None)
Expand All @@ -202,5 +203,5 @@ def profile_merge():
else:
session.pop('merge_buid', None)
return redirect(get_next_url(), code=303)
return render_template('merge.html.jinja2', form=form, user=g.user, other_user=other_user,
return render_template('merge.html.jinja2', form=form, user=current_auth.user, other_user=other_user,
login_registry=login_registry)
Loading

0 comments on commit 5b532f5

Please sign in to comment.