Skip to content
Repos

DevPlaceApp

C++ 3.6 KB62 lines Opens in site

Directory Raw Download

#include "TestHarness.hpp"
#include "api/ApiErrorDecoder.hpp"
#include "api/DevPlaceError.hpp"
#include "core/AlertMessage.hpp"

using namespace dp;

DP_TEST(api_error_string_form_prefers_messages_first) {
  auto d = ApiErrorDecoder::decodeBody(R"({"error":"Bad things","messages":["Specific one","two"]})");
  CHECK(d && d->isSpecific && d->message == "Specific one");
  auto e = ApiErrorDecoder::decodeBody(R"({"error":"Bad things"})");
  CHECK(e && e->isSpecific && e->message == "Bad things");
  auto empty = ApiErrorDecoder::decodeBody(R"({"error":"x","messages":[]})");
  CHECK(empty && empty->message == "x");
}

DP_TEST(api_error_object_form_order) {
  auto a = ApiErrorDecoder::decodeBody(R"({"error":{"status":422,"message":"M","errors":["E1"]},"messages":["T"]})");
  CHECK(a && a->message == "M" && a->isSpecific);
  auto b = ApiErrorDecoder::decodeBody(R"({"error":{"status":422,"errors":["E1","E2"]},"messages":["T"]})");
  CHECK(b && b->message == "E1");
  auto c = ApiErrorDecoder::decodeBody(R"({"error":{"errors":[]},"messages":["T"]})");
  CHECK(c && c->message == "T" && c->isSpecific);
  auto d = ApiErrorDecoder::decodeBody(R"({"error":{"message":""}})");
  CHECK(d && d->isSpecific && d->message.empty());  // empty strings are valid and win
  auto none = ApiErrorDecoder::decodeBody(R"({"error":{}})");
  CHECK(none && !none->isSpecific && none->message == "Something went wrong. Please try again.");
}

DP_TEST(api_error_unusable_bodies) {
  CHECK(!ApiErrorDecoder::decodeBody("<html>502</html>"));
  CHECK(!ApiErrorDecoder::decodeBody(R"({"messages":["x"]})"));
  CHECK(!ApiErrorDecoder::decodeBody(R"({"error":5})"));
  CHECK(!ApiErrorDecoder::decodeBody(R"({"error":{"status":"x"}})"));
  CHECK(!ApiErrorDecoder::decodeBody(R"({"error":"x","messages":"nope"})"));
  Error e = ApiErrorDecoder::fromResponse(502, "<html>");
  CHECK(e.kind == Error::Kind::Http && e.status == 502 && !e.isSpecific);
  Error u = ApiErrorDecoder::fromResponse(401, R"({"error":"Invalid credentials"})");
  CHECK(u.kind == Error::Kind::Unauthorized && u.isSpecific && u.message == "Invalid credentials");
}

DP_TEST(alert_message_mapping) {
  CHECK(!AlertMessage::none().isPresented);
  AlertMessage plain = AlertMessage::presentedError("boom");
  CHECK(plain.isPresented && plain.title == "Error" && plain.message == "boom" && plain.buttonText == "OK");
  AlertMessage msg = AlertMessage::presentedMessage("hello");
  CHECK(msg.isPresented && msg.title.empty() && msg.buttonText == "OK");
  CHECK(!AlertMessage::presentedError(Error{Error::Kind::Cancelled, 0, "", false}).isPresented);
  CHECK_EQ(AlertMessage::presentedError(DevPlaceError::notLoggedIn()).message, std::string("Not logged in"));
  CHECK_EQ(AlertMessage::presentedError(DevPlaceError::invalidUrl("x y")).message, std::string("Invalid URL: x y"));
  CHECK_EQ(AlertMessage::presentedError(DevPlaceError::postNotEditable()).message, std::string("This post cannot be edited"));
  // specific API message: shown as is; generic: prefixed with the status
  CHECK_EQ(AlertMessage::presentedError(ApiErrorDecoder::fromResponse(422, R"({"error":"Title too long"})")).message,
           std::string("Title too long"));
  CHECK_EQ(AlertMessage::presentedError(ApiErrorDecoder::fromResponse(500, R"({"error":{}})")).message,
           std::string("(500) Something went wrong. Please try again."));
  CHECK_EQ(AlertMessage::presentedError(ApiErrorDecoder::fromResponse(503, "gateway")).message,
           std::string("(503) Something went wrong. Please try again."));
  // transport errors: message only
  CHECK_EQ(AlertMessage::presentedError(Error{Error::Kind::Offline, 0, "You appear to be offline.", true}).message,
           std::string("You appear to be offline."));
}
61.9ms