예제 #1
0
async def test_respond_response_event_not_a_string(opsdroid, mock_api_obj, mock_api):
    await opsdroid.load(config=MINIMAL_CONFIG)
    async with running_opsdroid(opsdroid):
        action = InteractiveAction({"response_url": mock_api_obj.base_url})
        message = Message("this is a test response")
        response = await action.respond(message)
        assert response
예제 #2
0
async def test_receive_issue_comment(opsdroid, connector, mock_api, caplog):
    """Test a comment create event creates a message and parses it."""
    caplog.set_level(logging.INFO)

    @match_event(github_event.IssueCommented)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.target == "opsdroid/opsdroid#237"
        assert event.comment == "hello"
        assert event.issue_title == "test issue, please ignore"
        assert (
            event.comment_url ==
            "https://api.github.com/repos/opsdroid/opsdroid/issues/comments/439318644"
        )
        assert event.user == "jacobtomlinson"
        logging.getLogger(__name__).info("Test skill complete")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("issue_comment.json"),
        )
        assert resp.status == 201
        assert "Test skill complete" in caplog.text
        assert "Exception when running skill" not in caplog.text
예제 #3
0
async def test_end_to_end(connector, opsdroid, mock_api, caplog):
    """Connect, recieve a message, trigger a skill and respond.

    - When opsdroid starts it will connect to gitter
    - When the connector starts listening it will get a message
    - That message should trigger the test_skill
    - The skill will respond and we check that in the logs
    """

    caplog.set_level(logging.INFO)

    @match_regex("hi")
    async def test_skill(opsdroid, config, event):
        assert event.text == "hi"
        logging.getLogger(__name__).info("test_skill called")
        await event.respond("hey there!")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        assert connector.listening
        assert connector.bot_gitter_id == "12345"

        assert "test_skill called" in caplog.text
        assert "Successfully responded." in caplog.text

    assert not connector.listening
예제 #4
0
async def test_mr_generic_event(opsdroid, connector, mock_api, caplog):
    caplog.set_level(logging.INFO)

    @match_event(gitlab_events.GenericMREvent)
    async def test_skill(opsdroid, config, event):
        url = "https://gitlab.com/FabioRosado/test-project/-/merge_requests/2"
        assert event.connector.name == "gitlab"
        assert event.url == url
        assert event.project == "test-project"
        assert event.user == "FabioRosado"
        assert event.title == 'Revert Merge branch "test" into "main"'
        assert event.description == "This reverts merge request !1"
        assert event.labels == []
        logging.getLogger(__name__).info("Test skill complete")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/gitlab",
            "POST",
            headers={
                "X-Gitlab-Token": "secret-stuff!",
                "Content-Type": "application/json",
            },
            data=get_webhook_payload("generic_mr.json"),
        )

        assert resp.status == 200
        assert "Test skill complete" in caplog.text
        assert "Exception when running skill" not in caplog.text
예제 #5
0
async def test_pr_review_comment_deleted(opsdroid, connector, mock_api,
                                         caplog):
    """Test deleting a PR review comment."""
    caplog.set_level(logging.INFO)

    @match_event(github_event.PRReviewCommentDeleted)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.target == "test-org/test-repo#9"
        assert event.body == "This is a PR Review comment"
        assert event.deleted_by == "test-user"
        logging.getLogger(__name__).info("Test skill complete")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("pr_review_comment_deleted.json"),
        )
        assert resp.status == 201
        assert "Test skill complete" in caplog.text
        assert "Exception when running skill" not in caplog.text
예제 #6
0
async def test_issue_closed(opsdroid, connector, mock_api, caplog):
    caplog.set_level(logging.INFO)

    @match_event(gitlab_events.GitlabIssueClosed)
    async def test_skill(opsdroid, config, event):
        url = "https://gitlab.com/FabioRosado/test-project/-/issues/1"
        assert event.connector.name == "gitlab"
        assert event.url == url
        assert event.project == "test-project"
        assert event.user == "FabioRosado"
        assert event.title == "New test issue"
        assert event.description == "This should have been filled"
        assert event.labels == ["test-label"]
        logging.getLogger(__name__).info("Test skill complete")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/gitlab",
            "POST",
            headers={
                "X-Gitlab-Token": "secret-stuff!",
                "Content-Type": "application/json",
            },
            data=get_webhook_payload("issue_closed.json"),
        )

        assert resp.status == 200
        assert "Test skill complete" in caplog.text
        assert "Exception when running skill" not in caplog.text
예제 #7
0
async def test_bad_json_file(opsdroid, connector, mock_api, caplog):
    caplog.set_level(logging.DEBUG)

    @match_event(gitlab_events.GenericGitlabEvent)
    async def test_skill(opsdroid, config, event):
        url = "http://example.com/mike/diaspora"
        assert event.connector.name == "gitlab"
        assert event.url == url
        assert event.project == "Diaspora"
        assert event.user == "jsmith"
        assert event.title is None
        assert event.description is None
        assert event.labels == []
        logging.getLogger(__name__).info("Test skill complete")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/gitlab",
            "POST",
            headers={
                "X-Gitlab-Token": "secret-stuff!",
                "Content-Type": "application/json",
            },
            data=get_webhook_payload("bad_json.json"),
        )

        assert resp.status == 400
        assert "Unable to decode json" in caplog.text
        assert "Test skill complete" not in caplog.text
        assert "Exception when running skill" not in caplog.text
예제 #8
0
async def test_edit_pr(opsdroid, connector, mock_api, caplog):
    """Test a PR reopen event creates a message and parses it."""
    caplog.set_level(logging.INFO)

    @match_event(github_event.PREdited)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.target == "test-org/test-repo#9"
        assert event.title == "Readme change"
        assert (
            event.description ==
            "This is just a change to the Readme file. Editing the PR comment."
        )
        assert event.edited_by == "test-user"
        logging.getLogger(__name__).info("Test skill complete")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("pr_edited.json"),
        )
        assert resp.status == 201
        assert "Test skill complete" in caplog.text
        assert "Exception when running skill" not in caplog.text
예제 #9
0
async def test_close_pr(opsdroid, connector, mock_api, caplog):
    """Test a PR close event creates an event and parses it."""
    caplog.set_level(logging.INFO)

    @match_event(github_event.PRClosed)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.target == "opsdroid/opsdroid-audio#175"
        assert event.title == "Update pytest-timeout to 1.3.3"
        assert event.closed_by == "pyup-bot"
        assert event.user == "pyup-bot"
        logging.getLogger(__name__).info("Test skill complete")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("pr_closed.json"),
        )
        assert resp.status == 201
        assert "Test skill complete" in caplog.text
        assert "Exception when running skill" not in caplog.text
예제 #10
0
async def test_check_completed(opsdroid, connector, mock_api, caplog):
    """Test a check completed event creates an event and parses it."""
    caplog.set_level(logging.INFO)

    @match_event(github_event.CheckCompleted)
    async def test_skill(opsdroid, config, event):
        assert event.action == "completed"
        assert event.status == "completed"
        assert event.conclusion == "timed_out"
        assert event.repository == "Hello-World"
        assert event.sender == "Codertocat"
        logging.getLogger(__name__).info("Test skill complete")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("check_completed.json"),
        )
        assert resp.status == 201
        assert "Test skill complete" in caplog.text
        assert "Exception when running skill" not in caplog.text
예제 #11
0
async def test_with_running_opsdroid(opsdroid):
    await opsdroid.load(config=MINIMAL_CONFIG)

    assert not opsdroid.is_running()

    async with running_opsdroid(opsdroid):
        assert opsdroid.is_running()
예제 #12
0
async def test_send_message_to_invalid_target(opsdroid, connector, mock_api,
                                              mock_api_obj, caplog):
    caplog.set_level(logging.INFO)

    @match_regex(r"ping")
    async def test_skill(opsdroid, config, event):
        pass

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        await connector.send(Message("hello", target=None,
                                     connector=connector))
        assert "not a valid place to send a message" in caplog.text
예제 #13
0
async def test_send_message_to_room_not_spoken_in(opsdroid, connector,
                                                  mock_api, mock_api_obj,
                                                  caplog):
    caplog.set_level(logging.INFO)

    @match_regex(r"ping")
    async def test_skill(opsdroid, config, event):
        pass

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        await connector.send(
            Message("hello", target="foo_room", connector=connector))
        assert "Unable to send a message" in caplog.text
예제 #14
0
async def test_receive_status(opsdroid, connector, mock_api):
    """Test a PR create event creates a message and parses it."""

    test_skill = match_event(Message)(CoroutineMock())
    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("status.json"),
        )
        assert resp.status == 201

    assert not test_skill.called
예제 #15
0
async def test_receive_unlabel_event(opsdroid, connector, mock_api):
    """Test a unlabel event creates a message and parses it."""
    @match_event(github_event.Unlabeled)
    async def test_skill(opsdroid, config, event):
        assert event.labels == "bug"
        assert event.state == "open"

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("github_unlabel_payload.json"),
        )
        assert resp.status == 201
예제 #16
0
async def test_ignore_bot_message(connector, opsdroid, mock_api, caplog):
    """Ignore messages from self.

    If the ID in the ``/user/me`` call matches that in the message
    the message should be ignored and the skill is not triggered.
    """

    caplog.set_level(logging.INFO)

    @match_regex("hi")
    async def test_skill(opsdroid, config, event):
        logging.getLogger(__name__).info("test_skill called")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        assert "test_skill called" not in caplog.text
예제 #17
0
async def test_receive_issue(opsdroid, connector, mock_api):
    """Test a PR create event creates a message and parses it."""
    @match_event(Message)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.text == "test"
        assert event.target == "opsdroid/opsdroid#740"

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("github_issue_payload.json"),
        )
        assert resp.status == 201
예제 #18
0
async def test_receive_issue(opsdroid, connector, mock_api):
    """Test a issue create event creates a message and parses it."""
    @match_event(github_event.IssueCreated)
    async def test_skill(opsdroid, config, event):
        assert event.user == "jacobtomlinson"
        assert event.title == "A test please ignore"
        assert event.description == "test"

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("github_issue_payload.json"),
        )
        assert resp.status == 201
예제 #19
0
async def test_receive_pr(opsdroid, connector, mock_api):
    """Test a PR create event creates a message and parses it."""
    @match_event(github_event.PROpened)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.target == "opsdroid/opsdroid-audio#175"
        assert event.title == "Update pytest-timeout to 1.3.3"
        assert event.description == "hello world"

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("github_pr_payload.json"),
        )
        assert resp.status == 201
예제 #20
0
async def test_no_key_in_payload(opsdroid, connector, mock_api, caplog):
    """Test if payload doesn't contain 'action' key."""
    @match_event(github_event.PROpened)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.target == "opsdroid/opsdroid-audio#175"
        assert event.title == "Update pytest-timeout to 1.3.3"
        assert event.description == "hello world"

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        caplog.set_level(logging.DEBUG)
        await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("user_forked.json"),
        )
        assert "Key 'action' not found in payload" in caplog.text
예제 #21
0
async def test_received_issue_close(opsdroid, connector, mock_api):
    """Test a issue close event creates an event and parses it."""
    @match_event(github_event.IssueClosed)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.target == "FabioRosado/github-actions-test#10"
        assert event.title == "Test integration"
        assert event.description == "this is a test for the integration"
        assert event.user == "FabioRosado"

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("github_issue_close_payload.json"),
        )
        assert resp.status == 201
예제 #22
0
async def test_check_completed(opsdroid, connector, mock_api):
    """Test a check completed event creates an event and parses it."""
    @match_event(github_event.CheckCompleted)
    async def test_skill(opsdroid, config, event):
        assert event.action == "completed"
        assert event.status == "completed"
        assert event.conclusion == "timed_out"
        assert event.repository == "Hello-World"
        assert event.sender == "Codertocat"

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("github_check_completed_payload.json"),
        )
        assert resp.status == 201
예제 #23
0
async def test_close_pr(opsdroid, connector, mock_api):
    """Test a PR close event creates an event and parses it."""
    @match_event(github_event.PRClosed)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.target == "opsdroid/opsdroid-audio#175"
        assert event.title == "Update pytest-timeout to 1.3.3"
        assert event.closed_by == "pyup-bot"
        assert event.user == "pyup-bot"

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("github_pr_closed_payload.json"),
        )
        assert resp.status == 201
예제 #24
0
async def test_invalid_request(opsdroid, connector_with_validation, mock_api, caplog):
    """Test a payload with an invalid request."""

    @match_event(github_event.PROpened)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.target == "opsdroid/opsdroid-audio#175"
        assert event.title == "Update pytest-timeout to 1.3.3"
        assert event.description == "hello world"

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        caplog.set_level(logging.DEBUG)
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("deployment.json"),
        )
        assert resp.status == 401
예제 #25
0
async def test_no_event_to_parse(opsdroid, connector, mock_api, caplog):
    """Test a payload that github doesn't know how to parse."""
    @match_event(github_event.PROpened)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.target == "opsdroid/opsdroid-audio#175"
        assert event.title == "Update pytest-timeout to 1.3.3"
        assert event.description == "hello world"

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        caplog.set_level(logging.DEBUG)
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("deployment.json"),
        )
        assert "No message to respond to." in caplog.text
        assert resp.status == 200
예제 #26
0
async def test_receive_unlabel(opsdroid, connector, mock_api, caplog):
    """Test a unlabel event creates a message and parses it."""
    caplog.set_level(logging.INFO)

    @match_event(github_event.Unlabeled)
    async def test_skill(opsdroid, config, event):
        assert event.label_removed == "bug"
        assert len(event.labels) == 0
        assert event.state == "open"
        logging.getLogger(__name__).info("Test skill complete")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("unlabel.json"),
        )
        assert resp.status == 201
        assert "Test skill complete" in caplog.text
        assert "Exception when running skill" not in caplog.text
예제 #27
0
async def test_receive_issue(opsdroid, connector, mock_api, caplog):
    """Test a issue create event creates a message and parses it."""
    caplog.set_level(logging.INFO)

    @match_event(github_event.IssueCreated)
    async def test_skill(opsdroid, config, event):
        assert event.user == "jacobtomlinson"
        assert event.title == "A test please ignore"
        assert event.description == "test"
        logging.getLogger(__name__).info("Test skill complete")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("issue.json"),
        )
        assert resp.status == 201
        assert "Test skill complete" in caplog.text
        assert "Exception when running skill" not in caplog.text
예제 #28
0
async def test_ping_pong(opsdroid, connector, mock_api, mock_api_obj, caplog):
    """Test a message is received and response is sent."""
    caplog.set_level(logging.INFO)

    @match_regex(r"ping")
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "teams"
        assert event.text == "ping"

        logging.getLogger(__name__).info("ping called")
        await event.respond("pong")

        # Send message to the room with a string target too
        [target, *_] = event.target.conversation.id.split(";")
        msg = Message(
            text="pong",
            target=target,
        )
        await event.connector.send(msg)

    opsdroid.register_skill(test_skill, config={"name": "test"})

    with mock.patch("uuid.uuid1") as mock_uuid:
        mock_uuid.return_value = "ca153e8e-ed8c-11eb-9208-1e2940309485"
        async with running_opsdroid(opsdroid):
            with open(get_response_path("teams_ping_payload.json"), "r") as fh:
                data = fh.read()

                resp = await call_endpoint(
                    opsdroid,
                    "/connector/teams",
                    "POST",
                    data=data,
                    headers={"Content-Type": "application/json"},
                )
                assert resp.status == 200
                assert "ping called" in caplog.text
예제 #29
0
async def test_receive_comment(opsdroid, connector, mock_api):
    """Test a comment create event creates a message and parses it."""
    @match_event(github_event.IssueCommented)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.target == "opsdroid/opsdroid#237"
        assert event.comment == "hello"
        assert event.issue_title == "test issue, please ignore"
        assert (
            event.comment_url ==
            "https://api.github.com/repos/opsdroid/opsdroid/issues/comments/439318644"
        )
        assert event.user == "jacobtomlinson"

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("github_comment_payload.json"),
        )
        assert resp.status == 201
예제 #30
0
async def test_push_commit(opsdroid, connector, mock_api, caplog):
    """Test that pushing a commit creates an event and parses it."""
    caplog.set_level(logging.INFO)

    @match_event(github_event.Push)
    async def test_skill(opsdroid, config, event):
        assert event.connector.name == "github"
        assert event.target == "refs/heads/readme-change"
        assert event.pushed_by == "test-user"
        assert event.user == "test-user"
        logging.getLogger(__name__).info("Test skill complete")

    opsdroid.register_skill(test_skill, config={"name": "test"})

    async with running_opsdroid(opsdroid):
        resp = await call_endpoint(
            opsdroid,
            "/connector/github",
            "POST",
            data=get_webhook_payload("push.json"),
        )
        assert resp.status == 201
        assert "Test skill complete" in caplog.text
        assert "Exception when running skill" not in caplog.text