def test_cli_submit_sample_url(monkeypatch): mock = MockedResponse(ok=True, json=successful_submission) monkeypatch.setattr("requests.sessions.Session.post", mock) jbxapi.cli(["submit", "--sample-url", "https://example.net/sample"]) assert mock.requests[0].data["sample-url"] == "https://example.net/sample"
def test_cli_submit_url(monkeypatch): mock = MockedResponse(ok=True, json={"data": {"webids": ["1", "2"]}}) monkeypatch.setattr("requests.sessions.Session.post", mock) jbxapi.cli(["submit", "--url", "https://example.net"]) assert mock.requests[0].data["url"] == "https://example.net"
def test_cli_submit_file(monkeypatch): mock = MockedResponse(ok=True, json=successful_submission) monkeypatch.setattr("requests.sessions.Session.post", mock) with tempfile.NamedTemporaryFile(delete=False) as temp: temp.write(b'Some data') try: jbxapi.cli(["submit", temp.name]) finally: os.remove(temp.name) assert "chunked-sample" in mock.requests[0].data
def test_cli_submit_sample_with_cookbook(monkeypatch): mock = MockedResponse(ok=True, json=successful_submission) monkeypatch.setattr("requests.sessions.Session.post", mock) with tempfile.NamedTemporaryFile(delete=False) as temp1: temp1.write(b'Some data') with tempfile.NamedTemporaryFile(delete=False) as temp2: temp2.write(b'Some data') jbxapi.cli(["submit", "--cookbook", temp1.name, temp2.name]) assert "cookbook" in mock.requests[0].files assert "chunked-sample" in mock.requests[0].data
def test_cli_common_params_position(monkeypatch): mock = MockedResponse(ok=True, json={"data": "ok"}) monkeypatch.setattr("requests.sessions.Session.post", mock) # command at the start jbxapi.cli([ "analysis", "list", "--apikey", "1234", "--apiurl", "http://example.net", "--accept-tac" ]) # command at the end jbxapi.cli([ "--apikey", "1234", "--apiurl", "http://example.net", "--accept-tac", "analysis", "list" ])
def test_cli_no_check_certificate(monkeypatch): response = MockedResponse(ok=True, json=successful_submission) def request(self, *args, **kwargs): # self is the session response.verify_ssl = self.verify return MockedResponse(ok=True, json=successful_submission) monkeypatch.setattr("requests.sessions.Session.request", request) jbxapi.cli(["submit", "--url", "https://example.net"]) assert response.verify_ssl is True jbxapi.cli( ["submit", "--url", "https://example.net", "--no-check-certificate"]) assert response.verify_ssl is False
def test_cli_submit_dir(monkeypatch): mock = MockedResponse(ok=True, json=successful_submission) monkeypatch.setattr("requests.sessions.Session.post", mock) sample_dir = tempfile.mkdtemp() with tempfile.NamedTemporaryFile(dir=sample_dir, delete=False) as temp: temp.write(b'Some data') with tempfile.NamedTemporaryFile(dir=sample_dir, delete=False) as temp: temp.write(b'Some other data') try: jbxapi.cli(["submit", sample_dir]) finally: shutil.rmtree(sample_dir) assert "chunked-sample" in mock.requests[0].data print(mock.requests[0].files)
def test_cli_api_url_input_methods(monkeypatch): mock = MockedResponse(ok=True, json=successful_submission) monkeypatch.setattr("requests.sessions.Session.post", mock) monkeypatch.setattr("jbxapi.API_URL", "from_script") jbxapi.cli(["submit", "--url", "https://example.net"]) assert mock.requests[-1].url.startswith("from_script") monkeypatch.setenv("JBX_API_URL", "from_env") jbxapi.cli(["submit", "--url", "https://example.net"]) assert mock.requests[-1].url.startswith("from_env") jbxapi.cli( ["submit", "--url", "https://example.net", "--apiurl", "from_arg"]) assert mock.requests[-1].url.startswith("from_arg")
def test_cli_accept_tac_input_methods(monkeypatch): mock = MockedResponse(ok=True, json=successful_submission) monkeypatch.setattr("requests.sessions.Session.post", mock) # The test alternates between True and False to test the # order of precedence of the options monkeypatch.setattr("jbxapi.ACCEPT_TAC", True) jbxapi.cli(["submit", "--url", "https://example.net"]) assert mock.requests[-1].data["accept-tac"] == "1" monkeypatch.setenv("JBX_ACCEPT_TAC", "0") jbxapi.cli(["submit", "--url", "https://example.net"]) assert mock.requests[-1].data["accept-tac"] == "0" monkeypatch.setenv("JBX_ACCEPT_TAC", "1") jbxapi.cli(["submit", "--url", "https://example.net"]) assert mock.requests[-1].data["accept-tac"] == "1" # disable it again monkeypatch.setenv("JBX_ACCEPT_TAC", "0") jbxapi.cli(["submit", "--url", "https://example.net", "--accept-tac"]) assert mock.requests[-1].data["accept-tac"] == "1"