Repository navigation
Expand file tree
/
Copy pathtestnetworksettings.cpp
More file actions
164 lines (134 loc) · 6.57 KB
/
Copy pathtestnetworksettings.cpp
File metadata and controls
164 lines (134 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: CC0-1.0
*
* This software is in the public domain, furnished "as is", without technical
* support, and with no warranty, express or implied, as to its usefulness for
* any purpose.
*/
#include <QtTest>
#include <QLineEdit>
#include <QRadioButton>
#include <QWidget>
#include "account.h"
#include "configfile.h"
#include "foldermantestutils.h"
#include "logger.h"
#include "managedsettingstestutils.h"
#include "syncenginetestutils.h"
#include "testhelper.h"
#include "networksettings.h"
using namespace OCC;
using namespace Qt::StringLiterals;
class TestNetworkSettings : public QObject
{
Q_OBJECT
FolderManTestHelper helper;
private Q_SLOTS:
void initTestCase()
{
OCC::Logger::instance()->setLogFlush(true);
OCC::Logger::instance()->setLogDebug(true);
QStandardPaths::setTestModeEnabled(true);
}
// Keeps tests off the device policy of the machine running them.
void init()
{
ConfigFile::setDeviceSourcesFactory([] {
return std::vector<std::unique_ptr<SettingSource>>{};
});
}
void cleanup()
{
ConfigFile::setDeviceSourcesFactory([] {
return std::vector<std::unique_ptr<SettingSource>>{};
});
}
void test_whenAccountIsLoggedOut_doesNotCrash()
{
// Create an account that is not registered in AccountManager
// This simulates a logged-out account scenario
auto account = Account::create();
account->setUrl(QUrl(QStringLiteral("https://example.com")));
account->setDavUser(QStringLiteral("testuser"));
// Create NetworkSettings with this account
// When proxy settings are changed, saveProxySettings() will try to
// get accountState from AccountManager, which will return nullptr
// This test ensures the app doesn't crash in that scenario
NetworkSettings settings(account);
// The test passes if we reach here without crashing
QVERIFY(true);
}
void testEnforcedProxyFieldsDisableOnlyTheirControls_data()
{
QTest::addColumn<bool>("typeEnforced");
QTest::addColumn<bool>("hostAndPortEnforced");
QTest::newRow("host and port enforced") << false << true;
QTest::newRow("only type enforced") << true << false;
}
void testEnforcedProxyFieldsDisableOnlyTheirControls()
{
QFETCH(bool, typeEnforced);
QFETCH(bool, hostAndPortEnforced);
auto account = Account::create();
account->setUrl(QUrl(u"https://example.com"_s));
account->setCredentials(new FakeCredentials{new FakeQNAM({})});
account->setProxyType(QNetworkProxy::HttpProxy);
account->applyManagedProxySettings(managedProxyFields(true,
typeEnforced ? std::optional<int>(QNetworkProxy::HttpProxy) : std::nullopt,
hostAndPortEnforced ? std::optional<QString>(u"proxy.example.com"_s) : std::nullopt,
hostAndPortEnforced ? std::optional<int>(8080) : std::nullopt));
NetworkSettings settings(account);
const auto typeComboBox = settings.findChild<QWidget *>(u"typeComboBox"_s);
const auto manualProxyRadioButton = settings.findChild<QWidget *>(u"manualProxyRadioButton"_s);
const auto hostLineEdit = settings.findChild<QWidget *>(u"hostLineEdit"_s);
const auto portSpinBox = settings.findChild<QWidget *>(u"portSpinBox"_s);
const auto authRequiredCheckBox = settings.findChild<QWidget *>(u"authRequiredcheckBox"_s);
const auto proxyEnforcedLabel = settings.findChild<QWidget *>(u"proxyEnforcedLabel"_s);
QVERIFY(typeComboBox && manualProxyRadioButton && hostLineEdit && portSpinBox && authRequiredCheckBox && proxyEnforcedLabel);
QCOMPARE(typeComboBox->isEnabled(), !typeEnforced);
QCOMPARE(manualProxyRadioButton->isEnabled(), !typeEnforced);
QCOMPARE(hostLineEdit->isEnabled(), !hostAndPortEnforced);
QCOMPARE(portSpinBox->isEnabled(), !hostAndPortEnforced);
QVERIFY(authRequiredCheckBox->isEnabled());
QVERIFY(!proxyEnforcedLabel->isHidden());
}
// Enforcing only the proxy type must still let the user provide the host and port.
void testTypeOnlyEnforcementKeepsHostAndPortEditable()
{
auto account = Account::create();
account->setUrl(QUrl(u"https://example.com"_s));
account->setCredentials(new FakeCredentials{new FakeQNAM({})});
account->setProxyType(QNetworkProxy::DefaultProxy);
account->applyManagedProxySettings(managedProxyFields(true, QNetworkProxy::HttpProxy, std::nullopt, std::nullopt));
NetworkSettings settings(account);
const auto manualProxyRadioButton = settings.findChild<QRadioButton *>(u"manualProxyRadioButton"_s);
const auto hostLineEdit = settings.findChild<QLineEdit *>(u"hostLineEdit"_s);
const auto portSpinBox = settings.findChild<QWidget *>(u"portSpinBox"_s);
QVERIFY(manualProxyRadioButton && hostLineEdit && portSpinBox);
QVERIFY(manualProxyRadioButton->isChecked());
QVERIFY(hostLineEdit->isVisibleTo(&settings));
QVERIFY(portSpinBox->isVisibleTo(&settings));
QVERIFY(hostLineEdit->isEnabled());
QVERIFY(portSpinBox->isEnabled());
}
// A managed default must not surface as the account's own value in the editor.
void testExistingAccountShowsItsOwnProxyNotManagedDefault()
{
auto account = Account::create();
account->setUrl(QUrl(u"https://example.com"_s));
account->setCredentials(new FakeCredentials{new FakeQNAM({})});
account->setProxyType(QNetworkProxy::DefaultProxy);
account->applyManagedProxySettings(managedProxyFields(false, QNetworkProxy::HttpProxy, u"proxy.example.com"_s, 8080));
NetworkSettings settings(account);
const auto systemProxyRadioButton = settings.findChild<QRadioButton *>(u"systemProxyRadioButton"_s);
const auto manualProxyRadioButton = settings.findChild<QRadioButton *>(u"manualProxyRadioButton"_s);
const auto hostLineEdit = settings.findChild<QLineEdit *>(u"hostLineEdit"_s);
QVERIFY(systemProxyRadioButton && manualProxyRadioButton && hostLineEdit);
QVERIFY(systemProxyRadioButton->isChecked());
QVERIFY(!manualProxyRadioButton->isChecked());
QVERIFY(hostLineEdit->text().isEmpty());
}
};
QTEST_MAIN(TestNetworkSettings)
#include "testnetworksettings.moc"