status-desktop/libs/Onboarding/tests/OnboardingTestHelpers/ScopedTestAccount.cpp

188 lines
6.2 KiB
C++
Raw Normal View History

#include "ScopedTestAccount.h"
#include <Constants.h>
#include <IOTestHelpers.h>
#include <Onboarding/Accounts/AccountsService.h>
#include <Onboarding/OnboardingController.h>
#include <StatusGo/Accounts/Accounts.h>
#include <StatusGo/Accounts/AccountsAPI.h>
#include <Helpers/helpers.h>
#include <QCoreApplication>
namespace Testing = Status::Testing;
namespace Onboarding = Status::Onboarding;
namespace Accounts = Status::StatusGo::Accounts;
namespace fs = std::filesystem;
2022-10-19 13:41:53 +00:00
namespace Status::Testing
{
2022-10-19 13:41:53 +00:00
ScopedTestAccount::ScopedTestAccount(const std::string& tempTestSubfolderName,
const QString& accountName,
const QString& accountPassword)
: m_fusedTestFolder{std::make_unique<AutoCleanTempTestDir>(tempTestSubfolderName)}
, m_accountName(accountName)
, m_accountPassword(accountPassword)
{
int argc = 1;
std::string appName{"test"};
char* args[] = {appName.data()};
m_app = std::make_unique<QCoreApplication>(argc, reinterpret_cast<char**>(args));
m_dataDirPath = m_fusedTestFolder->tempFolder() / Constants::statusGoDataDirName;
fs::create_directory(m_dataDirPath);
// Setup accounts
auto accountsService = std::make_shared<Onboarding::AccountsService>();
auto result = accountsService->init(m_dataDirPath);
if(!result)
2022-10-19 13:41:53 +00:00
{
throw std::runtime_error("ScopedTestAccount - Failed to create temporary test account");
2022-10-19 13:41:53 +00:00
}
// TODO refactor and merge account creation events with login into Onboarding controller
//
// Create Login early to register and not miss onLoggedIn event signal from setupAccountAndLogin
//
// Beware, smartpointer is a requirement
m_onboarding = Helpers::makeSharedQObject<Onboarding::OnboardingController>(accountsService);
if(m_onboarding->getOpenedAccounts().size() != 0)
2022-10-19 13:41:53 +00:00
{
throw std::runtime_error("ScopedTestAccount - already have opened account");
2022-10-19 13:41:53 +00:00
}
int accountLoggedInCount = 0;
QObject::connect(m_onboarding.get(),
&Onboarding::OnboardingController::accountLoggedIn,
m_app.get(),
[&accountLoggedInCount]() { accountLoggedInCount++; });
bool accountLoggedInError = false;
2022-10-19 13:41:53 +00:00
QObject::connect(m_onboarding.get(),
&Onboarding::OnboardingController::accountLoginError,
m_app.get(),
2022-10-19 13:41:53 +00:00
[&accountLoggedInError]() { accountLoggedInError = true; });
// Create Accounts
auto genAccounts = accountsService->generatedAccounts();
if(genAccounts.size() == 0)
2022-10-19 13:41:53 +00:00
{
throw std::runtime_error("ScopedTestAccount - missing generated accounts");
2022-10-19 13:41:53 +00:00
}
if(accountsService->isFirstTimeAccountLogin())
2022-10-19 13:41:53 +00:00
{
throw std::runtime_error("ScopedTestAccount - Service::isFirstTimeAccountLogin returned true");
2022-10-19 13:41:53 +00:00
}
if(!accountsService->setupAccountAndLogin(genAccounts[0].id, m_accountPassword, m_accountName))
2022-10-19 13:41:53 +00:00
{
throw std::runtime_error("ScopedTestAccount - Service::setupAccountAndLogin failed");
2022-10-19 13:41:53 +00:00
}
if(!accountsService->isFirstTimeAccountLogin())
2022-10-19 13:41:53 +00:00
{
throw std::runtime_error("ScopedTestAccount - Service::isFirstTimeAccountLogin returned false");
2022-10-19 13:41:53 +00:00
}
if(!accountsService->getLoggedInAccount().isValid())
2022-10-19 13:41:53 +00:00
{
throw std::runtime_error("ScopedTestAccount - newly created account is not valid");
2022-10-19 13:41:53 +00:00
}
if(accountsService->getLoggedInAccount().name != accountName)
2022-10-19 13:41:53 +00:00
{
throw std::runtime_error("ScopedTestAccount - newly created account has a wrong name");
2022-10-19 13:41:53 +00:00
}
processMessages(2000, [accountLoggedInCount]() { return accountLoggedInCount == 0; });
if(accountLoggedInError)
2022-10-19 13:41:53 +00:00
{
throw std::runtime_error("ScopedTestAccount - account loggedin error");
2022-10-19 13:41:53 +00:00
}
if(accountLoggedInCount != 1)
{
throw std::runtime_error("ScopedTestAccount - missing confirmation of account creation");
}
}
ScopedTestAccount::~ScopedTestAccount()
{
const auto rootAccount = m_onboarding->accountsService()->getLoggedInAccount();
m_onboarding->accountsService()->deleteMultiAccount(rootAccount);
}
2022-10-19 13:41:53 +00:00
void ScopedTestAccount::processMessages(size_t maxWaitTimeMillis, std::function<bool()> shouldWaitUntilTimeout)
{
using namespace std::chrono_literals;
std::chrono::milliseconds maxWaitTime{maxWaitTimeMillis};
auto iterationSleepTime = 2ms;
2022-10-19 13:41:53 +00:00
auto remainingIterations = maxWaitTime / iterationSleepTime;
while(remainingIterations-- > 0 && shouldWaitUntilTimeout())
{
QCoreApplication::sendPostedEvents();
std::this_thread::sleep_for(iterationSleepTime);
}
// Provide chance to exit slot processing after we set the condition that might trigger shouldWaitUntilTimeout to false
std::this_thread::sleep_for(iterationSleepTime);
}
void ScopedTestAccount::logOut()
{
if(Status::StatusGo::Accounts::logout().containsError())
2022-10-19 13:41:53 +00:00
{
throw std::runtime_error("ScopedTestAccount - failed logging out");
2022-10-19 13:41:53 +00:00
}
}
Accounts::ChatOrWalletAccount ScopedTestAccount::firstChatAccount()
{
auto accounts = Accounts::getAccounts();
2022-10-19 13:41:53 +00:00
auto chatIt = std::find_if(accounts.begin(), accounts.end(), [](const auto& a) { return a.isChat; });
if(chatIt == accounts.end())
2022-10-19 13:41:53 +00:00
{
throw std::runtime_error("ScopedTestAccount::chatAccount: account not found");
2022-10-19 13:41:53 +00:00
}
return *chatIt;
}
Accounts::ChatOrWalletAccount ScopedTestAccount::firstWalletAccount()
{
auto accounts = Accounts::getAccounts();
2022-10-19 13:41:53 +00:00
auto walletIt = std::find_if(accounts.begin(), accounts.end(), [](const auto& a) { return a.isWallet; });
if(walletIt == accounts.end())
2022-10-19 13:41:53 +00:00
{
throw std::runtime_error("ScopedTestAccount::firstWalletAccount: account not found");
2022-10-19 13:41:53 +00:00
}
return *walletIt;
}
2022-10-19 13:41:53 +00:00
const Onboarding::MultiAccount& ScopedTestAccount::loggedInAccount() const
{
return m_onboarding->accountsService()->getLoggedInAccount();
}
2022-10-19 13:41:53 +00:00
Onboarding::OnboardingController* ScopedTestAccount::onboardingController() const
{
return m_onboarding.get();
}
2022-10-19 13:41:53 +00:00
const std::filesystem::path& ScopedTestAccount::fusedTestFolder() const
{
return m_fusedTestFolder->tempFolder();
}
const std::filesystem::path& ScopedTestAccount::testDataDir() const
{
return m_dataDirPath;
}
2022-10-19 13:41:53 +00:00
} // namespace Status::Testing