コード例 #1
0
    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)
コード例 #2
0
    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',
        )
コード例 #3
0
    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',
        )
コード例 #4
0
    def test_from_dict(self, memcache_delete_mock):
        """Ensure merging two non-acked doesn't ack."""
        from datetime import datetime
        from sosbeacon.school import School

        school_dict = {
            'name': 'ly hoang long',
        }

        school = School.from_dict(school_dict)

        self.assertEqual(school_dict['name'], school.name)

        self.assertFalse(memcache_delete_mock.call_count)
コード例 #5
0
    def test_from_dict(self, memcache_delete_mock):
        """Ensure merging two non-acked doesn't ack."""
        from datetime import datetime
        from sosbeacon.school import School

        school_dict = {
            'name': 'ly hoang long',
            }

        school = School.from_dict(school_dict)

        self.assertEqual(school_dict['name'], school.name)

        self.assertFalse(memcache_delete_mock.call_count)
コード例 #6
0
    def test_to_from_composition(self):
        """Ensure to_dict(from_dict(x)) returns a correctly setup object."""
        from datetime import datetime
        from sosbeacon.school import School

        school_dict = {
            'name': 'ly hoang long',
        }

        school = School.from_dict(school_dict)
        school.put()

        new_school = school.to_dict()

        self.assertEqual(school_dict, new_school)
コード例 #7
0
    def test_to_from_composition(self):
        """Ensure to_dict(from_dict(x)) returns a correctly setup object."""
        from datetime import datetime
        from sosbeacon.school import School

        school_dict = {
            'name': 'ly hoang long',
            }

        school = School.from_dict(school_dict)
        school.put()

        new_school = school.to_dict()

        self.assertEqual(school_dict, new_school)
コード例 #8
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)
コード例 #9
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.')
コード例 #10
0
    def setUp(self):
        from sosbeacon.school import School
        from sosbeacon.student import Student
        from sosbeacon.student import DEFAULT_STUDENT_ID

        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()

        url_map = [
            webapp2.Route(r'/service/student/<resource_id:.+>',
                          handler='sosbeacon.service.StudentHandler'),
            webapp2.Route(r'/service/student<:/?>',
                          handler='sosbeacon.service.StudentListHandler'),

            webapp2.Route(r'/service/admin/user<:/?>',
                          handler='sosbeacon.service.UserListHandler'),

            ('/authentication/login', LoginUserHandler)
        ]

        app = webapp2.WSGIApplication(
            url_map,
            config=webapp_config
        )
        self.testapp = webtest.TestApp(app)

        self.school1 = School(
            id='100',
            name='School_Test',
        )

        self.school2 = School(
            id='200',
            name='School_Test_2',
        )

        self.school1.put()
        self.school2.put()

        params = {
            'name': 'longly',
            'email': '*****@*****.**',
            'phone': '84973796065',
            'password': '******',
            'schools': [self.school1.key.urlsafe()]
        }
        self.testapp.post_json('/service/admin/user/', params)

        email = '*****@*****.**'
        password = '******'

        params1 = {'email': email, 'password': password}
        self.testapp.post('/authentication/login', params1)


        self.student1 = Student(
            id = '1',
            name = 'Student 1',
            contacts = [{
                            'name': 'contact 1',
                            'methods': ['123432', '*****@*****.**']
                        },
                        {
                            'name': 'contact 2',
                            'methods': ['123432', '*****@*****.**']
                        }
            ],
            school = self.school1.key,
            is_direct = False
        )

        self.student2 = Student(
            id = '2',
            name = 'Student 2',
            contacts = [{
                            'name': 'contact 1',
                            'methods': ['123432', '*****@*****.**']
                        },
                        {
                            'name': 'contact 2',
                            'methods': ['123432', '*****@*****.**']
                        }
            ],
            school = self.school1.key,
            is_direct = False
        )

        self.student3 = Student(
            id = '3',
            name = 'Student 3',
            contacts = [{
                            'name': 'contact 1',
                            'methods': ['123432', '*****@*****.**']
                        },
                        {
                            'name': 'contact 2',
                            'methods': ['123432', '*****@*****.**']
                        }
            ],
            school = self.school1.key,
            is_direct = True
        )

        self.student4 = Student(
            id = '4',
            name = 'Student 4',
            contacts = [{
                            'name': 'contact 1',
                            'methods': ['123432', '*****@*****.**']
                        },
                        {
                            'name': 'contact 2',
                            'methods': ['123432', '*****@*****.**']
                        }
            ],
            school = self.school2.key,
            is_direct = True
        )

        self.student5 = Student(
            id = '5',
            name = 'Student 5',
            contacts = [{
                            'name': 'contact 1',
                            'methods': ['123432', '*****@*****.**']
                        },
                        {
                            'name': 'contact 2',
                            'methods': ['123432', '*****@*****.**']
                        }
            ],
            school = self.school2.key,
            is_direct = False
        )
コード例 #11
0
class TestServiceContact(unittest.TestCase):
    """Test service contact"""
    def setUp(self):
        from sosbeacon.school import School
        from sosbeacon.student import Student
        from sosbeacon.student import DEFAULT_STUDENT_ID

        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()

        url_map = [
            webapp2.Route(r'/service/student/<resource_id:.+>',
                          handler='sosbeacon.service.StudentHandler'),
            webapp2.Route(r'/service/student<:/?>',
                          handler='sosbeacon.service.StudentListHandler'),

            webapp2.Route(r'/service/admin/user<:/?>',
                          handler='sosbeacon.service.UserListHandler'),

            ('/authentication/login', LoginUserHandler)
        ]

        app = webapp2.WSGIApplication(
            url_map,
            config=webapp_config
        )
        self.testapp = webtest.TestApp(app)

        self.school1 = School(
            id='100',
            name='School_Test',
        )

        self.school2 = School(
            id='200',
            name='School_Test_2',
        )

        self.school1.put()
        self.school2.put()

        params = {
            'name': 'longly',
            'email': '*****@*****.**',
            'phone': '84973796065',
            'password': '******',
            'schools': [self.school1.key.urlsafe()]
        }
        self.testapp.post_json('/service/admin/user/', params)

        email = '*****@*****.**'
        password = '******'

        params1 = {'email': email, 'password': password}
        self.testapp.post('/authentication/login', params1)


        self.student1 = Student(
            id = '1',
            name = 'Student 1',
            contacts = [{
                            'name': 'contact 1',
                            'methods': ['123432', '*****@*****.**']
                        },
                        {
                            'name': 'contact 2',
                            'methods': ['123432', '*****@*****.**']
                        }
            ],
            school = self.school1.key,
            is_direct = False
        )

        self.student2 = Student(
            id = '2',
            name = 'Student 2',
            contacts = [{
                            'name': 'contact 1',
                            'methods': ['123432', '*****@*****.**']
                        },
                        {
                            'name': 'contact 2',
                            'methods': ['123432', '*****@*****.**']
                        }
            ],
            school = self.school1.key,
            is_direct = False
        )

        self.student3 = Student(
            id = '3',
            name = 'Student 3',
            contacts = [{
                            'name': 'contact 1',
                            'methods': ['123432', '*****@*****.**']
                        },
                        {
                            'name': 'contact 2',
                            'methods': ['123432', '*****@*****.**']
                        }
            ],
            school = self.school1.key,
            is_direct = True
        )

        self.student4 = Student(
            id = '4',
            name = 'Student 4',
            contacts = [{
                            'name': 'contact 1',
                            'methods': ['123432', '*****@*****.**']
                        },
                        {
                            'name': 'contact 2',
                            'methods': ['123432', '*****@*****.**']
                        }
            ],
            school = self.school2.key,
            is_direct = True
        )

        self.student5 = Student(
            id = '5',
            name = 'Student 5',
            contacts = [{
                            'name': 'contact 1',
                            'methods': ['123432', '*****@*****.**']
                        },
                        {
                            'name': 'contact 2',
                            'methods': ['123432', '*****@*****.**']
                        }
            ],
            school = self.school2.key,
            is_direct = False
        )

    def test_service_create_student(self):
        """Ensure create success new student with json object send from client"""
        params = {
            'name': 'Student Test',
            'groups': [],
            'contacts': [{
                             'name': 'contact 1',
                             'methods': ['123432', '*****@*****.**']
                         },
                         {
                             'name': 'contact 2',
                             'methods': ['123432', '*****@*****.**']
                         }
            ]
        }

        response = self.testapp.post_json('/service/student', params)
        obj = json.loads(response.normal_body)

        self.assertEqual(response.status_int, 200)
        self.assertEqual(obj['name'], params['name'])
        self.assertEqual(obj['groups'], params['groups'])
        self.assertFalse(obj['is_direct'])

    def test_service_get_filter_student_contact(self):
        """Ensure server response student contact which same school"""
        from google.appengine.ext import ndb
        to_put = [self.student1, self.student2, self.student3, self.student4, self.student5]
        ndb.put_multi(to_put)

        response = self.testapp.get('/service/student?feq_is_direct=false')
        obj = json.loads(response.normal_body)

        self.assertEqual(len(obj), 2)

    def test_service_get_filter_direct_contact(self):
        """Ensure server response student contact which same school and default student"""
        from google.appengine.ext import ndb
        to_put = [self.student1, self.student2, self.student3, self.student4, self.student5]
        ndb.put_multi(to_put)

        response = self.testapp.get('/service/student?feq_is_direct=true')
        obj = json.loads(response.normal_body)

        self.assertEqual(len(obj), 2)
        self.assertTrue(obj[0]['default_student'])


    def test_service_edit_student(self):
        """Ensure student will be update new data"""
        self.student1.put()
        params = {
            'name': 'Update Student',
            'groups': [],
            'contacts': [{
                             'name': 'contact 1',
                             'methods': ['123432', '*****@*****.**']
                         },
                         {
                             'name': 'contact 2',
                             'methods': ['123432', '*****@*****.**']
                         }
            ]
        }

        response = self.testapp.put_json('/service/student/%s' % self.student1.key.urlsafe(), params)
        obj = json.loads(response.normal_body)

        self.assertEqual(response.status_int, 200)
        self.assertEqual(obj['name'], params['name'])
        self.assertEqual(obj['groups'], params['groups'])

    def test_service_delete_student(self):
        """Ensure this student will be None object"""
        from google.appengine.ext import ndb
        self.student1.put()
        response = self.testapp.delete('/service/student/%s' %self.student1.key.urlsafe())
        query_event = ndb.Key(urlsafe=self.student1.key.urlsafe())
        self.assertIsNone(query_event.get())
        self.assertEqual(response.status_int, 200)
コード例 #12
0
    def setUp(self):
        from sosbeacon.school import School
        from sosbeacon.group import Group
        from sosbeacon.group import ADMIN_GROUPS_ID
        from sosbeacon.group import STAFF_GROUPS_ID

        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/group/<resource_id:.+>',
                          handler='sosbeacon.service.GroupHandler'),
            webapp2.Route(r'/service/group<:/?>',
                          handler='sosbeacon.service.GroupListHandler'),

            webapp2.Route(r'/service/admin/user<:/?>',
                          handler='sosbeacon.service.UserListHandler'),

            ('/authentication/login', LoginUserHandler)
        ]

        app = webapp2.WSGIApplication(
            url_map,
            config=webapp_config
        )
        self.testapp = webtest.TestApp(app)

        self.school1 = School(
            id='100',
            name='School_Test',
        )

        self.school2 = School(
            id='200',
            name='School_Test_2',
        )

        self.school1.put()

        params = {
            'name': 'longly',
            'email': '*****@*****.**',
            'phone': '84973796065',
            'password': '******',
            'schools': [self.school1.key.urlsafe()]
        }
        self.testapp.post_json('/service/admin/user/', params)

        email = '*****@*****.**'
        password = '******'

        params1 = {'email': email, 'password': password}
        self.testapp.post('/authentication/login', params1)

        self.group1 = Group(
            id='1',
            name='Group 1',
            school=self.school1.key
        )

        self.group2 = Group(
            id='2',
            name='Group 2',
            school=self.school1.key
        )

        self.group3 = Group(
            id='3',
            name='Group 3',
            school=self.school1.key
        )

        self.group4 = Group(
            id='4',
            name='Group 4',
            school=self.school2.key
        )

        self.group_admin = Group(
            id=ADMIN_GROUPS_ID + "%s" % (self.school1.key.id()),
            name='Group Admin',
            school=self.school1.key,
            default = True
        )

        self.group_staff = Group(
            id=STAFF_GROUPS_ID + "%s" % (self.school1.key.id()),
            name='Group Staff',
            school=self.school1.key,
            default = True
        )
コード例 #13
0
class TestGroupService(unittest.TestCase):
    """Test service of group"""
    def setUp(self):
        from sosbeacon.school import School
        from sosbeacon.group import Group
        from sosbeacon.group import ADMIN_GROUPS_ID
        from sosbeacon.group import STAFF_GROUPS_ID

        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/group/<resource_id:.+>',
                          handler='sosbeacon.service.GroupHandler'),
            webapp2.Route(r'/service/group<:/?>',
                          handler='sosbeacon.service.GroupListHandler'),

            webapp2.Route(r'/service/admin/user<:/?>',
                          handler='sosbeacon.service.UserListHandler'),

            ('/authentication/login', LoginUserHandler)
        ]

        app = webapp2.WSGIApplication(
            url_map,
            config=webapp_config
        )
        self.testapp = webtest.TestApp(app)

        self.school1 = School(
            id='100',
            name='School_Test',
        )

        self.school2 = School(
            id='200',
            name='School_Test_2',
        )

        self.school1.put()

        params = {
            'name': 'longly',
            'email': '*****@*****.**',
            'phone': '84973796065',
            'password': '******',
            'schools': [self.school1.key.urlsafe()]
        }
        self.testapp.post_json('/service/admin/user/', params)

        email = '*****@*****.**'
        password = '******'

        params1 = {'email': email, 'password': password}
        self.testapp.post('/authentication/login', params1)

        self.group1 = Group(
            id='1',
            name='Group 1',
            school=self.school1.key
        )

        self.group2 = Group(
            id='2',
            name='Group 2',
            school=self.school1.key
        )

        self.group3 = Group(
            id='3',
            name='Group 3',
            school=self.school1.key
        )

        self.group4 = Group(
            id='4',
            name='Group 4',
            school=self.school2.key
        )

        self.group_admin = Group(
            id=ADMIN_GROUPS_ID + "%s" % (self.school1.key.id()),
            name='Group Admin',
            school=self.school1.key,
            default = True
        )

        self.group_staff = Group(
            id=STAFF_GROUPS_ID + "%s" % (self.school1.key.id()),
            name='Group Staff',
            school=self.school1.key,
            default = True
        )

    def test_service_url(self):
        """Ensure link to service group exists"""
        response = self.testapp.get('/service/group')
        self.assertEqual(response.status_int, 200)

    def test_service_create_group(self):
        """Ensure create success new event with json object send from client"""
        params = {
            'name': 'Group Test',
            }

        response = self.testapp.post_json('/service/group', params)
        obj = json.loads(response.normal_body)

        self.assertEqual(response.status_int, 200)
        self.assertEqual(obj['name'], params['name'])

    def test_service_create_exists_name_group(self):
        """Ensure can not create duplicate name group"""
        self.group1.put()
        params = {
            'name' : 'Group 1',
        }
        response = self.testapp.post_json('/service/group', params, status=400)

        self.assertEqual(response.status_int, 400)

    def test_service_get_filter_group(self):
        """Ensure server response group which same school"""
        to_put = [self.group1, self.group2, self.group3, self.group4, self.group_admin, self.group_staff]
        ndb.put_multi(to_put)

        response = self.testapp.get('/service/group')
        obj = json.loads(response.normal_body)

        self.assertEqual(len(obj), 5)

    def test_service_edit_group(self):
        """Ensure group will be update new data"""
        self.group1.put()
        params = {
            'name':'Update Group'
        }

        response = self.testapp.put_json('/service/group/%s' % self.group1.key.urlsafe(), params)
        obj = json.loads(response.normal_body)

        self.assertEqual(response.status_int, 200)
        self.assertEqual(obj['name'], params['name'])

#    def test_service_edit_duplicate_group(self):
#        """Ensure user can not edit duplication group name"""
#        self.group1.put()
#        params = {
#            'name':'Group 1'
#        }
#        response = self.testapp.put_json('/service/group/%s' % self.group1.key.urlsafe(), params)
#        logging.info(response)
#        self.assertEqual(response.status_int, 400)

    def test_service_edit_default_group(self):
        """Ensure can not update information of default group"""
        self.group_admin.put()
        params = {
            'name':'Update Group Admin'
        }
        response = self.testapp.put_json('/service/group/%s' % self.group_admin.key.urlsafe(), params, status=400)
        self.assertEqual(response.status_int, 400)

    def test_service_delete_group(self):
        """Ensure this group will be None object"""
        from google.appengine.ext import ndb
        self.group1.put()
        response = self.testapp.delete('/service/group/%s' %self.group1.key.urlsafe())
        query_group = ndb.Key(urlsafe=self.group1.key.urlsafe())
        self.assertIsNone(query_group.get())
        self.assertEqual(response.status_int, 200)

    def test_service_delete_default_group(self):
        """Ensure can not delete default group"""
        self.group_admin.put()
        response = self.testapp.delete('/service/group/%s' % self.group_admin.key.urlsafe(), status=400)
        self.assertEqual(response.status_int, 400)

    def test_number_student_of_group(self):
        """Ensure number student of group always > 1"""
        to_put = [self.group1, self.group2, self.group3, self.group4, self.group_admin, self.group_staff]
        ndb.put_multi(to_put)

        response = self.testapp.get('/service/group')
        obj = json.loads(response.normal_body)

        for i in obj:
            self.assertGreaterEqual(1, i['number_student'])

    def test_sort_admin_staff_group(self):
        """Ensure admin and staff always at position 1 and 2"""
        to_put = [self.group1, self.group2, self.group3, self.group4, self.group_admin, self.group_staff]
        ndb.put_multi(to_put)

        response = self.testapp.get('/service/group')
        obj = json.loads(response.normal_body)

        self.assertEqual(obj[0]['name'], self.group_admin.name)
        self.assertEqual(obj[1]['name'], self.group_staff.name)
コード例 #14
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)