summaryrefslogtreecommitdiff
path: root/battleNetApi/tests/src/tokenTests.cpp
blob: b7020245317a9bca6d7a4fc4c3f9b835b1fe8173 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/benchmark/catch_chronometer.hpp>
#include <catch2/benchmark/detail/catch_analyse.hpp>
#include <catch2/benchmark/detail/catch_benchmark_function.hpp>
#include <catch2/benchmark/detail/catch_estimate_clock.hpp>
#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 <tokenBuilder.hpp>

TEST_CASE("Token", "[Token is expired]") {
    REQUIRE(bnet::Token{}.setText("").setLifetime(std::chrono::seconds{0}).isExpired());
}

TEST_CASE("Token", "[Token is not expired]") {
    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();
}