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()
示例#3
0
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)
示例#4
0
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)
示例#5
0
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 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)