Files
desktop/test/testurischemehandler.cpp
Nathan van Dael f209efb1ec fix(settings): address review feedback on managed override settings
- Return whether setOverrideServerUrl() and setOverrideLocalDir() wrote
  the value, and show a hint when a policy blocks --overrideserverurl or
  --overridelocaldir.
- Reject a --overridelocaldir path that is not absolute while parsing the
  command line, using the schema validator.
- Add ConfigFile::isOverrideServerUrlLocked(), shared by the wizard and
  the nc:// URI scheme handler, so only a policy or command line value
  locks the server URL and restricts login links. Builds enable
  APPLICATION_SERVER_URL_ENFORCE by default, so the wizard now clears the
  lock for a device default.
- Apply an overrideLocalDir from device default or policy only to the
  first account, in the wizard and for account setup from the command
  line. A command line value still applies to the next account. Skip the
  sync options step only for a locked folder.
- Update the docs and add tests.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Nathan van Dael <[email protected]>
2026-10-26 16:50:31 +01:00

258 lines
10 KiB
C++

/*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: CC0-1.0
*/
#include <QtTest>
#include <QScopeGuard>
#include <QStandardPaths>
#include <QUrl>
#include "configfile.h"
#include "managedsettingstestutils.h"
#include "theme.h"
#include "urischemehandler.h"
using namespace OCC;
using namespace Qt::StringLiterals;
Q_DECLARE_METATYPE(OCC::UriSchemeHandler::Action)
class TestUriSchemeHandler : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase()
{
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({});
}
void parseUri_data()
{
QTest::addColumn<QUrl>("url");
QTest::addColumn<UriSchemeHandler::Action>("expectedAction");
QTest::addColumn<QUrl>("expectedServerUrl");
QTest::addColumn<bool>("expectError");
QTest::newRow("login")
<< QUrl(QStringLiteral("nc://login/server:https://cloud.example.com"))
<< UriSchemeHandler::Action::Login
<< QUrl(QStringLiteral("https://cloud.example.com"))
<< false;
QTest::newRow("login encoded server")
<< QUrl(QStringLiteral("nc://login/server:https%3A%2F%2Fcloud.example.com"))
<< UriSchemeHandler::Action::Login
<< QUrl(QStringLiteral("https://cloud.example.com"))
<< false;
QTest::newRow("login uppercase host")
<< QUrl(QStringLiteral("nc://LOGIN/server:https://cloud.example.com"))
<< UriSchemeHandler::Action::Login
<< QUrl(QStringLiteral("https://cloud.example.com"))
<< false;
QTest::newRow("login missing server prefix")
<< QUrl(QStringLiteral("nc://login/https://cloud.example.com"))
<< UriSchemeHandler::Action::Invalid
<< QUrl{}
<< true;
QTest::newRow("login missing server url")
<< QUrl(QStringLiteral("nc://login/server:"))
<< UriSchemeHandler::Action::Invalid
<< QUrl{}
<< true;
QTest::newRow("login relative server url")
<< QUrl(QStringLiteral("nc://login/server:cloud.example.com"))
<< UriSchemeHandler::Action::Invalid
<< QUrl{}
<< true;
QTest::newRow("login hostless server url")
<< QUrl(QStringLiteral("nc://login/server:https://"))
<< UriSchemeHandler::Action::Invalid
<< QUrl{}
<< true;
QTest::newRow("login unsupported server url scheme")
<< QUrl(QStringLiteral("nc://login/server:ftp://cloud.example.com"))
<< UriSchemeHandler::Action::Invalid
<< QUrl{}
<< true;
QTest::newRow("old add account")
<< QUrl(QStringLiteral("nc://addAccount/?serverUrl=https%3A%2F%2Fcloud.example.com"))
<< UriSchemeHandler::Action::Invalid
<< QUrl{}
<< true;
QTest::newRow("local edit")
<< QUrl(QStringLiteral("nc://open/[email protected]/Documents/file.txt?token=secret"))
<< UriSchemeHandler::Action::OpenLocalEdit
<< QUrl{}
<< false;
QTest::newRow("unknown action")
<< QUrl(QStringLiteral("nc://unknown/"))
<< UriSchemeHandler::Action::Invalid
<< QUrl{}
<< true;
}
void parseUri()
{
QFETCH(QUrl, url);
QFETCH(UriSchemeHandler::Action, expectedAction);
QFETCH(QUrl, expectedServerUrl);
QFETCH(bool, expectError);
auto theme = Theme::instance();
theme->setOverrideServerUrl(QString{});
theme->setForceOverrideServerUrl(false);
ConfigFile{}.setOverrideServerUrl(QString{});
const auto result = UriSchemeHandler::parseUri(url);
QCOMPARE(result.action, expectedAction);
QCOMPARE(result.serverUrl, expectedServerUrl);
QCOMPARE(result.error.isEmpty(), !expectError);
}
void parseUriHonoursForcedSingleServerOverride()
{
auto theme = Theme::instance();
const auto previousOverrideServerUrl = theme->overrideServerUrl();
const auto previousForceOverrideServerUrl = theme->forceOverrideServerUrl();
const auto restoreTheme = qScopeGuard([theme, previousOverrideServerUrl, previousForceOverrideServerUrl] {
theme->setOverrideServerUrl(previousOverrideServerUrl);
theme->setForceOverrideServerUrl(previousForceOverrideServerUrl);
});
theme->setOverrideServerUrl(QStringLiteral("https://cloud.example.com"));
theme->setForceOverrideServerUrl(true);
const auto acceptedResult = UriSchemeHandler::parseUri(QUrl(QStringLiteral("nc://login/server:https://cloud.example.com/")));
QCOMPARE(acceptedResult.action, UriSchemeHandler::Action::Login);
QCOMPARE(acceptedResult.serverUrl, QUrl(QStringLiteral("https://cloud.example.com/")));
QVERIFY(acceptedResult.error.isEmpty());
const auto rejectedResult = UriSchemeHandler::parseUri(QUrl(QStringLiteral("nc://login/server:https://unconfigured.example.com")));
QCOMPARE(rejectedResult.action, UriSchemeHandler::Action::Invalid);
QVERIFY(!rejectedResult.error.isEmpty());
}
void parseUriHonoursConfiguredServerOverride()
{
ConfigFile cfg;
const auto previousConfiguredOverrideServerUrl = cfg.overrideServerUrl();
auto theme = Theme::instance();
const auto previousOverrideServerUrl = theme->overrideServerUrl();
const auto previousForceOverrideServerUrl = theme->forceOverrideServerUrl();
const auto restoreState = qScopeGuard([&cfg, theme, previousConfiguredOverrideServerUrl, previousOverrideServerUrl, previousForceOverrideServerUrl] {
cfg.setOverrideServerUrl(previousConfiguredOverrideServerUrl);
theme->setOverrideServerUrl(previousOverrideServerUrl);
theme->setForceOverrideServerUrl(previousForceOverrideServerUrl);
});
cfg.setOverrideServerUrl(QStringLiteral("https://configured.example.com"));
theme->setOverrideServerUrl(QString{});
theme->setForceOverrideServerUrl(false);
const auto acceptedResult = UriSchemeHandler::parseUri(QUrl(QStringLiteral("nc://login/server:https://configured.example.com")));
QCOMPARE(acceptedResult.action, UriSchemeHandler::Action::Login);
QCOMPARE(acceptedResult.serverUrl, QUrl(QStringLiteral("https://configured.example.com")));
QVERIFY(acceptedResult.error.isEmpty());
const auto rejectedResult = UriSchemeHandler::parseUri(QUrl(QStringLiteral("nc://login/server:https://unconfigured.example.com")));
QCOMPARE(rejectedResult.action, UriSchemeHandler::Action::Invalid);
QVERIFY(!rejectedResult.error.isEmpty());
}
void parseUriRestrictsOnlyForLockedManagedServerOverride_data()
{
QTest::addColumn<bool>("enforced");
QTest::newRow("device default") << false;
QTest::newRow("device policy") << true;
}
void parseUriRestrictsOnlyForLockedManagedServerOverride()
{
QFETCH(bool, enforced);
ConfigFile cfg;
auto theme = Theme::instance();
const auto previousOverrideServerUrl = theme->overrideServerUrl();
const auto previousForceOverrideServerUrl = theme->forceOverrideServerUrl();
const auto restoreTheme = qScopeGuard([theme, previousOverrideServerUrl, previousForceOverrideServerUrl] {
theme->setOverrideServerUrl(previousOverrideServerUrl);
theme->setForceOverrideServerUrl(previousForceOverrideServerUrl);
});
cfg.clearOverrideServerUrl();
theme->setOverrideServerUrl(QString{});
theme->setForceOverrideServerUrl(false);
ConfigFile::setDeviceSourcesFactory([enforced] {
std::vector<std::unique_ptr<SettingSource>> sources;
sources.push_back(std::make_unique<MapSource>(enforced ? SettingSourceType::PlatformPolicy : SettingSourceType::PlatformDefault,
enforced ? EnforcementState::Enforced : EnforcementState::NotEnforced,
enforced ? 200 : 20,
QVariantMap{{u"overrideServerUrl"_s, u"https://configured.example.com"_s}}));
return sources;
});
const auto configuredResult = UriSchemeHandler::parseUri(QUrl(u"nc://login/server:https://configured.example.com"_s));
QCOMPARE(configuredResult.action, UriSchemeHandler::Action::Login);
const auto otherResult = UriSchemeHandler::parseUri(QUrl(u"nc://login/server:https://unconfigured.example.com"_s));
QCOMPARE(otherResult.action, enforced ? UriSchemeHandler::Action::Invalid : UriSchemeHandler::Action::Login);
}
void parseUriHonoursMultipleServerOverrides()
{
auto theme = Theme::instance();
const auto previousOverrideServerUrl = theme->overrideServerUrl();
const auto previousForceOverrideServerUrl = theme->forceOverrideServerUrl();
const auto restoreTheme = qScopeGuard([theme, previousOverrideServerUrl, previousForceOverrideServerUrl] {
theme->setOverrideServerUrl(previousOverrideServerUrl);
theme->setForceOverrideServerUrl(previousForceOverrideServerUrl);
});
theme->setOverrideServerUrl(QStringLiteral(
"["
R"({"name": "Primary", "url": "https://primary.example.com"},)"
R"({"name": "Secondary", "url": "https://secondary.example.com"})"
"]"));
theme->setForceOverrideServerUrl(true);
const auto acceptedResult = UriSchemeHandler::parseUri(QUrl(QStringLiteral("nc://login/server:https://secondary.example.com")));
QCOMPARE(acceptedResult.action, UriSchemeHandler::Action::Login);
QCOMPARE(acceptedResult.serverUrl, QUrl(QStringLiteral("https://secondary.example.com")));
QVERIFY(acceptedResult.error.isEmpty());
const auto rejectedResult = UriSchemeHandler::parseUri(QUrl(QStringLiteral("nc://login/server:https://unconfigured.example.com")));
QCOMPARE(rejectedResult.action, UriSchemeHandler::Action::Invalid);
QVERIFY(!rejectedResult.error.isEmpty());
}
};
QTEST_APPLESS_MAIN(TestUriSchemeHandler)
#include "testurischemehandler.moc"