예제 #1
0
    def test_get_request(self):
        class MockRequest:
            method = "GET"

        with patch('app.utils.security.request', new_callable=MockRequest):
            response = Security.validator(
                ['age|required:int'])(lambda *args, **kwargs: ('test', ))()
        self.assertEqual(response[0], 'test')
예제 #2
0
    def test_validator_validates_empty_request_json(self):
        class MockRequest:
            json = {}

        with patch('app.utils.security.request', new_callable=MockRequest):
            response = Security.validator([
                'mealList|required:list_int'
            ])(lambda *args, **kwargs: ('test', ))()

        self.assertEqual(response[0].get_json()['msg'],
                         'Bad Request - Request Must be JSON Formatted')
예제 #3
0
    def test_validator_validates_minimum_value_in_request_json(self):
        class MockRequest:
            json = {'age': 17}

            @classmethod
            def get_json(cls):
                return cls.json

        with patch('app.utils.security.request', new_callable=MockRequest):
            response = Security.validator(
                ['age|required:min-18'])(lambda *args, **kwargs: ('test', ))()

        self.assertEqual(response[0].get_json()['msg'],
                         'Bad Request - age can only have a min value of 18')
예제 #4
0
    def test_validator_validates_float_request_json(self):
        class MockRequest:
            json = {'age': 'test'}

            @classmethod
            def get_json(cls):
                return cls.json

        with patch('app.utils.security.request', new_callable=MockRequest):
            response = Security.validator(
                ['age|required:float'])(lambda *args, **kwargs: ('test', ))()

        self.assertEqual(response[0].get_json()['msg'],
                         'Bad Request - age must be float')
예제 #5
0
    def test_validator_validates_urls(self):
        class MockRequest:
            json = {'imageUrl': 'invalid@url'}

            @classmethod
            def get_json(cls):
                return cls.json

        with patch('app.utils.security.request', new_callable=MockRequest):
            response = Security.validator([
                'imageUrl|required:url'
            ])(lambda *args, **kwargs: ('test', ))()

        self.assertEqual(response[0].get_json()['msg'],
                         "Bad Request - 'invalid@url' is not a valid url.")
예제 #6
0
    def test_validator_validates_list_int_in_request_json(self):
        class MockRequest:
            json = {'mealList': 'invalid list'}

            @classmethod
            def get_json(cls):
                return cls.json

        with patch('app.utils.security.request', new_callable=MockRequest):
            response = Security.validator([
                'mealList|required:list_int'
            ])(lambda *args, **kwargs: ('test', ))()

        self.assertEqual(response[0].get_json()['msg'],
                         'Bad Request - mealList must be a list')
예제 #7
0
    def test_validator_validates_length_value_in_request_json(self):
        class MockRequest:
            json = {'username': '******'}

            @classmethod
            def get_json(cls):
                return cls.json

        with patch('app.utils.security.request', new_callable=MockRequest):
            response = Security.validator([
                'username|required:length-10'
            ])(lambda *args, **kwargs: ('test', ))()

        self.assertEqual(response[0].get_json()['msg'],
                         'Bad Request - username can only have a len of 10')
예제 #8
0
    def test_validator_validates_email(self):
        class MockRequest:
            json = {'email': 'invalid@email'}
            method = "POST"

            @classmethod
            def get_json(cls):
                return cls.json

        with patch('app.utils.security.request', new_callable=MockRequest):
            response = Security.validator(
                ['email|required:email'])(lambda *args, **kwargs: ('test', ))()

        self.assertEqual(
            response[0].get_json()['msg'],
            "Bad Request - 'invalid@email' is not a valid email address.")
예제 #9
0
    def test_validator_validates_date_in_request_json(self):
        class MockRequest:
            json = {'startDate': 'invalid date'}

            @classmethod
            def get_json(cls):
                return cls.json

        with patch('app.utils.security.request', new_callable=MockRequest):
            response = Security.validator([
                'startDate|required:date'
            ])(lambda *args, **kwargs: ('test', ))()

        self.assertEqual(
            response[0].get_json()['msg'],
            'Bad Request - startDate should be valid date. Format: YYYY-MM-DD')
예제 #10
0
    def test_validator_validates_list_int_contains_only_ints_in_request_json(
            self):
        class MockRequest:
            json = {'mealList': [1, 2, 'not an int']}
            method = "POST"

            @classmethod
            def get_json(cls):
                return cls.json

        with patch('app.utils.security.request', new_callable=MockRequest):
            response = Security.validator([
                'mealList|required:list_int'
            ])(lambda *args, **kwargs: ('test', ))()

        self.assertEqual(response[0].get_json()['msg'],
                         'Bad Request - [not an int] in list must be integer')
예제 #11
0
    def test_validator_validates_enums(self):
        class MockRequest:
            json = {'category': 'user_faqs'}

            @classmethod
            def get_json(cls):
                return cls.json

        with patch('app.utils.security.request', new_callable=MockRequest):
            response = Security.validator([
                'category|required:enum_FaqCategoryType'
            ])(lambda *args, **kwargs: ('test', ))()

        self.assertEqual(
            response[0].get_json()['msg'],
            "Bad Request - 'user_faqs' is not a valid value for key 'category'. "
            "values must be any of the following ['user_faq', 'admin_faq']")