def test_search_connections_negative(*mocks):
    """Test list localConnections negative response."""
    app = create_app()

    response = app.get("api/connection/query")
    assert response.status_code == 400

    result = json.loads(response.get_data(as_text=True))
    expected_result = "Failed to search items."
    assert result["detail"] == expected_result
Beispiel #2
0
def test_js():
    """Test that the home-page exits."""
    app = create_app()

    # sends HTTP GET request to the application
    # on the specified path
    result = app.get("/static/js/home.js")

    # assert the status code of the response
    assert result.status_code == 200
Beispiel #3
0
def test_list_connections(*mocks):
    """Test list localConnections."""
    app = create_app()

    response = app.get("api/agent/agent_name/connection")
    assert response.status_code == 200

    result = json.loads(response.get_data(as_text=True))
    expected_result = [{"name": "some-connection"}]
    assert result == expected_result
def test_create_agent_fail(*mocks):
    """Test creating an agent and failing."""
    app = create_app()
    agent_name = "test_agent_id"

    response_create = app.post("api/agent",
                               content_type="application/json",
                               data=json.dumps(agent_name))
    assert response_create.status_code == 400
    data = json.loads(response_create.get_data(as_text=True))
    assert data["detail"] == "Failed to create Agent. Message"
Beispiel #5
0
def test_home_page_exits():
    """Test that the home-page exits."""
    app = create_app()

    # sends HTTP GET request to the application
    # on the specified path
    result = app.get("/")

    # assert the status code of the response
    assert result.status_code == 200
    assert "AEA GUI" in str(result.data)
def test_create_agent(*mocks):
    """Test creating an agent."""
    app = create_app()
    agent_name = "test_agent_id"

    # Ensure there is now one agent
    response_create = app.post("api/agent",
                               content_type="application/json",
                               data=json.dumps(agent_name))
    assert response_create.status_code == 201
    data = json.loads(response_create.get_data(as_text=True))
    assert data == agent_name
def test_delete_agent(*mocks):
    """Test creating an agent."""
    app = create_app()
    agent_name = "test_agent_id"

    # Ensure there is now one agent
    response_delete = app.delete("api/agent/" + agent_name,
                                 data=None,
                                 content_type="application/json")
    assert response_delete.status_code == 200
    data = json.loads(response_delete.get_data(as_text=True))
    assert data == "Agent {} deleted".format(agent_name)
def test_delete_agent_fail(*mocks):
    """Test creating an agent and failing."""
    app = create_app()
    agent_name = "test_agent_id"

    response_delete = app.delete("api/agent/" + agent_name,
                                 data=None,
                                 content_type="application/json")
    assert response_delete.status_code == 400
    data = json.loads(response_delete.get_data(as_text=True))
    assert data[
        "detail"] == "Failed to delete Agent {} - it may not exist".format(
            agent_name)
Beispiel #9
0
def test_fetch_agent(*mocks):
    """Test fetch an agent."""
    app = create_app()

    agent_name = "test_agent_name"
    agent_id = "author/{}:0.1.0".format(agent_name)

    # Ensure there is now one agent
    resp = app.post(
        "api/fetch-agent", content_type="application/json", data=json.dumps(agent_id),
    )
    assert resp.status_code == 201
    data = json.loads(resp.get_data(as_text=True))
    assert data == agent_name
Beispiel #10
0
def test_search_connections(*mocks):
    """Test list localConnections."""
    app = create_app()

    response = app.get("api/connection/query")
    assert response.status_code == 200

    result = json.loads(response.get_data(as_text=True))
    expected_result = {
        "item_type": "connection",
        "search_result": [],
        "search_term": "query",
    }
    assert result == expected_result
Beispiel #11
0
def test_fetch_agent_fail(*mocks):
    """Test fetch agent fail."""
    app = create_app()

    agent_name = "test_agent_name"
    agent_id = "author/{}:0.1.0".format(agent_name)

    resp = app.post(
        "api/fetch-agent", content_type="application/json", data=json.dumps(agent_id),
    )
    assert resp.status_code == 400
    data = json.loads(resp.get_data(as_text=True))
    assert data["detail"] == "Failed to fetch an agent {}. {}".format(
        agent_id, "Message"
    )
Beispiel #12
0
def test_remove_item(*mocks):
    """Test remove a skill/connection/protocol.

    Actually we just do connection as code coverage is the same.
    """
    app = create_app()

    agent_name = "test_agent_id"
    connection_name = "fetchai/test_connection:0.1.0"

    response_remove = app.post(
        "api/agent/" + agent_name + "/connection/remove",
        content_type="application/json",
        data=json.dumps(connection_name),
    )
    assert response_remove.status_code == 201
    data = json.loads(response_remove.get_data(as_text=True))
    assert data == agent_name
Beispiel #13
0
def test_real_create_local(*mocks):
    """Really create an agent (have to test the call_aea at some point)."""
    # Set up a temporary current working directory to make agents in
    with TempCWD() as temp_cwd:
        app = create_app()

        # copy the 'packages' directory in the parent of the agent folder.
        shutil.copytree(Path(CUR_PATH, "..", "packages"),
                        Path(temp_cwd.temp_dir, "packages"))

        agent_id = "test_agent_id"

        # Make an agent
        # We do it programmatically as we need to create an agent with default author
        # that was prevented from GUI.
        ctx = Context(cwd=temp_cwd.temp_dir)
        create_aea(ctx, agent_id, local=True, author=DEFAULT_AUTHOR)

        # Give it a bit of time so the polling funcionts get called
        time.sleep(1)

        # Check that we can actually see this agent too
        response_agents = app.get(
            "api/agent",
            data=None,
            content_type="application/json",
        )
        data = json.loads(response_agents.get_data(as_text=True))
        assert response_agents.status_code == 200
        assert len(data) == 1
        assert data[0]["public_id"] == agent_id
        assert data[0]["description"] == "placeholder description"

        # do same but this time find that this is not an agent directory.
        with patch("os.path.isdir", return_value=False):
            response_agents = app.get(
                "api/agent",
                data=None,
                content_type="application/json",
            )
        data = json.loads(response_agents.get_data(as_text=True))
        assert response_agents.status_code == 200
        assert len(data) == 0
Beispiel #14
0
def test_remove_item_fail(*mocks):
    """Test remove a skill/connection/protocol when it fails.

    Actually we just do connection as code coverage is the same.
    """
    app = create_app()

    agent_name = "test_agent_id"
    connection_name = "fetchai/test_connection:0.1.0"

    response_remove = app.post(
        "api/agent/" + agent_name + "/connection/remove",
        content_type="application/json",
        data=json.dumps(connection_name),
    )
    assert response_remove.status_code == 400
    data = json.loads(response_remove.get_data(as_text=True))
    assert data[
        "detail"] == "Failed to remove connection {} from agent {}".format(
            connection_name, agent_name)
Beispiel #15
0
def test_create_and_run_agent():
    """Test for running and agent, reading TTY and errors."""
    # Set up a temporary current working directory in which to make agents
    with TempCWD() as temp_cwd:
        app = create_app()

        # copy the 'packages' directory in the parent of the agent folder.
        shutil.copytree(Path(CUR_PATH, "..", "packages"),
                        Path(temp_cwd.temp_dir, "packages"))

        agent_id = "test_agent"

        # Make an agent
        # We do it programmatically as we need to create an agent with default author
        # that was prevented from GUI.
        ctx = Context(cwd=temp_cwd.temp_dir)
        ctx.set_config("is_local", True)
        create_aea(ctx, agent_id, local=True, author=DEFAULT_AUTHOR)

        # Add the local connection
        with patch("aea.cli_gui.app_context.local", True):
            response_add = app.post(
                "api/agent/" + agent_id + "/connection",
                content_type="application/json",
                data=json.dumps(str(LOCAL_PUBLIC_ID)),
            )
            assert response_add.status_code == 201

        # Get the running status before we have run it
        response_status = app.get(
            "api/agent/" + agent_id + "/run",
            data=None,
            content_type="application/json",
        )
        assert response_status.status_code == 200
        data = json.loads(response_status.get_data(as_text=True))
        assert "NOT_STARTED" in data["status"]

        # run the agent with a non existent connection
        response_run = app.post(
            "api/agent/" + agent_id + "/run",
            content_type="application/json",
            data=json.dumps("author/non-existent-connection:0.1.0"),
        )
        assert response_run.status_code == 400

        # run the agent with default connection - should be something in the error output?
        response_run = app.post(
            "api/agent/" + agent_id + "/run",
            content_type="application/json",
            data=json.dumps(""),
        )
        assert response_run.status_code == 201
        time.sleep(2)

        # Stop the agent running
        response_stop = app.delete(
            "api/agent/" + agent_id + "/run",
            data=None,
            content_type="application/json",
        )
        assert response_stop.status_code == 200
        time.sleep(2)

        # run the agent with stub connection
        response_run = app.post(
            "api/agent/" + agent_id + "/run",
            content_type="application/json",
            data=json.dumps(str(STUB_CONNECTION_PUBLIC_ID)),
        )
        assert response_run.status_code == 201

        time.sleep(2)

        # Try running it again (this should fail)
        response_run = app.post(
            "api/agent/" + agent_id + "/run",
            content_type="application/json",
            data=json.dumps(str(STUB_CONNECTION_PUBLIC_ID)),
        )
        assert response_run.status_code == 400

        # Get the running status
        response_status = app.get(
            "api/agent/" + agent_id + "/run",
            data=None,
            content_type="application/json",
        )
        assert response_status.status_code == 200
        data = json.loads(response_status.get_data(as_text=True))

        assert data["error"] == ""
        assert "RUNNING" in data["status"]
        app.delete(
            "api/agent/" + agent_id + "/run",
            data=None,
            content_type="application/json",
        )
        time.sleep(1)

        # Get the running status
        response_status = app.get(
            "api/agent/" + agent_id + "/run",
            data=None,
            content_type="application/json",
        )
        assert response_status.status_code == 200
        data = json.loads(response_status.get_data(as_text=True))
        assert "process terminate" in data["error"]
        assert "NOT_STARTED" in data["status"]

        # run the agent again (takes a different path through code)
        response_run = app.post(
            "api/agent/" + agent_id + "/run",
            content_type="application/json",
            data=json.dumps(str(STUB_CONNECTION_PUBLIC_ID)),
        )
        assert response_run.status_code == 201

        time.sleep(2)

        # Get the running status
        response_status = app.get(
            "api/agent/" + agent_id + "/run",
            data=None,
            content_type="application/json",
        )
        assert response_status.status_code == 200
        data = json.loads(response_status.get_data(as_text=True))

        assert data["error"] == ""
        assert "RUNNING" in data["status"]

        # Stop the agent running
        response_stop = app.delete(
            "api/agent/" + agent_id + "/run",
            data=None,
            content_type="application/json",
        )
        assert response_stop.status_code == 200
        time.sleep(2)

        # Get the running status
        response_status = app.get(
            "api/agent/" + agent_id + "/run",
            data=None,
            content_type="application/json",
        )
        assert response_status.status_code == 200
        data = json.loads(response_status.get_data(as_text=True))
        assert "process terminate" in data["error"]
        assert "NOT_STARTED" in data["status"]

        # Stop a none existent agent running
        response_stop = app.delete(
            "api/agent/" + agent_id + "_NOT" + "/run",
            data=None,
            content_type="application/json",
        )
        assert response_stop.status_code == 400
        time.sleep(2)

        genuine_func = aea.cli_gui.call_aea_async

        def _dummycall_aea_async(param_list, dir_arg):
            assert param_list[0] == sys.executable
            assert param_list[1] == "-m"
            assert param_list[2] == "aea.cli"
            if param_list[3] == "run":
                return None
            else:
                return genuine_func(param_list, dir_arg)

        # Run when process files (but other call - such as status should not fail)
        with patch("aea.cli_gui.call_aea_async", _dummycall_aea_async):
            response_run = app.post(
                "api/agent/" + agent_id + "/run",
                content_type="application/json",
                data=json.dumps(str(STUB_CONNECTION_PUBLIC_ID)),
            )
        assert response_run.status_code == 400
Beispiel #16
0
 def setUp(self):
     """Set up test case."""
     self.app = create_app()