Skip to content

Commit 1e7a282

Browse files
committed
Added test for cookie jar migration
1 parent b13a96f commit 1e7a282

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ nextcloud_add_test(FileTagModel)
9494
nextcloud_add_test(SyncConflictsModel)
9595
nextcloud_add_test(DateFieldBackend)
9696
nextcloud_add_test(ClientStatusReporting)
97+
nextcloud_add_test(CookieJarMigration)
9798

9899
nextcloud_add_test(FolderStatusModel)
99100

test/testcookiejarmigration.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
This software is in the public domain, furnished "as is", without technical
3+
support, and with no warranty, express or implied, as to its usefulness for
4+
any purpose.
5+
*/
6+
7+
#include <QtTest>
8+
9+
#include "account.h"
10+
#include "accountmanager.h"
11+
#include "logger.h"
12+
13+
using namespace OCC;
14+
15+
class TestCookieJarMigration : public QObject
16+
{
17+
Q_OBJECT
18+
AccountPtr _account;
19+
QString oldCookieJarPath;
20+
21+
private slots:
22+
void initTestCase()
23+
{
24+
OCC::Logger::instance()->setLogFlush(true);
25+
OCC::Logger::instance()->setLogDebug(true);
26+
27+
QStandardPaths::setTestModeEnabled(true);
28+
// Create directories used in test, since Qt doesn't create its automatically
29+
QDir().mkpath(QStandardPaths::writableLocation(QStandardPaths::StateLocation));
30+
QDir().mkpath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
31+
32+
_account = Account::create();
33+
AccountManager::instance()->addAccount(_account);
34+
oldCookieJarPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/cookies" + _account->id() + ".db";
35+
}
36+
void testNoAction()
37+
{
38+
QFile jarFile(_account->cookieJarPath());
39+
jarFile.open(QFile::WriteOnly);
40+
jarFile.write("1", 1); // Write one byte to file
41+
jarFile.close();
42+
_account->tryMigrateCookieJar();
43+
44+
QVERIFY(!QFile::exists(oldCookieJarPath)); // Check that old file doesn't exits
45+
QCOMPARE(QFileInfo(_account->cookieJarPath()).size(), 1); // Check that this byte present in new file
46+
QFile::remove(_account->cookieJarPath()); // Cleanup
47+
}
48+
void testSimpleMigration()
49+
{
50+
QFile oldJarFile(oldCookieJarPath);
51+
oldJarFile.open(QFile::WriteOnly);
52+
oldJarFile.write("1", 1); // Write one byte to file
53+
oldJarFile.close();
54+
55+
_account->tryMigrateCookieJar();
56+
QVERIFY(!QFile::exists(oldCookieJarPath)); // Check that old file is deleted
57+
58+
QCOMPARE(QFileInfo(_account->cookieJarPath()).size(), 1); // Check that this byte present in new file
59+
QFile::remove(_account->cookieJarPath()); // Cleanup
60+
}
61+
void testNotOverwrite()
62+
{
63+
QFile oldJarFile(oldCookieJarPath);
64+
oldJarFile.open(QFile::WriteOnly);
65+
oldJarFile.write("1", 1); // Write one byte to file
66+
oldJarFile.close();
67+
68+
QFile newJarFile(_account->cookieJarPath());
69+
oldJarFile.open(QFile::WriteOnly);
70+
oldJarFile.write("123", 3); // Write three bytes to file
71+
oldJarFile.close();
72+
73+
74+
_account->tryMigrateCookieJar();
75+
76+
QCOMPARE(QFileInfo(_account->cookieJarPath()).size(), 3); // Check that these bytes still present
77+
78+
// Cleanup
79+
QFile::remove(_account->cookieJarPath());
80+
QFile::remove(oldCookieJarPath);
81+
}
82+
};
83+
84+
QTEST_APPLESS_MAIN(TestCookieJarMigration)
85+
#include "testcookiejarmigration.moc"

0 commit comments

Comments
 (0)