def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.setup_env(app_id='testapp') self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() app = webapp2.WSGIApplication([ ('/admin/authentication/login', LoginAdminHandler), ], config=webapp_config) self.testapp = webtest.TestApp(app) self.supper_user = User( id='2', name='supper user', password= '******', email='*****@*****.**', phone='84973796061', is_admin=True) self.normal_user = User( id='1', name='normal user', password= '******', email='*****@*****.**', phone='84973796065', )
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.setup_env(app_id='testapp') self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() app = webapp2.WSGIApplication([ ('/admin/authentication/login', LoginAdminHandler), ], config=webapp_config) self.testapp = webtest.TestApp(app) self.supper_user = User( id='2', name='supper user', password = '******', email = '*****@*****.**', phone = '84973796061', is_admin = True ) self.normal_user = User( id='1', name = 'normal user', password = '******', email = '*****@*****.**', phone = '84973796065', )
def update_user_txn(user_key, school_key): user_entity = user_key.get() if user_entity and school_key in user_entity.schools: return if not user_entity: user_entity = User(key=user_key, user=self.user, name=self.user.nickname(), default_school=school_key, schools=[]) user_entity.schools.append(school_key) user_entity.put()
def update_user_txn(user_key, school_key): user_entity = user_key.get() if user_entity and school_key in user_entity.schools: return if not user_entity: user_entity = User( key=user_key, user=self.user, name=self.user.nickname(), default_school=school_key, schools=[]) user_entity.schools.append(school_key) user_entity.put()
def post(self): """sent email to admin of sosbeacon school""" from sosbeacon.user import forgot_password email = self.request.POST['email'] if not email: out = self.render('forgot_password.mako', is_loggedin=False, message="Please enter your email address.") self.response.out.write(out) return user_key = User.query(User.email == email) user = user_key.get() if user: forgot_password(user) out = self.render( 'forgot_password.mako', is_loggedin=False, message= "Your new password has been sent to you by email message. " "You will now be returned to where you were before.") self.response.out.write(out) else: out = self.render('forgot_password.mako', is_loggedin=False, message=""" You have not entered a email address that we recognize, or your account has not been activated or you have not set a password in settings in your SOSbeacon app on your mobile phone. Please try again. """) self.response.out.write(out)
def post(self): """sent email to admin of sosbeacon school""" from sosbeacon.user import forgot_password email = self.request.POST['email'] if not email: out = self.render('forgot_password.mako', is_loggedin=False, message="Please enter your email address.") self.response.out.write(out) return user_key = User.query(User.email == email) user = user_key.get() if user: forgot_password(user) out = self.render('forgot_password.mako', is_loggedin=False, message="Your new password has been sent to you by email message. " "You will now be returned to where you were before.") self.response.out.write(out) else: out = self.render('forgot_password.mako', is_loggedin=False, message=""" You have not entered a email address that we recognize, or your account has not been activated or you have not set a password in settings in your SOSbeacon app on your mobile phone. Please try again. """) self.response.out.write(out)
def post(self, *args, **kwargs): if not 'u' in self.session: email = self.request.POST['email'] password = self.request.POST['password'] user = User.query(ndb.AND(User.email == email), namespace='_x_') if user.get() is None: self.render_user_login(is_loggedin=False, error='Email or Password is wrong!.') return if user.get().is_admin: self.render_user_login(is_loggedin=False, error='Email or Password is wrong!.') return else: if check_password_hash(password, user.get().password): self.delete_session() self.set_current_user(user) else: self.render_user_login( is_loggedin=False, error='Email or Password is wrong!.') return user_key = self.session.get('u') user = ndb.Key(urlsafe=user_key).get() school_length = len(user.schools) #check schools that user was asigned if school_length == 1: school_key = user.schools[0] school_key = school_key.get().key.urlsafe() self.set_current_school(school_key) self.redirect("/") return if school_length == 0: self.render_user_login( is_loggedin=False, error= "You don't have any schools!. Please contact with admin for this reason." ) self.delete_session() return if school_length > 1 and 'school' not in self.request.POST: schools = [school_key.get() for school_key in user.schools] self.render_user_login(is_loggedin=True, schools=schools) return school_key = self.request.POST['school'] self.set_current_school(school_key) self.redirect("/")
def setUp(self): from sosbeacon.school import School from main import AccountHandler self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.setup_env(app_id='testapp') url_map = [ ('/school/webapp/account', AccountHandler), ('/authentication/login', LoginUserHandler) ] app = webapp2.WSGIApplication( url_map, config=webapp_config ) self.testapp = webtest.TestApp(app) self.user = User( id='1', name='longly', password = '******', email = '*****@*****.**', phone = '84973796065' ) self.school1 = School( id='100', name='School_Test', ) self.school1.put() self.user.schools = [self.school1.key] self.user.put() email = '*****@*****.**' password = '******' params1 = {'email': email, 'password': password} self.testapp.post('/authentication/login', params1)
def process_post_user(request, schema, entity): from voluptuous import Schema from sosbeacon.student import create_default_student from sosbeacon.user import send_invitation_email obj = json.loads(request.body) schema = Schema(schema, extra=True) if obj['email'] == '' or obj['phone'] == '' or \ obj['first_name'] == '' or obj['last_name'] == '': return False if len(obj['password']) < 6: return False #check user exits check_email = User.query(User.email == obj['email'], namespace='_x_') check_phone = User.query(User.phone == obj['phone'], namespace='_x_') if check_email.get() or check_phone.get(): return False try: obj = schema(obj) except: logging.exception('validation failed') logging.info(obj) user = entity.from_dict(obj) user.set_password(obj['password']) to_put = [user] ndb.put_multi(to_put) create_default_student(user) send_invitation_email(user.first_name + " " + user.last_name, user.email, obj['password']) return user
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.setup_env(app_id='testapp') self.testbed.init_datastore_v3_stub() url_map = [ webapp2.Route(r'/service/admin/user/<resource_id>', handler='sosbeacon.service.UserHandler'), webapp2.Route(r'/service/admin/user<:/?>', handler='sosbeacon.service.UserListHandler'), ] app = webapp2.WSGIApplication(url_map, config=webapp_config) self.testapp = webtest.TestApp(app) self.user1 = User( id='100', name='longly1', email='*****@*****.**', phone='84973796061', password='******', ) self.user2 = User( id='200', name='longly2', email='*****@*****.**', phone='84973796062', password='******', ) # create a new test super user self.user3 = User(id='300', name='longly3', email='*****@*****.**', phone='84973796063', password='******', is_admin=True)
def process_post_user(request, schema, entity): from voluptuous import Schema from sosbeacon.student import create_default_student from sosbeacon.user import send_invitation_email obj = json.loads(request.body) schema = Schema(schema, extra=True) if obj['email'] == '' or obj['phone'] == '' or \ obj['first_name'] == '' or obj['last_name'] == '': return False if len(obj['password']) < 6: return False #check user exits check_email = User.query(User.email == obj['email'], namespace = '_x_') check_phone = User.query(User.phone == obj['phone'], namespace = '_x_') if check_email.get() or check_phone.get(): return False try: obj = schema(obj) except: logging.exception('validation failed') logging.info(obj) user = entity.from_dict(obj) user.set_password(obj['password']) to_put = [user] ndb.put_multi(to_put) create_default_student(user) send_invitation_email(user.first_name + " " + user.last_name, user.email, obj['password']) return user
def post(self, *args, **kwargs): if not 'u' in self.session: email = self.request.POST['email'] password = self.request.POST['password'] user = User.query(ndb.AND(User.email == email), namespace = '_x_') if user.get() is None: self.render_user_login(is_loggedin = False, error='Email or Password is wrong!.') return if user.get().is_admin: self.render_user_login(is_loggedin = False, error='Email or Password is wrong!.') return else: if check_password_hash(password, user.get().password): self.delete_session() self.set_current_user(user) else: self.render_user_login(is_loggedin = False, error='Email or Password is wrong!.') return user_key = self.session.get('u') user = ndb.Key(urlsafe=user_key).get() school_length = len(user.schools) #check schools that user was asigned if school_length == 1: school_key = user.schools[0] school_key = school_key.get().key.urlsafe() self.set_current_school(school_key) self.redirect("/") return if school_length == 0: self.render_user_login(is_loggedin = False, error="You don't have any schools!. Please contact with admin for this reason.") self.delete_session() return if school_length > 1 and 'school' not in self.request.POST: schools = [school_key.get() for school_key in user.schools] self.render_user_login(is_loggedin = True, schools=schools) return school_key = self.request.POST['school'] self.set_current_school(school_key) self.redirect("/")
def test_from_dict(self, memcache_delete_mock): """Ensure merging two non-acked doesn't ack.""" from sosbeacon.user import User user_dict = { 'name': 'ly hoang long', 'password': '******', 'email': '*****@*****.**', 'phone': '84973796065', 'schools': [] } user = User.from_dict(user_dict) self.assertEqual(user_dict['name'], user.name) self.assertFalse(memcache_delete_mock.call_count)
def test_to_from_composition(self): """Ensure to_dict(from_dict(x)) returns a correctly setup object.""" from sosbeacon.user import User user_dict = { 'name': 'ly hoang long', 'password': '******', 'email': '*****@*****.**', 'phone': '84973796065', 'schools': [] } user = User.from_dict(user_dict) user.put() new_user = user.to_dict() self.assertEqual(user_dict, new_user)
def post(self, *args, **kwargs): if not 'ad' in self.session: email = self.request.POST['email'] password = self.request.POST['password'] user = User.query(ndb.AND(User.email == email, User.is_admin == True), namespace = '_x_') if user.get() is None: self.render_admin_login(error='Email or Password is wrong!.') return else: if check_password_hash(password, user.get().password): self.setup_admin_session(user) else: self.render_admin_login(error='Email or Password is wrong!.') return self.redirect("/admin")
def post(self, *args, **kwargs): if not 'ad' in self.session: email = self.request.POST['email'] password = self.request.POST['password'] user = User.query(ndb.AND(User.email == email, User.is_admin == True), namespace='_x_') if user.get() is None: self.render_admin_login(error='Email or Password is wrong!.') return else: if check_password_hash(password, user.get().password): self.setup_admin_session(user) else: self.render_admin_login( error='Email or Password is wrong!.') return self.redirect("/admin")
class TestLogInSystem(unittest.TestCase): """Test login""" def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.setup_env(app_id='testapp') self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() app = webapp2.WSGIApplication([ ('/authentication/login', LoginUserHandler), ], config=webapp_config) self.testapp = webtest.TestApp(app) self.user = User( id='1', name='longly', password= '******', email='*****@*****.**', phone='84973796065') self.supper_user = User( id='2', name='supper user', password= '******', email='*****@*****.**', phone='84973796061', is_admin=True) self.school1 = School( id='100', name='School_Test', ) self.school2 = School( id='200', name='School_Test_2', ) def test_get_url_login(self, sessions_mock, template_mock): """Test the app, passing parameters to build a request.""" response = self.testapp.get('/authentication/login') self.assertEqual(response.status_int, 200) def test_login_wrong_password(self, sessions_mock, template_mock): """Test post wrong email and password to url login""" email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/authentication/login', params) self.assertEqual(response.status_int, 200) self.assertIn('Email or Password is wrong!.', response.normal_body) def test_login_wrong_email(self, sessions_mock, template_mock): """Test post wrong email and password to url login""" email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/authentication/login', params) self.assertEqual(response.status_int, 200) self.assertIn('Email or Password is wrong!.', response.normal_body) def test_login_user_no_assign_school(self, sessions_mock, template_mock): """Test post correct email and password to url login but do not assign to school""" self.user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/authentication/login', params) self.assertEqual(response.status_int, 200) self.assertIn( "You don't have any schools!. Please contact with admin for this reason.", response.normal_body) def test_login_user_assigned_one_school(self, sessions_mock, template_mock): """Test post correct email and password to url login and assigned to one school""" self.school1.put() self.user.schools = [self.school1.key] self.user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/authentication/login', params) self.assertEqual(response.location, 'http://localhost/') def test_login_assigned_multi_school(self, sessions_mock, template_mock): """Test post correct email and password to url login and assigned to multi school""" self.school1.put() self.school2.put() self.user.schools = [self.school1.key, self.school2.key] self.user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/authentication/login', params) self.assertEqual(response.status_int, 200) self.assertIn("Please choose school", response.normal_body) self.assertIn(self.school1.name, response.normal_body) self.assertIn(self.school2.name, response.normal_body) def test_user_login_is_admin(self, sessions_mock, template_mock): """Ensure superuser can not logged to system""" self.supper_user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/authentication/login', params) self.assertEqual(response.status_int, 200) self.assertIn('Email or Password is wrong!.', response.normal_body)
class TestLogInSystem(unittest.TestCase): """Test login""" def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.setup_env(app_id='testapp') self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() app = webapp2.WSGIApplication([ ('/authentication/login', LoginUserHandler), ], config=webapp_config) self.testapp = webtest.TestApp(app) self.user = User( id='1', name='longly', password = '******', email = '*****@*****.**', phone = '84973796065' ) self.supper_user = User( id='2', name='supper user', password = '******', email = '*****@*****.**', phone = '84973796061', is_admin = True ) self.school1 = School( id='100', name='School_Test', ) self.school2 = School( id='200', name='School_Test_2', ) def test_get_url_login(self, sessions_mock, template_mock): """Test the app, passing parameters to build a request.""" response = self.testapp.get('/authentication/login') self.assertEqual(response.status_int, 200) def test_login_wrong_password(self, sessions_mock, template_mock): """Test post wrong email and password to url login""" email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/authentication/login', params) self.assertEqual(response.status_int, 200) self.assertIn('Email or Password is wrong!.', response.normal_body) def test_login_wrong_email(self, sessions_mock, template_mock): """Test post wrong email and password to url login""" email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/authentication/login', params) self.assertEqual(response.status_int, 200) self.assertIn('Email or Password is wrong!.', response.normal_body) def test_login_user_no_assign_school(self, sessions_mock, template_mock): """Test post correct email and password to url login but do not assign to school""" self.user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/authentication/login', params) self.assertEqual(response.status_int, 200) self.assertIn("You don't have any schools!. Please contact with admin for this reason.", response.normal_body) def test_login_user_assigned_one_school(self, sessions_mock, template_mock): """Test post correct email and password to url login and assigned to one school""" self.school1.put() self.user.schools = [self.school1.key] self.user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/authentication/login', params) self.assertEqual(response.location, 'http://localhost/') def test_login_assigned_multi_school(self, sessions_mock, template_mock): """Test post correct email and password to url login and assigned to multi school""" self.school1.put() self.school2.put() self.user.schools = [self.school1.key, self.school2.key] self.user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/authentication/login', params) self.assertEqual(response.status_int, 200) self.assertIn("Please choose school",response.normal_body) self.assertIn(self.school1.name, response.normal_body) self.assertIn(self.school2.name, response.normal_body) def test_user_login_is_admin(self, sessions_mock, template_mock): """Ensure superuser can not logged to system""" self.supper_user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/authentication/login', params) self.assertEqual(response.status_int, 200) self.assertIn('Email or Password is wrong!.', response.normal_body)
class TestAdminLogin(unittest.TestCase): """Test admin login""" def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.setup_env(app_id='testapp') self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() app = webapp2.WSGIApplication([ ('/admin/authentication/login', LoginAdminHandler), ], config=webapp_config) self.testapp = webtest.TestApp(app) self.supper_user = User( id='2', name='supper user', password= '******', email='*****@*****.**', phone='84973796061', is_admin=True) self.normal_user = User( id='1', name='normal user', password= '******', email='*****@*****.**', phone='84973796065', ) def test_normal_user_can_not_login_admin(self): """Ensure normal user can not login to admin area""" self.normal_user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/admin/authentication/login', params) self.assertEqual(response.status_int, 200) self.assertIn('Email or Password is wrong!.', response.normal_body) def test_supper_user_login_admin(self): """Ensure supper can be login to admin area""" self.supper_user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/admin/authentication/login', params) self.assertEqual(response.location, 'http://localhost/admin') def test_supper_user_wrong_password(self): """Ensure supper user can not login with wrong password""" self.supper_user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/admin/authentication/login', params) self.assertIn('Email or Password is wrong!.', response.normal_body) def test_supper_user_wrong_email(self): """Ensure supper user can not login with wrong password""" self.supper_user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/admin/authentication/login', params) self.assertIn('Email or Password is wrong!.', response.normal_body)
class TestAccountHandler(unittest.TestCase): """Test edit account user when login""" def setUp(self): from sosbeacon.school import School from main import AccountHandler self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.setup_env(app_id='testapp') url_map = [ ('/school/webapp/account', AccountHandler), ('/authentication/login', LoginUserHandler) ] app = webapp2.WSGIApplication( url_map, config=webapp_config ) self.testapp = webtest.TestApp(app) self.user = User( id='1', name='longly', password = '******', email = '*****@*****.**', phone = '84973796065' ) self.school1 = School( id='100', name='School_Test', ) self.school1.put() self.user.schools = [self.school1.key] self.user.put() email = '*****@*****.**' password = '******' params1 = {'email': email, 'password': password} self.testapp.post('/authentication/login', params1) def test_empty_current_password(self): """Ensure user have to fill down textbox current password""" name = 'user1' email = '*****@*****.**' phone = '84973796061' current_password = '' params = { 'name': name, 'email': email, 'current_password': current_password, 'phone': phone, } response = self.testapp.post('/school/webapp/account', params) self.assertIn(response.normal_body, 'Field current password is required.') def test_wrong_current_password(self): """Ensure wrong current password can not update information user """ name = 'user1' email = '*****@*****.**' phone = '84973796061' current_password = '******' params = { 'name': name, 'email': email, 'current_password': current_password, 'phone': phone, } response = self.testapp.post('/school/webapp/account', params) self.assertIn(response.normal_body, 'Current password is wrong.') def test_confirm_password_wrong(self): """Ensure user have to complete confirm password to change new password""" name = 'user1' email = '*****@*****.**' phone = '84973796061' current_password = '******' new_password = '******' confirm_password = '******' params = { 'name': name, 'email': email, 'current_password': current_password, 'phone': phone, 'confirm_password': confirm_password, 'new_password': new_password } response = self.testapp.post('/school/webapp/account', params) self.assertIn(response.normal_body, 'Confirm password is not correct.') def test_update_successful(self): """Ensure user update successful with correct password""" name = 'user1' email = '*****@*****.**' phone = '84973796061' current_password = '******' new_password = '******' confirm_password = '******' params = { 'name': name, 'email': email, 'current_password': current_password, 'phone': phone, 'confirm_password': confirm_password, 'new_password': new_password } response = self.testapp.post('/school/webapp/account', params) self.assertIn(response.normal_body, 'Account updated successfully.')
class TestAdminLogin(unittest.TestCase): """Test admin login""" def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.setup_env(app_id='testapp') self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() app = webapp2.WSGIApplication([ ('/admin/authentication/login', LoginAdminHandler), ], config=webapp_config) self.testapp = webtest.TestApp(app) self.supper_user = User( id='2', name='supper user', password = '******', email = '*****@*****.**', phone = '84973796061', is_admin = True ) self.normal_user = User( id='1', name = 'normal user', password = '******', email = '*****@*****.**', phone = '84973796065', ) def test_normal_user_can_not_login_admin(self): """Ensure normal user can not login to admin area""" self.normal_user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/admin/authentication/login', params) self.assertEqual(response.status_int, 200) self.assertIn('Email or Password is wrong!.', response.normal_body) def test_supper_user_login_admin(self): """Ensure supper can be login to admin area""" self.supper_user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/admin/authentication/login', params) self.assertEqual(response.location, 'http://localhost/admin') def test_supper_user_wrong_password(self): """Ensure supper user can not login with wrong password""" self.supper_user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/admin/authentication/login', params) self.assertIn('Email or Password is wrong!.', response.normal_body) def test_supper_user_wrong_email(self): """Ensure supper user can not login with wrong password""" self.supper_user.put() email = '*****@*****.**' password = '******' params = {'email': email, 'password': password} response = self.testapp.post('/admin/authentication/login', params) self.assertIn('Email or Password is wrong!.', response.normal_body)