def test_10_Multipart_POST_then_update_on_EM_IRI(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") deposit_receipt = conn.create( payload="Multipart_POST_then_update_on_EM_IRI", metadata_entry=e, mimetype="text/plain", filename="readme.txt", packaging='http://purl.org/net/sword/package/Binary', col_iri=conn.workspaces[0][1][0].href, in_progress=True) assert deposit_receipt.edit_media != None dr = conn.update( payload="Multipart_POST_then_update_on_EM_IRI -- updated resource", mimetype="text/plain", filename="readthis.txt", packaging="http://purl.org/net/sword/package/Binary", edit_media_iri=deposit_receipt.edit_media) assert dr.code == 204 # empty response
def test_20_Multipart_POST_to_se_iri(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") deposit_receipt = conn.create( payload="Multipart_POST_then_update_on_EM_IRI", metadata_entry=e, mimetype="text/plain", filename="readme.txt", packaging='http://purl.org/net/sword/package/Binary', col_iri=conn.sd.workspaces[0][1][0].href, in_progress=True) assert deposit_receipt.se_iri != None e.add_fields(dcterms_identifier="doi://multipart_update_to_SE_IRI") dr = conn.append( se_iri=deposit_receipt.se_iri, payload= "Multipart_POST_then_appending_file_on_SE_IRI -- updated resource", mimetype="text/plain", filename="readthisextrafile.txt", packaging="http://purl.org/net/sword/package/Binary", metadata_entry=e) print dr.code assert dr.code == 201
def test_15_Metadata_POST_to_sss_w_coliri(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") dr = conn.create(metadata_entry = e, col_iri = conn.sd.workspaces[0][1][0].href, in_progress=True) assert dr.code == 201
def preflight_submission(self): opener = self.get_opener() conn = Connection(self.sword2_sd_url, error_response_raises_exceptions=False, http_impl=UrlLib2Layer(opener)) logger.debug("Retrieving the service document") conn.get_service_document() logger.debug("Retrieved the service document") self.assertIsNotNone(conn.sd) self.assertIsNotNone(conn.sd.workspaces) self.assertNotEqual(len(conn.sd.workspaces), 0) workspace = conn.sd.workspaces[0][1] # we require there to be at least one collection self.assertNotEqual(len(workspace), 0) col = workspace[0] testid = "testid_" + str(uuid.uuid4()) logger.debug("col iri = " + str(col.href)) e = Entry(id=testid, title="test title", dcterms_abstract="test description") print str(e) receipt = conn.create(col_iri=col.href, metadata_entry=e, suggested_identifier=testid) #col.href=http://192.168.2.237/swordv2/silo/test-silo self.assertIsNotNone(receipt) self.assertEquals(receipt.code, 201) return receipt.location
def test_04_advanced_create_resource_with_package(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO, http_impl=http) conn.get_service_document() col = conn.sd.workspaces[0][1][0] with open(PACKAGE) as pkg: receipt = conn.create( col_iri=col.href, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging='http://purl.org/net/sword/package/SimpleZip', in_progress=True, suggested_identifier="abcdefghijklmnop") assert receipt.code == 201 assert receipt.location != None # these last two assertions are contingent on if we actually get a # receipt back from the server (which we might not legitimately get) assert receipt.dom is None or receipt.parsed == True assert receipt.dom is None or receipt.valid == True
def test_41_get_service_document_unauthorised(self): conn = Connection(SSS_URL, user_name="alsdkfjsdz", user_pass="******", http_impl=http) conn.get_service_document() assert conn.sd is None
def test_05_basic_create_resource_with_multipart(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, http_impl=http) conn.get_service_document() col = conn.sd.workspaces[0][1][0] e = Entry(title="Foo", id="asidjasidj", dcterms_abstract="abstract", dcterms_title="my title") with open(PACKAGE) as pkg: receipt = conn.create( col_iri=col.href, metadata_entry=e, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging='http://purl.org/net/sword/package/SimpleZip') assert receipt.code == 201 assert receipt.location != None # these last two assertions are contingent on if we actually get a # receipt back from the server (which we might not legitimately get) assert receipt.dom is None or receipt.parsed == True assert receipt.dom is None or receipt.valid == True
def __init__(self, owner): raise NotImplementedError c = Connection(SD_URI, user_name = owner.username, user_pass=owner.password) c.get_service_document() # pick the first collection within the first workspace: workspace_1_title, workspace_1_collections = c.workspaces[0] collection = workspace_1_collections[0] # upload "package.zip" to this collection as a new (binary) resource: with open("package.zip", "r") as pkg: receipt = c.create(col_iri = collection.href, payload = pkg, mimetype = "application/zip", filename = "package.zip", packaging = 'http://purl.org/net/sword/package/Binary', in_progress = True) # As the deposit isn't yet finished # Add a metadata record to this newly created resource (or 'container') from sword2 import Entry # Entry can be passed keyword parameters to add metadata to the entry (namespace + '_' + tagname) e = Entry(id="atomid", title="atom-title", dcterms_abstract = "Info about the resource....", ) # to add a new namespace: e.register_namespace('skos', 'http://www.w3.org/2004/02/skos/core#') e.add_field("skos_Concept", "...") # Update the metadata entry to the resource: updated_receipt = c.update(metadata_entry = e, dr = receipt, # use the receipt to discover the right URI to use in_progress = False) # finish the deposit
def preflight_submission(self): opener = self.get_opener() conn = Connection(self.sword2_sd_url, error_response_raises_exceptions=False, http_impl=UrlLib2Layer(opener)) logger.debug("Retrieving the service document") conn.get_service_document() logger.debug("Retrieved the service document") self.assertIsNotNone(conn.sd) self.assertIsNotNone(conn.sd.workspaces) self.assertNotEqual(len(conn.sd.workspaces),0) workspace = conn.sd.workspaces[0][1] # we require there to be at least one collection self.assertNotEqual(len(workspace),0) col = workspace[0] testid = "testid_"+str(uuid.uuid4()) logger.debug("col iri = " + str(col.href)) e = Entry(id=testid, title="test title", dcterms_abstract="test description") print str(e) receipt = conn.create(col_iri=col.href, metadata_entry=e, suggested_identifier=testid) #col.href=http://192.168.2.237/swordv2/silo/test-silo self.assertIsNotNone(receipt) self.assertEquals(receipt.code,201) return receipt.location
def test_08_advanced_create_resource_with_entry(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO) conn.get_service_document() col = conn.sd.workspaces[0][1][0] e = Entry(title="An entry only deposit", id="asidjasidj", dcterms_abstract="abstract", dcterms_identifier="http://whatever/") e.register_namespace("oxds", "http://databank.ox.ac.uk/terms/") e.add_field("oxds_whatever", "whatever") receipt = conn.create(col_iri=col.href, metadata_entry=e, in_progress=True, suggested_identifier=str(uuid.uuid4())) assert receipt.code == 201 assert receipt.location != None # these last two assertions are contingent on if we actually get a # receipt back from the server (which we might not legitimately get) assert receipt.dom is None or receipt.parsed == True assert receipt.dom is None or receipt.valid == True
def test_24_get_sword_statement(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") deposit_receipt = conn.create( payload="Multipart_POST_then_update_on_EM_IRI", metadata_entry=e, mimetype="text/plain", filename="readme.txt", packaging='http://purl.org/net/sword/package/Binary', col_iri=conn.sd.workspaces[0][1][0].href, in_progress=True) ss_iri = None for item_dict in deposit_receipt.links[ 'http://purl.org/net/sword/terms/statement']: if item_dict.has_key('type') and item_dict.get( 'type', None) == "application/atom+xml;type=feed": ss_iri = item_dict.get('href') assert ss_iri != None ss = conn.get_atom_sword_statement(ss_iri) assert ss != None assert ss.entries[0].metadata.get('sword_depositedBy') == 'sword'
def test_12_Metadata_POST_to_sss(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") resp = conn.create(metadata_entry = e, workspace='Main Site', collection=conn.sd.workspaces[0][1][0].title, in_progress=True) assert resp != None
def complete_submission(self, edit_uri): opener = self.get_opener() conn = Connection(self.sword2_sd_url, error_response_raises_exceptions=False, http_impl=UrlLib2Layer(opener)) receipt = None try: receipt = conn.get_deposit_receipt(edit_uri) except urllib2.URLError as e: # The sword2 client does not catch network errors like this one, # which indicates that the url couldn't be reached at all # don't do anything about it here - we'll try again in a moment # and then error out appropriately later pass # at this stage we need to ensure that we actually got back a deposit # receipt i = 0 while (receipt is None or receipt.code >= 400) and i < self.retry_limit: err = None if receipt is None: err = "<unable to reach server>" else: err = str(receipt.code) logger.debug( "Attempt to retrieve Entry Document failed with error " + str(err) + " ... trying again in " + str(self.retry_delay) + " seconds") i += 1 time.sleep(self.retry_delay) try: receipt = conn.get_deposit_receipt(edit_uri) except urllib2.URLError as e: # The sword2 client does not catch network errors like this one, # which indicates that the url couldn't be reached at all # just try again up to the retry_limit continue self.assertIsNotNone(receipt) self.assertEquals(receipt.code, 200) # if we get to here we can go ahead with the deposit for real with open(self.zipFileName, "rb") as data: new_receipt = conn.update( dr=receipt, payload=data, mimetype="application/zip", filename=self.dataset_identifier + ".zip", packaging='http://dataflow.ox.ac.uk/package/DataBankBagIt') self.assertIsNotNone(new_receipt) self.assertEquals(new_receipt.code, 204) return
def test_04_init_from_sss_then_get_doc(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******") assert conn.sd_iri == "http://localhost:%s/sd-uri" % PORT_NUMBER assert conn.sd == None # Not asked to get sd doc yet conn.get_service_document() assert conn.sd != None assert conn.sd.parsed == True assert conn.sd.valid == True assert len(conn.sd.workspaces) == 1
def testServiceDocumentAccess(self): opener = self.get_opener() for i in range(10): conn = Connection(self.sword2_sd_url, error_response_raises_exceptions=False, http_impl=UrlLib2Layer(opener)) conn.get_service_document() self.assertIsNotNone(conn.sd, "Service document None (loop %d)"%(i)) self.assertIsNotNone(conn.sd.workspaces, "Service document workspace None (loop %d)"%(i)) self.assertNotEqual(len(conn.sd.workspaces),0, "Service document worksoacxe count %d (loop %d)"%(len(conn.sd.workspaces),i)) return
def test_03_init_then_load_from_string_t_history(self): conn = Connection("http://example.org/service-doc") assert conn.sd_iri == "http://example.org/service-doc" assert conn.sd == None conn.load_service_document(long_service_doc) # Should have made a two client 'transactions', the init and subsequent XML load assert len(conn.history) == 2 assert conn.history[0]['type'] == "init" assert conn.history[1]['type'] == "SD Parse"
def preflight_submission(self, dataset, opener, repository, silo ): logger.debug("Carrying out pre-flight submission") # verify that we can get a service document, and that there # is at least one silo and that we can authenticate if repository.sword2_sd_url is None: raise SwordServiceError("No sword2 service-document URL for repository configuration") # get the service document (for which we must be authenticated) conn = Connection(repository.sword2_sd_url, error_response_raises_exceptions=False, http_impl=UrlLib2Layer(opener)) conn.get_service_document() # we require there to be at least one workspace if conn.sd is None: raise SwordServiceError("did not successfully retrieve a service document") if conn.sd.workspaces is None: raise SwordServiceError("no workspaces defined in service document") if len(conn.sd.workspaces) == 0: raise SwordServiceError("no workspaces defined in service document") workspace = conn.sd.workspaces[0][1] # we require there to be at least one collection if len(workspace) == 0: raise SwordServiceError("no collections defined in workspace") # FIXME: we don't currently have a mechanism to make decisions about # which collection to put stuff in, so we just put stuff in the first # one for the time being col = workspace[0] silohref = repository.homepage + "swordv2/silo/" + silo # assemble the entry ready for deposit, using the basic metadata # FIXME: is there anything further we need to do about the metadata here? e = Entry(id=dataset.identifier, title=dataset.title, dcterms_abstract=dataset.description) # create the item using the metadata-only approach (suppress errors along the way, # we'll check for them below) #receipt = conn.create(col_iri=col.href, metadata_entry=e, suggested_identifier=dataset.identifier) logger.debug( "Deposit is being created" ) receipt = conn.create(col_iri=silohref, metadata_entry=e, suggested_identifier=dataset.identifier) logger.debug( "Deposit created" ) # check for errors if receipt.code >= 400: # this is an error logger.debug("Received error message from server: " + receipt.to_xml()) if receipt.error_href == "http://databank.ox.ac.uk/errors/DatasetConflict": raise SwordSlugRejected() raise SwordDepositError(receipt) logger.debug("Deposit carried out to: " + receipt.location) # return receipt.location return (receipt.alternate,receipt.location)
def test_34_check_metadata_only_state(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW) conn.get_service_document() col = conn.sd.workspaces[0][1][0] e = Entry(title="An entry only deposit", id="asidjasidj", dcterms_abstract="abstract", dcterms_identifier="http://whatever/") receipt = conn.create(col_iri = col.href, metadata_entry = e) statement = conn.get_ore_sword_statement(receipt.ore_statement_iri) assert len(statement.states) == 1 assert statement.states[0][0] == "http://databank.ox.ac.uk/state/EmptyContainer"
def test_06_Simple_POST_to_sss(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) resp = conn.create(payload = "Payload is just a load of text", mimetype = "text/plain", filename = "readme.txt", packaging = 'http://purl.org/net/sword/package/Binary', workspace = 'Main Site', collection = conn.sd.workspaces[0][1][0].title, in_progress=True, metadata_entry=None) assert resp.code == 201
def test_02_init_then_load_from_string(self): conn = Connection("http://example.org/service-doc") assert conn.sd_iri == "http://example.org/service-doc" assert conn.sd == None conn.load_service_document(long_service_doc) assert conn.sd != None assert len(conn.sd.workspaces) == 2 assert len(conn.workspaces) == 2 assert conn.sd.workspaces[0][0] == "Main Site" assert conn.sd.workspaces[1][0] == "Sub-site" assert len(conn.sd.workspaces[1][1]) == 2
def test_02_get_service_document_on_behalf_of(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO) conn.get_service_document() # given that the client is fully functional, testing that the # service document parses and is valid is sufficient. This, obviously, # doesn't test the validation routine itself. assert conn.sd != None assert conn.sd.parsed == True assert conn.sd.valid == True assert len(conn.sd.workspaces) == 1
def test_01_get_service_document(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW) conn.get_service_document() # given that the client is fully functional, testing that the # service document parses and is valid is sufficient. This, obviously, # doesn't test the validation routine itself. assert conn.sd != None assert conn.sd.parsed == True assert conn.sd.valid == True assert len(conn.sd.workspaces) == 1
def test_08_Simple_POST_to_sss_w_coliri(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") resp = conn.create(payload = "Payload is just a load of text", mimetype = "text/plain", filename = "readme.txt", packaging = 'http://purl.org/net/sword/package/Binary', col_iri = conn.sd.workspaces[0][1][0].href, in_progress=True, metadata_entry=None) assert resp.code == 201
def test_07_Multipart_POST_to_sss(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") resp = conn.create(payload = "Multipart payload here", metadata_entry = e, mimetype = "text/plain", filename = "readme.txt", packaging = 'http://purl.org/net/sword/package/Binary', workspace='Main Site', collection=conn.sd.workspaces[0][1][0].title, in_progress=True) assert resp.code == 201
def test_15_Metadata_POST_to_sss_w_coliri(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") dr = conn.create(metadata_entry=e, col_iri=conn.sd.workspaces[0][1][0].href, in_progress=True) assert dr.code == 201
def test_22_Create_deposit_and_delete_deposit(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") deposit_receipt = conn.create(payload = "Multipart_POST_then_update_on_EM_IRI", metadata_entry = e, mimetype = "text/plain", filename = "readme.txt", packaging = 'http://purl.org/net/sword/package/Binary', col_iri = conn.sd.workspaces[0][1][0].href, in_progress=True) assert deposit_receipt.edit != None dr = conn.delete(resource_iri = deposit_receipt.edit) assert dr.code == 204 or dr.code == 200
def complete_submission(self,edit_uri): opener = self.get_opener() conn = Connection(self.sword2_sd_url, error_response_raises_exceptions=False, http_impl=UrlLib2Layer(opener)) receipt = None try: receipt = conn.get_deposit_receipt(edit_uri) except urllib2.URLError as e: # The sword2 client does not catch network errors like this one, # which indicates that the url couldn't be reached at all # don't do anything about it here - we'll try again in a moment # and then error out appropriately later pass # at this stage we need to ensure that we actually got back a deposit # receipt i = 0 while (receipt is None or receipt.code >= 400) and i < self.retry_limit: err = None if receipt is None: err = "<unable to reach server>" else: err = str(receipt.code) logger.debug("Attempt to retrieve Entry Document failed with error " + str(err) + " ... trying again in " + str(self.retry_delay) + " seconds") i += 1 time.sleep(self.retry_delay) try: receipt = conn.get_deposit_receipt(edit_uri) except urllib2.URLError as e: # The sword2 client does not catch network errors like this one, # which indicates that the url couldn't be reached at all # just try again up to the retry_limit continue self.assertIsNotNone(receipt) self.assertEquals(receipt.code,200) # if we get to here we can go ahead with the deposit for real with open( self.zipFileName, "rb") as data: new_receipt = conn.update(dr = receipt, payload=data, mimetype="application/zip", filename=self.dataset_identifier + ".zip", packaging='http://dataflow.ox.ac.uk/package/DataBankBagIt') self.assertIsNotNone(new_receipt) self.assertEquals(new_receipt.code,204) return
def test_31_delete_container(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO, error_response_raises_exceptions=False, http_impl=http) conn.get_service_document() col = conn.sd.workspaces[0][1][0] e = Entry(title="Multipart deposit", id="asidjasidj", dcterms_abstract="abstract", dcterms_identifier="http://whatever/") with open(PACKAGE) as pkg: receipt = conn.create( col_iri=col.href, metadata_entry=e, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging='http://purl.org/net/sword/package/SimpleZip') # ensure that we have a receipt (the server may not give us one # by default) edit_iri = receipt.location receipt = conn.get_deposit_receipt(edit_iri) # delete the container new_receipt = conn.delete_container(dr=receipt) assert new_receipt.code == 204 assert new_receipt.dom is None # the next check is that this 404s appropriately now another_receipt = conn.get_deposit_receipt(edit_iri)
def test_23_Finish_in_progress_deposit(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") deposit_receipt = conn.create(payload = "Multipart_POST_then_update_on_EM_IRI", metadata_entry = e, mimetype = "text/plain", filename = "readme.txt", packaging = 'http://purl.org/net/sword/package/Binary', col_iri = conn.sd.workspaces[0][1][0].href, in_progress=True) assert deposit_receipt.edit != None dr = conn.complete_deposit(se_iri = deposit_receipt.se_iri) print "This will fail until the sss.py SWORD2 server responds properly, rather than with code 201" assert dr.code == 200
def test_12_Metadata_POST_to_sss(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") resp = conn.create(metadata_entry=e, workspace='Main Site', collection=conn.sd.workspaces[0][1][0].title, in_progress=True) assert resp != None
def test_07_basic_create_resource_with_entry(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW) conn.get_service_document() col = conn.sd.workspaces[0][1][0] e = Entry(title="An entry only deposit", id="asidjasidj", dcterms_abstract="abstract", dcterms_identifier="http://whatever/") receipt = conn.create(col_iri = col.href, metadata_entry = e) assert receipt.code == 201 assert receipt.location != None # these last two assertions are contingent on if we actually get a # receipt back from the server (which we might not legitimately get) assert receipt.dom is None or receipt.parsed == True assert receipt.dom is None or receipt.valid == True
def test_06_Simple_POST_to_sss(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) resp = conn.create( payload="Payload is just a load of text", mimetype="text/plain", filename="readme.txt", packaging='http://purl.org/net/sword/package/Binary', workspace='Main Site', collection=conn.sd.workspaces[0][1][0].title, in_progress=True, metadata_entry=None) assert resp.code == 201
def test_09_basic_retrieve_deposit_receipt(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW) conn.get_service_document() col = conn.sd.workspaces[0][1][0] e = Entry(title="An entry only deposit", id="asidjasidj", dcterms_abstract="abstract", dcterms_identifier="http://whatever/") receipt = conn.create(col_iri = col.href, metadata_entry = e) # we're going to work with the location assert receipt.location != None new_receipt = conn.get_deposit_receipt(receipt.location) assert new_receipt.code == 200 assert new_receipt.parsed == True assert new_receipt.valid == True
def test_17_Simple_POST_and_GET(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) col_iri = conn.sd.workspaces[0][1][0].href # pick the first collection dr = conn.create(payload = "Simple_POST_and_GET", mimetype = "text/plain", filename = "readme.txt", packaging = 'http://purl.org/net/sword/package/Binary', col_iri = col_iri, in_progress=True, metadata_entry=None) assert dr.code == 201 # Now to GET that resource with no prescribed for packaging content_object = conn.get_resource(dr.cont_iri) # Can't guarantee that sss.py won't mangle submissions, so can't validate response at this moment assert content_object != None
def test_14_Invalid_Packaging_cached_receipt(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True, honour_receipts=True) col_iri = conn.sd.workspaces[0][1][0].href # pick the first collection dr = conn.create(payload = "Payload is just a load of text", mimetype = "text/plain", filename = "readme.txt", packaging = 'http://purl.org/net/sword/package/Binary', col_iri = col_iri, in_progress=True) # Now to GET that resource with invalid packaging try: content = conn.get_resource(dr.cont_iri, packaging="foofar") assert 1 == 0 # fail except PackagingFormatNotAvailable: # test the 'honour_receipts' flag and cached deposit pass
def test_18_Metadata_POST_to_se_iri(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") deposit_receipt = conn.create(payload = "Multipart_POST_then_update_on_EM_IRI", metadata_entry = e, mimetype = "text/plain", filename = "readme.txt", packaging = 'http://purl.org/net/sword/package/Binary', col_iri = conn.sd.workspaces[0][1][0].href, in_progress=True) assert deposit_receipt.se_iri != None e.add_fields(dcterms_identifier="doi://somerubbish", dcterms_foo="blah blah") dr = conn.append(se_iri = deposit_receipt.se_iri, metadata_entry = e, in_progress=False) assert dr.code == 200
def testServiceDocumentAccess(self): opener = self.get_opener() for i in range(10): conn = Connection(self.sword2_sd_url, error_response_raises_exceptions=False, http_impl=UrlLib2Layer(opener)) conn.get_service_document() self.assertIsNotNone(conn.sd, "Service document None (loop %d)" % (i)) self.assertIsNotNone( conn.sd.workspaces, "Service document workspace None (loop %d)" % (i)) self.assertNotEqual( len(conn.sd.workspaces), 0, "Service document worksoacxe count %d (loop %d)" % (len(conn.sd.workspaces), i)) return
def _check_dataset(self, dataset_submission): retry_counter = 0 exception = None while retry_counter < SwordStatementCheckThread.retry_count: try: # logger.info("Checking state of dataset at " + dataset_submission.remote_url) opener = openers.get_opener(dataset_submission.repository, dataset_submission.submitting_user) conn = Connection(error_response_raises_exceptions=False, http_impl=UrlLib2Layer(opener)) receipt = conn.get_deposit_receipt( dataset_submission.remote_url) statement = conn.get_ore_sword_statement( receipt.ore_statement_iri) for state_uri, state_desc in statement.states: logger.info("Dataset has state URI: " + state_uri) if state_uri in ERROR_STATES: dataset_submission.status = 'error' logger.info( "URI: " + state_uri + " is an error state ... setting 'error' state on submission record" ) break dataset_submission.last_accessed = datetime.datetime.now() dataset_submission.save() time.sleep(SwordStatementCheckThread.throttle) except urllib2.URLError as e: # if we get an exception, try again up to the limit logger.info( "Got error connecting to the server ... retrying " + str(retry_counter + 1) + " of " + str(SwordStatementCheckThread.retry_count)) retry_counter += 1 exception = e time.sleep(SwordStatementCheckThread.retry_delay) continue else: # if we don't get an exception, we're done return # if we don't return from the else statement above, it means the retries # all failed, and we have a problem. Raise the last thrown exception. raise exception
def test_36_error_bad_request(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, error_response_raises_exceptions=False) conn.get_service_document() col = conn.sd.workspaces[0][1][0] with open(PACKAGE) as pkg: receipt = conn.create(col_iri = col.href, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging = 'http://purl.org/net/sword/package/SimpleZip', in_progress = "Invalid", # the API seems to allow this! suggested_identifier = "zyxwvutsrq") assert receipt.code == 400 assert isinstance(receipt, Error_Document) assert receipt.error_href == "http://purl.org/net/sword/error/ErrorBadRequest"
def test_19_advanced_replace_metadata(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO, http_impl=http) conn.get_service_document() col = conn.sd.workspaces[0][1][0] e = Entry(title="An entry only deposit", id="asidjasidj", dcterms_abstract="abstract", dcterms_identifier="http://whatever/") receipt = conn.create(col_iri=col.href, metadata_entry=e) # ensure that we have a receipt (the server may not give us one # by default) receipt = conn.get_deposit_receipt(receipt.location) # now do the replace ne = Entry(title="A metadata update", id="asidjasidj", dcterms_abstract="new abstract", dcterms_identifier="http://elsewhere/") new_receipt = conn.update(dr=receipt, metadata_entry=ne, in_progress=True) assert new_receipt.code == 204 or new_receipt.code == 200 if new_receipt.code == 204: assert new_receipt.dom is None if new_receipt.code == 200: assert new_receipt.parsed == True assert new_receipt.valid == True
def test_33_get_ore_statement(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO, http_impl=http) conn.get_service_document() col = conn.sd.workspaces[0][1][0] e = Entry(title="Multipart deposit", id="asidjasidj", dcterms_abstract="abstract", dcterms_identifier="http://whatever/") with open(PACKAGE) as pkg: receipt = conn.create( col_iri=col.href, metadata_entry=e, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging='http://purl.org/net/sword/package/SimpleZip') # ensure that we have a receipt (the server may not give us one # by default) edit_iri = receipt.location receipt = conn.get_deposit_receipt(edit_iri) assert receipt.ore_statement_iri is not None # get the statement statement = conn.get_ore_sword_statement(receipt.ore_statement_iri) assert isinstance(statement, Ore_Sword_Statement)
def test_17_advanced_replace_file_content(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO, http_impl=http) conn.get_service_document() col = conn.sd.workspaces[0][1][0] with open(PACKAGE) as pkg: receipt = conn.create( col_iri=col.href, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging='http://purl.org/net/sword/package/SimpleZip') # ensure that we have a receipt (the server may not give us one # by default) receipt = conn.get_deposit_receipt(receipt.location) # now do the replace with open(PACKAGE) as pkg: new_receipt = conn.update( dr=receipt, payload=pkg, mimetype=PACKAGE_MIME, filename="update.zip", packaging='http://purl.org/net/sword/package/SimpleZip', metadata_relevant=True) assert new_receipt.code == 204 assert new_receipt.dom is None
def test_28_advanced_add_metadata(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO, http_impl=http) conn.get_service_document() col = conn.sd.workspaces[0][1][0] e = Entry(title="Multipart deposit", id="asidjasidj", dcterms_abstract="abstract", dcterms_identifier="http://whatever/") with open(PACKAGE) as pkg: receipt = conn.create( col_iri=col.href, metadata_entry=e, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging='http://purl.org/net/sword/package/SimpleZip') # ensure that we have a receipt (the server may not give us one # by default) receipt = conn.get_deposit_receipt(receipt.location) ne = Entry(title="Multipart deposit", id="asidjasidj", dcterms_identifier="http://another/", dcterms_creator="Me!", dcterms_rights="CC0") new_receipt = conn.append(dr=receipt, metadata_entry=ne, in_progress=True) assert new_receipt.code == 200
def test_26_advanced_add_content_to_resource_package(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO, http_impl=http) conn.get_service_document() col = conn.sd.workspaces[0][1][0] with open(PACKAGE) as pkg: receipt = conn.create( col_iri=col.href, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging='http://purl.org/net/sword/package/SimpleZip') receipt = conn.get_deposit_receipt(receipt.location) with open(PACKAGE) as pkg: new_receipt = conn.add_file_to_resource( receipt.edit_media, pkg, "addition.zip", mimetype=PACKAGE_MIME, packaging="http://purl.org/net/sword/package/SimpleZip", metadata_relevant=True) assert new_receipt.code >= 200 and new_receipt.code < 400 assert new_receipt.location is not None assert new_receipt.location == receipt.edit_media
def test_14_error_retrieve_content_em_iri(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, error_response_raises_exceptions=False, http_impl=http) conn.get_service_document() col = conn.sd.workspaces[0][1][0] with open(PACKAGE) as pkg: receipt = conn.create( col_iri=col.href, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging='http://purl.org/net/sword/package/SimpleZip') # ensure that we have a receipt (the server may not give us one # by default) receipt = conn.get_deposit_receipt(receipt.location) error = 'http://purl.org/net/sword/package/IJustMadeThisUp' response = conn.get_resource(content_iri=receipt.edit_media, packaging=error) assert response.code == 406 assert isinstance(response, Error_Document) assert response.error_href == "http://purl.org/net/sword/error/ErrorContent"
def test_13_advanced_retrieve_content_em_iri(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, http_impl=http) conn.get_service_document() col = conn.sd.workspaces[0][1][0] with open(PACKAGE) as pkg: receipt = conn.create( col_iri=col.href, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging='http://purl.org/net/sword/package/SimpleZip') # ensure that we have a receipt (the server may not give us one # by default) receipt = conn.get_deposit_receipt(receipt.location) packaging = 'http://purl.org/net/sword/package/SimpleZip' if receipt.packaging is not None and len(receipt.packaging) > 0: packaging = receipt.packaging[0] resource = conn.get_resource(content_iri=receipt.edit_media, packaging=packaging, on_behalf_of=SSS_OBO) assert resource.code == 200 assert resource.content is not None
def test_15_retrieve_content_em_iri_as_feed(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, http_impl=http) conn.get_service_document() col = conn.sd.workspaces[0][1][0] with open(PACKAGE) as pkg: receipt = conn.create( col_iri=col.href, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging='http://purl.org/net/sword/package/SimpleZip') # ensure that we have a receipt (the server may not give us one # by default) receipt = conn.get_deposit_receipt(receipt.location) # we're going to work with the edit_media_feed iri assert receipt.edit_media_feed is not None response = conn.get_resource(content_iri=receipt.edit_media_feed) assert response.code == 200 assert response.content is not None # the response should be an xml document, so let's see if we can parse # it. This should give us an exception which will fail the test if not dom = etree.fromstring(response.content)
def test_22_delete_content(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO, http_impl=http) conn.get_service_document() col = conn.sd.workspaces[0][1][0] e = Entry(title="Multipart deposit", id="asidjasidj", dcterms_abstract="abstract", dcterms_identifier="http://whatever/") with open(PACKAGE) as pkg: receipt = conn.create( col_iri=col.href, metadata_entry=e, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging='http://purl.org/net/sword/package/SimpleZip') # ensure that we have a receipt (the server may not give us one # by default) receipt = conn.get_deposit_receipt(receipt.location) # now delete the content but not the container new_receipt = conn.delete_content_of_resource(dr=receipt) assert new_receipt.code == 204 assert new_receipt.dom is None
def test_31_delete_container(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO, error_response_raises_exceptions=False) conn.get_service_document() col = conn.sd.workspaces[0][1][0] #e = Entry(title="Multipart deposit", id="asidjasidj", dcterms_abstract="abstract", dcterms_identifier="http://whatever/") with open(PACKAGE) as pkg: receipt = conn.create(col_iri = col.href, #metadata_entry = e, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging = 'http://purl.org/net/sword/package/SimpleZip') # ensure that we have a receipt (the server may not give us one # by default) edit_iri = receipt.location receipt = conn.get_deposit_receipt(edit_iri) # delete the container new_receipt = conn.delete_container(dr=receipt) assert new_receipt.code == 204 assert new_receipt.dom is None # the next check is that this 404s appropriately now another_receipt = conn.get_deposit_receipt(edit_iri)
def test_24_get_sword_statement(self): conn = Connection("http://localhost:%s/sd-uri" % PORT_NUMBER, user_name="sword", user_pass="******", download_service_document=True) e = Entry(title="Foo", id="asidjasidj", dcterms_appendix="blah blah", dcterms_title="foo bar") deposit_receipt = conn.create(payload = "Multipart_POST_then_update_on_EM_IRI", metadata_entry = e, mimetype = "text/plain", filename = "readme.txt", packaging = 'http://purl.org/net/sword/package/Binary', col_iri = conn.sd.workspaces[0][1][0].href, in_progress=True) ss_iri = None for item_dict in deposit_receipt.links['http://purl.org/net/sword/terms/statement']: if item_dict.has_key('type') and item_dict.get('type', None) == "application/atom+xml;type=feed": ss_iri = item_dict.get('href') assert ss_iri != None ss = conn.get_atom_sword_statement(ss_iri) assert ss != None assert ss.resources[0].deposited_by == 'sword'
def test_37_error_target_owner_unknown(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, error_response_raises_exceptions=False) conn.get_service_document() col = conn.sd.workspaces[0][1][0] with open(PACKAGE) as pkg: receipt = conn.create(col_iri = col.href, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging = 'http://purl.org/net/sword/package/SimpleZip', in_progress = True, suggested_identifier = "zyxwvutsrq", on_behalf_of="richard") # we expressly set the wrong obo on the request rather than the connection assert receipt.code == 403 assert isinstance(receipt, Error_Document) assert receipt.error_href == "http://purl.org/net/sword/error/TargetOwnerUnknown"
def test_35_error_checksum_mismatch(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, error_response_raises_exceptions=False) conn.get_service_document() col = conn.sd.workspaces[0][1][0] with open(PACKAGE) as pkg: receipt = conn.create(col_iri = col.href, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging = 'http://purl.org/net/sword/package/SimpleZip', in_progress = True, suggested_identifier = "zyxwvutsrq", md5sum="123456789") assert receipt.code == 412 assert isinstance(receipt, Error_Document) assert receipt.error_href == "http://purl.org/net/sword/error/ErrorChecksumMismatch"
def test_03_basic_create_resource_with_package(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW) conn.get_service_document() col = conn.sd.workspaces[0][1][0] with open(PACKAGE) as pkg: receipt = conn.create(col_iri = col.href, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging = 'http://purl.org/net/sword/package/SimpleZip') assert receipt.code == 201 assert receipt.location != None # these last two assertions are contingent on if we actually get a # receipt back from the server (which we might not legitimately get) assert receipt.dom is None or receipt.parsed == True assert receipt.dom is None or receipt.valid == True
def get_silos(self, opener, repository): logger.debug("Carrying out get_silos for a repository selected") # verify that we can get a service document, and that there # is at least one silo and that we can authenticate if repository.sword2_sd_url is None: logger.debug("No sword2 service-document URL for repository configuration : ") logger.debug(repr(repository.sword2_sd_url)) raise SwordServiceError("No sword2 service-document URL for repository configuration") logger.debug("formulating a connection object for sword repo with url : " + repository.sword2_sd_url ) # get the service document (for which we must be authenticated) conn = Connection(repository.sword2_sd_url, error_response_raises_exceptions=False, http_impl=UrlLib2Layer(opener)) logger.debug("before getting service document : ") conn.get_service_document() logger.debug("Got the service document") # we require there to be at least one workspace if conn.sd is None: logger.debug("did not successfully retrieve a service document") raise SwordServiceError("did not successfully retrieve a service document") if conn.sd.workspaces is None: logger.debug("no workspaces defined in service document") raise SwordServiceError("no workspaces defined in service document") if len(conn.sd.workspaces) == 0: logger.debug("no workspaces defined in service document") raise SwordServiceError("no workspaces defined in service document") workspace = conn.sd.workspaces[0][1] # we require there to be at least one collection if len(workspace) == 0: logger.debug("no collections defined in workspace") raise SwordServiceError("no collections defined in workspace") # FIXME: we don't currently have a mechanism to make decisions about # which collection to put stuff in, so we just put stuff in the first # one for the time being #col = workspace[0] logger.debug("Finished with get_silos for a repository selected") return workspace
def test_09_basic_retrieve_deposit_receipt(self): conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW) conn.get_service_document() col = conn.sd.workspaces[0][1][0] with open(PACKAGE) as pkg: receipt = conn.create(col_iri = col.href, payload=pkg, mimetype=PACKAGE_MIME, filename="example.zip", packaging = 'http://purl.org/net/sword/package/SimpleZip') # we're going to work with the location assert receipt.location != None new_receipt = conn.get_deposit_receipt(receipt.location) assert new_receipt.code == 200 assert new_receipt.parsed == True assert new_receipt.valid == True