Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide WinRT - Part II #290

Merged
merged 12 commits into from
Sep 20, 2023
2 changes: 1 addition & 1 deletion storeapi/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set(StoreApi_SRCS
"../base/Exception.hpp"
"../base/Purchase.hpp"
"../base/StoreService.hpp"
"../agent/ServerStoreService.cpp"
"../agent/ServerStoreService.hpp"
"../gui/ClientStoreService.hpp"
)

Expand Down
39 changes: 20 additions & 19 deletions storeapi/test/ServerStoreServiceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,42 @@ namespace StoreApi {

using namespace ::testing;

TEST(UserInfo, PredictableSizes) {
auto user = UserInfo::Current().get();
auto size = user.id.size();
EXPECT_TRUE(size == 0 || size == 64)
<< "User ID of unexpected size: " << size << " <"
<< winrt::to_string(user.id) << '\n';
TEST(ServerStoreService, NoUsersLikeInCI) {
auto service = ServerStoreService<NoUsers>{};
EXPECT_THROW({auto user = service.CurrentUserInfo();}, Exception);
}

TEST(ServerStoreService, TooManyUsers) {
auto service = ServerStoreService<TooManyUsersContext>{};
EXPECT_THROW({auto user = service.CurrentUserInfo();}, Exception);
}

TEST(ServerStoreService, FindOneUser) {
static constexpr char goodHash[] = "goodHash";
auto service = ServerStoreService<FindOneUser>{};
FindOneUser::goodHash = goodHash;
auto user = service.CurrentUserInfo();
EXPECT_EQ(user.id, goodHash);
}

TEST(ServerStoreService, EmptyJwtThrows) {
auto service = ServerStoreService<EmptyJwtContext>{};
UserInfo user{.id = L"[email protected]"};
UserInfo user{.id = "[email protected]"};
EXPECT_THROW(
{
auto jwt = service.GenerateUserJwt("this-is-a-web-token", user).get();
auto jwt = service.GenerateUserJwt("this-is-a-web-token", user);
},
Exception);
}

TEST(ServerStoreService, NonEmptyJwtNeverThrows) {
auto service = ServerStoreService<IdentityJwtContext>{};
UserInfo user{.id = L"[email protected]"};
UserInfo user{.id = "[email protected]"};
std::string token{"this-is-a-web-token"};
auto jwt = service.GenerateUserJwt(token, user).get();
auto jwt = service.GenerateUserJwt(token, user);
EXPECT_EQ(token, jwt);
}

TEST(ServerStoreService, RealServerFailsUnderTest) {
auto service = ServerStoreService{};
UserInfo user{.id = L"[email protected]"};
std::string token{"this-is-a-web-token"};
// This fails because the test is not an app deployed through the store.
EXPECT_THROW({ auto jwt = service.GenerateUserJwt(token, user).get(); },
Exception);
}

TEST(ServerStoreService, ExpirationDateUnsubscribed) {
auto service = ServerStoreService<NeverSubscribedContext>{};

Expand Down
63 changes: 36 additions & 27 deletions storeapi/test/stubs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@

#include <base/Exception.hpp>
#include <base/Purchase.hpp>
// For WinRT basic types and coroutines.
#include <winrt/windows.foundation.h>
// For non-WinRT coroutines
#include <pplawait.h>

// Win32 APIs, such as the Timezone
#include <windows.h>

#include <chrono>
#include <functional>
#include <span>
#include <stdexcept>
#include <string>
#include <vector>
// For timegm
#include <time.h>

#if defined _MSC_VER
#include <time.h>
#include <windows.h>
#define timegm _mkgmtime
#endif
Expand Down Expand Up @@ -89,9 +82,8 @@ struct EmptyJwtContext {
return {Product{.kind = kinds[0], .id = ids[0]}};
}

winrt::Windows::Foundation::IAsyncOperation<winrt::hstring> GenerateUserJwt(
winrt::hstring hToken, winrt::hstring hUserId) {
co_return {};
std::string GenerateUserJwt(std::string hToken, std::string hUserId) const {
return {};
}
};

Expand All @@ -107,9 +99,8 @@ struct IdentityJwtContext {
return {Product{.kind = kinds[0], .id = ids[0]}};
}

winrt::Windows::Foundation::IAsyncOperation<winrt::hstring> GenerateUserJwt(
winrt::hstring hToken, winrt::hstring hUserId) {
co_return hToken;
std::string GenerateUserJwt(std::string hToken, std::string hUserId) const {
return hToken;
}
};

Expand All @@ -132,9 +123,8 @@ struct NeverSubscribedContext {
return {Product{.kind = kinds[0], .id = ids[0]}};
}

winrt::Windows::Foundation::IAsyncOperation<winrt::hstring> GenerateUserJwt(
winrt::hstring hToken, winrt::hstring hUserId) {
co_return hToken;
std::string GenerateUserJwt(std::string hToken, std::string hUserId) const {
return hToken;
}
};

Expand All @@ -158,9 +148,8 @@ struct UnixEpochContext {
return {Product{.kind = kinds[0], .id = ids[0]}};
}

winrt::Windows::Foundation::IAsyncOperation<winrt::hstring> GenerateUserJwt(
winrt::hstring hToken, winrt::hstring hUserId) {
co_return hToken;
std::string GenerateUserJwt(std::string hToken, std::string hUserId) const {
return hToken;
}
};

Expand All @@ -187,9 +176,8 @@ struct AlreadyPurchasedContext {
return {Product{.kind = kinds[0], .id = ids[0]}};
}

winrt::Windows::Foundation::IAsyncOperation<winrt::hstring> GenerateUserJwt(
winrt::hstring hToken, winrt::hstring hUserId) {
co_return hToken;
std::string GenerateUserJwt(std::string hToken, std::string hUserId) const {
return hToken;
}

// noop
Expand Down Expand Up @@ -219,11 +207,32 @@ struct PurchaseSuccessContext {
return {Product{.kind = kinds[0], .id = ids[0]}};
}

winrt::Windows::Foundation::IAsyncOperation<winrt::hstring> GenerateUserJwt(
winrt::hstring hToken, winrt::hstring hUserId) {
co_return hToken;
std::string GenerateUserJwt(std::string hToken, std::string hUserId) const {
return hToken;
}

// noop
void InitDialogs(Window window) {}
};

struct TooManyUsersContext {
struct Product {};
std::vector<std::string> AllLocallyAuthenticatedUserHashes() const {
return {"first-user", "second-user"};
}
};

struct NoUsers {
struct Product {};
std::vector<std::string> AllLocallyAuthenticatedUserHashes() const {
return {};
}
};

struct FindOneUser {
struct Product {};
static inline std::string goodHash{};
std::vector<std::string> AllLocallyAuthenticatedUserHashes() const {
return {goodHash};
}
};
EduardGomezEscandell marked this conversation as resolved.
Show resolved Hide resolved
Loading