How to use test_user_authentication method in tempest

Best Python code snippet using tempest_python

test_resources_base_metadata.py

Source:test_resources_base_metadata.py Github

copy

Full Screen

1from datetime import datetime, timezone2from django.test import TestCase3from django.contrib.auth.models import Permission4from api.constants import FILTERS5from api.tests.mixins import ResourceTestCaseMixin6from metadata.models import Tag7from dataset.tests.utils import create_test_dataset8class TestBaseMetadataResource(ResourceTestCaseMixin, TestCase):9 '''Test the BaseMetadataResource'''10 11 resource_name = 'base_metadata_test_resource'12 13 def setUp(self):14 super().setUp()15 # Create test data16 self.test_dataset = create_test_dataset()17 # Add the test user to the dataset user_group so that he has permission to do POST/PATCH/DELETE18 self.test_user.groups.add(self.test_dataset.user_group)19 self.test_tag1 = Tag.objects.create(name = 'test tag1')20 self.test_tag2 = Tag.objects.create(name = 'test tag2')21 self.test_tag3 = Tag.objects.create(name = 'test tag3')22 self.test_data_location1 = self.test_dataset.data_locations.create(file_url = 'https://test.com/file1.fits', file_size = 1, file_path = 'test_path/file1.fits', offline = False)23 self.test_data_location2 = self.test_dataset.data_locations.create(file_url = 'https://test.com/file2.fits', file_size = 2, file_path = 'test_path/file2.fits', offline = True)24 self.test_metadata1 = self.test_dataset.metadata_model.objects.create(oid = 'test_metadata1', data_location = self.test_data_location1)25 self.test_metadata2 = self.test_dataset.metadata_model.objects.create(oid = 'test_metadata2')26 self.test_metadata1.tags.add(self.test_tag1)27 self.test_metadata2.tags.add(self.test_tag1, self.test_tag2)28 29 self.test_instance = self.test_metadata130 self.test_post_data = {31 'oid': 'test_metadata3',32 'fits_header': 'fits header',33 'date_beg' : datetime(2000, 1, 1, tzinfo = timezone.utc),34 'date_end': datetime(2000, 1, 1, 0, 0, 1, tzinfo = timezone.utc),35 'wavemin': 0,36 'wavemax': 1,37 'data_location': self.test_data_location138 }39 self.test_patch_data = {40 'fits_header': 'new fits header',41 'date_beg' : datetime(2000, 1, 1, tzinfo = timezone.utc),42 'date_end': datetime(2000, 1, 1, 0, 0, 1, tzinfo = timezone.utc),43 'wavemin': 0,44 'wavemax': 1,45 'data_location': self.test_data_location246 }47 48 def test_init(self):49 '''Test the __init__ method of the resource'''50 51 msg = 'When the resource is instanciated, the ordering must contain all regular field names'52 self.assertCountEqual(self.resource._meta.ordering, ['oid', 'fits_header', 'date_beg', 'date_end', 'wavemin', 'wavemax'], msg=msg)53 54 msg = 'When the resource is instanciated, the filtering must contain all field names and appropriate filters'55 self.assertDictEqual(self.resource._meta.filtering, {56 'tags': FILTERS.RELATIONAL,57 'search': FILTERS.COMPLEX_SEARCH_EXPRESSION,58 'oid' : FILTERS.TEXT,59 'fits_header': FILTERS.TEXT,60 'date_beg': FILTERS.DATETIME,61 'date_end': FILTERS.DATETIME,62 'wavemin': FILTERS.NUMERIC,63 'wavemax': FILTERS.NUMERIC64 }, msg=msg)65 66 def test_get_list(self):67 '''Test a GET on the list URL'''68 69 msg = 'When no authentication is provided, a GET on the list URL must return a valid JSON response with the complete list of metadata'70 response = self.api_client.get(self.get_resource_uri(), format='json')71 self.assertValidJSONResponse(response, msg=msg)72 self.assertGetListResponseContains(response, oid = [self.test_metadata1.oid, self.test_metadata2.oid], msg=msg)73 74 msg = 'When authentication is provided, a GET on the list URL must return a valid JSON response with the complete list of metadata'75 response = self.api_client.get(self.get_resource_uri(), format='json', authentication=self.test_user_authentication)76 self.assertValidJSONResponse(response, msg=msg)77 self.assertGetListResponseContains(response, oid = [self.test_metadata1.oid, self.test_metadata2.oid], msg=msg)78 79 def test_get_list_filtered(self):80 '''Test the filter for a GET on the list URL'''81 82 msg = 'When a simple filter for an existing keyword is requested, the list of metadata that conform to the simple filter must be returned'83 data = {'oid': self.test_metadata1.oid}84 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)85 self.assertGetListResponseContains(response, oid = [self.test_metadata1.oid], msg=msg)86 87 msg = 'When a simple filter for an non-existing keyword is requested, the entire list of metadata must be returned'88 data = {'characteristic__in': 'irrelevant'}89 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)90 self.assertGetListResponseContains(response, oid = [self.test_metadata1.oid, self.test_metadata2.oid], msg=msg)91 92 msg = 'When a mix of simple filters for existing and non-existing keywords are requested, the list of metadata that conform to the existing keyword filter must be returned'93 data = {'oid': self.test_metadata1.oid, 'characteristic__in': 'irrelevant'}94 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)95 self.assertGetListResponseContains(response, oid = [self.test_metadata1.oid], msg=msg)96 97 def test_get_list_filtered_tag(self):98 '''Test the tag filter for a GET on the list URL'''99 100 msg = 'When a exact filter for an existing tag is requested, the list of metadata with that tag must be returned'101 data = {'tags': self.test_tag2.name}102 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)103 self.assertGetListResponseContains(response, oid = [self.test_metadata2.oid], msg=msg)104 105 msg = 'When a exact filter for an non-existing tag is requested, no metadata must be returned'106 data = {'tags': 'does not exist'}107 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)108 self.assertGetListResponseContains(response, oid = [], msg=msg)109 110 msg = 'When a exact filter for an existing tag and a simple filter is requested, the list of metadata with that tag that conform to the simple filter must be returned'111 data = {'tags': self.test_tag1.name, 'oid': self.test_metadata1.oid}112 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)113 self.assertGetListResponseContains(response, oid = [self.test_metadata1.oid], msg=msg)114 115 # BUG: The following test demonstrates that we don't correctly handle the __in filter, as we should be using distinct116 msg = 'When a __in filter is requested, the list of metadata with these tags must be returned'117 data = {'tags__in': ','.join([self.test_tag1.name, self.test_tag2.name])}118 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)119 self.assertGetListResponseContains(response, oid = [self.test_metadata1.oid, self.test_metadata2.oid], msg=msg)120 121 def test_get_list_filtered_search(self):122 '''Test the search filter for a GET on the list URL'''123 124 msg = 'When a simple search lookup is requested, the usual list must be returned'125 data = {'search': 'oid=test_metadata1'}126 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)127 self.assertGetListResponseContains(response, oid = [self.test_metadata1.oid], msg=msg)128 129 msg = 'When a complex search expression is requested, the list respecting that expression must be returned'130 data = {'search': 'oid=test_metadata1 or oid=test_metadata2'}131 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)132 self.assertGetListResponseContains(response, oid = [self.test_metadata1.oid, self.test_metadata2.oid], msg=msg)133 134 msg = 'When two complex search expressions are requested, the list respecting both expressions must be returned'135 data = {'search': ['oid=test_metadata1 or oid=test_metadata2', 'not oid=test_metadata2']}136 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)137 self.assertGetListResponseContains(response, oid = [self.test_metadata1.oid], msg=msg)138 139 msg = 'When a complex search expression is requested and a simple filter is requested, the list respecting both must be returned'140 data = {'search': 'oid=test_metadata1 or oid=test_metadata2', 'oid': 'test_metadata1'}141 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)142 self.assertGetListResponseContains(response, oid = [self.test_metadata1.oid], msg=msg)143 144 msg = 'When a complex search expression is requested for a non-existing keyword, a bad request response must be returned'145 data = {'search': 'characteristic__in=irrelevant'}146 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)147 self.assertHttpBadRequest(response, msg=msg)148 149 150 def test_post_list(self):151 '''Test a POST on the list URL'''152 153 msg = 'When no authentication is provided, a POST on the list URL must return an unauthorized response'154 data = self.get_test_post_data()155 response = self.api_client.post(self.get_resource_uri(), data=data, format='json')156 self.assertHttpUnauthorized(response, msg=msg)157 158 msg = 'When authentication is provided but user has not permission, a POST on the list URL must return an unauthorized response'159 data = self.get_test_post_data()160 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)161 self.assertHttpUnauthorized(response, msg=msg)162 163 msg = 'When authentication is provided and user has permission, a POST on the list URL must return a created response and the metadata must exist in the database'164 self.test_user.user_permissions.add(Permission.objects.get(codename='add_basemetadatatest'))165 data = self.get_test_post_data()166 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)167 self.assertHttpCreated(response, msg=msg)168 self.assertObjectCreated(self.test_dataset.metadata_model.objects.all(), oid=data['oid'], msg=msg)169 170 msg = 'When authentication is provided and user has permission, a POST on the list URL with data_location must return a created response and the metadata and data location must exist in the database'171 self.test_user.user_permissions.add(Permission.objects.get(codename='add_datalocation'))172 data_location = {173 'dataset': self.test_dataset,174 'file_url': 'https://test.com/new_file.fits',175 'file_size': 2048,176 'file_path': 'test_path/new_file.fits',177 'offline': True178 }179 data = self.get_test_post_data(oid='test_metadata4', data_location=data_location)180 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)181 self.assertHttpCreated(response, msg=msg)182 self.assertObjectCreated(self.test_dataset.metadata_model.objects.all(), oid=data['oid'], data_location=self.test_dataset.data_locations.get(file_url = data_location['file_url']), msg=msg)183 184 msg = 'When authentication is provided and user has permission, a POST on the list URL with tags must return a created response and the metadata and tags must exist in the database'185 self.test_user.user_permissions.add(Permission.objects.get(codename='change_tag'))186 tags = [187 self.test_tag3,188 {'name': 'new tag'},189 ]190 data = self.get_test_post_data(oid='test_metadata5', tags=tags)191 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)192 self.assertHttpCreated(response, msg=msg)193 self.assertObjectCreated(self.test_dataset.metadata_model.objects.all(), oid=data['oid'], msg=msg)194 self.assertQuerysetEqual(self.test_dataset.metadata_model.objects.filter(oid=data['oid']).values_list('tags__name', flat=True), [self.test_tag3.name, 'new tag'], ordered=False, msg=msg)195 196 def test_get_detail(self):197 '''Test a GET on the detail URL'''198 199 msg = 'When no authentication is provided, a GET on the detail URL must return a valid JSON response'200 response = self.api_client.get(self.get_resource_uri(self.test_metadata1), format='json')201 self.assertValidJSONResponse(response, msg=msg)202 self.assertResponseHasKeys(response, ['oid', 'fits_header', 'data_location', 'tags', 'date_beg', 'date_end', 'wavemin', 'wavemax'], msg=msg)203 204 msg = 'When authentication is provided, a GET on the detail URL must return a valid JSON response'205 response = self.api_client.get(self.get_resource_uri(self.test_metadata1), format='json', authentication=self.test_user_authentication)206 self.assertValidJSONResponse(response, msg=msg)207 self.assertResponseHasKeys(response, ['oid', 'fits_header', 'data_location', 'tags', 'date_beg', 'date_end', 'wavemin', 'wavemax'], msg=msg)208 209 def test_patch_detail(self):210 '''Test a PATCH on the detail URL'''211 212 msg = 'When no authentication is provided, a PATCH must return an unauthorized response'213 data = self.get_test_patch_data()214 response = self.api_client.patch(self.get_resource_uri(self.test_metadata1), data=data, format='json')215 self.assertHttpUnauthorized(response, msg=msg)216 217 msg = 'When authentication is provided but user has not permission, a PATCH must return an unauthorized response'218 data = self.get_test_patch_data()219 response = self.api_client.patch(self.get_resource_uri(self.test_metadata1), data=data, format='json', authentication=self.test_user_authentication)220 self.assertHttpUnauthorized(response, msg=msg)221 222 msg = 'When authentication is provided and user has permission, a PATCH must return an OK response and the metadata must have been updated'223 # WARNING if the metadata that is being modified has tags, it is necessary to have the change_tag permission even if the tags are not modified224 self.test_user.user_permissions.add(Permission.objects.get(codename='change_basemetadatatest'), Permission.objects.get(codename='change_tag'))225 data = self.get_test_patch_data()226 response = self.api_client.patch(self.get_resource_uri(self.test_metadata1), data=data, format='json', authentication=self.test_user_authentication)227 self.test_metadata1.refresh_from_db()228 self.assertHttpAccepted(response, msg=msg)229 self.assertAttributesEqual(self.test_metadata1, data, msg=msg)230 231 def test_delete_detail(self):232 '''Test a DELETE on the detail URL'''233 234 msg = 'When no authentication is provided, a DELETE must return an unauthorized response'235 response = self.api_client.delete(self.get_resource_uri(self.test_metadata1), format='json')236 self.assertHttpUnauthorized(response, msg=msg)237 238 msg = 'When authentication is provided but user has not permission, a DELETE must return an unauthorized response'239 response = self.api_client.delete(self.get_resource_uri(self.test_metadata1), format='json', authentication=self.test_user_authentication)240 self.assertHttpUnauthorized(response, msg=msg)241 242 msg = 'When authentication is provided and user has permission, a DELETE must return an empty response and the metadata must have been deleted'243 self.test_user.user_permissions.add(Permission.objects.get(codename='delete_basemetadatatest'))244 response = self.api_client.delete(self.get_resource_uri(self.test_metadata1), format='json', authentication=self.test_user_authentication)245 self.assertHttpAccepted(response, msg=msg)246 self.assertObjectDeleted(self.test_metadata1, msg=msg)247 248 def test_validation_errors(self):249 '''Test that creating/updating metadata with invalid data return appropriate error messages'''250 251 # Give user permission to create/update252 self.test_user.user_permissions.add(Permission.objects.get(codename='add_basemetadatatest'), Permission.objects.get(codename='change_basemetadatatest'), Permission.objects.get(codename='change_tag'))253 254 msg = 'When a metadata with the same oid already exists, a POST must return a bad request with a proper error message'255 data = self.get_test_post_data(oid = self.test_metadata1.oid)256 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)257 self.assertHttpBadRequest(response, msg=msg)258 self.assertIn('oid', response.content.decode(response.charset), msg=msg)259 260 msg = 'When oid is specified, a PATCH must return a bad request with a proper error message'261 data = self.get_test_patch_data(oid = 'test_metadata1_updated')262 response = self.api_client.patch(self.get_resource_uri(self.test_metadata1), data=data, format='json', authentication=self.test_user_authentication)263 self.assertHttpBadRequest(response, msg=msg)...

Full Screen

Full Screen

test_resources_data_location.py

Source:test_resources_data_location.py Github

copy

Full Screen

1from django.test import TestCase2from django.contrib.auth.models import Permission3from dataset.tests.utils import create_test_dataset4from api.tests.mixins import ResourceTestCaseMixin5class TestDataLocationResource(ResourceTestCaseMixin, TestCase):6 '''Test the DataLocationResource'''7 8 resource_name = 'data_location'9 10 def setUp(self):11 super().setUp()12 # Create test data13 self.test_dataset = create_test_dataset()14 self.test_data_location = self.test_dataset.data_locations.create(file_url = 'https://test.com/file.fits', file_size = 1024, file_path = 'test_path/file.fits', offline=True)15 16 self.test_instance = self.test_data_location17 self.test_post_data = {18 'dataset': self.test_dataset,19 'file_url': 'https://test.com/new_file.fits',20 'file_size': 2048,21 'file_path': 'test_path/new_file.fits',22 'offline': True23 }24 self.test_patch_data = {25 'file_url': 'https://test.com/other_file.fits',26 'file_size': 4096,27 'file_path': 'test_path/other_file.fits',28 'offline': False29 }30 31 def test_get_list(self):32 '''Test a GET on the list URL'''33 34 msg = 'When no authentication is provided, a GET on the list URL must return a valid JSON response with the complete list of data locations'35 response = self.api_client.get(self.get_resource_uri(), format='json')36 self.assertValidJSONResponse(response, msg=msg)37 self.assertGetListResponseContains(response, file_url = [self.test_data_location.file_url], msg=msg)38 39 msg = 'When authentication is provided, a GET on the list URL must return a valid JSON response with the complete list of data locations'40 response = self.api_client.get(self.get_resource_uri(), format='json', authentication=self.test_user_authentication)41 self.assertValidJSONResponse(response, msg=msg)42 self.assertGetListResponseContains(response, file_url = [self.test_data_location.file_url], msg=msg)43 44 def test_post_list(self):45 '''Test a POST on the list URL'''46 47 msg = 'When no authentication is provided, a POST must return an unauthorized response'48 data = self.get_test_post_data()49 response = self.api_client.post(self.get_resource_uri(), data=data, format='json')50 self.assertHttpUnauthorized(response, msg=msg)51 52 msg = 'When authentication is provided but user has not permission, a POST must return an unauthorized response'53 data = self.get_test_post_data()54 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)55 self.assertHttpUnauthorized(response, msg=msg)56 57 msg = 'When authentication is provided and user has permission, but the user does not belong to the dataset user_group, a POST must return an unauthorized response'58 self.test_user.user_permissions.add(Permission.objects.get(codename='add_datalocation'))59 data = self.get_test_post_data()60 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)61 self.assertHttpUnauthorized(response, msg=msg)62 63 msg = 'When authentication is provided and user has permission, and the user belongs to the dataset user_group, a POST must return an OK response and the data location must have been created'64 self.test_user.groups.add(self.test_dataset.user_group)65 data = self.get_test_post_data()66 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)67 self.assertHttpCreated(response, msg=msg)68 self.assertObjectCreated(self.test_dataset.data_locations, file_url = data['file_url'], file_size = data['file_size'], msg=msg)69 70 def test_get_detail(self):71 '''Test a GET on the detail URL'''72 73 msg = 'When no authentication is provided, a GET on the detail URL must return a valid JSON response'74 response = self.api_client.get(self.get_resource_uri(self.test_data_location), format='json')75 self.assertValidJSONResponse(response, msg=msg)76 self.assertResponseHasKeys(response, ['dataset', 'file_url', 'file_size', 'file_path', 'thumbnail_url', 'update_time', 'offline'], msg=msg)77 78 msg = 'When authentication is provided, a GET on the detail URL must return a valid JSON response'79 response = self.api_client.get(self.get_resource_uri(self.test_data_location), format='json', authentication=self.test_user_authentication)80 self.assertValidJSONResponse(response, msg=msg)81 self.assertResponseHasKeys(response, ['dataset', 'file_url', 'file_size', 'file_path', 'thumbnail_url', 'update_time', 'offline'], msg=msg)82 83 def test_patch_detail(self):84 '''Test a PATCH on the detail URL'''85 86 msg = 'When no authentication is provided, a PATCH must return an unauthorized response'87 data = self.get_test_patch_data()88 response = self.api_client.patch(self.get_resource_uri(self.test_data_location), data=data, format='json')89 self.assertHttpUnauthorized(response, msg=msg)90 91 msg = 'When authentication is provided but user has not permission, a PATCH must return an unauthorized response'92 data = self.get_test_patch_data()93 response = self.api_client.patch(self.get_resource_uri(self.test_data_location), data=data, format='json', authentication=self.test_user_authentication)94 self.assertHttpUnauthorized(response, msg=msg)95 96 msg = 'When authentication is provided and user has permission, but the user does not belong to the dataset user_group, a PATCH must return an unauthorized response'97 self.test_user.user_permissions.add(Permission.objects.get(codename='change_datalocation'))98 data = self.get_test_patch_data()99 response = self.api_client.patch(self.get_resource_uri(self.test_data_location), data=data, format='json', authentication=self.test_user_authentication)100 self.assertHttpUnauthorized(response, msg=msg)101 102 msg = 'When authentication is provided and user has permission, and the user belong to the dataset user_group, a PATCH must return an OK response and the data location must have been updated'103 self.test_user.groups.add(self.test_dataset.user_group)104 data = self.get_test_patch_data()105 response = self.api_client.patch(self.get_resource_uri(self.test_data_location), data=data, format='json', authentication=self.test_user_authentication)106 self.test_data_location.refresh_from_db()107 self.assertHttpAccepted(response, msg=msg)108 self.assertAttributesEqual(self.test_data_location, data, msg=msg)109 110 def test_delete_detail(self):111 '''Test a DELETE on the detail URL'''112 113 msg = 'When no authentication is provided, a DELETE must return an unauthorized response'114 response = self.api_client.delete(self.get_resource_uri(self.test_data_location), format='json')115 self.assertHttpUnauthorized(response, msg=msg)116 117 msg = 'When authentication is provided but user has not permission, a DELETE must return an unauthorized response'118 response = self.api_client.delete(self.get_resource_uri(self.test_data_location), format='json', authentication=self.test_user_authentication)119 self.assertHttpUnauthorized(response, msg=msg)120 121 msg = 'When authentication is provided and user has permission, but the user does not belong to the dataset user_group, a DELETE must return an unauthorized response'122 self.test_user.user_permissions.add(Permission.objects.get(codename='delete_datalocation'))123 response = self.api_client.delete(self.get_resource_uri(self.test_data_location), format='json', authentication=self.test_user_authentication)124 self.assertHttpUnauthorized(response, msg=msg)125 126 msg = 'When authentication is provided and user has permission, and the user belong to the dataset user_group, a DELETE must return an empty response and the data location must have been deleted'127 self.test_user.groups.add(self.test_dataset.user_group)128 response = self.api_client.delete(self.get_resource_uri(self.test_data_location), format='json', authentication=self.test_user_authentication)129 self.assertHttpAccepted(response, msg=msg)130 self.assertObjectDeleted(self.test_data_location, msg=msg)131 132 def test_validation_errors(self):133 '''Test that creating data location with invalid data return appropriate error messages'''134 135 # Give user permission to create/update136 self.test_user.groups.add(self.test_dataset.user_group)137 self.test_user.user_permissions.add(Permission.objects.get(codename='add_datalocation'))138 self.test_user.user_permissions.add(Permission.objects.get(codename='change_datalocation'))139 140 msg = 'When file_url is not a proper URL, a POST must return a bad request with a proper error message'141 data = self.get_test_post_data(file_url = 'not a proper URL')142 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)143 self.assertHttpBadRequest(response, msg=msg)144 self.assertIn('file_url', response.content.decode(response.charset), msg=msg)145 146 msg = 'When file_size is not a positive integer, a POST must return a bad request with a proper error message'147 data = self.get_test_post_data(file_size = -1)148 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)149 self.assertHttpBadRequest(response, msg=msg)150 self.assertIn('file_size', response.content.decode(response.charset), msg=msg)151 152 msg = 'When file_path is not an accepted path, a POST must return a bad request with a proper error message'153 for file_path in [r'/unix/absolute', r'c:\\windows\absolute']:154 with self.subTest(file_path = file_path):155 data = self.get_test_post_data(file_path = file_path)156 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)157 self.assertHttpBadRequest(response, msg=msg)158 self.assertIn('file_path', response.content.decode(response.charset), msg=msg)159 160 msg = 'When thumbnail_url is not a proper URL, a POST must return a bad request with a proper error message'161 data = self.get_test_post_data(thumbnail_url = 'not a proper URL')162 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)163 self.assertHttpBadRequest(response, msg=msg)164 self.assertIn('thumbnail_url', response.content.decode(response.charset), msg=msg)165 166 msg = 'When offline is not a accepted boolean value, a POST must return a bad request with a proper error message'167 data = self.get_test_post_data(offline = 'true')168 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)169 self.assertHttpBadRequest(response, msg=msg)170 self.assertIn('offline', response.content.decode(response.charset), msg=msg)171 172 msg = 'When a data location with the same dataset and file_url already exists, a POST must return a bad request with a proper error message'173 data = self.get_test_post_data(file_url = self.test_data_location.file_url)174 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)175 self.assertHttpBadRequest(response, msg=msg)176 self.assertIn('__all__', response.content.decode(response.charset), msg=msg)177 178 msg = 'When a data location with the same dataset and file_path already exists, a POST must return a bad request with a proper error message'179 data = self.get_test_post_data(file_path = self.test_data_location.file_path)180 response = self.api_client.post(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)181 self.assertHttpBadRequest(response, msg=msg)...

Full Screen

Full Screen

test_resources_dataset.py

Source:test_resources_dataset.py Github

copy

Full Screen

1from urllib.parse import urlencode2from django.test import TestCase3from dataset.models import Characteristic4from dataset.tests.utils import create_test_dataset5from api.tests.mixins import ReadOnlyResourceTestCaseMixin6class TestDatasetResource(ReadOnlyResourceTestCaseMixin, TestCase):7 '''Test the DatasetResource'''8 9 resource_name = 'dataset'10 11 def setUp(self):12 super().setUp()13 14 # Create test data15 self.test_characteristic1 = Characteristic.objects.create(name = 'test characteristic1')16 self.test_characteristic2 = Characteristic.objects.create(name = 'test characteristic2')17 self.test_characteristic3 = Characteristic.objects.create(name = 'test characteristic3')18 self.test_dataset1 = create_test_dataset(name = 'test dataset 1', characteristic_names = [self.test_characteristic1.name])19 self.test_metadata = self.test_dataset1.metadata_model.objects.create(oid='test_metadata')20 self.test_dataset2 = create_test_dataset(name = 'test dataset 2', metadata_model = None, characteristic_names = [self.test_characteristic1.name, self.test_characteristic2.name])21 22 self.test_instance = self.test_dataset123 24 def test_get_list(self):25 '''Test a GET on the list URL'''26 27 msg = 'When no authentication is provided, a GET on the list URL must return a valid JSON response with the complete list of datasets'28 response = self.api_client.get(self.get_resource_uri(), format='json')29 self.assertValidJSONResponse(response, msg=msg)30 self.assertGetListResponseContains(response, name = [self.test_dataset1.name, self.test_dataset2.name], msg=msg)31 32 msg = 'When authentication is provided, a GET on the list URL must return a valid JSON response with the complete list of datasets'33 response = self.api_client.get(self.get_resource_uri(), format='json', authentication=self.test_user_authentication)34 self.assertValidJSONResponse(response, msg=msg)35 self.assertGetListResponseContains(response, name = [self.test_dataset1.name, self.test_dataset2.name], msg=msg)36 37 def test_get_list_filtered_characteristic(self):38 '''Test the characteristic filter for a GET on the list URL'''39 40 msg = 'When a exact filter for an existing charcteristic is requested, the list of dataset with that characteristic must be returned'41 data = {'characteristics': self.test_characteristic2.name}42 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)43 self.assertGetListResponseContains(response, name = [self.test_dataset2.name], msg=msg)44 45 msg = 'When a exact filter for an non-existing characteristic is requested, no dataset must be returned'46 data = {'characteristics': 'does not exist'}47 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)48 self.assertGetListResponseContains(response, name = [], msg=msg)49 50 msg = 'When a exact filter for an existing characteristic and a simple filter is requested, the list of dataset with that characteristic that conform to the simple filter must be returned'51 data = {'characteristics': self.test_characteristic1.name, 'name': self.test_dataset1.name}52 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)53 self.assertGetListResponseContains(response, name = [self.test_dataset1.name], msg=msg)54 55 # The following test demonstrates that we correctly handle the __in filter, as we use distinct56 msg = 'When a __in filter is requested, the list of dataset with these characteristics must be returned'57 data = {'characteristics__in': ','.join([self.test_characteristic1.name, self.test_characteristic2.name])}58 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)59 self.assertGetListResponseContains(response, name = [self.test_dataset1.name, self.test_dataset2.name], msg=msg)60 61 def test_get_list_metadata(self):62 '''Test the metadata field for a GET on the list URL'''63 64 metadata_resource_list_url = self.get_resource_uri(resource_name='base_metadata_test_resource')65 66 msg = 'When no filter is requested, the metadata field resource_uri must be exactly the metadata ressource list url'67 response = self.api_client.get(self.get_resource_uri(), format='json', authentication=self.test_user_authentication)68 self.assertGetListResponseContains(response, metadata = [{'resource_uri': metadata_resource_list_url, 'count': 1}, None], msg=msg)69 70 msg = 'When a simple filter for the dataset resource is requested, the metadata field resource_uri must be exactly the metadata ressource list url'71 data = {'characteristics': self.test_characteristic1.name}72 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)73 self.assertGetListResponseContains(response, metadata = [{'resource_uri': metadata_resource_list_url, 'count': 1}, None], msg=msg)74 75 msg = 'When a simple filter for the metadata resource is requested, the metadata field resource_uri must include the filter'76 data = {'oid': self.test_metadata.oid}77 response = self.api_client.get(self.get_resource_uri(), data=data, format='json',authentication=self.test_user_authentication)78 self.assertGetListResponseContains(response, metadata = [{'resource_uri': metadata_resource_list_url + '?' + urlencode(data), 'count': 1}, None], msg=msg)79 80 msg = 'When multiple filters for the metadata resource are requested, the metadata field resource_uri must include the filters'81 data = {'oid': self.test_metadata.oid, 'date_beg__isnull': True}82 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)83 self.assertGetListResponseContains(response, metadata = [{'resource_uri': metadata_resource_list_url + '?' + urlencode(data), 'count': 1}, None], msg=msg)84 85 msg = 'When mixed filters for the dataset resource and the metadata resource are requested, the metadata field resource_uri must include the filters only for teh metadata resource'86 data = {'oid': self.test_metadata.oid, 'characteristics': self.test_characteristic1.name}87 response = self.api_client.get(self.get_resource_uri(), data=data, format='json', authentication=self.test_user_authentication)88 self.assertGetListResponseContains(response, metadata = [{'resource_uri': metadata_resource_list_url + '?' + urlencode({'oid': self.test_metadata.oid}), 'count': 1}, None], msg=msg)89 90 def test_get_detail(self):91 '''Test a GET on the detail URL'''92 93 msg = 'When no authentication is provided, a GET on the detail URL must return a valid JSON response'94 response = self.api_client.get(self.get_resource_uri(self.test_dataset1), format='json')95 self.assertValidJSONResponse(response, msg=msg)96 self.assertResponseHasKeys(response, ['name', 'description', 'archive_url', 'telescope', 'instrument', 'characteristics', 'metadata'], msg=msg)97 98 msg = 'When authentication is provided, a GET on the detail URL must return a valid JSON response'99 response = self.api_client.get(self.get_resource_uri(self.test_dataset1), format='json', authentication=self.test_user_authentication)100 self.assertValidJSONResponse(response, msg=msg)101 self.assertResponseHasKeys(response, ['name', 'description', 'archive_url', 'telescope', 'instrument', 'characteristics', 'metadata'], msg=msg)102 103 def test_get_detail_metadata(self):104 '''Test the metadata field for a GET on the detail URL'''105 106 metadata_resource_list_url = self.get_resource_uri(resource_name='base_metadata_test_resource')107 108 msg = 'When the dataset metadata_content_type is correctly set, the metadata field must be a dict with the metadata ressource list url and the estimated count'109 response = self.api_client.get(self.get_resource_uri(self.test_dataset1), format='json', authentication=self.test_user_authentication)110 self.assertGetDetailResponseContains(response, metadata = {'resource_uri': metadata_resource_list_url, 'count': 1}, msg=msg)111 112 msg = 'When the dataset metadata_content_type is None, the metadata field must be None'113 response = self.api_client.get(self.get_resource_uri(self.test_dataset2), format='json', authentication=self.test_user_authentication)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tempest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful