def test_dehydrate(self): api = Api() msg_uri = api.resources['message'].get_resource_list_uri() client = Client() future_data = { 'description' : 'soy una descripcion', 'message' : 'yo no deberia estar aca', 'time_to_reveal' : 6000, 'admin_code' : "ASDSAAFA", } past_data = { 'description' : 'soy una descripcion', 'message' : 'yo si', 'time_to_reveal' : -6000, 'admin_code' : "ASDSAAFA", } response = client.post(msg_uri, future_data, parse='json') self.assertEqual(201, response.status_code, response.content) future_uri = response._headers['location'][1] response = client.post(msg_uri, past_data, parse='json') self.assertEqual(201, response.status_code, response.content) past_uri = response._headers['location'][1] response = client.get(past_uri, parse='json') self.assertTrue( 'code' in response.data.keys() and len(response.data['code']) ) self.assertTrue( 'admin_code' not in response.data.keys()) self.assertEqual(200, response.status_code, response.content) self.assertEqual(past_data['message'], response.data['message']) response = client.get(future_uri, parse='json') self.assertEqual(200, response.status_code) self.assertTrue( 'code' in response.data.keys() and len(response.data['code']) ) self.assertEqual("",response.data['message'])
def test_dehydrate(self): api = Api() msg_uri = api.resources['message'].get_resource_list_uri() client = Client() future_data = { 'description': 'soy una descripcion', 'message': 'yo no deberia estar aca', 'time_to_reveal': 6000, 'admin_code': "ASDSAAFA", } past_data = { 'description': 'soy una descripcion', 'message': 'yo si', 'time_to_reveal': -6000, 'admin_code': "ASDSAAFA", } response = client.post(msg_uri, future_data, parse='json') self.assertEqual(201, response.status_code, response.content) future_uri = response._headers['location'][1] response = client.post(msg_uri, past_data, parse='json') self.assertEqual(201, response.status_code, response.content) past_uri = response._headers['location'][1] response = client.get(past_uri, parse='json') self.assertTrue('code' in response.data.keys() and len(response.data['code'])) self.assertTrue('admin_code' not in response.data.keys()) self.assertEqual(200, response.status_code, response.content) self.assertEqual(past_data['message'], response.data['message']) response = client.get(future_uri, parse='json') self.assertEqual(200, response.status_code) self.assertTrue('code' in response.data.keys() and len(response.data['code'])) self.assertEqual("", response.data['message'])
def multi_readonly_post(self, resource_name, resource, field_name, field): client = Client() if field.readonly and resource.can_create() : post_data = resource.get_example_data('POST') bad_value = UnderResourceFields.generate_field_test_data(field) post_data[field_name] = bad_value post_response = client.post(resource.get_resource_list_uri(), post_data, parse='json') if post_response.status_code == 201: (prop, location) = post_response._headers['location'] get_response = client.get(location, parse='json') msg = "%s.%s can be setted by a POST request even though it's readonly!." msg %= (resource_name, field_name) self.assertNotEqual(get_response.get(field_name,''), bad_value, msg)
def multi_example_get_data(self, resource_name, resource): #only if resource allows detail GET if 'GET' not in resource._meta.detail_allowed_methods: return client = Client() if self.api.resource_allows_method(resource_name, 'GET'): resource.create_test_resource() get_response = client.get(resource.get_resource_list_uri()) response_dict = json.loads(get_response.content) if len(response_dict['objects']) > 0: object_keys = set(response_dict['objects'][0].keys()) expected_keys = set(resource._meta.examples['GET'].keys()) msg = "GET example data does not match the example for resource %s - %s vs %s" msg %= (resource_name, expected_keys, object_keys) self.assertEqual(expected_keys, object_keys, msg)
def multi_example_get_data(self, resource_name, resource): #only if resource allows detail GET if 'GET' not in resource._meta.detail_allowed_methods: return client = Client() if self.api.resource_allows_method(resource_name, 'GET'): resource.create_test_resource() get_response = client.get(resource.get_resource_list_uri()) response_dict = json.loads(get_response.content) if len(response_dict['objects']) > 0: object_keys = set(response_dict['objects'][0].keys()) expected_keys = set(resource._meta.examples['GET'].keys()) msg = "GET example data does not match the example for resource %s - %s vs %s" msg %= (resource_name, expected_keys, object_keys) self.assertEqual(expected_keys, object_keys, msg)
def multi_readonly_post(self, resource_name, resource, field_name, field): client = Client() if field.readonly and resource.can_create(): post_data = resource.get_example_data('POST') bad_value = UnderResourceFields.generate_field_test_data(field) post_data[field_name] = bad_value post_response = client.post(resource.get_resource_list_uri(), post_data, parse='json') if post_response.status_code == 201: (prop, location) = post_response._headers['location'] get_response = client.get(location, parse='json') msg = "%s.%s can be setted by a POST request even though it's readonly!." msg %= (resource_name, field_name) self.assertNotEqual(get_response.get(field_name, ''), bad_value, msg)
def multi_readonly_patch(self, resource_name, resource, field_name, field): client = Client() api = Api() if field.readonly and resource.can_create() : #Create a resource to modify it (location, obj) = resource.create_test_resource() bad_value = UnderResourceFields.generate_field_test_data(field) #authenticate to be allowed to modify the resource post_data = api.resources['userprofile'].get_example_data('POST') client.login(username=post_data['email'], password=post_data['password']) #attempt to PATCH patch_data = {} patch_data[field_name] = bad_value patch_response = client.patch(location,patch_data, parse='json') get_response = client.get(location, parse='json') msg = "%s.%s can be changed by a PATCH request even though it's readonly!." msg %= (resource_name, field_name) self.assertNotEqual(get_response.data.get(field_name,None), bad_value,msg)
def multi_readonly_patch(self, resource_name, resource, field_name, field): client = Client() api = Api() if field.readonly and resource.can_create(): #Create a resource to modify it (location, obj) = resource.create_test_resource() bad_value = UnderResourceFields.generate_field_test_data(field) #authenticate to be allowed to modify the resource post_data = api.resources['userprofile'].get_example_data('POST') client.login(username=post_data['email'], password=post_data['password']) #attempt to PATCH patch_data = {} patch_data[field_name] = bad_value patch_response = client.patch(location, patch_data, parse='json') get_response = client.get(location, parse='json') msg = "%s.%s can be changed by a PATCH request even though it's readonly!." msg %= (resource_name, field_name) self.assertNotEqual(get_response.data.get(field_name, None), bad_value, msg)