Skip to content

Commit

Permalink
Add test for searching Entries via UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde committed May 20, 2020
1 parent ceafaf7 commit 805f8c7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/browser/BrowserService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,15 +980,19 @@ bool BrowserService::removeFirstDomain(QString& hostname)
* Otherwise, this simply delegates to handleURL(). */
bool BrowserService::handleEntry(Entry* entry, const QString& url, const QString& submitUrl)
{
qDebug() << "Handle entry: " << entry << " url: " << url;

// Use this special scheme to find entries by UUID
if (url.startsWith("keepassxc://")) {
qDebug() << "Keepassxc " << ("keepassxc://by-uuid/" + entry->uuidToHex());
return ("keepassxc://by-uuid/" + entry->uuidToHex()) == url;
}
return handleURL(entry->url(), url, submitUrl);
}

bool BrowserService::handleURL(const QString& entryUrl, const QString& url, const QString& submitUrl)
{
qDebug() << "Handle URL: " << entryUrl << " url: " << url;
if (entryUrl.isEmpty()) {
return false;
}
Expand Down
52 changes: 52 additions & 0 deletions tests/TestBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,57 @@ void TestBrowser::testSearchEntries()
QCOMPARE(result[3]->url(), QString("github.com/login"));
}

void TestBrowser::testSearchEntriesByUUID()
{
auto db = QSharedPointer<Database>::create();
auto* root = db->rootGroup();

/* The URLs don't really matter for this test, we just need some entries */
QStringList urls = {"https://github.com/login_page",
"https://github.com/login",
"https://github.com/",
"github.com/login",
"http://github.com",
"https://github.com/login",
"github.com",
"github.com/login",
"https://github",
"github.com",
"",
"not an URL"};
auto entries = createEntries(urls, root);

for (Entry* entry : entries) {
QString testUrl = "keepassxc://by-uuid/" + entry->uuidToHex();
/* Look for an entry with that UUID. First using handleEntry, then through the search */
QCOMPARE(m_browserService->handleEntry(entry, testUrl, ""), true);
auto result = m_browserService->searchEntries(db, testUrl, "");
qDebug() << "Browser result: " << result;
// QCOMPARE(result.length(), 1);
// QCOMPARE(result[0], entry);
}

/* Test for entries that don't exist */
QStringList uuids = {"00000000000000000000000000000000",
"00000000000000000000000000000001",
"00000000000000000000000000000002/",
"invalid uuid",
"000000000000000000000000000000000000000"
"00000000000000000000000"};

for (QString uuid : uuids) {
QString testUrl = "keepassxc://by-uuid/" + uuid;

for (Entry* entry : entries) {
QCOMPARE(m_browserService->handleEntry(entry, testUrl, ""), false);
}

auto result = m_browserService->searchEntries(db, testUrl, "");
qDebug() << "Browser result 2: " << result;
QCOMPARE(result.length(), 0);
}
}

void TestBrowser::testSearchEntriesWithPort()
{
auto db = QSharedPointer<Database>::create();
Expand Down Expand Up @@ -404,6 +455,7 @@ QList<Entry*> TestBrowser::createEntries(QStringList& urls, Group* root) const
entry->beginUpdate();
entry->setUrl(urls[i]);
entry->setUsername(QString("User %1").arg(i));
entry->setUuid(QUuid::createUuid());
entry->endUpdate();
entries.push_back(entry);
}
Expand Down
1 change: 1 addition & 0 deletions tests/TestBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private slots:
void testBaseDomain();
void testSortPriority();
void testSearchEntries();
void testSearchEntriesByUUID();
void testSearchEntriesWithPort();
void testSearchEntriesWithAdditionalURLs();
void testInvalidEntries();
Expand Down

0 comments on commit 805f8c7

Please sign in to comment.