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')
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')
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')
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')
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.")
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')
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')
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.")
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')
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')
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']")