def test_read_config_from_url_invalid_config(self): capture_output = io.StringIO() sys.stderr = capture_output with pytest.raises(SystemExit) as cm: read_config_from_url(self.invalid_config_url) self.assertEqual(cm.type, SystemExit) self.assertEqual(cm.value.code, 1) output_value = "Error reading YAML content due to:" self.assertIn(output_value, capture_output.getvalue())
def test_read_config_from_url_connection_error(self): url = "https://" + fake.hostname() capture_output = io.StringIO() sys.stderr = capture_output with pytest.raises(SystemExit) as cm: read_config_from_url(url) self.assertEqual(cm.type, SystemExit) self.assertEqual(cm.value.code, 1) output_value = f"Cannot open URL: {url}" self.assertIn(output_value, capture_output.getvalue())
def test_read_config_from_url(self): config = read_config_from_url(self.config_url) remote = config["askanna-remote"] self.assertEqual(remote, self.remote_url)
def test_read_config_from_url_fail(self): with pytest.raises(SystemExit) as cm: read_config_from_url(self.config_url_fail) self.assertEqual(cm.type, SystemExit) self.assertEqual(cm.value.code, 1)
def login( self, email: str, password: str, remote_url: str = "", ui_url: str = "", server: str = "default", update_config_file: bool = False, ) -> None: client.config.server.server = server if ui_url: if ui_url[-1] == "/": ui_url = ui_url[:-1] ui_config = read_config_from_url(f"{ui_url}/askanna-config.yml") try: ui_remote_url = ui_config["askanna-remote"] except KeyError as e: raise KeyError( f"The config file on the URL '{ui_url}' did not contain the key {e}" ) if remote_url and remote_url != ui_remote_url: raise ValueError( "The remote URL does not match the remote URL configured on the frontend" ) client.config.server.ui = ui_url remote_url = ui_remote_url if remote_url: self.base_url = remote_url.replace("/v1/", "") client.config.server.remote = self.base_url if self.base_url == "https://beta-api.askanna.eu" and not ui_url: client.config.server.ui = "https://beta.askanna.eu" url = f"{self.base_url}/rest-auth/login/" r = client.post(url, json={ "username": email.strip(), "password": password.strip() }) if r.status_code == 400: raise PostError( "{} - We could not log you in. Please check your credentials.". format(r.status_code)) if r.status_code == 404: raise PostError( "{} - We could not log you in. Please check the url or remote you provided." .format(r.status_code)) if r.status_code != 200: raise PostError("{} - We could not log you in: {}".format( r.status_code, r.reason)) client.config.server.token = str(r.json().get("key")) client.update_session() if update_config_file: client.config.server.save_server_to_config_file()