Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
datashaman committed Dec 15, 2017
1 parent 484bf2d commit f45a912
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 16 deletions.
2 changes: 1 addition & 1 deletion auth/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class LoginVoucherForm(FlaskForm):

gw_address = HiddenField('Gateway Address', default=args_get('gw_address'))
gw_port = HiddenField('Gateway Port', default=args_get('gw_port'))
gateway_id = HiddenField('Gateway ID', default=args_get('gw_id'))
gw_id = HiddenField('Gateway ID', default=args_get('gw_id'))
mac = HiddenField('MAC', default=args_get('mac'))
url = HiddenField('URL', default=args_get('url'))

Expand Down
19 changes: 10 additions & 9 deletions auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
roles_accepted
from PIL import Image
from pytz import common_timezones
from sqlalchemy import func


bp = Blueprint('auth', __name__)
Expand Down Expand Up @@ -876,7 +877,7 @@ def wifidog_login():

if form.validate_on_submit():
voucher_code = form.voucher_code.data.upper()
voucher = Voucher.query.filter_by(code=voucher_code).first()
voucher = Voucher.query.filter(func.upper(Voucher.code) == voucher_code).first()

if voucher is None:
flash(
Expand All @@ -900,14 +901,14 @@ def wifidog_login():
return redirect(url)

if request.method == 'GET':
gateway_id = request.args.get('gw_id')
gw_id = request.args.get('gw_id')
else:
gateway_id = form.gateway_id.data
gw_id = form.gw_id.data

if gateway_id is None:
if gw_id is None:
abort(404)

gateway = Gateway.query.filter_by(id=gateway_id).first_or_404()
gateway = Gateway.query.filter_by(id=gw_id).first_or_404()

return render_template('wifidog/login.html', form=form, gateway=gateway)

Expand Down Expand Up @@ -942,7 +943,7 @@ def wifidog_auth():
gateway_id=request.args.get('gw_id'),
)
(status, messages) = process_auth(args)
return ("Auth: %s\nMessages: %s\n" % (status, messages), 200)
return "Auth: %s\nMessages: %s\n" % (status, messages), 200


@bp.route('/wifidog/portal/')
Expand All @@ -952,10 +953,10 @@ def wifidog_portal():
voucher = Voucher.query.filter_by(token=voucher_token).first()
else:
voucher = None
gateway_id = request.args.get('gw_id')
if gateway_id is None:
gw_id = request.args.get('gw_id')
if gw_id is None:
abort(404)
gateway = Gateway.query.filter_by(id=gateway_id).first_or_404()
gateway = Gateway.query.filter_by(id=gw_id).first_or_404()
logo_url = None
if gateway.logo:
logo_url = logos.url(gateway.logo)
Expand Down
12 changes: 6 additions & 6 deletions auth/vouchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ def process_auth(auth):
return (constants.AUTH_ALLOWED, 'Token is already in use but details match: %s' % auth['token'])
return (constants.AUTH_DENIED, 'Token is already in use: %s' % auth['token'])
elif auth['stage'] in [ constants.STAGE_LOGOUT, constants.STAGE_COUNTERS ]:
messages = ''
messages = []

if auth['incoming'] is not None or auth['outgoing'] is not None:
if auth['incoming'] > voucher.incoming:
voucher.incoming = auth['incoming']
else:
messages += '| Warning: Incoming counter is smaller than stored value; counter not updated'
messages.append('Warning: Incoming counter is smaller than stored value; counter not updated')

if auth['outgoing'] > voucher.outgoing:
voucher.outgoing = auth['outgoing']
else:
messages += '| Warning: Outgoing counter is smaller than stored value; counter not updated'
messages.append('Warning: Outgoing counter is smaller than stored value; counter not updated')
else:
messages += '| Incoming or outgoing counter is missing; counters not updated'
messages.append('Incoming or outgoing counter is missing; counters not updated')

if auth['stage'] == constants.STAGE_LOGOUT:
# Ignore this, when you login the timer starts, that's it
# (at least it is for this model)
messages += '| Logout is not implemented'
messages.append('Logout is not implemented')

if voucher.should_end():
voucher.end()
Expand All @@ -70,6 +70,6 @@ def process_auth(auth):
voucher.end()
return (constants.AUTH_DENIED, 'Token megabytes are finished: %s' % auth['token'])

return (constants.AUTH_ALLOWED, messages)
return (constants.AUTH_ALLOWED, ' | ' .join(messages))
else:
return (constants.AUTH_ERROR, 'Unknown stage: %s' % auth['stage'])
69 changes: 69 additions & 0 deletions tests/test_wifidog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from six.moves.urllib import parse

from auth import constants
from auth.models import Voucher
from tests import TestCase


Expand All @@ -14,6 +18,71 @@ def test_login_with_valid_gw(self):
response = self.client.get('/wifidog/login/?gw_id=main-gateway1')
self.assertEqual(200, response.status_code)

def test_login(self):
with self.app.test_request_context():
voucher_code = 'main-1-1'

data = {
'gw_address': '192.168.2.1',
'gw_port': 2060,
'gw_id': 'main-gateway1',
'ip': '192.168.2.254',
'mac': '11:22:33:44:55:66',
'url': 'http://www.google.com/',
}

url = '/wifidog/login/?%s' % parse.urlencode(data)

html = self.assertOk(url)
url = html.find('//form').get('action')

data.update({
'voucher_code': voucher_code,
})

response = self.client.post(url, data=data)
voucher = Voucher.query.filter(Voucher.code == data['voucher_code']).first()

self.assertEqual(302, response.status_code)
self.assertEqual('http://%s:%d/wifidog/auth?token=%s'
% (data['gw_address'],
data['gw_port'],
voucher.token),
response.headers['Location'])

data = {
'stage': 'login',
'mac': '11:22:33:44:55:66',
'token': voucher.token,
'incoming': 0,
'outgoing': 0,
'gw_id': 'main-gateway1',
}
url = '/wifidog/auth/?%s' % parse.urlencode(data)
response = self.client.get(url)

self.assertEqual(200, response.status_code)
self.assertEqual('Auth: %d\nMessages: None\n'
% constants.AUTH_ALLOWED,
response.get_data().decode())

data['stage'] = 'counters'
data['incoming'] = 100
data['outgoing'] = 200

url = '/wifidog/auth/?%s' % parse.urlencode(data)
response = self.client.get(url)

self.assertEqual(200, response.status_code)
self.assertEqual('Auth: %d\nMessages: \n'
% constants.AUTH_ALLOWED,
response.get_data().decode())

voucher = Voucher.query.filter(Voucher.code == voucher_code).first()

self.assertEquals(data['incoming'], voucher.incoming)
self.assertEquals(data['outgoing'], voucher.outgoing)

def test_portal_without_gw_id(self):
response = self.client.get('/wifidog/portal/')
self.assertEqual(404, response.status_code)
Expand Down
Binary file modified tests/tests.db
Binary file not shown.

0 comments on commit f45a912

Please sign in to comment.