How to use _create_image method in tempest

Best Python code snippet using tempest_python

tests.py

Source:tests.py Github

copy

Full Screen

...38 temp_file = tempfile.NamedTemporaryFile()39 image.save(temp_file, extension)40 temp_file.seek(0)41 return temp_file42 def _create_image(self, auth, size=(200, 200)):43 """44 need to be invoked inside:45 with self.settings(MEDIA_ROOT=self.temporary_dir.name):46 :param auth: username, password iterable47 :param size: height, width tuple48 :return: newly created Image49 """50 owner = User.objects.filter(username=auth[0]).first()51 test_image = self._get_temporary_image(size, 'jpeg')52 file = InMemoryUploadedFile(test_image, None, test_image.name, 'image/jpeg', test_image.__sizeof__(), None)53 image = Image.objects.create(owner=owner, image=file)54 return image55class ExpiringLinksTest(APITestCaseWithMedia):56 """57 Test creating and using expiring links to images58 """59 fixtures = ['accounts.json']60 def test_get_image_invalid_link(self):61 """62 Try to get image from not existing expiring link63 """64 with self.settings(MEDIA_ROOT=self.temporary_dir.name):65 response = client.get(reverse('get-expiring', args=['12345']))66 self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)67 def test_get_image_from_expired_link(self):68 """69 Try to get image from expired link70 """71 with self.settings(MEDIA_ROOT=self.temporary_dir.name):72 image = self._create_image(('sunshine', 'YUsPygfgf8rLaU7'))73 expiring = timezone.now() - timezone.timedelta(seconds=1)74 expired_link = ExpiringLink.objects.create(image=image, expiring=expiring)75 client.login(username='sunshine', password='YUsPygfgf8rLaU7')76 response = client.get(reverse('get-expiring', args=[expired_link.name]))77 client.logout()78 self.assertEqual(response.status_code, status.HTTP_410_GONE)79 def test_fetch_expiring_invalid_time(self):80 """81 Try to fetch expiring link to image with invalid time parameter82 """83 with self.settings(MEDIA_ROOT=self.temporary_dir.name):84 image = self._create_image(('chessGM', 'YUsPygfgf8rLaU7'))85 client.login(username='chessGM', password='YUsPygfgf8rLaU7')86 response = client.get(reverse('create-expiring', args=[image.image.name, 299]))87 client.logout()88 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)89 def test_fetch_expiring_link_valid(self):90 """91 Fetch expiring link to your own image with 'expiring link' perk assigned to your account tier92 """93 with self.settings(MEDIA_ROOT=self.temporary_dir.name):94 image = self._create_image(('chessGM', 'YUsPygfgf8rLaU7'))95 client.login(username='chessGM', password='YUsPygfgf8rLaU7')96 response = client.get(reverse('create-expiring', args=[image.image.name, 300]))97 client.logout()98 self.assertEqual(response.status_code, status.HTTP_200_OK)99 response = client.get(response.json()['link'])100 img_bytes = next(response.streaming_content)101 pil_image = PIL_Image.open(io.BytesIO(img_bytes))102 self.assertEqual(pil_image.size, (200, 200))103 def test_fetch_expiring_link_invalid_perks(self):104 """105 Fetch expiring link to your own image without 'expiring link' perk assigned to your account tier106 """107 with self.settings(MEDIA_ROOT=self.temporary_dir.name):108 image = self._create_image(('seamel', 'ZwpDu9BGHRTTqKX'))109 client.login(username='seamel', password='ZwpDu9BGHRTTqKX')110 response = client.get(reverse('create-expiring', args=[image.image.name, 300]))111 client.logout()112 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)113 self.assertEqual(ExpiringLink.objects.all().count(), 0)114class PostImageTest(APITestCaseWithMedia):115 """116 Test uploading image117 """118 fixtures = ['accounts.json']119 def test_upload_unauthorized(self):120 """121 Try uploading image without authorization122 """123 with self.settings(MEDIA_ROOT=self.temporary_dir.name):124 file = io.BytesIO()125 pil_image = self._get_temporary_image((200, 200), 'jpeg', file)126 data = {127 'image': File(pil_image, 'name.jpg')128 }129 response = client.post(reverse('images-list'), data)130 self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)131 self.assertEqual(Image.objects.all().count(), 0)132 def test_upload_valid_jpeg(self):133 """134 Test uploading valid jpeg image135 """136 with self.settings(MEDIA_ROOT=self.temporary_dir.name):137 file = io.BytesIO()138 pil_image = self._get_temporary_image((200, 200), 'jpeg', file)139 client.login(username='admin', password='admin')140 data = {141 'image': File(pil_image, 'name.jpg')142 }143 response = client.post(reverse('images-list'), data)144 client.logout()145 self.assertEqual(response.status_code, status.HTTP_201_CREATED)146 self.assertEqual(Image.objects.all().count(), 1)147 def test_upload_valid_png(self):148 """149 Test uploading valid png image150 """151 with self.settings(MEDIA_ROOT=self.temporary_dir.name):152 file = io.BytesIO()153 pil_image = self._get_temporary_image((200, 200), 'png', file)154 client.login(username='admin', password='admin')155 data = {156 'image': File(pil_image, 'name.png')157 }158 response = client.post(reverse('images-list'), data)159 client.logout()160 self.assertEqual(response.status_code, status.HTTP_201_CREATED)161 self.assertEqual(Image.objects.all().count(), 1)162 def test_upload_bmp(self):163 """164 Try uploading bmp image. Server should only accept jpeg and png.165 """166 with self.settings(MEDIA_ROOT=self.temporary_dir.name):167 file = io.BytesIO()168 pil_image = self._get_temporary_image((200, 200), 'bmp', file)169 client.login(username='admin', password='admin')170 data = {171 'image': File(pil_image, 'name.bmp')172 }173 response = client.post(reverse('images-list'), data)174 client.logout()175 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)176 self.assertEqual(Image.objects.all().count(), 0)177 def test_upload_image_too_big(self):178 """179 Try uploading image that is too big180 """181 with self.settings(MEDIA_ROOT=self.temporary_dir.name):182 file = io.BytesIO()183 pil_image = self._get_temporary_image((10_000, 24_000), 'jpeg', file)184 client.login(username='admin', password='admin')185 data = {186 'image': File(pil_image, 'name.jpeg')187 }188 response = client.post(reverse('images-list'), data)189 client.logout()190 self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)191 self.assertEqual(Image.objects.all().count(), 0)192class GetImageTest(APITestCaseWithMedia):193 """194 Test getting image195 """196 fixtures = ['accounts.json']197 def test_get_image_unauthorized(self):198 """199 Try to get image without authorization200 """201 with self.settings(MEDIA_ROOT=self.temporary_dir.name):202 image = self._create_image(('seamel', 'ZwpDu9BGHRTTqKX'))203 response = client.get(reverse('media', args=[image.image.name]))204 self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)205 def test_get_image_without_perk(self):206 """207 Try getting image without 'original image' perk208 """209 with self.settings(MEDIA_ROOT=self.temporary_dir.name):210 image = self._create_image(('seamel', 'ZwpDu9BGHRTTqKX'))211 client.login(username='seamel', password='ZwpDu9BGHRTTqKX')212 response = client.get(reverse('media', args=[image.image.name]))213 client.logout()214 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)215 def test_get_image_valid(self):216 """217 Test getting your image with 'original image' perk218 """219 with self.settings(MEDIA_ROOT=self.temporary_dir.name):220 image = self._create_image(('sunshine', 'YUsPygfgf8rLaU7'), (333, 443))221 client.login(username='sunshine', password='YUsPygfgf8rLaU7')222 response = client.get(reverse('media', args=[image.image.name]))223 client.logout()224 self.assertEqual(response.status_code, status.HTTP_200_OK)225 img_bytes = next(response.streaming_content)226 pil_image = PIL_Image.open(io.BytesIO(img_bytes))227 self.assertEqual(pil_image.size, (333, 443))228 def test_get_another_user_image(self):229 """230 Try to get a picture of another user with the 'original image' perk231 """232 with self.settings(MEDIA_ROOT=self.temporary_dir.name):233 image = self._create_image(('seamel', 'ZwpDu9BGHRTTqKX'), (333, 443))234 client.login(username='sunshine', password='YUsPygfgf8rLaU7')235 response = client.get(reverse('media', args=[image.image.name]))236 client.logout()237 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)238class GetThumbnailTest(APITestCaseWithMedia):239 """240 Test getting image thumbnail241 """242 fixtures = ['accounts.json']243 def test_get_thumbnail_unauthorized(self):244 """245 Try to get thumbnail without authorization246 """247 with self.settings(MEDIA_ROOT=self.temporary_dir.name):248 image = self._create_image(('seamel', 'ZwpDu9BGHRTTqKX'))249 response = client.get(reverse('thumbnail', args=[image.image.name, 400]))250 self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)251 def test_get_thumbnail_valid(self):252 """253 Test getting thumbnail as owner with necessary perks254 """255 with self.settings(MEDIA_ROOT=self.temporary_dir.name):256 image = self._create_image(('seamel', 'ZwpDu9BGHRTTqKX'), (300, 300))257 client.login(username='seamel', password='ZwpDu9BGHRTTqKX')258 response = client.get(reverse('thumbnail', args=[image.image.name, 200]))259 client.logout()260 self.assertEqual(response.status_code, status.HTTP_200_OK)261 img_bytes = next(response.streaming_content)262 pil_image = PIL_Image.open(io.BytesIO(img_bytes))263 self.assertEqual(pil_image.size, (200, 200))264 def test_get_thumbnail_with_original_size_valid(self):265 """266 Test getting thumbnail with the same height as the original size.267 User have perk to request thumbnail of this height but dont have perk to request original image268 """269 with self.settings(MEDIA_ROOT=self.temporary_dir.name):270 image = self._create_image(('seamel', 'ZwpDu9BGHRTTqKX'), (200, 200))271 client.login(username='seamel', password='ZwpDu9BGHRTTqKX')272 response = client.get(reverse('thumbnail', args=[image.image.name, 200]))273 client.logout()274 self.assertEqual(response.status_code, status.HTTP_200_OK)275 img_bytes = next(response.streaming_content)276 pil_image = PIL_Image.open(io.BytesIO(img_bytes))277 self.assertEqual(pil_image.size, (200, 200))278 def test_get_thumbnail_invalid_perks(self):279 """280 Test getting a thumbnail with a height you do not have permission to access281 """282 with self.settings(MEDIA_ROOT=self.temporary_dir.name):283 image = self._create_image(('seamel', 'ZwpDu9BGHRTTqKX'))284 client.login(username='seamel', password='ZwpDu9BGHRTTqKX')285 response = client.get(reverse('thumbnail', args=[image.image.name, 400]))286 client.logout()287 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)288 def test_get_thumbnail_as_staff_user(self):289 """290 Test access to another user's image thumbnail as staff user291 """292 with self.settings(MEDIA_ROOT=self.temporary_dir.name):293 image = self._create_image(('seamel', 'ZwpDu9BGHRTTqKX'), (700, 700))294 client.login(username='admin', password='admin')295 response = client.get(reverse('thumbnail', args=[image.image.name, 666]))296 client.logout()297 self.assertEqual(response.status_code, status.HTTP_200_OK)298 img_bytes = next(response.streaming_content)299 pil_image = PIL_Image.open(io.BytesIO(img_bytes))300 self.assertEqual(pil_image.size, (666, 666))301 def test_get_another_user_thumbnail(self):302 """303 Test access to another user's image thumbnail as regular user with valid perks304 """305 with self.settings(MEDIA_ROOT=self.temporary_dir.name):306 image = self._create_image(('admin', 'admin'), (700, 700))307 client.login(username='seamel', password='ZwpDu9BGHRTTqKX')308 response = client.get(reverse('thumbnail', args=[image.image.name, 200]))309 client.logout()310 self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)311class GetAllImagesTest(APITestCaseWithMedia):312 """313 Test getting list of all images belonging to requesting user314 """315 fixtures = ['accounts.json']316 def test_get_images_list_unauthorized(self):317 """318 request images list as unauthorized user319 """320 response = client.get(reverse('images-list'))321 self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)322 def test_get_images_list_valid(self):323 """324 create two images one of them owned by 'admin'. Then get list of all images owned by 'admin'325 """326 with self.settings(MEDIA_ROOT=self.temporary_dir.name):327 self._create_image(('seamel', 'ZwpDu9BGHRTTqKX'))328 self._create_image(('admin', 'admin'))329 client.login(username='admin', password='admin')330 response = client.get(reverse('images-list'))331 client.logout()332 self.assertEqual(response.status_code, status.HTTP_200_OK)333 self.assertEqual(len(response.data), 1)...

Full Screen

Full Screen

test_utils.py

Source:test_utils.py Github

copy

Full Screen

...9 new_image = resize(image, target_shape)10 self.assertEqual(new_image.shape, target_shape)11 new_image = resize(new_image, original_image_shape, interpolation="linear")12 self.assertEqual(new_image.shape, original_image_shape)13 def _create_image(self, image_shape):14 data = np.asarray(np.arange(np.prod(image_shape)).reshape(image_shape), dtype=np.float)15 affine = np.zeros((4, 4))16 np.fill_diagonal(affine, 1)17 return nib.Nifti1Image(data, affine)18 def test_resize_image_1(self):19 image_shape = (4, 4, 4)20 image = self._create_image(image_shape)21 new_size = (2, 2, 2)22 self._resize_image_test(image, new_size)23 def test_resize_image_2(self):24 self._resize_image_test(self._create_image((12, 10, 8)), (8, 8, 8))25 def test_resize_image_2d(self):26 data = np.arange(1, 5).reshape((2, 2))27 new_data = resample_to_spacing(data, (2, 2), (1, 1), interpolation="nearest")28 self.assertTrue(np.all(new_data == np.asarray([[1, 1, 2, 2],29 [1, 1, 2, 2],30 [3, 3, 4, 4],31 [3, 3, 4, 4]])))32 orig_data = resample_to_spacing(new_data, (1, 1), (2, 2), interpolation="linear")33 self.assertTrue(np.all(data == orig_data))34 def test_resize_image_3(self):35 self._resize_image_test(self._create_image((2, 5, 3)), (7, 5, 11))36 def test_resize_image_3d(self):37 data = np.arange(1, 9).reshape((2, 2, 2))38 new_data = resample_to_spacing(data, (2, 2, 2), (1, 1, 1), interpolation="nearest")39 self.assertTrue(np.all(new_data[0] == np.asarray([[1, 1, 2, 2],40 [1, 1, 2, 2],41 [3, 3, 4, 4],42 [3, 3, 4, 4]])))43 orig_data = resample_to_spacing(new_data, (1, 1, 1), (2, 2, 2), interpolation="linear")44 self.assertTrue(np.all(data == orig_data))45 def test_images_align(self):46 data = np.arange(1, 9).reshape((2, 2, 2))47 affine = np.diag(np.ones(4) * 2)48 affine[3, 3] = 149 image_nib = nib.Nifti1Image(data, affine=affine)...

Full Screen

Full Screen

NodeSprite.py

Source:NodeSprite.py Github

copy

Full Screen

...11class NodeSprite(sprite.Sprite):12 def __init__(self, color: Color, color_hover: Color, rect: Rect, lmb_callback=None, rmb_callback=None, mmb_callback=None, outline: "Color|None" = None):13 super().__init__()14 tmp_rect = Rect(0, 0, *rect.size)15 self.org = self._create_image(color, outline, tmp_rect)16 self.unwalkable = self._create_image(BLACK, None, tmp_rect)17 self.hover = self._create_image(color_hover, outline, tmp_rect)18 self.image = self.org19 self.rect = rect20 self.lmb_callback = lmb_callback21 self.rmb_callback = rmb_callback22 self.mmb_callback = mmb_callback23 def _create_image(self, color: Color, outline: "Color|None", rect: Rect):24 img = Surface(rect.size)25 if outline:26 img.fill(outline)27 img.fill(color, rect.inflate(-4, -4))28 else:29 img.fill(color)30 return img31 def update(self, events: List[Event], mouse_position: Tuple[int, int], grid):32 if self.rect:33 hit = self.rect.collidepoint(mouse_position)34 node = grid.get_node_for_sprite(self)35 if node and node.walkable:36 self.image = self.hover if hit else self.org37 for event in events:...

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