from unittest.mock import patch
import demistomock as demisto
import pytest
from freezegun import freeze_time
from GetCampaignDuration import get_duration_html
CAMPAIGN_WITH_DURATION_RESULT = """
| 🕙 |
30 |
: |
22 |
: |
39 |
|
Days |
|
Hours |
|
Minutes |
"""
CURRENT_TIME_MOCK = "05-08-2021 14:40:22"
@pytest.mark.parametrize(
"EmailCampaign_context, result", [({"firstIncidentDate": "2021-04-07T16:00:52.602353+00:00"}, CAMPAIGN_WITH_DURATION_RESULT)]
)
@freeze_time(CURRENT_TIME_MOCK)
def test_campaign_with_duration(mocker, EmailCampaign_context, result):
"""
Given:
- Email campaign context with first incident time
When:
- Get the campaign duration
Then:
- Calculate the duration and return valid html output
"""
# prepare
mocker.patch.object(demisto, "incident", return_value={"id": "100"})
mocker.patch.object(
demisto, "executeCommand", return_value=[{"Contents": {"context": {"EmailCampaign": EmailCampaign_context}}}]
)
# run
res = get_duration_html()
assert "".join(res.split()) == "".join(result.split())
@patch("GetCampaignDuration.return_error")
@freeze_time(CURRENT_TIME_MOCK)
def test_campaign_without_duration(mock_return_error, mocker):
"""
Given:
- Email campaign context without first incident time
When:
- Get the campaign duration
Then:
- Return error, invalid Cant find firstIncidentDat
"""
# prepare
mocker.patch.object(demisto, "incident", return_value={"id": "100"})
mocker.patch.object(demisto, "executeCommand", return_value=[{"Contents": {"context": {"EmailCampaign": {}}}}])
# run
res = get_duration_html()
assert res == 'Duration is not available.
'