def test_init_track(session): # site1 = Wifisite.query.get(1) apmac = randomMAC() mac = randomMAC() #test creating a new track track = init_track(site1, guestmac=mac, apmac=apmac) count = Guesttrack.query.count() assert 1 == count, 'Guesttrack count is :%s instead of expected 1 ' % count #another track for same MAC done immediately shouldn't create track track = init_track(site1, guestmac=mac, apmac=apmac) count = Guesttrack.query.count() assert 1 == count, 'Guesttrack count is :%s instead of expected 1 ' % count assert isinstance( track, Guesttrack), 'init_track is not returning Guestrack instance' #different MAC track = init_track(site1, guestmac=randomMAC(), apmac=apmac) count = Guesttrack.query.count() assert 2 == count, 'Guesttrack count is :%s instead of expected 2 ' % count #same MAC after track expiry track = Guesttrack.query.get(1) track.timestamp = arrow.utcnow().replace( seconds=-(current_app.config['GUESTTRACK_LIFETIME'] + 100)).naive session.commit() track = init_track(site1, guestmac=mac, apmac=apmac) count = Guesttrack.query.count() assert 3 == count, 'Guesttrack count is :%s instead of expected 3 ' % count #check device count dcount = Device.query.count() assert 2 == dcount, 'Device count is :%s instead of expected 2 ' % count
def populate_analytics_tracks(request, session): '''fixture used to create a number of guestracks spans today,yesterday and day after ''' site1 = Wifisite.query.filter_by(id=1).first() tzinfo = tz.gettz(site1.timezone) day_start = arrow.now(tzinfo).floor('day').to('UTC') apmac = randomMAC() #create 20 tracks, starting from day start and spaced 1minutes apart for i in range(20): track_dict = {'num_visits': 1} track = init_track(site1, guestmac=randomMAC(), apmac=apmac) track.timestamp = day_start.replace(minutes=+i * 1).naive track.save() day_start = arrow.now(tzinfo).floor('day').to('UTC').replace(days=-1) #create 20 tracks, starting from day start and spaced 1minutes apart on previous day for i in range(20): track_dict = {'num_visits': 1} track = init_track(site1, guestmac=randomMAC(), apmac=apmac) track.timestamp = day_start.replace(minutes=+i * 1).naive track.save() day_start = arrow.now(tzinfo).floor('day').to('UTC').replace(days=+1) #create 20 tracks, starting from day start and spaced 1minutes apart on next day for i in range(20): track = init_track(site1, guestmac=randomMAC(), apmac=apmac) track.loginstat = {'num_visits': 1} track.timestamp = day_start.replace(minutes=+i * 1).naive track.save()
def test_guest_login2(session, client): #form resubmission test of a fresh device site1 = Wifisite.query.get(1) guestmac = randomMAC() track1 = init_track(site1, guestmac=guestmac, apmac=randomMAC()) #configure site with email mandatory site1.auth_methods = {'auth_email': 1} emailconfig = Emailconfig() emailconfig.session_limit_control = 1 emailconfig.data_limit = 500 emailconfig.time_limit = 400 emailconfig.siteid = site1.id emailconfig.save() firstname = fake.first_name() lastname = fake.last_name() email = fake.email() form_data = {'firstname': firstname, 'email': email, 'lastname': lastname} url = get_guestlogin_url('email', track1.trackid) #first post resp = client.post(url, data=form_data) #second post #simulate client visit after 10minues track1.timestamp = arrow.utcnow().replace(minutes=-10).naive track1.save() track2 = init_track(site1, guestmac=guestmac, apmac=randomMAC()) assert track2.id == 2, 'Second track not created' url = get_guestlogin_url('email', track2.trackid) resp = client.get(url) auth_url = get_guestauth_url(site1, track2.trackid) assert auth_url in resp.location, 'Valid second login not leading auth URL:%s instead\ goes to :%s' % (auth_url, resp.location) #check all values in DB ##---check emailauth emailauth = Emailauth.query.get(1) assert emailauth.state == LOGINAUTH_REPEATED , \ 'login state is :%s not LOGINAUTH_FIRST'%emailauth.state assert emailauth.data_limit == 500 assert emailauth.time_limit == 400 #check if startime is reset assert arrow.get(emailauth.starttime) < arrow.utcnow() assert arrow.get(emailauth.starttime) > arrow.utcnow().replace(seconds=-10) ##--check guesttrack track = Guesttrack.query.get(2) assert track.state == GUESTTRACK_AUTH , \ 'track state is :%s not GUESTTRACK_AUTH'%emailauth.state assert track.loginstat == {'num_visits': 1, 'relogin': 1 ,'auth_email':1} \ ,'Check loginstat' assert track.loginauthid == 1, 'check if track is assigned to loginauth'
def test_guest_login(session, client): #try to access non configured site site1 = Wifisite.query.get(1) track = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) #site without email_login config url = get_guestlogin_url('email', track.trackid) resp = client.get(url) assert '404 NOT FOUND' == resp.status, 'Email login not configured site is giving :%s instead of \ 404 NOT FOUND' % resp.status #valid site site1.auth_methods = {'auth_email': 1} session.commit() url = get_guestlogin_url('email', track.trackid) resp = client.get(url) assert '200 OK' == resp.status, 'Email login configured site is giving :%s instead of \ 200 OK' % resp.status #check if Emailconfig is created assert 1 == Emailconfig.query.count( ), 'Emailconfig is not created the first time non configured site \ is visited' assert '200 OK' == resp.status, 'Email login configured site is giving :%s instead of \ 200 OK' % resp.status assert 1 == Emailconfig.query.count( ), 'Emailconfig is not only the first time non configured site \
def guest_portal(sitekey): #--get all URL parameters, expected URL format-- devicemac = request.args.get('id') apmac = request.args.get('ap') origurl = request.args.get('url') demo = request.args.get('demo') utcnow = arrow.utcnow().naive if not devicemac or not apmac: logger.error("Guest portal called with empty ap_mac/user_mac URL:%s"\ %request.url) abort(404) if not validators.mac_address(devicemac) or not validators.mac_address(apmac): logger.error("Guest portal called with invalid ap_mac/user_mac URL:%s"%\ request.url) abort(404) wifisite = Wifisite.query.filter_by(sitekey=sitekey).first() if not wifisite: logger.error("Guest portal called with unknown UnifiID URL:%s"%request.url) abort(404) if demo and current_user.is_authenticated: demo = 1 else: demo = 0 guesttrack = init_track(wifisite,devicemac,apmac,origurl,demo) return redirect_guest(wifisite,guesttrack)
def test_handle_override1( session, client, ): #test all negetive cases site1 = Wifisite.query.get(1) guestmac = randomMAC() track = init_track(site1, guestmac=guestmac, apmac=randomMAC()) #configure site with email mandatory site1.auth_methods = {'auth_email': 1} emailconfig = Emailconfig() emailconfig.data_limit = 500 emailconfig.time_limit = 400 emailconfig.siteid = site1.id emailconfig.session_limit_control = 1 emailconfig.save() #try to visit the site with fresh device overrideurl = url_for('unifispot.modules.email.guest_override', trackid=track.trackid) assert '404 NOT FOUND' == client.get( overrideurl).status, 'Fresh device not throwing 404' #guest visited once trying withour first login url = get_guestlogin_url('email', track.trackid) resp = client.get(url) assert '404 NOT FOUND' == client.get( overrideurl).status, 'Fresh device not throwing 404' #login a guest track2 = init_track(site1, guestmac=guestmac, apmac=randomMAC()) track2.timestamp = arrow.utcnow().replace(days=-1).naive track2.save() firstname = fake.first_name() lastname = fake.last_name() email = fake.email() form_data = {'firstname': firstname, 'email': email, 'lastname': lastname} url = get_guestlogin_url('email', track2.trackid) #first post resp = client.post(url, data=form_data) #try override URL resp = client.get(overrideurl) assert '200 OK' == resp.status, \ 'Already loggedin device not throwing 200' assert 'Looks like your quota have expired' in resp.data, \ 'Quota expired message not seen'
def test_data_available(self): site1 = Wifisite.query.get(1) track = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) loginauth = self.init_loginauth(site1, track) #without any sessions assert loginauth.data_available() == 500, 'data_lavailable \ is returning :%s instead of %s' % (loginauth.time_available(), 500) #wtih unused sessions session = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) session.starttime = arrow.utcnow().replace(minutes=-15).naive session.save() assert loginauth.data_available() == 500, 'data_lavailable \ is returning :%s instead of %s' % (loginauth.time_available(), 500) #single session uses some data session.data_used = 200 session.save() assert loginauth.data_available() == 300, 'data_lavailable \ is returning :%s instead of %s' % (loginauth.time_available(), 300) #multiple sessions oldsess = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) oldsess.starttime = arrow.utcnow().replace(hours=-1).naive #very old oldsess.data_used = 450 oldsess.save() oldsess1 = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) oldsess1.starttime = arrow.utcnow().replace( minutes=-10).naive #part of current window oldsess1.data_used = 100 oldsess1.save() #overided sessions oldsess2 = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) oldsess2.starttime = arrow.utcnow().replace( minutes=-10).naive #part of current window oldsess2.data_used = 100 oldsess2.duration = 10 oldsess2.override = 1 oldsess2.save() assert loginauth.data_available() == 200, 'data_lavailable \ is returning :%s instead of %s' % (loginauth.time_available(), 200)
def populate_monthlysessions(app, session, client): #login a guest and then create a bunch of sessions #on a day and the day before site1 = Wifisite.query.get(1) guestmac = randomMAC() track1 = init_track(site1, guestmac=guestmac, apmac=randomMAC()) #configure site with email mandatory site1.auth_methods = {'auth_email': 1} emailconfig = Emailconfig() emailconfig.session_limit_control = 2 emailconfig.data_limit = 500 emailconfig.time_limit = 400 emailconfig.siteid = site1.id emailconfig.save() firstname = fake.first_name() lastname = fake.last_name() email = fake.email() form_data = {'firstname': firstname, 'email': email, 'lastname': lastname} url = get_guestlogin_url('email', track1.trackid) #first post resp = client.post(url, data=form_data) #get emailauth emailauth = Emailauth.query.get(1) #create a bunch of sessions daytime = arrow.utcnow().floor('month').replace(days=15) #15th of month track1.timestamp = daytime.replace(days=-3).naive track1.save() for i in range(10): #sessions at 15th 12th 9th 6th 3rd of this month # and 5 days last month time = daytime.replace(days=-(3 * i + 1)).naive print time track = init_track(site1, guestmac=guestmac, apmac=randomMAC()) track.timestamp = time track.save() sess = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=emailauth.id) sess.starttime = time sess.data_used = 100 sess.duration = 10 sess.save()
def test_increamentstat(self, session): site1 = Wifisite.query.get(1) track = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) track.increamentstat('num_visits') track.increamentstat('auth_email') assert track.loginstat == {'num_visits':2,'auth_email':1}, \ 'login stat is %s'%track.loginstat
def test_redirect_guest(client, session): site1 = Wifisite.query.get(1) track = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) #nologin methods with current_app.test_request_context(): resp = redirect_guest(site1, track) url = get_guestauth_url(site1, track.trackid) assert url == resp.location, 'Guest in no auth site is getting redirected to :%s instead of :%s'%\ (resp.location,url)
def test_guest_login1(session, client): #form submission test of a fresh device site1 = Wifisite.query.get(1) track = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) #configure site with email mandatory site1.auth_methods = {'auth_email': 1} emailconfig = Emailconfig() emailconfig.data_limit = 500 emailconfig.time_limit = 400 emailconfig.siteid = site1.id emailconfig.save() #emptry post should give same page url = get_guestlogin_url('email', track.trackid) resp = client.post(url, data={}) assert None == resp.location, ' email page submited with empty form is not giving same page ' firstname = fake.first_name() lastname = fake.last_name() email = fake.email() form_data = {'firstname': firstname, 'email': email, 'lastname': lastname} url = get_guestlogin_url('email', track.trackid) resp = client.post(url, data=form_data) auth_url = get_guestauth_url(site1, track.trackid) assert auth_url in resp.location, 'Valid email form submission not leading to auth URL:%s instead\ goes to :%s' % (auth_url, resp.location) newguest = Guest.query.get(1) assert firstname == newguest.firstname, "Guest firstname don't match" assert email == newguest.email, "Guest email don't match" assert site1.id == newguest.siteid, "Guest siteid don't match" #check all values in DB ##---check emailauth emailauth = Emailauth.query.get(1) assert emailauth.state == LOGINAUTH_FIRST , \ 'login state is :%s not LOGINAUTH_FIRST'%emailauth.state assert emailauth.data_limit == 500 assert emailauth.time_limit == 400 #check if startime is reset assert arrow.get(emailauth.starttime) < arrow.utcnow() assert arrow.get(emailauth.starttime) > arrow.utcnow().replace(seconds=-10) ##--check guesttrack track = Guesttrack.query.get(1) assert track.state == GUESTTRACK_AUTH , \ 'track state is :%s not GUESTTRACK_AUTH'%emailauth.state assert track.loginstat == { 'newguest': 1, 'num_visits': 1, 'auth_email': 1 }, 'Check loginstat' assert track.loginauthid == 1, 'check if track is assigned to loginauth'
def test_updatestat(self, session): site1 = Wifisite.query.get(1) track = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) track.loginstat = {'num_visits': 5} track.save() track.updatestat('auth_email', 1) assert track.loginstat == {'num_visits':5,'auth_email':1}, \ 'login stat is %s'%track.loginstat
def test_handle_override2(session, client, populate_dailysessions): #test password submission #for daily override site1 = Wifisite.query.get(1) emailconfig = Emailconfig.query.get(1) device = Device.query.get(1) #time expired, data expired emailconfig.data_limit = 600 emailconfig.time_limit = 60 emailconfig.save() track = init_track(site1, guestmac=device.devicemac, apmac=randomMAC()) overrideurl = url_for('unifispot.modules.email.guest_override', trackid=track.trackid) #try with empty password and empty configured pass resp = client.post(overrideurl) assert 'This field is required.' in resp.data, 'Empty password' #try with password and empty configured pass resp = client.post(overrideurl, data={'password': '******'}) assert 'Wrong password' in resp.data, 'wrong password' #try with different password emailconfig.session_overridepass = '******' emailconfig.save() resp = client.post(overrideurl, data={'password': '******'}) assert 'Wrong password' in resp.data, 'wrong password' resp = client.post(overrideurl, data={'password': '******'}) auth_url = get_guestauth_url(site1, track.trackid) assert auth_url in resp.location, 'Valid override login not leading auth URL:%s instead\ goes to :%s' % (auth_url, resp.location) #check DB level values track2 = Guesttrack.query.get(track.id) assert track2.loginstat == {'auth_email': 1, 'num_visits': 1, 'relogin': 1},\ 'guesttrack loginstat is not properly populated' daystart = arrow.utcnow().floor('day') daysess = daystart.replace(hours=11) #11AM of a day #check if all today's session are overriden for sess in Guestsession.query.filter( and_(Guestsession.starttime > daystart.naive, Guestsession.starttime < daysess.naive)).all(): assert sess.override == 1, 'old session of the day not overridden' #check if all yesterday's session are not overriden for sess in Guestsession.query.filter( and_(Guestsession.starttime < daystart.naive)).all(): assert sess.override == 0, ' dau before sessions overridden'
def test_get_loginauth_validator7(session, client, populate_monthlysessions): #monthly limit validation site1 = Wifisite.query.get(1) emailconfig = Emailconfig.query.get(1) device = Device.query.get(1) track2 = init_track(site1, guestmac=device.devicemac, apmac=randomMAC()) url = get_guestlogin_url('email', track2.trackid) #time expired, data expired emailconfig.data_limit = 500 emailconfig.time_limit = 50 emailconfig.save() resp = client.get(url) assert '/email/override/' in resp.location, 'Invalid 2nd login not leading override\ instead goes to :%s' % (resp.location) #time available, data expired emailconfig.data_limit = 500 emailconfig.time_limit = 500 emailconfig.save() resp = client.get(url) assert '/email/override/' in resp.location, 'Invalid 2nd login not leading override\ instead goes to :%s' % (resp.location) #time expired, data available emailconfig.data_limit = 5000 emailconfig.time_limit = 50 emailconfig.save() resp = client.get(url) assert '/email/override/' in resp.location, 'Invalid 2nd login not leading override\ instead goes to :%s' % (resp.location) #time unlimited, data expired emailconfig.data_limit = 500 emailconfig.time_limit = 0 emailconfig.save() resp = client.get(url) assert '/email/override/' in resp.location, 'Invalid 2nd login not leading override\ instead goes to :%s' % (resp.location) #time expired, data unlimited emailconfig.data_limit = 0 emailconfig.time_limit = 50 emailconfig.save() resp = client.get(url) assert '/email/override/' in resp.location, 'Invalid 2nd login not leading override\ instead goes to :%s' % (resp.location) #check DB level values track2 = Guesttrack.query.get(track2.id) assert track2.state == GUESTTRACK_PRELOGIN, 'track2 state is not GUESTTRACK_PRELOGIN but\ %s after redirection to override' % track2.state
def test_reset_usage(self): site1 = Wifisite.query.get(1) track = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) loginauth = self.init_loginauth(site1, track) fromtime = arrow.utcnow().replace(minutes=-30).naive session = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) session.starttime = arrow.utcnow().replace(minutes=-15).naive session.data_used = 200 session.duration = 5 session.save() #multiple sessions oldsess = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) oldsess.starttime = arrow.utcnow().replace(hours=-1).naive #very old oldsess.data_used = 450 oldsess.duration = 30 oldsess.save() oldsess1 = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) oldsess1.starttime = arrow.utcnow().replace( minutes=-10).naive #part of current window oldsess1.data_used = 100 oldsess1.duration = 10 oldsess1.save() oldsess2 = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) oldsess2.starttime = arrow.utcnow().replace( minutes=-10).naive #part of current window oldsess2.data_used = 100 oldsess2.duration = 10 oldsess2.override = 1 oldsess2.save() ##------usage check before and after reset assert loginauth.get_usage(fromtime) == (15, 300), 'get_usage \ is returning :(%s,%s) instead of(15,300)' % loginauth.get_usage( fromtime) loginauth.reset_usage(fromtime) assert loginauth.get_usage(fromtime) == (0, 0), 'get_usage \ is returning :(%s,%s) instead of(15,300)' % loginauth.get_usage( fromtime) ###---session which is before starttime shouldn't be affected assert Guestsession.query.get(2).override == 0
def populate_analytics_logins_site3(request, session): '''fixture used to create a number of guestracks with logins ''' site1 = Wifisite.query.filter_by(id=3).first() tzinfo = tz.gettz(site1.timezone) day_start = arrow.now(tzinfo).floor('day').to('UTC') apmac = randomMAC() #create 20 tracks, for email login #half of them new user for i in range(20): track = init_track(site1, guestmac=randomMAC(), apmac=apmac) track.timestamp = day_start.replace(minutes=+i * 1).naive track.loginstat = {'num_visits': 1, 'auth_email': 1} track.save() if i % 2: track.updatestat('newguest', 1) track.save() #create 20 tracks, for FB login #half of them new user for i in range(20): track = init_track(site1, guestmac=randomMAC(), apmac=apmac) track.timestamp = day_start.replace(minutes=+i * 1).naive track.loginstat = { 'num_visits': 1, 'auth_facebook': 1, 'fbcheckedin': 1 } track.save() if i % 2: track.updatestat('newguest', 1) track.updatestat('fbliked', 1) track.save()
def test_get_loginauth_validator6(session, client, populate_dailysessions): site1 = Wifisite.query.get(1) emailconfig = Emailconfig.query.get(1) device = Device.query.get(1) #time expired, data expired emailconfig.data_limit = 600 emailconfig.time_limit = 60 emailconfig.save() track2 = init_track(site1, guestmac=device.devicemac, apmac=randomMAC()) url = get_guestlogin_url('email', track2.trackid) resp = client.get(url) assert '/email/override/' in resp.location, 'Invalid 2nd login not leading override\ instead goes to :%s' % (resp.location)
def test_get_loginauth_validator2(session, client, populate_dailysessions): site1 = Wifisite.query.get(1) emailconfig = Emailconfig.query.get(1) device = Device.query.get(1) #both limits are unlimited emailconfig.data_limit = 0 emailconfig.time_limit = 0 emailconfig.save() track2 = init_track(site1, guestmac=device.devicemac, apmac=randomMAC()) url = get_guestlogin_url('email', track2.trackid) resp = client.get(url) auth_url = get_guestauth_url(site1, track2.trackid) assert auth_url in resp.location, 'Valid second login not leading auth URL:%s instead\ goes to :%s' % (auth_url, resp.location)
def test_time_available(self): site1 = Wifisite.query.get(1) track = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) loginauth = self.init_loginauth(site1, track) # assert loginauth.time_available() == 40, 'time_available\ is returning :%s instead of %s' % (loginauth.time_available(), 40) #unlimited timelimit should return 480 loginauth.time_limit = 0 loginauth.save() assert loginauth.time_available() == 480, 'Unlimited(0) time_limit\ is not returning 480' #expired loginauth loginauth.time_limit = 59 loginauth.starttime = arrow.utcnow().replace(minutes=-60).naive loginauth.save() assert loginauth.time_available() == 0, 'time_available\ is returning :%s instead of %s' % (loginauth.time_available(), 0)
def guest_portal(sitekey): #--get all URL parameters, expected URL format-- devicemac = request.args.get('id') apmac = request.args.get('ap') visitedurl = request.args.get('url') demo = request.args.get('demo') utcnow = arrow.utcnow().naive ua = request.headers.get('User-Agent') if ua and 'CaptiveNetworkSupport' in ua: current_app.logger.debug( 'Detected apple CNA, sending our ERROR response') return '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <TITLE>ERROR</TITLE></HEAD><BODY>ERROR</BODY></HTML>''' if not devicemac or not apmac: current_app.logger.error("Guest portal called with empty ap_mac/user_mac URL:%s"\ %request.url) abort(404) if not validators.mac_address(devicemac) or not validators.mac_address( apmac): current_app.logger.error("Guest portal called with invalid ap_mac/user_mac URL:%s"%\ request.url) abort(404) wifisite = Wifisite.query.filter_by(sitekey=sitekey).first() if not wifisite: current_app.logger.error( "Guest portal called with unknown UnifiID URL:%s" % request.url) abort(404) if demo and current_user.is_authenticated: demo = 1 else: demo = 0 guesttrack = init_track(wifisite, devicemac, apmac, visitedurl, demo) return redirect_guest(wifisite, guesttrack)
def test_handle_override3(session, client, populate_monthlysessions): #test password submission #for monthly override site1 = Wifisite.query.get(1) emailconfig = Emailconfig.query.get(1) device = Device.query.get(1) #time expired, data expired emailconfig.data_limit = 500 emailconfig.time_limit = 50 emailconfig.save() track = init_track(site1, guestmac=device.devicemac, apmac=randomMAC()) overrideurl = url_for('unifispot.modules.email.guest_override', trackid=track.trackid) #try with empty password and empty configured pass resp = client.post(overrideurl) assert 'This field is required.' in resp.data, 'Empty password' #try with password and empty configured pass resp = client.post(overrideurl, data={'password': '******'}) assert 'Wrong password' in resp.data, 'wrong password' #try with different password emailconfig.session_overridepass = '******' emailconfig.save() resp = client.post(overrideurl, data={'password': '******'}) assert 'Wrong password' in resp.data, 'wrong password' resp = client.post(overrideurl, data={'password': '******'}) auth_url = get_guestauth_url(site1, track.trackid) assert auth_url in resp.location, 'Valid override login not leading auth URL:%s instead\ goes to :%s' % (auth_url, resp.location) #check DB level values track2 = Guesttrack.query.get(track.id) assert track2.loginstat == {'auth_email': 1, 'num_visits': 1, 'relogin': 1},\ 'guesttrack loginstat is not properly populated'
def init_demo(): with app.app_context(): from sqlalchemy.exc import OperationalError from flask_security.utils import encrypt_password from unifispot.core.models import User, Account, Admin, Client, Wifisite, Landingpage from unifispot.core.guestutils import init_track, assign_guest_entry from unifispot.modules.analytics.methods import update_daily_stat from tests.helpers import randomMAC, randomGuestEmailForm from random import randint account = Account.query.filter_by(id=1).first() enc_pass = encrypt_password('password') client1 = Client.query.filter_by(email='*****@*****.**').first() if not client1: #add a client client1 = Client(email='*****@*****.**', password=enc_pass, displayname="Client User", active=1) client1.account_id = account.id client1.save() site1 = Wifisite.query.get(1) if not site1: site1 = Wifisite(name='Site1', sitekey='site1') site1.client_id = client1.id site1.account_id = account.id site1.save() landing1 = Landingpage() landing1.siteid = site1.id landing1.save() now = arrow.now() month_start = now.replace(days=-30) days = (now - month_start).days for d in range(days): day_start = month_start.replace(days=d).floor('day') logger.warn('-------Generating data for :%s' % day_start) for i in range(randint(5, 10)): track = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) track.timestamp = day_start.replace(minutes=+i * 1).naive track.loginstat = {'num_visits': 1, 'auth_email': 1} track.save() if i % 2: f = randomGuestEmailForm() g = assign_guest_entry(site1, track, f) g.createdat = day_start.replace(minutes=+i * 1).naive g.save() track.save() logger.warn('track added for email ID;%s for :%s' % (track.id, day_start)) #create random #half of them new user for i in range(randint(5, 10)): track = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) track.timestamp = day_start.replace(minutes=+i * 1).naive track.loginstat = { 'num_visits': 1, 'auth_facebook': 1, 'fbcheckedin': 1 } track.save() if i % 2: g = assign_guest_entry(site1, track, f) g.createdat = day_start.replace(minutes=+i * 1).naive g.save() track.updatestat('fbliked', 1) track.save() track.save() logger.warn('track added for FB ID;%s for :%s' % (track.id, day_start)) update_daily_stat(site1, day_start)
def test_get_usage(self): site1 = Wifisite.query.get(1) track = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) loginauth = self.init_loginauth(site1, track) fromtime = arrow.utcnow().replace(minutes=-30).naive #without any sessions assert loginauth.get_usage(fromtime) == (0, 0), 'get_usage \ is returning :%s instead of (0,0)' % ( loginauth.get_usage(fromtime)) #wtih unused sessions session = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) session.starttime = arrow.utcnow().replace(minutes=-15).naive session.save() assert loginauth.get_usage(fromtime) == (0, 0), 'get_usage \ is returning :(%s,%s) instead of (0,0)' % ( loginauth.get_usage(fromtime)) #single session uses some data session.data_used = 200 session.duration = 5 session.save() assert loginauth.get_usage(fromtime) == (5, 200), 'get_usage \ is returning :(%s,%s) instead of (30,200)' % ( loginauth.get_usage(fromtime)) #multiple sessions oldsess = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) oldsess.starttime = arrow.utcnow().replace(hours=-1).naive #very old oldsess.data_used = 450 oldsess.duration = 30 oldsess.save() oldsess1 = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) oldsess1.starttime = arrow.utcnow().replace( minutes=-10).naive #part of current window oldsess1.data_used = 100 oldsess1.duration = 10 oldsess1.save() #overided sessions oldsess2 = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) oldsess2.starttime = arrow.utcnow().replace( minutes=-10).naive #part of current window oldsess2.data_used = 100 oldsess2.duration = 10 oldsess2.override = 1 oldsess2.save() assert loginauth.get_usage(fromtime) == (15, 300), 'get_usage \ is returning :(%s,%s) instead of(15,300)' % loginauth.get_usage( fromtime)
def test_assign_guest_entry(client, session): #create dummy email and phone forms class DummyForm1(Form): email = TextField('Email') firstname = TextField('Firstname') extra1 = TextField('Extra1') extra2 = TextField('Extra2') class DummyForm2(Form): phonenumber = TextField('Email') firstname = TextField('Firstname') extra1 = TextField('Extra1') class DummyFBProfile(): first_name = None last_name = None email = None gender = None birthday = None age_range = None eform = DummyForm1() eform.email.data = '*****@*****.**' eform.firstname.data = 'firstname' eform.extra1.data = 'extra1' eform.extra2.data = 'extra2' pform = DummyForm2() pform.phonenumber.data = '+1234567890' pform.firstname.data = 'firstname' pform.extra1.data = 'extra1' profile = { 'first_name': 'first_name', 'last_name': 'last_name', 'email': '*****@*****.**', 'age_range': { 'min': 21, 'max': 28 } } site1 = Wifisite.query.get(1) #test creating a new track ##-----test email form track1 = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) track2 = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) guest1 = assign_guest_entry(site1, track1, form=eform) guest1 = assign_guest_entry(site1, track2, form=eform) cnt = Guest.query.count() assert 1 == cnt, 'number of guest created is not 1 but :%s ' % cnt newguest = Guest.query.get(1) assert newguest.details == { 'Extra1': 'extra1', 'Extra2': 'extra2' }, 'Guest details is :%s insteads \ of expected :%s' % (newguest.details, { 'Extra1': 'extra1', 'Extra2': 'extra2' }) assert newguest.siteid == site1.id, "Guest siteid is not correctly populated" assert 1 == Guesttrack.query.get(1).loginstat.get('newguest'),\ 'newguest is not set to 1 after new guest added' assert None == Guesttrack.query.get(2).loginstat.get('newguest'),\ 'newguest is not set to None after existing guest found' ##-----test phone form track3 = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) track4 = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) guest2 = assign_guest_entry(site1, track3, form=pform) guest2 = assign_guest_entry(site1, track4, form=pform) cnt = Guest.query.count() assert 2 == cnt, 'number of guest created is not 2 but :%s ' % cnt assert 1 == Guesttrack.query.get(3).loginstat.get('newguest'),\ 'newguest is not set to 1 after new guest added' assert None == Guesttrack.query.get(4).loginstat.get('newguest'),\ 'newguest is not set to None after existing guest found' ##-----test FB profile track5 = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) track6 = init_track(site1, guestmac=randomMAC(), apmac=randomMAC()) guest1 = assign_guest_entry(site1, track5, fbprofile=profile) guest1 = assign_guest_entry(site1, track6, fbprofile=profile) cnt = Guest.query.count() assert 3 == cnt, 'number of guest created is not 3 but :%s ' % cnt newguest = Guest.query.get(3) assert '*****@*****.**' == newguest.email, 'Wrong email ' assert '21-28' == newguest.agerange, 'Wrong age range' assert 1 == Guesttrack.query.get(5).loginstat.get('newguest'),\ 'newguest is not set to 1 after new guest added' assert None == Guesttrack.query.get(6).loginstat.get('newguest'),\ 'newguest is not set to None after existing guest found'
def test_validate_loginauth_usage(client, session): site1 = Wifisite.query.get(1) apmac = randomMAC() mac = randomMAC() #test creating a new track track = init_track(site1, guestmac=mac, apmac=apmac) loginauth = Loginauth(siteid=site1.id, deviceid=track.deviceid) loginauth.save() #timenow for refference utcnow = arrow.utcnow() #create bunch of sessions for i in range(10): #wtih unused sessions days = -(i + 1) session = Guestsession(siteid=site1.id, deviceid=track.deviceid, loginauthid=loginauth.id) session.starttime = utcnow.replace(days=days).naive session.data_used = 50 session.duration = 20 session.save() #fake login config class Loginconfig: def __init__(self, time_limit, data_limit): self.time_limit = time_limit self.data_limit = data_limit #expired data lconf = Loginconfig(100, 50) starttime = utcnow.replace(days=-2).naive assert False == validate_loginauth_usage( site1, track, lconf, loginauth, starttime), 'Expired datalimit not returning false' #expired time lconf = Loginconfig(20, 500) starttime = utcnow.replace(days=-2).naive assert False == validate_loginauth_usage( site1, track, lconf, loginauth, starttime), 'Expired timelimit not returning false' #nonexpired lconf = Loginconfig(200, 500) starttime = utcnow.replace(days=-2).naive assert True == validate_loginauth_usage( site1, track, lconf, loginauth, starttime), 'Non limits not returning True' chkauth = Loginauth.query.get(1) assert int(chkauth.data_limit) == 400,'datlimit is :%s instead of expected 400'%\ chkauth.data_limit assert int(chkauth.time_limit) == 160,'time_limit is :%s instead of expected 160'%\ chkauth.time_limit #unlimited data and limited time not expired lconf = Loginconfig(50, 0) starttime = utcnow.replace(days=-2).naive assert True == validate_loginauth_usage( site1, track, lconf, loginauth, starttime), 'Non limits not returning True' chkauth = Loginauth.query.get(1) assert int(chkauth.data_limit) == 1000,'datlimit is :%s instead of expected 1000'%\ chkauth.data_limit assert int(chkauth.time_limit) == 10,'time_limit is :%s instead of expected 10'%\ chkauth.time_limit #unlimited data and limited time expired lconf = Loginconfig(30, 0) starttime = utcnow.replace(days=-2).naive assert False == validate_loginauth_usage( site1, track, lconf, loginauth, starttime), 'Non limits not returning True' #unlimited time and limited data not expired lconf = Loginconfig(0, 300) starttime = utcnow.replace(days=-2).naive assert True == validate_loginauth_usage( site1, track, lconf, loginauth, starttime), 'Non limits not returning True' chkauth = Loginauth.query.get(1) assert int(chkauth.data_limit) == 200,'datlimit is :%s instead of expected 200'%\ chkauth.data_limit assert int(chkauth.time_limit) == 480,'time_limit is :%s instead of expected 480'%\ chkauth.time_limit #unlimited time and limited data expired lconf = Loginconfig(0, 30) starttime = utcnow.replace(days=-2).naive assert False == validate_loginauth_usage( site1, track, lconf, loginauth, starttime), 'Non limits not returning True'