def test_no_response(self, mock_put): mock_put.return_value = None emp_dep = EmpiarDepositor("ABC123", self.json_path, "") c = emp_dep.redeposit() self.assertEqual(c, 1)
def test_no_response(self, mock_post): mock_post.return_value = None emp_dep = EmpiarDepositor("ABC123", self.json_path, "", entry_id=1) c = emp_dep.grant_rights() self.assertEqual(c, 1)
def test_no_response_token_auth(self, mock_post): mock_post.return_value = None emp_dep = EmpiarDepositor("ABC123", self.json_path, "") c = emp_dep.make_request(requests.post) self.assertEqual(c, None)
def test_successful_deposition(self, mock_post): mock_post.return_value = Mock(ok=True, spec=Response) mock_post.return_value.json.return_value = {'deposition': True, 'directory': 'DIR', 'entry_id': 1} empDep = EmpiarDepositor("ABC123", "tests/deposition_json/working_example.json", "", "") c = empDep.create_new_deposition() self.assertEqual(c, 0)
def test_no_response(self, mock_post): mock_post.return_value = None empDep = EmpiarDepositor("ABC123", "tests/deposition_json/working_example.json", "", "") with self.assertRaises(SystemExit) as cm: c = empDep.create_new_deposition() self.assertEqual(cm.exception.args[0], 1)
def test_failed_upload(self, mock_popen): mock_popen.return_value.stdout.readline.return_value = '' mock_popen.return_value.returncode = 1 empDep = EmpiarDepositor("ABC123", "tests/deposition_json/working_example.json", "", "", entry_id=1, entry_directory='DIR') with self.assertRaises(SystemExit) as cm: c = empDep.aspera_upload() self.assertEqual(cm.exception.args[0], 1)
def test_non_int_entry_id(self, mock_post): mock_post.return_value = Mock(ok=True, spec=Response) mock_post.return_value.json.return_value = {'deposition': True, 'directory': 'DIR', 'entry_id': 'ID'} empDep = EmpiarDepositor("ABC123", "tests/deposition_json/working_example.json", "", "") with self.assertRaises(SystemExit) as cm: c = empDep.create_new_deposition() self.assertTrue('Error occurred while trying to create an EMPIAR deposition. Returned entry id is not an integer number' in cm.exception.args[0])
def test_unauthorized(self, mock_post): mock_post.return_value = Mock(ok=True, spec=Response) mock_post.return_value.status_code = 401 mock_post.return_value.json.return_value = {'detail': 'Invalid token.'} empDep = EmpiarDepositor("ABC123", "tests/deposition_json/working_example.json", "", "") with self.assertRaises(SystemExit) as cm: c = empDep.create_new_deposition() self.assertTrue('The creation of an EMPIAR deposition was not successful. Returned response:' in cm.exception.args[0] and 'Status code: 401' in cm.exception.args[0])
def test_unauthorized_return(self, mock_post): mock_post = mock_response(mock_post, status_code=401, headers={'content-type': 'application/json'}, json={'detail': 'Invalid token.'}) emp_dep = EmpiarDepositor("ABC123", self.json_path, "") c = emp_dep.create_new_deposition() self.assertEqual(c, 1)
def test_no_response(self, mock_post): mock_post.return_value = None emp_dep = EmpiarDepositor("ABC123", self.json_path, "", entry_thumbnail=self.thumbnail_path) c = emp_dep.thumbnail_upload() self.assertEqual(c, 1)
def test_successful_upload(self, mock_post): mock_post = mock_response( mock_post, headers={'content-type': 'application/json'}, json={'submission': True, 'empiar_id': 'EMPIAR-10001'} ) emp_dep = EmpiarDepositor("ABC123", self.json_path, "") c = emp_dep.submit_deposition() self.assertEqual(c, 0)
def test_failed_upload(self, mock_popen): mock_popen.return_value.stdout.readline.return_value = b'' mock_popen.return_value.returncode = 1 emp_dep = EmpiarDepositor("ABC123", self.json_path, "", "ascp", entry_id=1, entry_directory='DIR') c = emp_dep.aspera_upload() self.assertEqual(c, 1)
def test_non_int_entry_id_return(self, mock_put): mock_put = mock_response( mock_put, headers={'content-type': 'application/json'}, json={'deposition': True, 'directory': 'DIR', 'entry_id': 'ID'} ) emp_dep = EmpiarDepositor("ABC123", self.json_path, "") c = emp_dep.redeposit() self.assertEqual(c, 1)
def test_no_permission_return(self, mock_put): mock_put = mock_response( mock_put, status_code=403, headers={'content-type': 'application/json'}, json={'detail': 'You do not have permission to perform this action.'} ) emp_dep = EmpiarDepositor("ABC123", self.json_path, "") c = emp_dep.redeposit() self.assertEqual(c, 1)
def test_no_rights_granting_input_return(self, mock_post): mock_post = mock_response( mock_post, status_code=403, headers={'content-type': 'application/json'}, json={'detail': 'You do not have permission to perform this action.'} ) emp_dep = EmpiarDepositor("ABC123", self.json_path, "", entry_id=1) c = emp_dep.grant_rights() self.assertEqual(c, 1)
def test_successful_deposition(self, mock_post): mock_post = mock_response(mock_post, headers={'content-type': 'application/json'}, json={ 'deposition': True, 'directory': 'DIR', 'entry_id': 1 }) emp_dep = EmpiarDepositor("ABC123", self.json_path, "") c = emp_dep.create_new_deposition() self.assertEqual(c, 0)
def test_successful_upload(self, mock_post): mock_post = mock_response(mock_post, status_code=200, headers={'content-type': 'application/json'}, json={'thumbnail_upload': True}) emp_dep = EmpiarDepositor("ABC123", self.json_path, "", entry_thumbnail=self.thumbnail_path) c = emp_dep.thumbnail_upload() self.assertEqual(c, 0)
def test_no_response(self, mock_post): mock_post.return_value = None empDep = EmpiarDepositor( "ABC123", "tests/deposition_json/working_example.json", "", "", entry_thumbnail="tests/img/entry_thumbnail.gif") with self.assertRaises(SystemExit) as cm: c = empDep.thumbnail_upload() self.assertEqual(cm.exception.args[0], 1)
def test_successful_upload(self, mock_post): mock_post.return_value = Mock(ok=True, spec=Response) mock_post.return_value.json.return_value = { 'submission': True, 'empiar_id': 'EMPIAR-10001' } empDep = EmpiarDepositor("ABC123", "tests/deposition_json/working_example.json", "", "") c = empDep.submit_deposition() self.assertEqual(c, 0)
def test_successful_upload(self, mock_post): mock_post.return_value = Mock(ok=True, spec=Response) mock_post.return_value.json.return_value = {'thumbnail_upload': True} empDep = EmpiarDepositor( "ABC123", "tests/deposition_json/working_example.json", "", "", entry_thumbnail="tests/img/entry_thumbnail.gif") c = empDep.thumbnail_upload() self.assertEqual(c, 0)
def test_failed_init_return(self, mock_popen): mock_popen.return_value.communicate.return_value = (None, None) mock_popen.return_value.returncode = 1 emp_dep = EmpiarDepositor("ABC123", self.json_path, "globus_obj", "", "globusid", { "is_dir": False, "obj_name": "globus_obj" }, entry_id=1, entry_directory="entry_dir") c = emp_dep.globus_upload() self.assertEqual(c, 1)
def test_no_permission(self, mock_post): mock_post.return_value = Mock(ok=True, spec=Response) mock_post.return_value.status_code = 403 mock_post.return_value.json.return_value = { 'detail': 'You do not have permission to perform this action.' } empDep = EmpiarDepositor("ABC123", "tests/deposition_json/working_example.json", "", "") with self.assertRaises(SystemExit) as cm: c = empDep.redeposit() self.assertTrue( 'The update of an EMPIAR deposition was not successful. Returned response:' in cm.exception.args[0] and 'Status code: 403' in cm.exception.args[0])
def test_failed_wait_upload(self, mock_popen): mock_popen.return_value.communicate.return_value = ("Waiting", None) mock_popen.return_value.stdout.readline.return_value = b'' mock_popen.return_value.returncode = 1 task_id = "Task123" c = EmpiarDepositor.globus_upload_wait(task_id) self.assertEqual(c, 1)
def test_invalid_json_stdout(self, mock_popen): mock_popen.return_value.communicate.return_value = ( b'The transfer has been accepted and a task has been ' b'created and queued for execution. Task ID: 123', None) mock_popen.return_value.returncode = 0 emp_dep = EmpiarDepositor("ABC123", self.json_path, "globus_obj", "", "globusid", { "is_dir": False, "obj_name": "globus_obj" }, entry_id=1, entry_directory="entry_dir") c = emp_dep.globus_upload() self.assertEqual(c, 1)
def test_successful_rights_granting(self, mock_post): mock_post = mock_response( mock_post, status_code=200, headers={'content-type': 'application/json'}, json={ 'test1': True,'test2': True, '*****@*****.**': True, '*****@*****.**': True, '0000-0000-0000-0000': True, '0000-0000-0000-0001': True } ) emp_dep = EmpiarDepositor("ABC123", self.json_path, "", entry_id = 1, grant_rights_usernames=self.grant_rights_usernames, grant_rights_emails=self.grant_rights_emails, grant_rights_orcids=self.grant_rights_orcids ) c = emp_dep.grant_rights() self.assertEqual(c, 0)
def test_unauthorized_stdout(self, mock_post): mock_post = mock_response(mock_post, status_code=401, headers={'content-type': 'application/json'}, json={'detail': 'Invalid token.'}) emp_dep = EmpiarDepositor("ABC123", self.json_path, "") with capture(emp_dep.create_new_deposition) as output: self.assertTrue( 'The creation of an EMPIAR deposition was not successful. Returned response:' in output and 'Status code: 401' in output)
def test_non_int_entry_id_stdout(self, mock_put): mock_put = mock_response( mock_put, headers={'content-type': 'application/json'}, json={'deposition': True, 'directory': 'DIR', 'entry_id': 'ID'} ) emp_dep = EmpiarDepositor("ABC123", self.json_path, "") with capture(emp_dep.redeposit) as output: self.assertTrue('Error occurred while trying to update an EMPIAR deposition. Returned entry id is not an ' 'integer number' in output)
def test_no_rights_granting_input_stdout(self, mock_post): mock_post = mock_response( mock_post, status_code=403, headers={'content-type': 'application/json'}, json={'detail': 'You do not have permission to perform this action.'} ) emp_dep = EmpiarDepositor("ABC123", self.json_path, "", entry_id=1) with capture(emp_dep.grant_rights) as output: self.assertTrue('The granting rights for EMPIAR deposition was not successful.' in output)
def test_no_permission_stdout(self, mock_put): mock_put = mock_response( mock_put, status_code=403, headers={'content-type': 'application/json'}, json={'detail': 'You do not have permission to perform this action.'} ) emp_dep = EmpiarDepositor("ABC123", self.json_path, "") with capture(emp_dep.redeposit) as output: self.assertTrue('The update of an EMPIAR deposition was not successful. Returned response:' in output and 'Status code: 403' in output)
def test_no_permission_stdout(self, mock_post): mock_post = mock_response( mock_post, status_code=403, headers={'content-type': 'application/json'}, json={'detail': 'You do not have permission to perform this action.'} ) emp_dep = EmpiarDepositor("ABC123", self.json_path, "", entry_id=1, grant_rights_usernames=self.grant_rights_usernames) with capture(emp_dep.grant_rights) as output: self.assertTrue('You do not have permission to perform this action.' in output and 'Status code: 403' in output)