Пример #1
0
 def test_get_UserHackathonRel_list_result_not_empty(self):
     db_adapter = Mock()
     UserHackathonRel1 = UserHackathonRel(id=1, real_name='test1', email='*****@*****.**', hackathon_id=1)
     UserHackathonRel2 = UserHackathonRel(id=2, real_name='test2', email='*****@*****.**', hackathon_id=1)
     db_adapter.find_all_objects_by.return_value = [UserHackathonRel1, UserHackathonRel2]
     rm = RegisterManager(db_adapter)
     with app.test_request_context('/'):
         self.assertEqual(len(rm.get_all_registration_by_hackathon_id(1)), 2)
         db_adapter.find_all_objects_by.assert_called_once_with(UserHackathonRel, hackathon_id=1)
Пример #2
0
    def test_validate_request_token_not_found(self):
        token_value = "token_value"

        mock_db = Mock()
        mock_db.find_first_object_by.return_value = None
        am = AdminManager(mock_db)

        with app.test_request_context('/api', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertFalse(am.validate_request())
            mock_db.find_first_object_by.assert_called_once_with(AdminToken, token=token_value)
Пример #3
0
    def test_validate_request_token_not_found(self):
        token_value = "token_value"

        mock_db = Mock(spec=SQLAlchemyAdapter)
        mock_db.find_first_object_by.return_value = None

        um = UserManager(mock_db)

        with app.test_request_context('/api', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertFalse(um.validate_login())
            mock_db.find_first_object_by.assert_called_once_with(UserToken, token=token_value)
 def test_validate_request_if_token_missing(self):
     um = UserManager(None)
     '''more args for app.text_request_context:
              path='/', base_url=None, query_string=None,
              method='GET', input_stream=None, content_type=None,
              content_length=None, errors_stream=None, multithread=False,
              multiprocess=False, run_once=False, headers=None, data=None,
              environ_base=None, environ_overrides=None, charset='utf-8'
     '''
     with app.test_request_context('/', headers=None):
         self.assertFalse("token" in request.headers)
         self.assertFalse(um.validate_request())
Пример #5
0
    def test_validate_request_token_not_found(self):
        token_value = "token_value"

        mock_db = Mock()
        mock_db.find_first_object_by.return_value = None
        am = AdminManager(mock_db)

        with app.test_request_context('/api', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertFalse(am.validate_request())
            mock_db.find_first_object_by.assert_called_once_with(
                AdminToken, token=token_value)
Пример #6
0
 def test_validate_request_if_token_missing(self):
     um = UserManager(None)
     '''more args for app.text_request_context:
              path='/', base_url=None, query_string=None,
              method='GET', input_stream=None, content_type=None,
              content_length=None, errors_stream=None, multithread=False,
              multiprocess=False, run_once=False, headers=None, data=None,
              environ_base=None, environ_overrides=None, charset='utf-8'
     '''
     with app.test_request_context('/', headers=None):
         self.assertFalse("token" in request.headers)
         self.assertFalse(um.validate_request())
Пример #7
0
    def test_validate_request_token_not_found(self):
        token_value = "token_value"

        mock_db = Mock(spec=SQLAlchemyAdapter)
        mock_db.find_first_object_by.return_value = None

        um = UserManager(mock_db)

        with app.test_request_context('/api', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertFalse(um.validate_request())
            mock_db.find_first_object_by.assert_called_once_with(UserToken, token=token_value)
Пример #8
0
    def test_create_admin_already_exist(self):
        args = {'email': '*****@*****.**'}
        user = User(id=7)
        user_email = UserEmail(id=7, user=user, user_id=7)

        db_adapter = Mock()
        db_adapter.find_first_object.side_effect = [user_email, "test"]
        am = AdminManager(db_adapter)

        with app.test_request_context('/', headers=None):
            g.hackathon = Hackathon(id=7)
            self.assertEqual(am.create_admin(args), ok())
            self.assertEqual(db_adapter.find_first_object.call_count, 2)
Пример #9
0
    def test_check_role_common_admin_faild(self):
        admin_email_test = [AdminEmail(email='*****@*****.**')]
        admin_user_hackathon_rel = None

        mock_db = Mock()
        mock_db.find_all_objects_by.return_value = admin_email_test
        mock_db.find_all_objects.return_value = admin_user_hackathon_rel

        am = AdminManager(mock_db)
        with app.test_request_context('/'):
            g.admin = AdminUser(id=1, name='testadmin')
            self.assertFalse(am.check_role(1))
            mock_db.find_all_objects_by.assert_called_once_with(AdminEmail, admin_id=1)
Пример #10
0
    def test_validate_request_token_expired(self):
        token_value = "token_value"
        token = AdminToken(token=token_value, admin=None, expire_date=datetime.utcnow() - timedelta(seconds=30))

        mock_db = Mock()
        mock_db.find_first_object_by.return_value = token

        am = AdminManager(mock_db)

        with app.test_request_context('/', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertFalse(am.validate_request())
            mock_db.find_first_object_by.assert_called_once_with(AdminToken, token=token_value)
Пример #11
0
    def test_validate_request_token_expired(self):
        token_value = "token_value"
        token = UserToken(token=token_value, user=None, expire_date=get_now() - timedelta(seconds=30))

        mock_db = Mock(spec=SQLAlchemyAdapter)
        mock_db.find_first_object_by.return_value = token

        um = UserManager(mock_db)

        with app.test_request_context('/', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertFalse(um.validate_login())
            self.assertEqual(mock_db.find_first_object_by.call_count, 1)
            mock_db.find_first_object_by.assert_called_once_with(UserToken, token=token_value)
Пример #12
0
    def test_check_role_common_admin_faild(self):
        admin_email_test = [AdminEmail(email='*****@*****.**')]
        admin_user_hackathon_rel = None

        mock_db = Mock()
        mock_db.find_all_objects_by.return_value = admin_email_test
        mock_db.find_all_objects.return_value = admin_user_hackathon_rel

        am = AdminManager(mock_db)
        with app.test_request_context('/'):
            g.admin = AdminUser(id=1, name='testadmin')
            self.assertFalse(am.check_role(1))
            mock_db.find_all_objects_by.assert_called_once_with(AdminEmail,
                                                                admin_id=1)
Пример #13
0
    def test_validate_request_token_expired(self):
        token_value = "token_value"
        token = UserToken(token=token_value, user=None, expire_date=datetime.utcnow() - timedelta(seconds=30))

        mock_db = Mock(spec=SQLAlchemyAdapter)
        mock_db.find_first_object_by.return_value = token

        um = UserManager(mock_db)

        with app.test_request_context('/', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertFalse(um.validate_request())
            self.assertEqual(mock_db.find_first_object_by.call_count, 1)
            mock_db.find_first_object_by.assert_called_once_with(UserToken, token=token_value)
Пример #14
0
    def test_check_role_super_admin_success(self):
        admin_email_test = [AdminEmail(email='*****@*****.**')]
        admin_user_hackathon_rel = [AdminUserHackathonRel(hackathon_id=-1)]

        mock_db = Mock()
        mock_db.find_all_objects_by.return_value = admin_email_test
        mock_db.find_all_objects.return_value = admin_user_hackathon_rel

        am = AdminManager(mock_db)
        with app.test_request_context('/'):
            g.admin = AdminUser(id=1, name='testadmin')
            self.assertTrue(am.check_role(0))
            mock_db.find_all_objects_by.assert_called_once_with(AdminEmail, admin_id=1)
            mock_db.find_all_objects.assert_called_once_with(AdminUserHackathonRel, ANY)
Пример #15
0
    def test_create_admin_common_logic(self):
        args = {'email': '*****@*****.**', 'role_type': 1, 'remarks': 'test'}
        user = User(id=7)
        user_email = UserEmail(id=7, user=user, user_id=7)

        db_adapter = Mock()
        db_adapter.find_first_object.side_effect = [user_email, None]
        db_adapter.add_object.return_value = "success"
        am = AdminManager(db_adapter)

        with app.test_request_context('/', headers=None):
            g.hackathon = Hackathon(id=7)
            self.assertEqual(am.create_admin(args), ok())
            self.assertEqual(db_adapter.find_first_object.call_count, 2)
            db_adapter.add_object.assert_called_once_with(ANY)
Пример #16
0
    def test_validate_admin_hackathon_request_havnt_authority(self):
        token_value = "token_value"
        admin_email_test = [AdminEmail(email='*****@*****.**')]
        admin_user_hackathon_rel = [AdminUserHackathonRel(hackathon_id=1L)]

        mock_db = Mock()
        mock_db.find_all_objects_by.return_value = admin_email_test
        mock_db.find_all_objects.return_value = admin_user_hackathon_rel

        am = AdminManager(mock_db)
        with app.test_request_context('/', headers={"token": token_value}):
            g.admin = AdminUser(id=1, name='testadmin')
            self.assertFalse(am.validate_admin_hackathon_request(2))
            mock_db.find_all_objects_by.assert_called_once_with(AdminEmail, admin_id=1)
            mock_db.find_all_objects.assert_called_once_with(AdminUserHackathonRel, ANY)
Пример #17
0
    def test_validate_request_token_valid(self):
        token_value = "token_value"
        user = User(name="test_name")
        token = UserToken(token=token_value, user=user, expire_date=get_now() + timedelta(seconds=30))

        mock_db = Mock(spec=SQLAlchemyAdapter)
        mock_db.find_first_object_by.return_value = token

        um = UserManager(mock_db)

        with app.test_request_context('/', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertTrue(um.validate_login())
            mock_db.find_first_object_by.assert_called_once_with(UserToken, token=token_value)
            self.assertEqual(g.user, user)
Пример #18
0
    def test_check_role_super_admin_success(self):
        admin_email_test = [AdminEmail(email='*****@*****.**')]
        admin_user_hackathon_rel = [AdminUserHackathonRel(hackathon_id=-1)]

        mock_db = Mock()
        mock_db.find_all_objects_by.return_value = admin_email_test
        mock_db.find_all_objects.return_value = admin_user_hackathon_rel

        am = AdminManager(mock_db)
        with app.test_request_context('/'):
            g.admin = AdminUser(id=1, name='testadmin')
            self.assertTrue(am.check_role(0))
            mock_db.find_all_objects_by.assert_called_once_with(AdminEmail,
                                                                admin_id=1)
            mock_db.find_all_objects.assert_called_once_with(
                AdminUserHackathonRel, ANY)
Пример #19
0
    def test_validate_admin_hackathon_request_havnt_authority(self):
        token_value = "token_value"
        admin_email_test = [AdminEmail(email='*****@*****.**')]
        admin_user_hackathon_rel = [AdminUserHackathonRel(hackathon_id=1L)]

        mock_db = Mock()
        mock_db.find_all_objects_by.return_value = admin_email_test
        mock_db.find_all_objects.return_value = admin_user_hackathon_rel

        am = AdminManager(mock_db)
        with app.test_request_context('/', headers={"token": token_value}):
            g.admin = AdminUser(id=1, name='testadmin')
            self.assertFalse(am.validate_admin_hackathon_request(2))
            mock_db.find_all_objects_by.assert_called_once_with(AdminEmail,
                                                                admin_id=1)
            mock_db.find_all_objects.assert_called_once_with(
                AdminUserHackathonRel, ANY)
Пример #20
0
    def test_validate_request_token_expired(self):
        token_value = "token_value"
        token = AdminToken(token=token_value,
                           admin=None,
                           expire_date=datetime.utcnow() -
                           timedelta(seconds=30))

        mock_db = Mock()
        mock_db.find_first_object_by.return_value = token

        am = AdminManager(mock_db)

        with app.test_request_context('/', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertFalse(am.validate_request())
            mock_db.find_first_object_by.assert_called_once_with(
                AdminToken, token=token_value)
Пример #21
0
 def test_get_UserHackathonRel_list_result_not_empty(self):
     db_adapter = Mock()
     UserHackathonRel1 = UserHackathonRel(id=1,
                                          real_name='test1',
                                          email='*****@*****.**',
                                          hackathon_id=1)
     UserHackathonRel2 = UserHackathonRel(id=2,
                                          real_name='test2',
                                          email='*****@*****.**',
                                          hackathon_id=1)
     db_adapter.find_all_objects_by.return_value = [
         UserHackathonRel1, UserHackathonRel2
     ]
     rm = RegisterManager(db_adapter)
     with app.test_request_context('/'):
         self.assertEqual(len(rm.get_all_registration_by_hackathon_id(1)),
                          2)
         db_adapter.find_all_objects_by.assert_called_once_with(
             UserHackathonRel, hackathon_id=1)
Пример #22
0
    def test_validate_request_token_valid(self):
        token_value = "token_value"
        user = User(name="test_name")
        token = UserToken(token=token_value,
                          user=user,
                          expire_date=datetime.utcnow() +
                          timedelta(seconds=30))

        mock_db = Mock(spec=SQLAlchemyAdapter)
        mock_db.find_first_object_by.return_value = token

        um = UserManager(mock_db)

        with app.test_request_context('/', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertTrue(um.validate_request())
            mock_db.find_first_object_by.assert_called_once_with(
                UserToken, token=token_value)
            self.assertEqual(g.user, user)
Пример #23
0
 def test_check_admin_hackathon_authority_hearder_miss_hackathon_id(self, mock_method):
     with app.test_request_context('/'):
         self.assertFalse(admin_manager.check_admin_hackathon_authority())
         self.assertEqual(mock_method.call_count, 0)
Пример #24
0
 def test_check_admin_hackathon_authority_hearder_miss_hackathon_id(
         self, mock_method):
     with app.test_request_context('/'):
         self.assertFalse(admin_manager.check_admin_hackathon_authority())
         self.assertEqual(mock_method.call_count, 0)
Пример #25
0
 def test_validate_admin_hackathon_request_token_missing(self):
     am = AdminManager(None)
     with app.test_request_context('/', headers=None):
         g.admin = AdminUser(id=1, name='testadmin')
         self.assertTrue(am.validate_admin_hackathon_request(1))
         self.assertFalse('token' in request.headers)
Пример #26
0
 def test_check_admin_hackathon_authority_hearder_hackathon_id_is_not_num(
         self, mock_method):
     with app.test_request_context('/', headers={"hackathon_id": "test"}):
         self.assertFalse(admin_manager.check_admin_hackathon_authority())
         self.assertEqual(mock_method.call_count, 0)
Пример #27
0
 def test_get_UserHackathonRel_list_result_empty(self):
     db_adapter = Mock()
     db_adapter.find_all_objects_by.return_value = []
     rm = RegisterManager()
     with app.test_request_context('/'):
         self.assertEqual(rm.get_all_registration_by_hackathon_id(1), [])
Пример #28
0
 def test_check_admin_hackathon_authority_success(self, mock_thethod):
     with app.test_request_context('/', headers={"hackathon_id": 1}):
         self.assertTrue(admin_manager.check_admin_hackathon_authority())
         mock_thethod.assert_called_once_with(1)
Пример #29
0
 def test_check_admin_hackathon_authority_success(self, mock_thethod):
     with app.test_request_context('/', headers={"hackathon_id": 1}):
         self.assertTrue(admin_manager.check_admin_hackathon_authority())
         mock_thethod.assert_called_once_with(1)
Пример #30
0
 def test_validate_admin_hackathon_request_token_missing(self):
     am = AdminManager(None)
     with app.test_request_context('/', headers=None):
         g.admin = AdminUser(id=1, name='testadmin')
         self.assertTrue(am.validate_admin_hackathon_request(1))
         self.assertFalse('token' in request.headers)
Пример #31
0
 def test_check_admin_hackathon_authority_hearder_hackathon_id_is_not_num(self, mock_method):
     with app.test_request_context('/', headers={"hackathon_id": "test"}):
         self.assertFalse(admin_manager.check_admin_hackathon_authority())
         self.assertEqual(mock_method.call_count, 0)
Пример #32
0
 def test_get_UserHackathonRel_list_result_empty(self):
     db_adapter = Mock()
     db_adapter.find_all_objects_by.return_value = []
     rm = RegisterManager()
     with app.test_request_context('/'):
         self.assertEqual(rm.get_all_registration_by_hackathon_id(1), [])