Skip to content

Commit

Permalink
refactor: separated errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaskowicz1 committed Feb 19, 2024
1 parent b54ef4c commit 8f368e0
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 240 deletions.
117 changes: 117 additions & 0 deletions src/unittest/errors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/************************************************************************************
*
* D++, A Lightweight C++ library for Discord
*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2021 Craig Edwards and D++ contributors
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
#include "test.h"

void errors_unit_tests() {
set_test(ERRORS, false);

/* Prepare a confirmation_callback_t in error state (400) */
dpp::confirmation_callback_t error_test;
bool error_message_success = false;
error_test.http_info.status = 400;

error_test.http_info.body = "{\
\"message\": \"Invalid Form Body\",\
\"code\": 50035,\
\"errors\": {\
\"options\": {\
\"0\": {\
\"name\": {\
\"_errors\": [\
{\
\"code\": \"STRING_TYPE_REGEX\",\
\"message\": \"String value did not match validation regex.\"\
},\
{\
\"code\": \"APPLICATION_COMMAND_INVALID_NAME\",\
\"message\": \"Command name is invalid\"\
}\
]\
}\
}\
}\
}\
}";
error_message_success = (error_test.get_error().human_readable == "50035: Invalid Form Body\n\t- options[0].name: String value did not match validation regex. (STRING_TYPE_REGEX)\n\t- options[0].name: Command name is invalid (APPLICATION_COMMAND_INVALID_NAME)");

error_test.http_info.body = "{\
\"message\": \"Invalid Form Body\",\
\"code\": 50035,\
\"errors\": {\
\"type\": {\
\"_errors\": [\
{\
\"code\": \"BASE_TYPE_CHOICES\",\
\"message\": \"Value must be one of {4, 5, 9, 10, 11}.\"\
}\
]\
}\
}\
}";
error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - type: Value must be one of {4, 5, 9, 10, 11}. (BASE_TYPE_CHOICES)");

error_test.http_info.body = "{\
\"message\": \"Unknown Guild\",\
\"code\": 10004\
}";
error_message_success = (error_message_success && error_test.get_error().human_readable == "10004: Unknown Guild");

error_test.http_info.body = "{\
\"message\": \"Invalid Form Body\",\
\"code\": 50035,\
\"errors\": {\
\"allowed_mentions\": {\
\"_errors\": [\
{\
\"code\": \"MESSAGE_ALLOWED_MENTIONS_PARSE_EXCLUSIVE\",\
\"message\": \"parse:[\\\"users\\\"] and users: [ids...] are mutually exclusive.\"\
}\
]\
}\
}\
}";
error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - allowed_mentions: parse:[\"users\"] and users: [ids...] are mutually exclusive. (MESSAGE_ALLOWED_MENTIONS_PARSE_EXCLUSIVE)");

error_test.http_info.body = "{\
\"message\": \"Invalid Form Body\",\
\"code\": 50035,\
\"errors\": {\
\"1\": {\
\"options\": {\
\"1\": {\
\"description\": {\
\"_errors\": [\
{\
\"code\": \"BASE_TYPE_BAD_LENGTH\",\
\"message\": \"Must be between 1 and 100 in length.\"\
}\
]\
}\
}\
}\
}\
}\
}";
error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - <array>[1].options[1].description: Must be between 1 and 100 in length. (BASE_TYPE_BAD_LENGTH)");

set_test(ERRORS, error_message_success);
}
4 changes: 2 additions & 2 deletions src/unittest/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void http_unit_tests(const std::string& token) {
}
);
std::string hdr1 = c.get_header("server");
std::string content1 = c.get_content();
//std::string content1 = c.get_content();
set_test(HTTPS, hdr1 == "cloudflare" && c.get_status() == 200);
}
catch (const dpp::exception& e) {
Expand All @@ -71,7 +71,7 @@ void http_unit_tests(const std::string& token) {
try {
dpp::https_client c2("github.com", 80, "/", "GET", "", {}, true);
std::string hdr2 = c2.get_header("location");
std::string content2 = c2.get_content();
//std::string content2 = c2.get_content();
set_test(HTTP, hdr2 == "https://github.com/" && c2.get_status() == 301);
}
catch (const dpp::exception& e) {
Expand Down
239 changes: 1 addition & 238 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,107 +79,7 @@ int main(int argc, char *argv[])
std::cout << "Running offline and " << (extended ? "extended" : "limited") << " online unit tests. Guild ID: " << TEST_GUILD_ID << " Text Channel ID: " << TEST_TEXT_CHANNEL_ID << " VC ID: " << TEST_VC_ID << " User ID: " << TEST_USER_ID << " Event ID: " << TEST_EVENT_ID << "\n";
}

set_test(COMPARISON, false);
dpp::user u1;
dpp::user u2;
dpp::user u3;
u1.id = u2.id = 666;
u3.id = 777;
set_test(COMPARISON, u1 == u2 && u1 != u3);


set_test(ERRORS, false);

/* Prepare a confirmation_callback_t in error state (400) */
dpp::confirmation_callback_t error_test;
bool error_message_success = false;
error_test.http_info.status = 400;

error_test.http_info.body = "{\
\"message\": \"Invalid Form Body\",\
\"code\": 50035,\
\"errors\": {\
\"options\": {\
\"0\": {\
\"name\": {\
\"_errors\": [\
{\
\"code\": \"STRING_TYPE_REGEX\",\
\"message\": \"String value did not match validation regex.\"\
},\
{\
\"code\": \"APPLICATION_COMMAND_INVALID_NAME\",\
\"message\": \"Command name is invalid\"\
}\
]\
}\
}\
}\
}\
}";
error_message_success = (error_test.get_error().human_readable == "50035: Invalid Form Body\n\t- options[0].name: String value did not match validation regex. (STRING_TYPE_REGEX)\n\t- options[0].name: Command name is invalid (APPLICATION_COMMAND_INVALID_NAME)");

error_test.http_info.body = "{\
\"message\": \"Invalid Form Body\",\
\"code\": 50035,\
\"errors\": {\
\"type\": {\
\"_errors\": [\
{\
\"code\": \"BASE_TYPE_CHOICES\",\
\"message\": \"Value must be one of {4, 5, 9, 10, 11}.\"\
}\
]\
}\
}\
}";
error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - type: Value must be one of {4, 5, 9, 10, 11}. (BASE_TYPE_CHOICES)");

error_test.http_info.body = "{\
\"message\": \"Unknown Guild\",\
\"code\": 10004\
}";
error_message_success = (error_message_success && error_test.get_error().human_readable == "10004: Unknown Guild");

error_test.http_info.body = "{\
\"message\": \"Invalid Form Body\",\
\"code\": 50035,\
\"errors\": {\
\"allowed_mentions\": {\
\"_errors\": [\
{\
\"code\": \"MESSAGE_ALLOWED_MENTIONS_PARSE_EXCLUSIVE\",\
\"message\": \"parse:[\\\"users\\\"] and users: [ids...] are mutually exclusive.\"\
}\
]\
}\
}\
}";
error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - allowed_mentions: parse:[\"users\"] and users: [ids...] are mutually exclusive. (MESSAGE_ALLOWED_MENTIONS_PARSE_EXCLUSIVE)");

error_test.http_info.body = "{\
\"message\": \"Invalid Form Body\",\
\"code\": 50035,\
\"errors\": {\
\"1\": {\
\"options\": {\
\"1\": {\
\"description\": {\
\"_errors\": [\
{\
\"code\": \"BASE_TYPE_BAD_LENGTH\",\
\"message\": \"Must be between 1 and 100 in length.\"\
}\
]\
}\
}\
}\
}\
}\
}";
error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - <array>[1].options[1].description: Must be between 1 and 100 in length. (BASE_TYPE_BAD_LENGTH)");

set_test(ERRORS, error_message_success);
errors_unit_tests();

http_unit_tests(token);

Expand Down Expand Up @@ -514,33 +414,6 @@ int main(int argc, char *argv[])
);
}

{ // avatar size function
set_test(UTILITY_AVATAR_SIZE, false);
bool success = false;
success = dpp::utility::avatar_size(0).empty();
success = dpp::utility::avatar_size(16) == "?size=16" && success;
success = dpp::utility::avatar_size(256) == "?size=256" && success;
success = dpp::utility::avatar_size(4096) == "?size=4096" && success;
success = dpp::utility::avatar_size(8192).empty() && success;
success = dpp::utility::avatar_size(3000).empty() && success;
set_test(UTILITY_AVATAR_SIZE, success);
}

{ // cdn endpoint url getter
set_test(UTILITY_CDN_ENDPOINT_URL_HASH, false);
bool success = false;
success = dpp::utility::cdn_endpoint_url_hash({ dpp::i_png }, "foobar/test", "", dpp::i_jpg, 0).empty();
success = dpp::utility::cdn_endpoint_url_hash({ dpp::i_png }, "foobar/test", "", dpp::i_png, 0) == "https://cdn.discordapp.com/foobar/test.png" && success;
success = dpp::utility::cdn_endpoint_url_hash({ dpp::i_png }, "foobar/test", "", dpp::i_png, 128) == "https://cdn.discordapp.com/foobar/test.png?size=128" && success;
success = dpp::utility::cdn_endpoint_url_hash({ dpp::i_png, dpp::i_gif }, "foobar/test", "12345", dpp::i_gif, 0, false, true) == "https://cdn.discordapp.com/foobar/test/a_12345.gif" && success;
success = dpp::utility::cdn_endpoint_url_hash({ dpp::i_png, dpp::i_gif }, "foobar/test", "12345", dpp::i_png, 0, false, true) == "https://cdn.discordapp.com/foobar/test/a_12345.png" && success;
success = dpp::utility::cdn_endpoint_url_hash({ dpp::i_png, dpp::i_gif }, "foobar/test", "12345", dpp::i_png, 0, false, false) == "https://cdn.discordapp.com/foobar/test/12345.png" && success;
success = dpp::utility::cdn_endpoint_url_hash({ dpp::i_png, dpp::i_gif }, "foobar/test", "12345", dpp::i_png, 0, true, true) == "https://cdn.discordapp.com/foobar/test/a_12345.gif" && success;
success = dpp::utility::cdn_endpoint_url_hash({ dpp::i_png, dpp::i_gif }, "foobar/test", "", dpp::i_png, 0, true, true) == "https://cdn.discordapp.com/foobar/test.gif" && success;
success = dpp::utility::cdn_endpoint_url_hash({ dpp::i_png, dpp::i_gif }, "foobar/test", "", dpp::i_gif, 0, false, false).empty() && success;
set_test(UTILITY_CDN_ENDPOINT_URL_HASH, success);
}

{ // sticker url getter
set_test(STICKER_GET_URL, false);
dpp::sticker s;
Expand Down Expand Up @@ -666,116 +539,6 @@ int main(int argc, char *argv[])
);
}

{ // utility methods
set_test(UTILITY_GUILD_NAVIGATION, false);
auto gn1 = dpp::utility::guild_navigation(123, dpp::utility::gnt_customize);
auto gn2 = dpp::utility::guild_navigation(1234, dpp::utility::gnt_browse);
auto gn3 = dpp::utility::guild_navigation(12345, dpp::utility::gnt_guide);
set_test(UTILITY_GUILD_NAVIGATION, gn1 == "<123:customize>" && gn2 == "<1234:browse>" && gn3 == "<12345:guide>");

set_test(UTILITY_ICONHASH, false);
auto iconhash1 = dpp::utility::iconhash("a_5532c6414c70765a28cf9448c117205f");
set_test(UTILITY_ICONHASH, iconhash1.first == 6139187225817019994 &&
iconhash1.second == 2940732121894297695 &&
iconhash1.to_string() == "5532c6414c70765a28cf9448c117205f"
);

set_test(UTILITY_MAKE_URL_PARAMETERS, false);
auto url_params1 = dpp::utility::make_url_parameters({
{"foo", 15},
{"bar", 7}
});
auto url_params2 = dpp::utility::make_url_parameters({
{"foo", "hello"},
{"bar", "two words"}
});
set_test(UTILITY_MAKE_URL_PARAMETERS, url_params1 == "?bar=7&foo=15" && url_params2 == "?bar=two%20words&foo=hello");

set_test(UTILITY_MARKDOWN_ESCAPE, false);
auto escaped = dpp::utility::markdown_escape(
"> this is a quote\n"
"**some bold text**");
set_test(UTILITY_MARKDOWN_ESCAPE, "\\>this is a quote\\n\\*\\*some bold text\\*\\*");

set_test(UTILITY_TOKENIZE, false);
auto tokens = dpp::utility::tokenize("some Whitespace seperated Text to Tokenize", " ");
std::vector<std::string> expected_tokens = {"some", "Whitespace", "seperated", "Text", "to", "Tokenize"};
set_test(UTILITY_TOKENIZE, tokens == expected_tokens);

set_test(UTILITY_URL_ENCODE, false);
auto url_encoded = dpp::utility::url_encode("S2-^$1Nd+U!g'8+_??o?p-bla bla");
set_test(UTILITY_URL_ENCODE, url_encoded == "S2-%5E%241Nd%2BU%21g%278%2B_%3F%3Fo%3Fp-bla%20bla");

set_test(UTILITY_SLASHCOMMAND_MENTION, false);
auto mention1 = dpp::utility::slashcommand_mention(123, "name");
auto mention2 = dpp::utility::slashcommand_mention(123, "name", "sub");
auto mention3 = dpp::utility::slashcommand_mention(123, "name", "group", "sub");
bool success = mention1 == "</name:123>" && mention2 == "</name sub:123>" && mention3 == "</name group sub:123>";
set_test(UTILITY_SLASHCOMMAND_MENTION, success);

set_test(UTILITY_CHANNEL_MENTION, false);
auto channel_mention = dpp::utility::channel_mention(123);
set_test(UTILITY_CHANNEL_MENTION, channel_mention == "<#123>");

set_test(UTILITY_USER_MENTION, false);
auto user_mention = dpp::utility::user_mention(123);
set_test(UTILITY_USER_MENTION, user_mention == "<@123>");

set_test(UTILITY_ROLE_MENTION, false);
auto role_mention = dpp::utility::role_mention(123);
set_test(UTILITY_ROLE_MENTION, role_mention == "<@&123>");

set_test(UTILITY_EMOJI_MENTION, false);
auto emoji_mention1 = dpp::utility::emoji_mention("role1", 123, false);
auto emoji_mention2 = dpp::utility::emoji_mention("role2", 234, true);
auto emoji_mention3 = dpp::utility::emoji_mention("white_check_mark", 0, false);
auto emoji_mention4 = dpp::utility::emoji_mention("white_check_mark", 0, true);
set_test(UTILITY_EMOJI_MENTION,
emoji_mention1 == "<:role1:123>" &&
emoji_mention2 == "<a:role2:234>" &&
emoji_mention3 == ":white_check_mark:" &&
emoji_mention4 == ":white_check_mark:"
);

set_test(UTILITY_USER_URL, false);
auto user_url = dpp::utility::user_url(123);
set_test(UTILITY_USER_URL,
user_url == dpp::utility::url_host + "/users/123" &&
dpp::utility::user_url(0) == ""
);

set_test(UTILITY_MESSAGE_URL, false);
auto message_url = dpp::utility::message_url(1,2,3);
set_test(UTILITY_MESSAGE_URL,
message_url == dpp::utility::url_host+ "/channels/1/2/3" &&
dpp::utility::message_url(0,2,3) == "" &&
dpp::utility::message_url(1,0,3) == "" &&
dpp::utility::message_url(1,2,0) == "" &&
dpp::utility::message_url(0,0,3) == "" &&
dpp::utility::message_url(0,2,0) == "" &&
dpp::utility::message_url(1,0,0) == "" &&
dpp::utility::message_url(0,0,0) == ""
);

set_test(UTILITY_CHANNEL_URL, false);
auto channel_url = dpp::utility::channel_url(1,2);
set_test(UTILITY_CHANNEL_URL,
channel_url == dpp::utility::url_host+ "/channels/1/2" &&
dpp::utility::channel_url(0,2) == "" &&
dpp::utility::channel_url(1,0) == "" &&
dpp::utility::channel_url(0,0) == ""
);

set_test(UTILITY_THREAD_URL, false);
auto thread_url = dpp::utility::thread_url(1,2);
set_test(UTILITY_THREAD_URL,
thread_url == dpp::utility::url_host+ "/channels/1/2" &&
dpp::utility::thread_url(0,2) == "" &&
dpp::utility::thread_url(1,0) == "" &&
dpp::utility::thread_url(0,0) == ""
);
}

{
set_test(TS, false);
dpp::managed m(189759562910400512);
Expand Down
Loading

0 comments on commit 8f368e0

Please sign in to comment.