def test_GIVEN_secret_name_but_not_secret_version_provided_WHEN_activate_secrets_called_THEN_proper_secrets_activated(self): # GIVEN secret_name = 'password' secret_version = None item1 = MagicMock() item2 = MagicMock() mock_load_all_entries = MagicMock(return_value = [item1, item2]) patch( 'kaurna.load_all_entries', mock_load_all_entries ).start() # WHEN activate_secrets(secret_name=secret_name, secret_version=secret_version, region=self.region) # THEN assert_equals( mock_load_all_entries.call_args_list, [call(secret_name=secret_name, secret_version=secret_version, region=self.region)] ) assert_equals( item1.mock_calls, [call.__setitem__('deprecated', False), call.save()] ) assert_equals( item2.mock_calls, [call.__setitem__('deprecated', False), call.save()] )
def test_WHEN__reencrypt_item_and_save_called_THEN_item_reencrypted_and_saved(self): # GIVEN item = MagicMock() item.getitem.side_effect = ['old_encrypted_secret', 'old_encrypted_data_key', '{"Mallory Archer": "kaurna"}', '["Sterling Archer", "Cyril Figgis"]'] mock_get_data_key = MagicMock(return_value = {'Plaintext':'data_key_plaintext', 'CiphertextBlob':'data_key_ciphertext'}) patch( 'kaurna.get_data_key', mock_get_data_key ).start() mock_decrypt_with_kms = MagicMock(return_value = {'Plaintext': 'decrypt_with_kms_output'}) patch( 'kaurna.decrypt_with_kms', mock_decrypt_with_kms ).start() mock_decrypt_with_key = MagicMock(return_value = 'decrypt_with_key_output') patch( 'kaurna.decrypt_with_key', mock_decrypt_with_key ).start() mock_encrypt_with_key = MagicMock(return_value = 'encrypt_with_key_output') patch( 'kaurna.encrypt_with_key', mock_encrypt_with_key ).start() patch( 'kaurna.time.time', Mock(return_value = 1234.567) ).start() # WHEN output = kaurna._reencrypt_item_and_save(item=item, region=self.region) # THEN assert_equals( item.mock_calls, [ call.getitem('encrypted_secret'), call.getitem('encrypted_data_key'), call.getitem('encryption_context'), call.getitem('authorized_entities'), call.__setitem__('encryption_context', '{"Sterling Archer": "kaurna", "Cyril Figgis": "kaurna"}'), call.__setitem__('encrypted_secret', 'encrypt_with_key_output'), call.__setitem__('encrypted_data_key', 'ZGF0YV9rZXlfY2lwaGVydGV4dA==\n'), call.__setitem__('last_data_key_rotation', 1234), call.save() ] ) assert_equals( item, output )
def test_GIVEN_secret_name_and_secret_version_provided_WHEN_deprecate_secrets_called_THEN_proper_secrets_deprecated(self): # GIVEN secret_name = 'password' secret_version = 2 item = MagicMock() mock_load_all_entries = MagicMock(return_value = [item]) patch( 'kaurna.load_all_entries', mock_load_all_entries ).start() # WHEN deprecate_secrets(secret_name=secret_name, secret_version=secret_version, region=self.region) # THEN assert_equals( mock_load_all_entries.call_args_list, [call(secret_name=secret_name, secret_version=secret_version, region=self.region)] ) assert_equals( item.mock_calls, [call.__setitem__('deprecated', True), call.save()] )
def test_iiif_image_api_quality(self, _getter): # Quality: default image = self.mock_image(return_from=_getter) self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', 'max', '0', 'default', 'jpg']), user=self.superuser, ).follow() self.assertEqual(image.mock_calls, [call.save(ANY, format='jpeg')]) # Quality: color image = self.mock_image(return_from=_getter) self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', 'max', '0', 'color', 'jpg']), user=self.superuser, ).follow() self.assertEqual(image.mock_calls, [call.save(ANY, format='jpeg')]) # Quality: gray image = self.mock_image(return_from=_getter) self.app.get( reverse('iiif_image_api', args=[self.ik_image.pk, 'full', 'max', '0', 'gray', 'jpg']), user=self.superuser, ).follow() self.assertEqual( image.mock_calls, [ # Convert image to black-and-white (L) mode call.convert('L'), call.convert().save(ANY, format='jpeg') ]) # Quality: bitonal (not yet supported) image = self.mock_image(return_from=_getter) response = self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', 'max', '0', 'bitonal', 'jpg']), user=self.superuser, expect_errors=True, ) self.assertEqual(501, response.status_code) self.assertEqual( "Image API quality parameters other than ('default', 'color'," " 'gray') are not yet supported: bitonal", response.content)
def test_service_edit_POST_request_perform_service_check( self, mock_messages): view = setup_view(ServiceEditView(), self.post_request, pk=self.service.pk) view.get_object = lambda: MagicMock(name='service') response = view.post(view.request) view.object.assert_has_calls([call.validate(), call.save()])
def test_service_add_POST_request_perform_service_check(self): view = setup_view(ServiceAddView(), self.get_request) view.object = MagicMock(spec_set=Service) view.object.pk = self.service.pk response = view.get_success_url() calls = [call.validate(), call.save()] view.object.assert_has_calls(calls)
def test_iiif_image_api_rotation(self, _getter): # Rotation: 0 image = self.mock_image(return_from=_getter) self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', 'max', '0', 'default', 'jpg']), user=self.superuser, ).follow() self.assertEqual(image.mock_calls, [call.save(ANY, format='jpeg')]) # Rotation: 360 (converted to 0 since % 360) image = self.mock_image(return_from=_getter) self.app.get( reverse('iiif_image_api', args=[ self.ik_image.pk, 'full', 'max', '360', 'default', 'jpg' ]), user=self.superuser, ).follow() self.assertEqual(image.mock_calls, [call.save(ANY, format='jpeg')]) # Rotation: invalid value response = self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', 'max', 'x', 'default', 'jpg']), user=self.superuser, expect_errors=True, ) self.assertEqual(400, response.status_code) # Rotation other than 0 or 360 not yet supported response = self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', 'max', '90', 'default', 'jpg']), user=self.superuser, expect_errors=True, ) self.assertEqual(501, response.status_code) self.assertEqual( "Image API rotation parameters other than (0,) degrees are not" " yet supported: 90", response.content)
def test_service_satus_POST_request_perform_service_check(self): view = setup_view(ServiceStatusView(), self.get_request) service = MagicMock(name='service') view.get_object = MagicMock(name='get_object', return_value=service) try: view.post(self.get_request) except: pass calls = [call.validate(), call.save()] service.assert_has_calls(calls)
def test_iiif_image_api_basics(self): # Not a privileged user user = G(User) response = self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', 'max', '0', 'default', 'jpg']), user=user, expect_errors=True, ) self.assertEqual(302, response.status_code) self.assertTrue( response.headers.get('Location', '').endswith( '/login/?next=/iiif/%d/full/max/0/default.jpg' % self.ik_image.pk)) # Now is a privileged user user.user_permissions.add( Permission.objects.get(codename='can_use_iiif_image_api')) response = self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', 'max', '0', 'default', 'jpg']), user=user, ) # Invalid image identifier response = self.app.get( reverse('iiif_image_api', args=[0, 'full', 'max', '0', 'default', 'jpg']), user=self.superuser, expect_errors=True, ) self.assertEqual(404, response.status_code) # Correct use image = self.mock_image() with patch('icekit.plugins.iiif.views._get_image_or_404') as _getter: _getter.return_value = (self.ik_image, image) response = self.app.get( reverse('iiif_image_api', args=[ self.ik_image.pk, 'full', 'full', '0', 'default', 'jpg' ]), user=self.superuser, ) # No image transform operations necessary or called, just save self.assertEqual(image.mock_calls, [call.save(ANY, format='jpeg')]) self.FileResponse.assert_called_with(ANY, content_type='image/jpeg')
def test_iiif_image_api_size(self, _getter): # Size: max image = self.mock_image(return_from=_getter) self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', 'max', '0', 'default', 'jpg']), user=self.superuser, ).follow() self.assertEqual(image.mock_calls, [call.save(ANY, format='jpeg')]) # Size: full image = self.mock_image(return_from=_getter) self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', 'full', '0', 'default', 'jpg']), user=self.superuser, ) self.assertEqual(image.mock_calls, [call.save(ANY, format='jpeg')]) # Size: pct image = self.mock_image(return_from=_getter) self.app.get( reverse('iiif_image_api', args=[ self.ik_image.pk, 'full', 'pct:25', '0', 'default', 'jpg' ]), user=self.superuser, ).follow() self.assertEqual(image.mock_calls, [ call.resize((image.width / 4, image.height / 4)), call.resize().convert('RGB'), call.resize().convert().save(ANY, format='jpeg') ]) # Size: w,h image = self.mock_image(return_from=_getter) self.app.get( reverse('iiif_image_api', args=[ self.ik_image.pk, 'full', '100,150', '0', 'default', 'jpg' ]), user=self.superuser, ).follow() self.assertEqual(image.mock_calls, [ call.resize((100, 150)), call.resize().convert('RGB'), call.resize().convert().save(ANY, format='jpeg') ]) # Size: w, (maintain aspect ratio) image = self.mock_image(return_from=_getter) self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', '150,', '0', 'default', 'jpg']), user=self.superuser, ) self.assertEqual(image.mock_calls, [ call.resize((150, 225)), call.resize().convert('RGB'), call.resize().convert().save(ANY, format='jpeg') ]) # Size: ,h (maintain aspect ratio, scale beyond original image size) image = self.mock_image(return_from=_getter) self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', ',600', '0', 'default', 'jpg']), user=self.superuser, ).follow() self.assertEqual(image.mock_calls, [ call.resize((400, 600)), call.resize().convert('RGB'), call.resize().convert().save(ANY, format='jpeg') ]) # Size: !w,h (best-fit) image = self.mock_image(return_from=_getter) self.app.get( reverse('iiif_image_api', args=[ self.ik_image.pk, 'full', '!75,125', '0', 'default', 'jpg' ]), user=self.superuser, ).follow() self.assertEqual( image.mock_calls, [ # Width is best fit call.resize((75, 112)), call.resize().convert('RGB'), call.resize().convert().save(ANY, format='jpeg') ]) image = self.mock_image(return_from=_getter) self.app.get( reverse('iiif_image_api', args=[ self.ik_image.pk, 'full', '!300,240', '0', 'default', 'jpg' ]), user=self.superuser, ).follow() self.assertEqual( image.mock_calls, [ # Height is best fit call.resize((160, 240)), call.resize().convert('RGB'), call.resize().convert().save(ANY, format='jpeg') ])
def test_iiif_image_api_region(self, _getter): # Region: full image = self.mock_image(return_from=_getter) self.app.get( reverse( 'iiif_image_api', args=[self.ik_image.pk, 'full', 'max', '0', 'default', 'jpg']), user=self.superuser, ).follow() self.assertEqual(image.mock_calls, [call.save(ANY, format='jpeg')]) # Region: square, 200 x 300 image image = self.mock_image(return_from=_getter) self.app.get( reverse('iiif_image_api', args=[ self.ik_image.pk, 'square', 'max', '0', 'default', 'jpg' ]), user=self.superuser, ).follow() self.assertEqual( image.mock_calls, [ # Auto-cropped to center square in image call.crop((0, 50, 200, 250)), call.crop().convert('RGB'), call.crop().convert().save(ANY, format='jpeg') ]) # Region: x,y,w,h image = self.mock_image(return_from=_getter) self.app.get( reverse('iiif_image_api', args=[ self.ik_image.pk, '20,30,50,90', 'max', '0', 'default', 'jpg' ]), user=self.superuser, ).follow() self.assertEqual(image.mock_calls, [ call.crop((20, 30, 70, 120)), call.crop().convert('RGB'), call.crop().convert().save(ANY, format='jpeg') ]) # Region: x,y,w,h (crop isn't permitted to exceed original image) image = self.mock_image(return_from=_getter) self.app.get( reverse('iiif_image_api', args=[ self.ik_image.pk, '100,50,150,300', 'max', '0', 'default', 'jpg' ]), user=self.superuser, ).follow() self.assertEqual( image.mock_calls, [ # Width & height reduced to fit bounds call.crop((100, 50, 200, 300)), call.crop().convert('RGB'), call.crop().convert().save(ANY, format='jpeg') ]) # Region: pct:x,y,w,h image = self.mock_image(return_from=_getter) self.app.get( reverse('iiif_image_api', args=[ self.ik_image.pk, 'pct:10,50,75,50', 'max', '0', 'default', 'jpg' ]), user=self.superuser, ).follow() self.assertEqual( image.mock_calls, [ # Width & height reduced to fit bounds call.crop((20, 150, 170, 300)), call.crop().convert('RGB'), call.crop().convert().save(ANY, format='jpeg') ])