Example #1
0
def test_configure_replacement_context_email_type_found(tmpdir, smtpserver,
                                                        httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_settings = {"email_type": "test_alert",
                      "subject": "subject test line",
                      "body": "body test",
                      "sender": {"real_name": "Alarmageddon Monitor",
                                 "address": "*****@*****.**"},
                      "recipients": [{"real_name": "Test Recipient",
                                      "address":
                                      "*****@*****.**"}]
                      }
    emailer.enrich(http_validator, email_settings)
    result = Failure("validation name", http_validator,
                     description="A failure occurred.")
    email_pub.configure_replacement_context(result)
    replacement_context = email_pub._replacement_context
    assert replacement_context["test_name"] == "validation name"
    assert replacement_context["test_description"] == "A failure occurred."
    assert replacement_context["env"] == "test"
    assert replacement_context["email_type"] == "test_alert"
    assert replacement_context["email_custom_message"] == \
        "Validation failed in environment {{env}}: {{test_name}}."
Example #2
0
def test_send(tmpdir, smtpserver, httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    create_subject_template(tmpdir)
    create_body_template(tmpdir)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    general_defaults = email_pub._config["email_defaults"]["general"]
    email_settings = {"email_type": "test_alert",
                      "subject": general_defaults["email_subject_template"],
                      "body": general_defaults["email_template"],
                      "sender": general_defaults["email_sender"],
                      "recipients": general_defaults["email_recipients"]}
    emailer.enrich(http_validator, email_settings)
    failure_message = "Validation failure. Expected 200, received 404."
    result = Failure("Check Status Route", http_validator,
                     description=failure_message)
    email_pub.send(result)
    print(smtpserver.outbox[0])
    assert len(smtpserver.outbox) == 1
    payload = str(smtpserver.outbox[0].get_payload()[0])
    assert payload.split('\n')[5] == "Validation Failure in environment test:"
    assert payload.split('\n')[6] == "Test Name: [Check Status Route]"
    assert payload.split('\n')[7] == \
        "Test Description: [Validation failure. Expected 200, received 404.]"
    custom_message = "Custom Message: [Validation failed in environment " \
                     "test: Check Status Route.]"
    assert payload.split('\n')[8] == custom_message
Example #3
0
def test_get_email_settings(tmpdir, smtpserver, httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_type = "test_alert"
    subject = "subject test line"
    body = "body test"
    recipient_array = [{"real_name": "Test Recipient",
                        "address": "*****@*****.**"}]
    sender = {"real_name": "Alarmageddon Monitor",
              "address": "*****@*****.**"}
    email_settings = {"email_type": email_type,
                      "subject": subject,
                      "body": body,
                      "sender": sender,
                      "recipients": recipient_array
                      }
    emailer.enrich(http_validator, email_settings)
    result = Success("validation name", http_validator)
    enriched_email_settings = email_pub.get_email_settings(result)
    assert enriched_email_settings['email_type'] == email_type
    assert enriched_email_settings['subject'] == subject
    assert enriched_email_settings['body'] == body
    assert enriched_email_settings['sender'] == sender
    assert enriched_email_settings['recipients'] == recipient_array
Example #4
0
def test_enrich_requires_recipients(httpserver):
    httpserver.serve_content("Not Found", 404)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_settings = {"email_type": "test_alert",
                      "subject": "subject test line",
                      "body": "body test",
                      "sender": {"real_name": "Alarmageddon Monitor",
                                 "address": "*****@*****.**"}
                      }
    with pytest.raises(KeyError):
        emailer.enrich(http_validator, email_settings)
Example #5
0
def test_enrich_requires_validator():
    email_settings = {"email_type": "test_alert",
                      "subject": "subject test line",
                      "body": "body test",
                      "sender": {"real_name": "Alarmageddon Monitor",
                                 "address": "*****@*****.**"},
                      "recipients": [{"real_name": "Test Recipient",
                                      "address":
                                      "*****@*****.**"}]
                      }
    with pytest.raises(ValueError):
        emailer.enrich(None, email_settings)
Example #6
0
def test_can_publish(tmpdir, smtpserver, httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_settings = {"email_type": "test_alert",
                      "subject": "subject test line",
                      "body": "body test",
                      "sender": {"real_name": "Alarmageddon Monitor",
                                 "address": "*****@*****.**"},
                      "recipients": [{"real_name": "Test Recipient",
                                      "address":
                                      "*****@*****.**"}]
                      }
    emailer.enrich(http_validator, email_settings)
    result = Success("validation name", http_validator)
    assert email_pub._can_publish(result) is True
Example #7
0
def test_enrich_no_runtime_context(httpserver):
    httpserver.serve_content("Not Found", 404)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_settings = {"email_type": "test_alert",
                      "subject": "subject test line",
                      "body": "body test",
                      "sender": {"real_name": "Alarmageddon Monitor",
                                 "address": "*****@*****.**"},
                      "recipients": [{"real_name": "Test Recipient",
                                      "address":
                                      "*****@*****.**"}]
                      }

    emailer.enrich(http_validator, email_settings)
    temp_pub = EmailPublisher({"fake": "config"})
    data = http_validator.get_enriched(temp_pub)
    assert data["email_settings"] == email_settings
    assert data["runtime_context"] == {}
Example #8
0
def test_can_publish_requires_recipients(tmpdir, smtpserver,
                                         monkeypatch, httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_settings = {"email_type": "test_alert",
                      "subject": "subject test line",
                      "body": "body test",
                      "sender": {"real_name": "Alarmageddon Monitor",
                                 "address": "*****@*****.**"},
                      "recipients": [{"real_name": "Test Recipient",
                                      "address":
                                      "*****@*****.**"}]
                      }
    emailer.enrich(http_validator, email_settings)
    temp_pub = EmailPublisher({"fake": "config"})
    data = http_validator.get_enriched(temp_pub)
    monkeypatch.delitem(data['email_settings'], "recipients")
    result = Success("validation name", http_validator)
    assert email_pub._can_publish(result) is False
Example #9
0
def test_get_runtime_context(tmpdir, smtpserver, httpserver):
    email_pub = create_default_email_publisher(tmpdir, smtpserver)
    http_validator = \
        HttpValidation.get(httpserver.url).expect_status_codes([200])
    email_type = "test_alert"
    subject = "subject test line"
    body = "body test"
    recipient_array = [{"real_name": "Test Recipient",
                        "address": "*****@*****.**"}]
    sender = {"real_name": "Alarmageddon Monitor",
              "address": "*****@*****.**"}
    email_settings = {"email_type": email_type,
                      "subject": subject,
                      "body": body,
                      "sender": sender,
                      "recipients": recipient_array
                      }
    context = {"custom_replacement": "hello world"}
    emailer.enrich(http_validator, email_settings, runtime_context=context)
    result = Success("validation name", http_validator)
    enriched_context = email_pub.get_runtime_context(result)
    assert enriched_context['custom_replacement'] == "hello world"