def test_concordiaCollectionView_get_page2(self): """ Test GET on route /transcribe/<slug-value>/ (collection) on page 2 :return: """ # Arrange # add an item to Collection self.collection = Collection(title='TextCollection', slug='test-slug2', description='Collection Description', metadata={"key": "val1"}, status=Status.PCT_0) self.collection.save() # Act response = self.client.get('/transcribe/test-slug2/', {'page': 2}) # Assert self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, template_name='transcriptions/collection.html')
def test_ConcordiaAssetView_get(self): """ This unit test test the GET route /transcribe/<collection>/asset/<Asset_name>/ with already in use. Verify the updated_on time is updated on PageInUse :return: """ # Arrange self.login_user() # create a collection self.collection = Collection( title="TestCollection", slug="Collection1", description="Collection Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection.save() # create an Asset asset_slug = "Asset1" self.asset = Asset( title="TestAsset", slug=asset_slug, description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset.save() # add a Transcription object self.transcription = Transcription( asset=self.asset, user_id=self.user.id, text="Test transcription 1", status=Status.EDIT, ) self.transcription.save() url = "/transcribe/Collection1/asset/Asset1/" # Act response = self.client.get(url) # Assert self.assertEqual(response.status_code, 200) # get PageInUse value page_in_use = PageInUse.objects.get(page_url=url) # sleep so update time can be tested against original time time.sleep(2) # Act response = self.client.get(url) self.assertEqual(response.status_code, 200) # get PageInUse value page_in_use2 = PageInUse.objects.get(page_url=url) self.assertNotEqual(page_in_use.updated_on, page_in_use2.updated_on) self.assertEqual(page_in_use.created_on, page_in_use2.created_on)
def test_ConcordiaAssetView_post_anonymous_invalid_captcha(self): """ This unit test test the POST route /transcribe/<collection>/asset/<Asset_name>/ for an anonymous user with missing captcha. This user should not be able to tag also :return: """ # Arrange # create a collection self.collection = Collection( title="TestCollection", slug="Collection1", description="Collection Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection.save() # create an Asset self.asset = Asset( title="TestAsset", slug="Asset1", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset.save() # create anonymous user anon_user = User.objects.create(username="******", email="*****@*****.**") anon_user.set_password("blah_anonymous!") anon_user.save() # add a Transcription object self.transcription = Transcription( asset=self.asset, user_id=anon_user.id, text="Test transcription 1", status=Status.EDIT, ) self.transcription.save() tag_name = "Test tag 1" # Act # post as anonymous user without captcha data response = self.client.post( "/transcribe/Collection1/asset/Asset1/", { "tx": "First Test Transcription", "tags": tag_name, "action": "Save" }, ) # Assert self.assertEqual(response.status_code, 200) # Verify the new transcription are not in db transcription = Transcription.objects.filter( text="First Test Transcription", asset=self.asset) self.assertEqual(len(transcription), 0)
def test_ConcordiaAssetView_post_anonymous_happy_path(self): """ This unit test test the POST route /transcribe/<collection>/asset/<Asset_name>/ for an anonymous user. This user should not be able to tag :return: """ # Arrange # create a collection self.collection = Collection( title="TestCollection", slug="Collection1", description="Collection Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection.save() # create an Asset self.asset = Asset( title="TestAsset", slug="Asset1", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset.save() # create anonymous user anon_user = User.objects.create(username="******", email="*****@*****.**") anon_user.set_password("blah_anonymous!") anon_user.save() # add a Transcription object self.transcription = Transcription( asset=self.asset, user_id=anon_user.id, text="Test transcription 1", status=Status.EDIT, ) self.transcription.save() tag_name = "Test tag 1" # Act response = self.client.get("/transcribe/Collection1/asset/Asset1/") self.assertEqual(response.status_code, 200) hash_ = re.findall(r'value="([0-9a-f]+)"', str(response.content))[0] captcha_response = CaptchaStore.objects.get(hashkey=hash_).response response = self.client.post( "/transcribe/Collection1/asset/Asset1/", { "tx": "First Test Transcription 1", "tags": tag_name, "action": "Save", "captcha_0": hash_, "captcha_1": captcha_response }, ) # Assert self.assertEqual(response.status_code, 302) self.assertEqual(response.url, "/transcribe/Collection1/asset/Asset1/") # Verify the new transcription in the db transcription = Transcription.objects.filter( text="First Test Transcription 1", asset=self.asset) self.assertEqual(len(transcription), 1) tags = UserAssetTagCollection.objects.filter(asset=self.asset, user_id=anon_user.id) # Tag is not in db, as anonymous user can't tag self.assertEqual(len(tags), 0)
def test_ConcordiaAssetView_post_contact_community_manager(self): """ This unit test test the POST route /transcribe/<collection>/asset/<Asset_name>/ for an anonymous user. Clicking the contact community manager button should redirect to the contact us page. :return: """ # Arrange # create a collection self.collection = Collection( title="TestCollection", slug="Collection1", description="Collection Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection.save() # create an Asset self.asset = Asset( title="TestAsset", slug="Asset1", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset.save() # create anonymous user anon_user = User.objects.create(username="******", email="*****@*****.**") anon_user.set_password("blah_anonymous!") anon_user.save() # add a Transcription object self.transcription = Transcription( asset=self.asset, user_id=anon_user.id, text="Test transcription 1", status=Status.EDIT, ) self.transcription.save() tag_name = "Test tag 1" # Act response = self.client.get("/transcribe/Collection1/asset/Asset1/") self.assertEqual(response.status_code, 200) response = self.client.post( "/transcribe/Collection1/asset/Asset1/", { "tx": "", "tags": "", "action": "Contact Manager" }, ) # Assert self.assertEqual(response.status_code, 302) self.assertEqual(response.url, "/contact/?pre_populate=true")
def test_ConcordiaAssetView_post(self): """ This unit test test the POST route /transcribe/<collection>/asset/<Asset_name>/ :return: """ # Arrange self.login_user() # create a collection self.collection = Collection( title="TestCollection", slug="Collection1", description="Collection Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection.save() # create an Asset self.asset = Asset( title="TestAsset", slug="Asset1", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset.save() # add a Transcription object self.transcription = Transcription( asset=self.asset, user_id=self.user.id, text="Test transcription 1", status=Status.EDIT, ) self.transcription.save() tag_name = "Test tag 1" # Act response = self.client.post( "/transcribe/Collection1/asset/Asset1/", { "tx": "First Test Transcription", "tags": tag_name, "action": "Save" }, ) # Assert self.assertEqual(response.status_code, 302) self.assertEqual(response.url, "/transcribe/Collection1/asset/Asset1/") # Verify the new transcription and tag are in the db transcription = Transcription.objects.filter( text="First Test Transcription", asset=self.asset) self.assertEqual(len(transcription), 1) tags = UserAssetTagCollection.objects.filter(asset=self.asset, user_id=self.user.id) if tags: separate_tags = tags[0].tags.all() self.assertEqual(len(tags), 1) self.assertEqual(separate_tags[0].name, tag_name)
def test_DeleteAsset_get(self, mock_requests): """ Test GET route /transcribe/delete/asset/<slug-value>/ (asset) :return: """ # Arrange mock_requests.get.return_value.status_code = 200 mock_requests.get.return_value.json.return_value = { "concordia_data": "abc123456" } # add an item to Collection self.collection = Collection( title="TextCollection", slug="test-collection-slug", description="Collection Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection.save() self.asset = Asset( title="TestAsset", slug="test-asset-slug", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset.save() self.asset = Asset( title="TestAsset1", slug="test-asset-slug1", description="Asset Description1", media_url="http://www.foo1.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val1"}, status=Status.EDIT, ) self.asset.save() # Act response = self.client.get( "/transcribe/test-collection-slug/delete/asset/test-asset-slug1/", follow=True) # Assert self.assertEqual(response.status_code, 200) collection2 = Collection.objects.get(slug="test-collection-slug") all_assets = Asset.objects.filter(collection=collection2) hided_assets = Asset.objects.filter(collection=collection2, status=Status.INACTIVE) self.assertEqual(len(all_assets), 2) self.assertEqual(len(hided_assets), 1)
def test_ExportCollectionToBagit_get(self): """ Test the http GET on route /transcribe/exportBagit/<collectionname>/ :return: """ # Arrange self.login_user() ## Build test data for local storage collection ## # Collection Info (local storage) locstor_media_url_str = "/locstorcollection/testasset/asset.jpg" locstor_collection_name_str = "locstorcollection" locstor_asset_folder_name_str = "testasset" locstor_asset_name_str = "asset.jpg" # create a collection (local Storage) self.collection1 = Collection( title="LocStorCollection", slug=locstor_collection_name_str, description="Collection Description", metadata={"key": "val1"}, is_active=True, s3_storage=False, status=Status.EDIT, ) self.collection1.save() # create an Asset (local Storage) self.asset1 = Asset( title="TestAsset", slug=locstor_asset_folder_name_str, description="Asset Description", media_url=locstor_media_url_str, media_type=MediaType.IMAGE, collection=self.collection1, sequence=0, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset1.save() # add a Transcription object self.transcription1 = Transcription( asset=self.asset1, user_id=self.user.id, status=Status.EDIT, text="Sample" ) self.transcription1.save() ## Build test data for S3 Storage Collection ## # Collection Info (S3 storage) s3_media_url_str = "https://s3.us-east-2.amazonaws.com/chc-collections/test_s3/mss859430177/0.jpg" s3_collection_name_str = "test_s3" s3_asset_folder_name_str = "testasset" s3_asset_name_str = "asset.jpg" # create a collection (local Storage) self.collection2 = Collection( title="Test S3", slug=s3_collection_name_str, description="Collection Description", metadata={"key": "val1"}, is_active=True, s3_storage=True, status=Status.EDIT, ) self.collection2.save() # create an Asset (local Storage) self.asset2 = Asset( title="TestAsset", slug=s3_asset_folder_name_str, description="Asset Description", media_url=s3_media_url_str, media_type=MediaType.IMAGE, collection=self.collection2, sequence=0, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset2.save() # add a Transcription object self.transcription2 = Transcription( asset=self.asset2, user_id=self.user.id, status=Status.EDIT, text="Sample" ) self.transcription2.save() # Make sure correct folders structure exists for Local Storage Collection collection_folder = "{0}/{1}".format(settings.MEDIA_ROOT, locstor_collection_name_str) if not os.path.exists(collection_folder): os.makedirs(collection_folder) item_dir = "{0}/{1}".format(collection_folder, locstor_asset_folder_name_str) if not os.path.exists(item_dir): os.makedirs(item_dir) # create source asset file for Local Storage Collection with open("{0}/{1}".format(item_dir, locstor_asset_name_str), "w+") as csv_file: writer = csv.writer(csv_file) writer.writerow( [ "Collection", "Title", "Description", "MediaUrl", "Transcription", "Tags", ] ) # Act (local storage collection) response = self.client.get("/transcribe/exportBagit/locstorcollection/") # Assert for Local Storage Collection self.assertEqual(response.status_code, 200) self.assertEquals( response.get("Content-Disposition"), "attachment; filename=locstorcollection.zip", ) try: f = io.BytesIO(response.content) zipped_file = zipfile.ZipFile(f, "r") # self.assertIsNone(zipped_file.testzip()) self.assertIn("bagit.txt", zipped_file.namelist()) self.assertIn("bag-info.txt", zipped_file.namelist()) self.assertIn("data/testasset/asset.txt", zipped_file.namelist()) finally: zipped_file.close() f.close() # Act (s3 collection) response2 = self.client.get("/transcribe/exportBagit/test_s3/") # Assert for s3 Collection self.assertEqual(response2.status_code, 200) self.assertEquals( response2.get("Content-Disposition"), "attachment; filename=test_s3.zip", ) try: f = io.BytesIO(response2.content) zipped_file = zipfile.ZipFile(f, "r") self.assertIn("bagit.txt", zipped_file.namelist()) self.assertIn("bag-info.txt", zipped_file.namelist()) self.assertIn("data/mss859430177/0.txt", zipped_file.namelist()) self.assertIn("data/mss859430177/0.jpg", zipped_file.namelist()) finally: zipped_file.close() f.close() # Clean up temp folders try: shutil.rmtree(collection_folder) except Exception as e: pass
def test_ExportCollectionToBagit_get(self): """ Test the http GET on route /transcribe/exportBagit/<collectionname>/ :return: """ media_url_str = "/foocollection/testasset/asset.jpg" collection_name_str = "foocollection" asset_folder_name_str = "testasset" asset_name_str = "asset.jpg" # Arrange self.login_user() # create a collection self.collection = Collection( title="FooCollection", slug=collection_name_str, description="Collection Description", metadata={"key": "val1"}, is_active=True, s3_storage=False, status=Status.EDIT, ) self.collection.save() # create an Asset self.asset = Asset( title="TestAsset", slug=asset_folder_name_str, description="Asset Description", media_url=media_url_str, media_type=MediaType.IMAGE, collection=self.collection, sequence=0, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset.save() # add a Transcription object self.transcription = Transcription(asset=self.asset, user_id=self.user.id, status=Status.EDIT, text="Sample") self.transcription.save() # Make sure correct folders structure exists collection_folder = "{0}/{1}".format(settings.MEDIA_ROOT, collection_name_str) if not os.path.exists(collection_folder): os.makedirs(collection_folder) source_dir = "{0}/{1}".format(collection_folder, asset_folder_name_str) if not os.path.exists(source_dir): os.makedirs(source_dir) # create source asset file with open("{0}/{1}".format(source_dir, asset_name_str), "w+") as csv_file: writer = csv.writer(csv_file) writer.writerow([ "Collection", "Title", "Description", "MediaUrl", "Transcription", "Tags", ]) # Act response = self.client.get("/transcribe/exportBagit/foocollection/") # Assert self.assertEqual(response.status_code, 200) self.assertEquals( response.get("Content-Disposition"), "attachment; filename=foocollection.zip", ) try: f = io.BytesIO(response.content) zipped_file = zipfile.ZipFile(f, "r") # self.assertIsNone(zipped_file.testzip()) self.assertIn("bagit.txt", zipped_file.namelist()) self.assertIn("bag-info.txt", zipped_file.namelist()) self.assertIn("data/testasset/export.csv", zipped_file.namelist()) csv_file = zipped_file.read("data/testasset/export.csv") self.assertEqual( str(csv_file), "b'Collection,Title,Description,MediaUrl,Transcription,Tags\\r\\nFooCollection,TestAsset,Asset Description,{0},,\\r\\n'" .format(media_url_str), # noqa ) finally: zipped_file.close() f.close() # Clean up temp folders try: shutil.rmtree(collection_folder) except Exception as e: pass
def test_GetTags_get(self): """ Test getting the tags for an asset, route /ws/tags/<asset> :return: """ # Arrange self.login_user() # create a second user username = "******" self.user2 = User.objects.create(username=username, email="*****@*****.**") self.user2.set_password("top_secret") self.user2.save() # create a collection self.collection = Collection( title="TextCollection", slug="www.foo.com/slug2", description="Collection Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection.save() # create Assets self.asset = Asset( title="TestAsset", slug="www.foo.com/slug1", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset.save() self.tag1 = Tag(name="Tag1", value="Tag1") self.tag1.save() self.tag2 = Tag(name="Tag2", value="Tag2") self.tag2.save() self.tag3 = Tag(name="Tag3", value="Tag3") self.tag3.save() # Save for User1 self.asset_tag_collection = UserAssetTagCollection( asset=self.asset, user_id=self.user.id) self.asset_tag_collection.save() self.asset_tag_collection.tags.add(self.tag1, self.tag2) # Save for User2 self.asset_tag_collection2 = UserAssetTagCollection( asset=self.asset, user_id=self.user2.id) self.asset_tag_collection2.save() self.asset_tag_collection2.tags.add(self.tag3) # Act response = self.client.get("/ws/tags/%s/" % self.asset.id) json_resp = json.loads(response.content) # Assert self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(json_resp["results"]), 3)
def test_Transcriptions_create_post(self): """ Test creating a transcription. route ws/transcription_create/ :return: """ # Arrange self.login_user() # create a collection self.collection = Collection( title="TextCollection", slug="www.foo.com/slug2", description="Collection Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection.save() # create Assets self.asset = Asset( title="TestAsset", slug="www.foo.com/slug1", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset.save() # Act response = self.client.post( "/ws/transcription_create/", { "asset": self.asset.id, "user_id": self.user.id, "status": Status.EDIT, "text": "T1" }, ) # Assert self.assertEqual(response.status_code, status.HTTP_201_CREATED) # Get all Transcriptions for the asset transcriptions_count = Transcription.objects.filter( asset=self.asset).count() self.assertEqual(transcriptions_count, 1) # Add Another transcription response = self.client.post( "/ws/transcription_create/", { "asset": self.asset.id, "user_id": self.user.id, "status": Status.EDIT, "text": "T2" }, ) # Get all Transcriptions for the asset, should be another one transcriptions_count = Transcription.objects.filter( asset=self.asset).count() self.assertEqual(transcriptions_count, 2)
def test_Transcriptions_latest_get(self): """ Test getting latest transcription for an asset. route ws/transcriptions/asset/ :return: """ # Arrange self.login_user() # create a second user username = "******" self.user2 = User.objects.create(username=username, email="*****@*****.**") self.user2.set_password("top_secret") self.user2.save() # create a collection self.collection = Collection( title="TextCollection", slug="www.foo.com/slug2", description="Collection Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection.save() # create Assets self.asset = Asset( title="TestAsset", slug="www.foo.com/slug1", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset.save() self.asset2 = Asset( title="TestAsset2", slug="www.foo.com/slug2", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset2.save() # add Transcription objects self.transcription = Transcription(asset=self.asset, user_id=self.user.id, status=Status.EDIT, text="T1") self.transcription.save() t2_text = "T2" self.transcription2 = Transcription(asset=self.asset, user_id=self.user2.id, status=Status.EDIT, text=t2_text) self.transcription2.save() # Act response = self.client.get("/ws/transcription/%s/" % self.asset.id) json_resp = json.loads(response.content) # Assert self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(json_resp["text"], t2_text)
def test_get_assets_by_collection_and_slug_no_match(self): """ Test getting an asset by collection and slug using a slug that doesn't exist :return: """ # Arrange self.login_user() # create 2 collections self.collection = Collection( title="TextCollection", slug="slug1", description="Collection Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection.save() self.collection2 = Collection( title="TextCollection2", slug="slug2", description="Collection2 Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection2.save() # Add 2 assets to collection2, 1 asset to collection1 self.asset = Asset( title="TestAsset", slug="Asset1", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection2, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset.save() self.asset2 = Asset( title="TestAsset2", slug="Asset2", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection2, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset2.save() self.asset3 = Asset( title="TestAsset3", slug="Asset3", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset3.save() # Act response = self.client.get("/ws/asset_by_slug/slugxxx/Asset3xxx/") # Assert self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertJSONEqual( force_text(response.content), { "title": "", "slug": "", "description": "", "media_url": "", "media_type": None, "collection": None, "sequence": None, "metadata": None, "subcollection": None, "status": None, })
def test_get_assets_by_collection(self): """ Test getting a list of assets by collection :return: """ # Arrange self.login_user() # create 2 collections self.collection = Collection( title="TextCollection", slug="slug1", description="Collection Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection.save() self.collection2 = Collection( title="TextCollection2", slug="slug2", description="Collection2 Description", metadata={"key": "val1"}, status=Status.EDIT, ) self.collection2.save() # Add 2 assets to collection2, 1 asset to collection1 self.asset = Asset( title="TestAsset", slug="Asset1", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection2, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset.save() self.asset2 = Asset( title="TestAsset2", slug="Asset2", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection2, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset2.save() self.asset3 = Asset( title="TestAsset3", slug="Asset3", description="Asset Description", media_url="http://www.foo.com/1/2/3", media_type=MediaType.IMAGE, collection=self.collection, metadata={"key": "val2"}, status=Status.EDIT, ) self.asset3.save() # Act response = self.client.get("/ws/asset/slug2/") json_resp = json.loads(response.content) # Assert self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(json_resp['results']), 2)