const describe = require("mocha").describe;
const it = require("mocha").it;
const assert = require("chai").assert;

describe("handler", function () {
  const config = {
    ENCRYPTION_KEY: "encryptionKey",
    SENDER_ARN:
      "arn:aws:ses:eu-west-1:123456789123:identity/sender@example.com",
    SENDER: "Sender",
    MSG_SUBJECT: "Subject",
    MSG_RECEIVE_SUCCESS: "Received",
  };

  const sendEmailSuccess = (email) => Promise.resolve(email);
  const sendEmailFailure = (error) => Promise.reject(error).catch(() => {});

  const loadTemplateSuccess = () =>
    Promise.resolve("<!DOCTYPE html><html><body>{{ message }}</body></html>");
  const loadTemplateFailure = (error) => Promise.reject(error).catch(() => {});

  const isValidRecaptchaSuccess = (success) => Promise.resolve(success);
  const isValidRecaptchaFailure = (error) =>
    Promise.reject(error).catch(() => {});

  it("should return a successful html response", async function () {
    // given
    const event = {
      body: "_to=test%40example.com&testing=true",
      requestContext: {
        identity: {
          sourceIp: "127.0.0.1",
        },
      },
    };

    // when
    const response = await require("./handler")({
      config,
      isValidRecaptcha: isValidRecaptchaSuccess,
      sendEmail: sendEmailSuccess,
      loadTemplate: loadTemplateSuccess,
    })(event);

    // then
    assert.deepEqual(response, {
      statusCode: 200,
      headers: {
        "Content-Type": "text/html",
      },
      body: "<!DOCTYPE html><html><body>Received</body></html>",
    });
  });

  it("should return a successful json response", async function () {
    // given
    const event = {
      queryStringParameters: {
        format: "json",
      },
      body: "_to=test%40example.com&testing=true",
      requestContext: {
        identity: {
          sourceIp: "127.0.0.1",
        },
      },
    };

    // when
    const response = await require("./handler")({
      config,
      isValidRecaptcha: isValidRecaptchaSuccess,
      sendEmail: sendEmailSuccess,
      loadTemplate: loadTemplateSuccess,
    })(event);

    // then
    assert.deepEqual(response, {
      statusCode: 200,
      headers: {
        "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*",
      },
      body: '{"statusCode":200,"message":"Received"}',
    });
  });

  it("should return a successful redirect response", async function () {
    // given
    const event = {
      body: "_to=test%40example.com&_redirect=http%3A%2F%2Fexample.com&testing=true",
      requestContext: {
        identity: {
          sourceIp: "127.0.0.1",
        },
      },
    };

    // when
    const response = await require("./handler")({
      config,
      isValidRecaptcha: isValidRecaptchaSuccess,
      sendEmail: sendEmailSuccess,
      loadTemplate: loadTemplateSuccess,
    })(event);

    // then
    assert.deepEqual(response, {
      statusCode: 302,
      headers: {
        "Content-Type": "text/plain",
        Location: "http://example.com",
      },
      body: "Received",
    });
  });

  it("should return a 403 response when recaptcha validation fails", async function () {
    // given
    const event = {};

    // when
    const response = await require("./handler")({
      config,
      isValidRecaptcha: () => isValidRecaptchaSuccess(false),
      sendEmail: sendEmailSuccess,
      loadTemplate: loadTemplateSuccess,
    })(event);

    // then
    assert.deepEqual(response, {
      statusCode: 403,
      headers: {
        "Content-Type": "text/html",
      },
      body: "<!DOCTYPE html><html><body>Form submission failed recaptcha</body></html>",
    });
  });

  it("should return a 500 response when recaptcha validation throws an error", async function () {
    // given
    const event = {};

    // when
    const response = await require("./handler")({
      config,
      isValidRecaptcha: isValidRecaptchaFailure(new Error("testing")),
      sendEmail: sendEmailSuccess,
      loadTemplate: loadTemplateSuccess,
    })(event);

    // then
    assert.deepEqual(response, {
      statusCode: 500,
      headers: {
        "Content-Type": "text/html",
      },
      body: "<!DOCTYPE html><html><body>An unexpected error occurred</body></html>",
    });
  });

  it("should return a 422 response when request validation fails", async function () {
    // given
    const event = {};

    // when
    const response = await require("./handler")({
      config,
      isValidRecaptcha: isValidRecaptchaSuccess,
      sendEmail: sendEmailSuccess,
      loadTemplate: loadTemplateSuccess,
    })(event);

    // then
    assert.deepEqual(response, {
      statusCode: 422,
      headers: {
        "Content-Type": "text/html",
      },
      body: "<!DOCTYPE html><html><body>Invalid '_to' recipient</body></html>",
    });
  });

  it("should return a 500 response when email validation fails", async function () {
    // given
    const event = {
      body: "_to=test%40example.com&testing=true",
      requestContext: {
        identity: {
          sourceIp: "127.0.0.1",
        },
      },
    };

    // when
    const response = await require("./handler")({
      config: { ...config, MSG_SUBJECT: null },
      isValidRecaptcha: isValidRecaptchaSuccess,
      sendEmail: sendEmailSuccess,
      loadTemplate: loadTemplateSuccess,
    })(event);

    // then
    assert.deepEqual(response, {
      statusCode: 500,
      headers: {
        "Content-Type": "text/html",
      },
      body: "<!DOCTYPE html><html><body>Subject is invalid</body></html>",
    });
  });

  it("should return a 500 response when email sending fails", async function () {
    // given
    const event = {
      body: "_to=test%40example.com&testing=true",
      requestContext: {
        identity: {
          sourceIp: "127.0.0.1",
        },
      },
    };

    // when
    const response = await require("./handler")({
      config: config,
      isValidRecaptcha: isValidRecaptchaSuccess,
      sendEmail: sendEmailFailure(new Error("testing")),
      loadTemplate: loadTemplateSuccess,
    })(event);

    // then
    assert.deepEqual(response, {
      statusCode: 500,
      headers: {
        "Content-Type": "text/html",
      },
      body: "<!DOCTYPE html><html><body>An unexpected error occurred</body></html>",
    });
  });

  it("should handle template loading failures", async function () {
    // given
    const event = {
      body: "_to=test%40example.com&testing=true",
      requestContext: {
        identity: {
          sourceIp: "127.0.0.1",
        },
      },
    };

    // when
    const response = await require("./handler")({
      config: config,
      isValidRecaptcha: isValidRecaptchaSuccess,
      sendEmail: sendEmailSuccess,
      loadTemplate: loadTemplateFailure(new Error("testing")),
    })(event);

    // then
    assert.deepEqual(response, {
      statusCode: 200,
      headers: {
        "Content-Type": "text/plain",
      },
      body: "Received",
    });
  });
});

@@ -4845,754 +4845,8 @@
 );%0A%0A
-  it(%22should return a 500 response when email validation fails%22, async function () %7B%0A    // given%0A    const event = %7B%0A      body: %22_to=test%2540example.com&testing=true%22,%0A      requestContext: %7B%0A        identity: %7B%0A          sourceIp: %22127.0.0.1%22,%0A        %7D,%0A      %7D,%0A    %7D;%0A%0A    // when%0A    const response = await require(%22./handler%22)(%7B%0A      config: %7B ...config, MSG_SUBJECT: null %7D,%0A      isValidRecaptcha: isValidRecaptchaSuccess,%0A      sendEmail: sendEmailSuccess,%0A      loadTemplate: loadTemplateSuccess,%0A    %7D)(event);%0A%0A    // then%0A    assert.deepEqual(response, %7B%0A      statusCode: 500,%0A      headers: %7B%0A        %22Content-Type%22: %22text/html%22,%0A      %7D,%0A      body: %22%3C!DOCTYPE html%3E%3Chtml%3E%3Cbody%3ESubject is invalid%3C/body%3E%3C/html%3E%22,%0A    %7D);%0A  %7D);%0A%0A
   it

remove email validation test