diff options
Diffstat (limited to 'battleNetApi/tests/src/tokenTests.cpp')
| -rw-r--r-- | battleNetApi/tests/src/tokenTests.cpp | 59 |
1 files changed, 52 insertions, 7 deletions
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(); } |
