summaryrefslogtreecommitdiff
path: root/battleNetApi/tests
diff options
context:
space:
mode:
authorAotrix <[email protected]>2026-07-29 18:31:43 +0200
committerAotrix <[email protected]>2026-07-29 18:31:43 +0200
commit1fbfd13f5d7220122b2a127627cc9105013b4893 (patch)
tree0ca51f463996ea96eddc5b8939c07d3ef207a311 /battleNetApi/tests
parent2c286a812a3bdaa2531e8f87f5503988bb331a78 (diff)
Add tests for HearthstoneTokenBuilder
Diffstat (limited to 'battleNetApi/tests')
-rw-r--r--battleNetApi/tests/CMakeLists.txt2
-rw-r--r--battleNetApi/tests/resources/credentials_expired_token.json10
-rw-r--r--battleNetApi/tests/src/tests.cpp4
-rw-r--r--battleNetApi/tests/src/tokenTests.cpp59
4 files changed, 66 insertions, 9 deletions
diff --git a/battleNetApi/tests/CMakeLists.txt b/battleNetApi/tests/CMakeLists.txt
index 6918fb5..b59fd8a 100644
--- a/battleNetApi/tests/CMakeLists.txt
+++ b/battleNetApi/tests/CMakeLists.txt
@@ -36,6 +36,8 @@ set(BATTLE_NET_API_DIR ${GIT_REPOSITORY_FOLDER_PATH}/battleNetApi)
set(INCLUDE_TOKEN_DIR ${BATTLE_NET_API_DIR}/include)
set(SRC_TOKEN_TESTS
${BATTLE_NET_API_DIR}/src/token.cpp
+ ${BATTLE_NET_API_DIR}/src/hearthstoneTokenBuilder.cpp
+ ${BATTLE_NET_API_DIR}/src/tokenBuilder.cpp
${BATTLE_NET_API_DIR}/tests/src/tokenTests.cpp
)
add_executable(tokenTests ${SRC_TOKEN_TESTS})
diff --git a/battleNetApi/tests/resources/credentials_expired_token.json b/battleNetApi/tests/resources/credentials_expired_token.json
new file mode 100644
index 0000000..a5ebb3b
--- /dev/null
+++ b/battleNetApi/tests/resources/credentials_expired_token.json
@@ -0,0 +1,10 @@
+{
+ "Agent": {
+ "Password": "MrMkhVMNdOeqPL4Q4XVQAsRPR14J7Yq6",
+ "Username": "2b50f1123b424d5e9c21971a110fdd04"
+ },
+ "Token": {
+ "Content": "EUaRHt538jT3Wco5A59cFF5LIQmH6eYRfG",
+ "ExpirationTime": 0
+ }
+} \ No newline at end of file
diff --git a/battleNetApi/tests/src/tests.cpp b/battleNetApi/tests/src/tests.cpp
index d3c09c7..edc0641 100644
--- a/battleNetApi/tests/src/tests.cpp
+++ b/battleNetApi/tests/src/tests.cpp
@@ -103,7 +103,7 @@ TEST_CASE("Retrieve pages 10 times with parallelization", "[retrievePagesParalle
REQUIRE(aNumberOfPages >= aNumberOfRetrievals);
for (int i{1}; i < aNumberOfRetrievals; ++i) {
aApiUrl.setPage(std::to_string(i + 1));
- aRequests.push_back(aApiUrl.text());
+ aRequests.emplace_back(aApiUrl.text());
}
std::vector<json> aApiResponses = HearthstoneApi::makeApiRequest(aRequests, aTokenText);
aApiResponses.emplace(aApiResponses.begin(), aFirstPageJson);
@@ -121,7 +121,7 @@ TEST_CASE("Merge two card lists from two page requests", "[Merge pages]") {
json::reference aMergedCards = aFirstPageJson["cards"];
for (auto &card : aSecondPageJson["cards"]) {
- aMergedCards.push_back(std::move(card));
+ aMergedCards.emplace_back(std::move(card));
}
REQUIRE(aMergedCards.size() == 32);
std::ofstream aOutputJson("build/output_pages_merge.json");
diff --git a/battleNetApi/tests/src/tokenTests.cpp b/battleNetApi/tests/src/tokenTests.cpp
index 2e2d5b4..b702024 100644
--- a/battleNetApi/tests/src/tokenTests.cpp
+++ b/battleNetApi/tests/src/tokenTests.cpp
@@ -6,18 +6,63 @@
#include <catch2/catch_approx.hpp>
#include <catch2/catch_config.hpp>
#include <catch2/catch_test_macros.hpp>
+#include <fstream>
+#include <hearthstoneTokenBuilder.hpp>
+#include <iomanip>
#include <nlohmann/json.hpp>
-#include <string>
#include <tokenBuilder.hpp>
TEST_CASE("Token", "[Token is expired]") {
- const std::string aCredentialsFile{"../tests/resources/credentials.json"};
- Token aToken(aCredentialsFile, std::chrono::seconds(0));
- REQUIRE(aToken.isExpired());
+ REQUIRE(bnet::Token{}.setText("").setLifetime(std::chrono::seconds{0}).isExpired());
}
TEST_CASE("Token", "[Token is not expired]") {
- const std::string aCredentialsFile{"../tests/resources/credentials.json"};
- Token aToken(aCredentialsFile, std::chrono::seconds(20));
- REQUIRE_FALSE(aToken.isExpired());
+ REQUIRE_FALSE(bnet::Token{}.setText("").setLifetime(std::chrono::seconds{20}).isExpired());
+}
+
+TEST_CASE("Token", "[Check token text]") {
+ bnet::Token aToken;
+ aToken.setText("currenttext");
+ REQUIRE(aToken.text() == "currenttext");
+}
+
+TEST_CASE("Token", "[Change token text]") {
+ bnet::Token aToken;
+ aToken.setText("currenttext");
+ REQUIRE(aToken.text() == "currenttext");
+ aToken.setText("newtext");
+ REQUIRE(aToken.text() == "newtext");
+}
+
+TEST_CASE("TokenBuilder", "[Retrieve token from JSON]") {
+ bnet::HearthstoneTokenBuilder aTokenBuilder{"resources/credentials.json"};
+ REQUIRE(aTokenBuilder.allowExpiredToken().buildToken().getToken());
+ REQUIRE(aTokenBuilder.getToken()->text() == "ThisIsTheContentOfTokenContenthehe");
+}
+
+TEST_CASE("TokenBuilder", "[Expired token in JSON file : generate new one]") {
+ bnet::HearthstoneTokenBuilder aTokenBuilder{"resources/credentials_expired_token.json"};
+ REQUIRE(aTokenBuilder.allowBattleNetApiCall().buildToken().getToken()->text()->length() > 20);
+}
+
+TEST_CASE("TokenBuilder", "[Overwrite file with generated token]") {
+ const std::string aCredentialsFilePath{"resources/credentials_expired_token.json"};
+ std::ifstream aCredentialsFileRead{aCredentialsFilePath};
+ nlohmann::json aCredentialsJson = nlohmann::json::parse(aCredentialsFileRead);
+ aCredentialsJson["Token"]["ExpirationTime"] = 0;
+ aCredentialsFileRead.close();
+ std::ofstream aCredentialsFileWrite{aCredentialsFilePath};
+ aCredentialsFileWrite << std::setw(4) << aCredentialsJson;
+ aCredentialsFileWrite.close();
+ bnet::HearthstoneTokenBuilder aTokenBuilder{aCredentialsFilePath};
+ REQUIRE(aTokenBuilder.allowBattleNetApiCall()
+ .allowUpdateSourceFile()
+ .buildToken()
+ .getToken()
+ ->text()
+ ->length() > 20);
+ aCredentialsJson["Token"]["ExpirationTime"] = 0;
+ aCredentialsFileWrite.open(aCredentialsFilePath);
+ aCredentialsFileWrite << std::setw(4) << aCredentialsJson;
+ aCredentialsFileWrite.close();
}