Skip to content

Commit

Permalink
Database merging: migrate GUI test on Catch2
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaslenko committed Mar 15, 2024
1 parent ff2d32b commit 14d9ba9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 31 deletions.
30 changes: 0 additions & 30 deletions tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,36 +159,6 @@ void TestGui::cleanupTestCase()
m_dbFile.remove();
}

void TestGui::testMergeDatabase()
{
// It is safe to ignore the warning this line produces
QSignalSpy dbMergeSpy(m_dbWidget.data(), SIGNAL(databaseMerged(QSharedPointer<Database>)));
QApplication::processEvents();

// set file to merge from
fileDialog()->setNextFileName(QString(KEEPASSX_TEST_DATA_DIR).append("/MergeDatabase.kdbx"));
triggerAction("actionDatabaseMerge");

QTRY_COMPARE(QApplication::focusWidget()->objectName(), QString("passwordEdit"));
auto* editPasswordMerge = QApplication::focusWidget();
QVERIFY(editPasswordMerge->isVisible());

QTest::keyClicks(editPasswordMerge, "a");
QTest::keyClick(editPasswordMerge, Qt::Key_Enter);

QTRY_COMPARE(dbMergeSpy.count(), 1);
QTRY_VERIFY(m_tabWidget->tabText(m_tabWidget->currentIndex()).contains("*"));

m_db = m_tabWidget->currentDatabaseWidget()->database();

// there are seven child groups of the root group
QCOMPARE(m_db->rootGroup()->children().size(), 7);
// the merged group should contain an entry
QCOMPARE(m_db->rootGroup()->children().at(6)->entries().size(), 1);
// the General group contains one entry merged from the other db
QCOMPARE(m_db->rootGroup()->findChildByName("General")->entries().size(), 1);
}

void TestGui::testAutoreloadDatabase()
{
config()->set(Config::AutoReloadOnChange, false);
Expand Down
1 change: 0 additions & 1 deletion tests/gui/TestGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ private slots:
void cleanup();
void cleanupTestCase();

void testMergeDatabase();
void testAutoreloadDatabase();
void testEditEntry();
void testSearchEditEntry();
Expand Down
Binary file added tests2/data/MergeDatabase.kdbx
Binary file not shown.
1 change: 1 addition & 0 deletions tests2/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(TEST_GUI_SOURCES
TestAppSettingsGui.cpp
TestDbCreatingGui.cpp
TestDbLockingGui.cpp
TestDbMergingGui.cpp
TestDbSavingGui.cpp
TestDbSettingsGui.cpp
TestTotpGui.cpp)
Expand Down
65 changes: 65 additions & 0 deletions tests2/gui/TestDbMergingGui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2024 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../streams.h" // for printable logs on QStrings verification
#include "config-keepassx-tests.h"

#include <QApplication>
#include <QSignalSpy>
#include <QTest>

#include "FixtureWithDb.h"
#include "catch2/catch_all.hpp"
#include "gui/DatabaseTabWidget.h"
#include "gui/FileDialog.h"

SCENARIO_METHOD(FixtureWithDb, "Test Merging from a Database", "[gui]")
{
// It is safe to ignore the warning this line produces
QSignalSpy dbMergeSpy(m_dbWidget.data(), SIGNAL(databaseMerged(QSharedPointer<Database>)));
QApplication::processEvents();

WHEN("User opens a file to merge from and enters a correct password")
{
fileDialog()->setNextFileName(QString(KEEPASSX_TEST_DATA_DIR).append("/MergeDatabase.kdbx"));
triggerAction("actionDatabaseMerge");

auto* editPasswordMerge = QApplication::focusWidget();
REQUIRE(editPasswordMerge->objectName() == QString("passwordEdit"));
REQUIRE(editPasswordMerge->isVisible());

QTest::keyClicks(editPasswordMerge, "a");
QTest::keyClick(editPasswordMerge, Qt::Key_Enter);
wait(250);

THEN("The current DB was merged successfully")
{
REQUIRE(dbMergeSpy.count() == 1);
REQUIRE(m_tabWidget->tabText(m_tabWidget->currentIndex()).contains("*"));

m_db = m_tabWidget->currentDatabaseWidget()->database();

// there are seven child groups of the root group
REQUIRE(m_db->rootGroup()->children().size() == 7);

// the merged group should contain an entry
REQUIRE(m_db->rootGroup()->children().at(6)->entries().size() == 1);

// the General group contains one entry merged from the other db
REQUIRE(m_db->rootGroup()->findChildByName("General")->entries().size() == 1);
}
}
}

0 comments on commit 14d9ba9

Please sign in to comment.