import requests
import pytest
from pycapo import CapoConfig


def settings():
    return CapoConfig().settings("edu.nrao.archive.workspaces.NotificationSettings").serviceUrl

@pytest.mark.skip("Because pipeline weird")
def test_create_template():
    expected_result = "Template new_template created."

    url = settings() + '/notify/create'
    headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8', 'subject': 'testing'}
    json_packet = {"name": "new_template", "description": "a test template", "template": "Subject: {{foo}}\n\n{{bar}}"}
    r = requests.post(url, headers=headers, json=json_packet)

    actual_result = r.text

    assert actual_result == expected_result


@pytest.mark.skip("Because pipeline weird")
def test_get_templates():
    expected_result = "Available notification templates:\nemail\nsubmitted_email\ncomplete_email\nnew_template\n"

    url = settings() + '/notify'
    headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
    r = requests.get(url, headers=headers)

    print(r)

    actual_result = r.text

    assert actual_result == expected_result


@pytest.mark.skip(
    reason="Test succeeds, but we don't want to spam ourselves whenever the tests run... "
           "See about getting a dummy account?"
)
def test_send_email():
    """check that calling the send email function returns without error"""
    expected_result = """Email sent.\nSubject: first thing\n\nsecond thing"""

    url = settings() + '/notify/email/send'
    headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8', 'subject': 'testing'}
    json_packet = {"destination_email": "your-email@nrao.edu", "subject": "first thing", "message": "second thing"}
    r = requests.post(url, headers=headers, json=json_packet)

    print(r)

    actual_result = r.text

    assert actual_result == expected_result


@pytest.mark.skip("Because pipeline weird")
def test_bad_template_name():
    """check that calling with a non-existant template name gives an error message"""
    expected_result = """No template 'nonsense' found."""

    url = settings() + '/notify/nonsense/send'
    headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8', 'subject': 'testing'}
    json_packet = {"destination_email": "your-email@nrao.edu", "stuff": "first thing", "more": "second thing"}
    r = requests.post(url, headers=headers, json=json_packet)

    print(r)

    actual_result = r.text

    assert actual_result == expected_result


@pytest.mark.skip("Because pipeline weird")
def test_delete_template():
    expected_result = "Template new_template deleted."

    url = settings() + '/notify/new_template/delete'
    headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8', 'subject': 'testing'}
    r = requests.delete(url, headers=headers)

    actual_result = r.text

    assert actual_result == expected_result