Improve remote sync tests

This commit is contained in:
Jonathan White 2025-02-23 17:05:54 -05:00
parent af2d0b1429
commit 244ed42231
4 changed files with 36 additions and 35 deletions

View File

@ -375,26 +375,27 @@ void TestGui::testMergeDatabase()
QCOMPARE(m_db->rootGroup()->findChildByName("General")->entries().size(), 1); QCOMPARE(m_db->rootGroup()->findChildByName("General")->entries().size(), 1);
} }
void TestGui::prepareAndTriggerRemoteSync(const QString& sourceToSync) void TestGui::prepareAndTriggerRemoteSync()
{ {
auto* menuRemoteSync = m_mainWindow->findChild<QMenu*>("menuRemoteSync");
QSignalSpy remoteAboutToShow(menuRemoteSync, &QMenu::aboutToShow);
QApplication::processEvents();
// create remote settings in settings dialog // create remote settings in settings dialog
triggerAction("actionDatabaseSettings"); triggerAction("actionDatabaseSettings");
auto* dbSettingsDialog = m_dbWidget->findChild<QWidget*>("databaseSettingsDialog");
auto* dbSettingsCategoryList = dbSettingsDialog->findChild<CategoryListWidget*>("categoryList"); auto dbSettingsDialog = m_dbWidget->findChild<DatabaseSettingsDialog*>("databaseSettingsDialog");
auto* dbSettingsStackedWidget = dbSettingsDialog->findChild<QStackedWidget*>("stackedWidget"); QVERIFY(dbSettingsDialog);
dbSettingsCategoryList->setCurrentCategory(2); // go into remote category dbSettingsDialog->showRemoteSettings();
auto name = "testCommand"; auto name = "testCommand";
auto* nameEdit = dbSettingsStackedWidget->findChild<QLineEdit*>("nameLineEdit"); auto nameEdit = dbSettingsDialog->findChild<QLineEdit*>("nameLineEdit");
auto* downloadCommandEdit = dbSettingsStackedWidget->findChild<QLineEdit*>("downloadCommand"); QVERIFY(nameEdit);
QVERIFY(downloadCommandEdit != nullptr); QVERIFY(nameEdit->isVisible());
downloadCommandEdit->setText(sourceToSync);
nameEdit->setText(name); nameEdit->setText(name);
auto* saveSettingsButton = dbSettingsStackedWidget->findChild<QPushButton*>("saveSettingsButton");
QVERIFY(saveSettingsButton != nullptr); auto downloadCommandEdit = dbSettingsDialog->findChild<QLineEdit*>("downloadCommand");
QVERIFY(downloadCommandEdit);
downloadCommandEdit->setText("sftp user@server:Database.kdbx");
auto saveSettingsButton = dbSettingsDialog->findChild<QPushButton*>("saveSettingsButton");
QVERIFY(saveSettingsButton);
QTest::mouseClick(saveSettingsButton, Qt::LeftButton); QTest::mouseClick(saveSettingsButton, Qt::LeftButton);
auto okButton = dbSettingsDialog->findChild<QDialogButtonBox*>("buttonBox")->button(QDialogButtonBox::Ok); auto okButton = dbSettingsDialog->findChild<QDialogButtonBox*>("buttonBox")->button(QDialogButtonBox::Ok);
@ -403,32 +404,33 @@ void TestGui::prepareAndTriggerRemoteSync(const QString& sourceToSync)
QTRY_COMPARE(m_dbWidget->getRemoteParams().size(), 1); QTRY_COMPARE(m_dbWidget->getRemoteParams().size(), 1);
// trigger aboutToShow to create remote actions // Show menu to trigger populating with remote sync action
menuRemoteSync->popup(QPoint(0, 0)); auto menuRemoteSync = m_mainWindow->findChild<QMenu*>("menuRemoteSync");
QVERIFY(menuRemoteSync);
menuRemoteSync->popup({0, 0});
QApplication::processEvents(); QApplication::processEvents();
QTRY_COMPARE(remoteAboutToShow.count(), 1); menuRemoteSync->close();
// close the opened menu
QTest::keyClick(menuRemoteSync, Qt::Key::Key_Escape);
// trigger remote sync action // Trigger the remote sync action
for (auto* remoteAction : menuRemoteSync->actions()) { for (const auto remoteAction : menuRemoteSync->actions()) {
if (remoteAction->text() == name) { if (remoteAction->text() == name) {
remoteAction->trigger(); remoteAction->trigger();
break; return;
} }
} }
QApplication::processEvents();
// If we get here then something didn't work properly
QFAIL("Remote sync action not present in menu.");
} }
void TestGui::testRemoteSyncDatabaseSameKey() void TestGui::testRemoteSyncDatabaseSameKey()
{ {
QString sourceToSync = "sftp user@server:Database.kdbx"; RemoteHandler::setRemoteProcessFunc([](QObject* parent) {
RemoteHandler::setRemoteProcessFunc([sourceToSync](QObject* parent) {
return QScopedPointer<RemoteProcess>( return QScopedPointer<RemoteProcess>(
new MockRemoteProcess(parent, QString(KEEPASSX_TEST_DATA_DIR).append("/SyncDatabase.kdbx"))); new MockRemoteProcess(parent, QString(KEEPASSX_TEST_DATA_DIR).append("/SyncDatabase.kdbx")));
}); });
QSignalSpy dbSyncSpy(m_dbWidget.data(), &DatabaseWidget::databaseSyncCompleted); QSignalSpy dbSyncSpy(m_dbWidget.data(), &DatabaseWidget::databaseSyncCompleted);
prepareAndTriggerRemoteSync(sourceToSync); prepareAndTriggerRemoteSync();
QTRY_COMPARE(dbSyncSpy.count(), 1); QTRY_COMPARE(dbSyncSpy.count(), 1);
m_db = m_tabWidget->currentDatabaseWidget()->database(); m_db = m_tabWidget->currentDatabaseWidget()->database();
@ -443,13 +445,12 @@ void TestGui::testRemoteSyncDatabaseSameKey()
void TestGui::testRemoteSyncDatabaseRequiresPassword() void TestGui::testRemoteSyncDatabaseRequiresPassword()
{ {
QString sourceToSync = "sftp user@server:Database.kdbx"; RemoteHandler::setRemoteProcessFunc([](QObject* parent) {
RemoteHandler::setRemoteProcessFunc([sourceToSync](QObject* parent) {
return QScopedPointer<RemoteProcess>(new MockRemoteProcess( return QScopedPointer<RemoteProcess>(new MockRemoteProcess(
parent, QString(KEEPASSX_TEST_DATA_DIR).append("/SyncDatabaseDifferentPassword.kdbx"))); parent, QString(KEEPASSX_TEST_DATA_DIR).append("/SyncDatabaseDifferentPassword.kdbx")));
}); });
QSignalSpy dbSyncSpy(m_dbWidget.data(), &DatabaseWidget::databaseSyncCompleted); QSignalSpy dbSyncSpy(m_dbWidget.data(), &DatabaseWidget::databaseSyncCompleted);
prepareAndTriggerRemoteSync(sourceToSync); prepareAndTriggerRemoteSync();
// need to process more events as opening with the same key did not work and more events have been fired // need to process more events as opening with the same key did not work and more events have been fired
QApplication::processEvents(QEventLoop::WaitForMoreEvents); QApplication::processEvents(QEventLoop::WaitForMoreEvents);

View File

@ -89,7 +89,7 @@ private:
Qt::KeyboardModifiers stateKey = {}); Qt::KeyboardModifiers stateKey = {});
void checkSaveDatabase(); void checkSaveDatabase();
void checkStatusBarText(const QString& textFragment); void checkStatusBarText(const QString& textFragment);
void prepareAndTriggerRemoteSync(const QString& sourceToSync); void prepareAndTriggerRemoteSync();
QScopedPointer<MainWindow> m_mainWindow; QScopedPointer<MainWindow> m_mainWindow;
QPointer<QLabel> m_statusBarLabel; QPointer<QLabel> m_statusBarLabel;

View File

@ -19,9 +19,9 @@
#include "MockRemoteProcess.h" #include "MockRemoteProcess.h"
MockRemoteProcess::MockRemoteProcess(QObject* parent, const QString& dbPath) MockRemoteProcess::MockRemoteProcess(QObject* parent, QString dbPath)
: RemoteProcess(parent) : RemoteProcess(parent)
, m_dbPath(dbPath) , m_dbPath(std::move(dbPath))
{ {
} }

View File

@ -23,7 +23,7 @@
class MockRemoteProcess : public RemoteProcess class MockRemoteProcess : public RemoteProcess
{ {
public: public:
explicit MockRemoteProcess(QObject* parent, const QString& dbPath); explicit MockRemoteProcess(QObject* parent, QString dbPath);
~MockRemoteProcess() override = default; ~MockRemoteProcess() override = default;
void start(const QString& program) override; void start(const QString& program) override;