def test_images_collection_post(self): scan = self.add_one_scan(enabled_web_chat=False).json # check sanity - we should have one image at this point response = self.app.get(localurl(scan['URL'] + '/images')) self.assertEqual(response.json['total_results'], 1) # we add another image to this scan image_data = {'is_default': True} upload_files = [('file', 'img3', TEST_IMAGE_PNG)] response = self.app.post(localurl(scan['URL'] + '/images'), image_data, upload_files=upload_files) self.assertEqual(response.json['success'], True) self.assertEqual(len(response.json['results']), 1) # and we now find two images for this scan response = self.app.get(localurl(scan['URL'] + '/images')) self.assertEqual(response.json['total_results'], 2) self.assertTrue(response.json['results'][0]['is_default']) self.assertFalse(response.json['results'][1]['is_default']) # the non default image (the second one) should be the original one self.assertEqual(scan['images'][0]['URL'], response.json['results'][1]['URL']) image_data['user'] = '******' self.app.post(localurl(scan['URL'] + '/images'), image_data, upload_files=upload_files)
def test_file_url(self): scan_data = self._add_one_scan_with_two_images() images_collection_data = self.app.get( localurl(os.path.join(scan_data['URL'], 'images'))).json image1_url = localurl(scan_data['images'][0]['URL']) image1_data = self.app.get(localurl(image1_url)).json self.assertEqual(scan_data['image_url'], images_collection_data['results'][0]['image_url']) self.assertEqual(scan_data['image_url'], image1_data['image_url'])
def test_image_url(self): response = self.app.get(config.SERVICE_SCAN_COLLECTION) results = response.json['results'] some_scan = results[2] # test if image_url is key self.assertTrue('image_url' in some_scan) # image_url should result in the same image as image/file img1 = self.app.get(localurl(some_scan['URL']) + '/image').body img2 = self.app.get(localurl(some_scan['image_url'])).body self.assertEqual(img1, img2)
def test_delete(self): """delete an EAD file""" put_res = self.add_one_ead(dontlog=True) res = self.app.get('/ead') self.assertEqual(len(res.json['results']), 1) url = put_res.json['URL'] self.app.delete(localurl(url)) res = self.app.get('/ead') self.assertEqual(len(res.json['results']), 0) # trying to get the EAD should now return an 400 error self.app.get(localurl(url), status=404) # we should now be able to add a new file with the same id put_res = self.add_one_ead(dontlog=True)
def test_scan_dpi(self): "Ensure a thumbnail is correctly generated" img = Image.open(StringIO(TEST_IMAGE_300DPI)) self.assertEqual(img.info['dpi'], (300, 300)) scan = self.add_one_scan(filecontents=TEST_IMAGE_300DPI).json url = localurl(scan['URL']) + '/image?size=80x80' res = self.app.get(url) img = Image.open(StringIO(res.body)) self.assertEqual(img.info['dpi'], (300, 300)) url = localurl(scan['URL']) + '/image?size=10000x10000' res = self.app.get(url) img = Image.open(StringIO(res.body)) self.assertEqual(img.info['dpi'], (300, 300))
def test_images_collection_get(self): scan = self._add_one_scan_with_two_images() response = self.app.get(localurl(scan['URL'] + '/images')) self.assertEqual(response.json['total_results'], 2) self.assertEqual(response.json['results'][0]['filename'], 'img1') self.assertEqual(response.json['results'][1]['filename'], 'img2') self.assertEqual(response.json['results'][0]['is_default'], True)
def test_ead_update_with_empty_body(self): filecontents = self.get_default_filecontents() res = self.add_one_ead(filecontents=filecontents, dontlog=True) url = localurl(res.json['URL']) # an empty body should just return a 200 OK and leave the data intact res1 = self.app.put(url) self.assertEqual(res.json, res1.json)
def test_move_scan_backward(self): """ We insert 5 scans and then move the last one to the second place. S=Source, D=Destination +---+---+---+---+---+ | | D | | | S | +---+---+---+---+---+ """ scans = self.add_five_scans() pre_result = self.app.get(config.SERVICE_SCAN_COLLECTION, self.search_data).json url = localurl(scans[4]['URL']) + '/move' self.app.post(url, {'after': 1}) post_result = self.app.get(config.SERVICE_SCAN_COLLECTION, self.search_data).json numbers = [a['sequenceNumber'] for a in post_result['results']] self.assertEqual(numbers, sorted(numbers)) ids_before = [a['number'] for a in pre_result['results']] ids_after = [a['number'] for a in post_result['results']] self.assertEqual(ids_before[0], ids_after[0]) # Leave him alone! self.assertEqual(ids_before[1], ids_after[2]) # Three elements self.assertEqual(ids_before[2], ids_after[3]) # should be shifted self.assertEqual(ids_before[3], ids_after[4]) # to the right. # And the target element should have reached its position self.assertEqual(ids_before[4], ids_after[1])
def test_scan_thumbnail_height(self): "Ensure a thumbnail is correctly generated" scan = self.add_one_scan(filecontents=TEST_IMAGE_ZACKTHECAT).json url = localurl(scan['URL']) + '/image?size=x100' res = self.app.get(url) img = Image.open(StringIO(res.body)) self.assertEqual(img.size, (109, 99))
def test_update_archive_id_moves_scanNumber(self): """ check if scan position is updated when it's moved to a different collection (changing archive_id or archiveFile). Also check solr index stays in sync. """ set1, set2 = [], [] for _i in range(3): set1.append(self.add_one_scan(self.scan_data).json) set2_data = dict(self.scan_data, archiveFile='foo') for _i in range(3): set2.append(self.add_one_scan(set2_data).json) # We have two sets of scans. We move the second scan from set1 to set2 url = localurl(set1[1]['URL']) self.app.put(url, dict(archiveFile='foo')) moved_scan = self.app.get(url).json self.assertEqual(moved_scan['sequenceNumber'], 4) pg_newset1 = self.app.get(config.SERVICE_SCAN_COLLECTION, dict(archiveFile='foo')).json['results'] pg_newset2 = self.app.get(config.SERVICE_SCAN_COLLECTION, dict(archiveFile=self.scan_data['archiveFile'])).json['results'] # Ensure sequenceNumbers have been updated self.assertEqual([a['sequenceNumber'] for a in pg_newset2], [1, 2]) self.assertEqual([a['sequenceNumber'] for a in pg_newset1], [1, 2, 3, 4]) solr_newset1 = self.solr_scan.search( q='archiveFile:' + 'foo').documents solr_newset2 = self.solr_scan.search( q='archiveFile:' + self.scan_data['archiveFile']).documents # Ensure sequenceNumbers have been updated self.assertEqual([a['sequenceNumber'] for a in solr_newset2], [1, 2]) self.assertEqual([a['sequenceNumber'] for a in solr_newset1], [1, 2, 3, 4])
def test_error_if_updated_scan_file_is_not_an_image(self): "If an updated file is not a TIFF, GIF, PNG or JPEG a 400 error is raised" scan = self.add_one_scan() url = localurl(scan.json['URL']) filetuple = ('file', 'test_fn', "silly text") res = self.app.put(url, upload_files=[filetuple], status=400) self.assertEqual(res.json['errors'][0]['name'], 'invalid_file')
def test_wrong_file_variable_update(self): "Give a proper error code when the file parameter is not a file" scan = self.add_one_scan() url = localurl(scan.json['URL']) scan_data = dict(self.scan_data, file="A string") res = self.app.put(url, scan_data, status=400) self.assertEqual(res.json['errors'][0]['name'], 'missing_file')
def test_ead_update_status0(self): filecontents = self.get_default_filecontents() res = self.add_one_ead(filecontents=filecontents) url = localurl(res.json['URL']) res = self.app.put(url, {'status': 0}, status=400).json self.assertEqual(res['errors'][0]['name'], ERRORS.invalid_parameter.name)
def test_get_raw_ead(self): "After storing a file we can get its contents at URL" filecontents = self.get_default_filecontents() res = self.add_one_ead(filecontents=filecontents, dontlog=True) url = localurl(res.json['URL']) ead = self.app.get(url + '/file') self.assertEqual(ead.content_type, 'text/xml') self.assertEqual(ead.body, filecontents)
def test_scan_thumbnail_validation(self): "Invalid values for `size` should trigger a meaningful error" scan = self.add_one_scan().json url = localurl(scan['URL']) + '/image?size=' self.app.get(url + 'x', status=400) self.app.get(url + 'ax', status=400) self.app.get(url + '120xc', status=400) self.app.get(url + 'axb', status=400)
def test_ead_update_different_filename(self): filecontents = self.get_default_filecontents() res = self.add_one_ead(filecontents=filecontents).json url = localurl(res['URL']) new_filecontents = filecontents.replace('test_file_123.xml', 'another_id') filetuple = ('file', 'another_filename.xml', new_filecontents) res = self.app.put(url, upload_files=[filetuple], status=400).json
def test_scan_thumbnail_width(self): "Ensure a thumbnail is correctly generated" scan = self.add_one_scan(filecontents=TEST_IMAGE_ZACKTHECAT).json url = localurl(scan['URL']) + '/image?size=100x' res = self.app.get(url) self.assertEqual(res.content_type, 'image/jpeg') img = Image.open(StringIO(res.body)) self.assertEqual(img.size, (100, 91))
def test_move_scan_validation(self): """ An error is raised if we ask to move a scan to a non-existent position """ scans = self.add_five_scans() url = localurl(scans[1]['URL']) + '/move' self.app.post(url, {'after': 50}, status=400)
def test_images_collection_item_delete(self): scan = self._add_one_scan_with_two_images() response = self.app.get(localurl(scan['URL'] + '/images')) img1_url = localurl(response.json['results'][0]['URL']) img2_url = localurl(response.json['results'][1]['URL']) # delete the first image on the collection self.app.delete(img1_url) response = self.app.get(localurl(scan['URL'] + '/images')) # we have one image left self.assertEqual(response.json['total_results'], 1) # and the single image is img2 self.assertEqual(localurl(response.json['results'][0]['URL']), img2_url) # it became the default one self.assertEqual(response.json['results'][0]['is_default'], True) # if we try to delete the last image, we raise an error response = self.app.delete(img2_url, status=400)
def test_images_item_get(self): scan = self._add_one_scan_with_two_images() # now, get the info of this scan scan_info = self.app.get(localurl(scan['URL'])) self.assertEqual(len(scan_info.json['images']), 2) # files are uploaded in the order given self.assertEqual(scan_info.json['images'][0]['filename'], 'img1') self.assertEqual(scan_info.json['images'][1]['filename'], 'img2') # they have a url argument (where the info of the image can be found self.assertTrue('URL' in scan_info.json['images'][0]) url = localurl(scan_info.json['images'][0]["URL"]) scaninfo = self.app.get(url).json self.assertEqual(scaninfo["filename"], 'img1') self.assertEqual(scaninfo["is_default"], True) url = localurl(scan_info.json['images'][1]["URL"]) scaninfo = self.app.get(url).json self.assertEqual(scaninfo["is_default"], False)
def test_scan_thumbnail_is_erased_on_update(self): "After updating a scan all its thumbnails are recalculated" scan = self.add_one_scan(filecontents=TEST_IMAGE_ZACKTHECAT).json url = localurl(scan['URL']) first_thumbnail = self.app.get(url + '/image?size=100x').body filetuple = ('file', 'test_fn', TEST_IMAGE_TIF) self.app.put(url, upload_files=[filetuple]) second_thumbnail = self.app.get(url + '/image?size=100x').body self.assertTrue(first_thumbnail != second_thumbnail)
def test_get_ead(self): "After storing a file we can get its contents at URL" filecontents = self.get_default_filecontents() res = self.add_one_ead(filecontents=filecontents, dontlog=True) url = localurl(res.json['URL']) ead = self.app.get(url) self.assertEqual(ead.json['institution'], self.default_institution) self.assertEqual(ead.json['title'], 'Title') self.assertEqual(ead.json['country'], self.default_country)
def test_images_collection_item_raw_file(self): scan = self._add_one_scan_with_two_images() img1_url = localurl(scan['images'][0]['URL']) response = self.app.get(os.path.join(img1_url, 'file')) self.assertEqual(response.status_code, 200) result1 = response.body response = self.app.get( os.path.join(img1_url, 'filearbitrary_hash_value')) self.assertEqual(response.status_code, 200) self.assertEqual(response.body, result1)
def test_update_number_raises_error(self): "number and sequenceNumber are not (directly) mutable" result = self.add_one_scan(self.scan_data).json url = localurl(result['URL']) new_res = self.app.put(url, {'number': '12'}, status=400).json self.assertEqual(new_res['errors'][0]['name'], 'invalid_parameter') new_res = self.app.put(url, {'sequenceNumber': '12'}, status=400).json self.assertEqual(new_res['errors'][0]['name'], 'invalid_parameter')
def test_update_translationID(self): "translationID is correctly updated" result = self.add_one_scan(self.scan_data).json self.assertEqual(result.get('translationID'), None) url = localurl(result['URL']) then = now() self.app.put(url, {'translationID': 'Some new value'}) result = self.app.get(url).json self.assertEqual(result['translationID'], 'Some new value') self.assertMoreRecent(result['dateLastModified'], then)
def test_update_status(self): "changing status works only for valid values" result = self.add_one_scan(self.scan_data).json self.assertEqual(result['status'], 1) url = localurl(result['URL']) then = now() self.app.put(url, {'status': 2}) result = self.app.get(url).json self.assertEqual(result['status'], 2) self.assertMoreRecent(result['dateLastModified'], then)
def test_ead_update_status1(self): filecontents = self.get_default_filecontents() res = self.add_one_ead(filecontents=filecontents) url = localurl(res.json['URL']) # change the status res = self.app.put(url, {'status': 2}).json self.assertEqual(res['status'], 2) # check if the status is really there res = self.app.get(url).json self.assertEqual(res['status'], 2)
def test_scan_thumbnail_height_width(self): "Ensure a thumbnail is correctly generated" scan = self.add_one_scan(filecontents=TEST_IMAGE_ZACKTHECAT).json url = localurl(scan['URL']) + '/image?size=80x80' res = self.app.get(url) img = Image.open(StringIO(res.body)) self.assertEqual(img.size, (80, 72)) # just passing a number is interpreted as a max on the height url = localurl(scan['URL']) + '/image?size=80' res = self.app.get(url) img = Image.open(StringIO(res.body)) self.assertEqual(img.size[1], 79) # 0 should give us a 404 url = localurl(scan['URL']) + '/image?size=0' res = self.app.get(url, expect_errors=True) self.assertEqual(res.status_code, 400) url = localurl(scan['URL']) + '/image?size=0x' res = self.app.get(url, expect_errors=True) self.assertEqual(res.status_code, 400) url = localurl(scan['URL']) + '/image?size=x0' res = self.app.get(url, expect_errors=True) self.assertEqual(res.status_code, 400) url = localurl(scan['URL']) + '/image?size=0x0' res = self.app.get(url, expect_errors=True) self.assertEqual(res.status_code, 400)
def test_images_collection_set_default_image(self): scan = self._add_one_scan_with_two_images() img1_url = localurl(scan['images'][0]['URL']) img2_url = localurl(scan['images'][1]['URL']) response = self.app.get(img1_url) self.assertEqual(response.json['is_default'], True) response = self.app.get(img2_url) self.assertEqual(response.json['is_default'], False) self.app.put(img2_url, {'is_default': True}) response = self.app.get(img1_url) self.assertEqual(response.json['is_default'], False) response = self.app.get(img2_url) self.assertEqual(response.json['is_default'], True) # now if we delete the default image, we want to other image to becausem the default image self.app.delete(img2_url) response = self.app.get(img1_url) self.assertEqual(response.json['is_default'], True)
def test_search_sort(self): scans = self.add_five_scans() an_early_date = '2000-01-01' first_archiveFile = 'a_arepo_this_comes_first' self.app.put(localurl(scans[3]['URL']), {'date': an_early_date}) self.app.put(localurl(scans[3]['URL']), {'archive_id': 2}) oldestid = scans[3]['number'] result = self.app.get(config.SERVICE_SCAN_COLLECTION, {'order_by': 'date'}).json self.assertEqual(result['results'][0]['number'], oldestid) result = self.app.get(config.SERVICE_SCAN_COLLECTION, {'order_by': 'date', 'order_dir': 'DESC'}).json self.assertEqual(result['results'][-1]['number'], oldestid) result = self.app.get(config.SERVICE_SCAN_COLLECTION, {'order_by': '-date'}).json self.assertEqual(result['results'][-1]['number'], oldestid) result = self.app.get(config.SERVICE_SCAN_COLLECTION, {'order_by': '-date'}).json self.assertEqual(result['results'][-1]['number'], oldestid) self.app.put(localurl(scans[1]['URL']), {'archiveFile': first_archiveFile}) result = self.app.get(config.SERVICE_SCAN_COLLECTION, {'order_by': 'archiveFile'}).json self.assertEqual(result['results'][0]['archiveFile'], first_archiveFile) result = self.app.get(config.SERVICE_SCAN_COLLECTION, {'order_by': '-archiveFile,date'}).json self.assertEqual(result['results'][0]['date'][:len(an_early_date)], an_early_date) self.assertEqual(result['results'][-1]['archiveFile'], first_archiveFile) # by default results are sorted default_result = self.app.get(config.SERVICE_SCAN_COLLECTION).json default_result = [img['number'] for img in default_result['results']] sorted_result = self.app.get(config.SERVICE_SCAN_COLLECTION, {'order_by': 'archive_id,archiveFile,sequenceNumber'}).json sorted_result = [image['number'] for image in sorted_result['results']] self.assertEqual(default_result, sorted_result)