Skip to content

Commit

Permalink
Chore: Added test cases for webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
lordsarcastic committed Nov 8, 2024
1 parent e3da008 commit 8418bc5
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions integrations/jira/tests/test_webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
from typing import Any

Check failure on line 1 in integrations/jira/tests/test_webhook.py

View workflow job for this annotation

GitHub Actions / JUnit Test Report

test_webhook.test_issue_creation_event_will_create_issue

failed on setup with "port_ocean.exceptions.context.PortOceanContextAlreadyInitializedError: PortOcean context is already initialized"
Raw output
@pytest.fixture
    def ocean_app() -> Ocean:
>       return app()

tests/conftest.py:71: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/conftest.py:65: in app
    application = get_integration_ocean_app(INTEGRATION_PATH, config)
.venv/lib/python3.11/site-packages/port_ocean/tests/helpers/ocean_app.py:24: in get_integration_ocean_app
    default_app = create_default_app(
.venv/lib/python3.11/site-packages/port_ocean/bootstrap.py:41: in create_default_app
    return Ocean(
.venv/lib/python3.11/site-packages/port_ocean/ocean.py:41: in __init__
    initialize_port_ocean_context(self)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

ocean_app = <port_ocean.ocean.Ocean object at 0x7efd0a6deb90>

    def initialize_port_ocean_context(ocean_app: "Ocean") -> None:
        global _port_ocean
    
        if _port_ocean.initialized:
>           raise PortOceanContextAlreadyInitializedError(
                "PortOcean context is already initialized"
            )
E           port_ocean.exceptions.context.PortOceanContextAlreadyInitializedError: PortOcean context is already initialized

.venv/lib/python3.11/site-packages/port_ocean/context/ocean.py:145: PortOceanContextAlreadyInitializedError

Check failure on line 1 in integrations/jira/tests/test_webhook.py

View workflow job for this annotation

GitHub Actions / JUnit Test Report

test_webhook.test_issue_deletion_event_will_delete_issue

failed on setup with "pydantic.errors.ConfigError: duplicate validator function "port_ocean.config.dynamic.dynamic_parse"; if this is intended, set `allow_reuse=True`"
Raw output
@pytest.fixture
    def ocean_app() -> Ocean:
>       return app()

tests/conftest.py:71: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/conftest.py:65: in app
    application = get_integration_ocean_app(INTEGRATION_PATH, config)
.venv/lib/python3.11/site-packages/port_ocean/tests/helpers/ocean_app.py:22: in get_integration_ocean_app
    config_factory = default_config_factory(spec_file.get("configurations", []))
.venv/lib/python3.11/site-packages/port_ocean/config/dynamic.py:66: in default_config_factory
    __validators__={"dynamic_parse": validator("*", pre=True)(dynamic_parse)},
pydantic/class_validators.py:93: in pydantic.class_validators.validator.dec
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

>   ???
E   pydantic.errors.ConfigError: duplicate validator function "port_ocean.config.dynamic.dynamic_parse"; if this is intended, set `allow_reuse=True`

pydantic/class_validators.py:156: ConfigError

Check failure on line 1 in integrations/jira/tests/test_webhook.py

View workflow job for this annotation

GitHub Actions / JUnit Test Report

test_webhook.test_issue_update_event_will_update_issue

failed on setup with "pydantic.errors.ConfigError: duplicate validator function "port_ocean.config.dynamic.dynamic_parse"; if this is intended, set `allow_reuse=True`"
Raw output
@pytest.fixture
    def ocean_app() -> Ocean:
>       return app()

tests/conftest.py:71: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/conftest.py:65: in app
    application = get_integration_ocean_app(INTEGRATION_PATH, config)
.venv/lib/python3.11/site-packages/port_ocean/tests/helpers/ocean_app.py:22: in get_integration_ocean_app
    config_factory = default_config_factory(spec_file.get("configurations", []))
.venv/lib/python3.11/site-packages/port_ocean/config/dynamic.py:66: in default_config_factory
    __validators__={"dynamic_parse": validator("*", pre=True)(dynamic_parse)},
pydantic/class_validators.py:93: in pydantic.class_validators.validator.dec
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

>   ???
E   pydantic.errors.ConfigError: duplicate validator function "port_ocean.config.dynamic.dynamic_parse"; if this is intended, set `allow_reuse=True`

pydantic/class_validators.py:156: ConfigError

Check failure on line 1 in integrations/jira/tests/test_webhook.py

View workflow job for this annotation

GitHub Actions / JUnit Test Report

test_webhook.test_project_deletion_event_will_delete_project

failed on setup with "pydantic.errors.ConfigError: duplicate validator function "port_ocean.config.dynamic.dynamic_parse"; if this is intended, set `allow_reuse=True`"
Raw output
@pytest.fixture
    def ocean_app() -> Ocean:
>       return app()

tests/conftest.py:71: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/conftest.py:65: in app
    application = get_integration_ocean_app(INTEGRATION_PATH, config)
.venv/lib/python3.11/site-packages/port_ocean/tests/helpers/ocean_app.py:22: in get_integration_ocean_app
    config_factory = default_config_factory(spec_file.get("configurations", []))
.venv/lib/python3.11/site-packages/port_ocean/config/dynamic.py:66: in default_config_factory
    __validators__={"dynamic_parse": validator("*", pre=True)(dynamic_parse)},
pydantic/class_validators.py:93: in pydantic.class_validators.validator.dec
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

>   ???
E   pydantic.errors.ConfigError: duplicate validator function "port_ocean.config.dynamic.dynamic_parse"; if this is intended, set `allow_reuse=True`

pydantic/class_validators.py:156: ConfigError
from unittest.mock import AsyncMock

from port_ocean import Ocean
from port_ocean.context.ocean import ocean
from starlette.testclient import TestClient

from client import JiraClient

from .fixtures import ISSUE_WEBHOOK, PROJECT_WEBHOOK

ENDPOINT = "/integration/webhook"


def test_project_creation_event_will_create_project(
monkeypatch: Any, ocean_app: Ocean
) -> None:
client = TestClient(ocean_app)
project: dict[str, Any] = {**PROJECT_WEBHOOK, "webhookEvent": "project_created"}
get_single_project_mock = AsyncMock()
get_single_project_mock.return_value = project["project"]
register_raw_mock = AsyncMock()
monkeypatch.setattr(JiraClient, "get_single_project", get_single_project_mock)
monkeypatch.setattr(ocean, "register_raw", register_raw_mock)

response = client.post(ENDPOINT, json=project)

assert response.status_code == 200

Check failure on line 28 in integrations/jira/tests/test_webhook.py

View workflow job for this annotation

GitHub Actions / JUnit Test Report

test_webhook.test_project_creation_event_will_create_project

assert 500 == 200 + where 500 = <Response [500 Internal Server Error]>.status_code
Raw output
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f9a11d1fa10>
ocean_app = <port_ocean.ocean.Ocean object at 0x7f9a127228d0>

    def test_project_creation_event_will_create_project(
        monkeypatch: Any, ocean_app: Ocean
    ) -> None:
        client = TestClient(ocean_app)
        project: dict[str, Any] = {**PROJECT_WEBHOOK, "webhookEvent": "project_created"}
        get_single_project_mock = AsyncMock()
        get_single_project_mock.return_value = project["project"]
        register_raw_mock = AsyncMock()
        monkeypatch.setattr(JiraClient, "get_single_project", get_single_project_mock)
        monkeypatch.setattr(ocean, "register_raw", register_raw_mock)
    
        response = client.post(ENDPOINT, json=project)
    
>       assert response.status_code == 200
E       assert 500 == 200
E        +  where 500 = <Response [500 Internal Server Error]>.status_code

tests/test_webhook.py:28: AssertionError
assert response.json() == {"ok": True}
assert get_single_project_mock.called
assert register_raw_mock.called
register_raw_mock.assert_called_once_with("project", [project["project"]])
get_single_project_mock.assert_called_once_with(project["project"]["key"])


def test_project_deletion_event_will_delete_project(
monkeypatch: Any, ocean_app: Ocean
) -> None:
client = TestClient(ocean_app)
project = {**PROJECT_WEBHOOK, "webhookEvent": "project_deleted"}
unregister_raw_mock = AsyncMock()
monkeypatch.setattr(ocean, "unregister_raw", unregister_raw_mock)

response = client.post(ENDPOINT, json=project)

assert response.status_code == 200
assert response.json() == {"ok": True}
assert unregister_raw_mock.called
unregister_raw_mock.assert_called_once_with("project", [project["project"]])


def test_issue_creation_event_will_create_issue(
monkeypatch: Any, ocean_app: Ocean
) -> None:
client = TestClient(ocean_app)
issue: dict[str, Any] = {**ISSUE_WEBHOOK, "webhookEvent": "jira:issue_created"}
get_all_issues_mock = AsyncMock()
get_all_issues_mock.__aiter__.return_value = issue["issue"]
register_raw_mock = AsyncMock()
monkeypatch.setattr(JiraClient, "get_all_issues", get_all_issues_mock)
monkeypatch.setattr(ocean, "register_raw", register_raw_mock)

response = client.post(ENDPOINT, json=issue)

assert response.status_code == 200
assert response.json() == {"ok": True}
assert get_all_issues_mock.called
assert register_raw_mock.called
register_raw_mock.assert_called_once_with("issue", [issue["issue"]])
get_all_issues_mock.assert_called_once_with(
{"jql": f"key = {issue['issue']['key']}", "fields": "*all"}
)


def test_issue_deletion_event_will_delete_issue(
monkeypatch: Any, ocean_app: Ocean
) -> None:
client = TestClient(ocean_app)
issue = {**ISSUE_WEBHOOK, "webhookEvent": "jira:issue_deleted"}
unregister_raw_mock = AsyncMock()
monkeypatch.setattr(ocean, "unregister_raw", unregister_raw_mock)

response = client.post(ENDPOINT, json=issue)

assert response.status_code == 200
assert response.json() == {"ok": True}
assert unregister_raw_mock.called
unregister_raw_mock.assert_called_once_with("issue", [issue["issue"]])


def test_issue_update_event_will_update_issue(
monkeypatch: Any, ocean_app: Ocean
) -> None:
client = TestClient(ocean_app)
issue: dict[str, Any] = {**ISSUE_WEBHOOK, "webhookEvent": "jira:issue_updated"}
get_single_issue_mock = AsyncMock()
get_single_issue_mock.return_value = issue["issue"]
register_raw_mock = AsyncMock()
monkeypatch.setattr(JiraClient, "get_single_issue", get_single_issue_mock)
monkeypatch.setattr(ocean, "register_raw", register_raw_mock)

response = client.post(ENDPOINT, json=issue)

assert response.status_code == 200
assert response.json() == {"ok": True}
assert get_single_issue_mock.called
assert register_raw_mock.called
register_raw_mock.assert_called_once_with("issue", [issue["issue"]])
get_single_issue_mock.assert_called_once_with(
{"jql": f"key = {issue['issue']['key']}", "fields": "*all"}
)

0 comments on commit 8418bc5

Please sign in to comment.