File indexing completed on 2024-02-25 03:57:40

0001 /*
0002     SPDX-FileCopyrightText: 2012 Dario Freddi <drf@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "BackendsManager.h"
0008 #include <QTest>
0009 #include <kauth/actionreply.h>
0010 #include <kauth/executejob.h>
0011 
0012 class SetupActionTest : public QObject
0013 {
0014     Q_OBJECT
0015 
0016 public:
0017     SetupActionTest(QObject *parent = nullptr)
0018         : QObject(parent)
0019     {
0020     }
0021 
0022 private Q_SLOTS:
0023     void initTestCase();
0024     void init()
0025     {
0026     }
0027 
0028     void testBasicActionProperties();
0029     void testUserAuthorization();
0030     void testAuthorizationFail();
0031 
0032     void cleanup()
0033     {
0034     }
0035     void cleanupTestCase()
0036     {
0037     }
0038 
0039 Q_SIGNALS:
0040     void changeCapabilities(KAuth::AuthBackend::Capabilities capabilities);
0041 
0042 private:
0043 };
0044 
0045 void SetupActionTest::initTestCase()
0046 {
0047     connect(this,
0048             SIGNAL(changeCapabilities(KAuth::AuthBackend::Capabilities)),
0049             KAuth::BackendsManager::authBackend(),
0050             SLOT(setNewCapabilities(KAuth::AuthBackend::Capabilities)));
0051 }
0052 
0053 void SetupActionTest::testBasicActionProperties()
0054 {
0055     Q_EMIT changeCapabilities(KAuth::AuthBackend::AuthorizeFromHelperCapability);
0056     KAuth::Action::DetailsMap detailsMap{{KAuth::Action::AuthDetail::DetailOther, QLatin1String("details")}};
0057     KAuth::Action action(QLatin1String("always.authorized"), detailsMap);
0058     QVERIFY(action.isValid());
0059 
0060     QCOMPARE(action.name(), QLatin1String("always.authorized"));
0061     QCOMPARE(action.detailsV2(), detailsMap);
0062     QVERIFY(!action.hasHelper());
0063     QVERIFY(action.helperId().isEmpty());
0064     QCOMPARE(action.status(), KAuth::Action::AuthorizedStatus);
0065 
0066     QVERIFY(action.arguments().isEmpty());
0067     QVariantMap args;
0068     args.insert(QLatin1String("akey"), QVariant::fromValue(42));
0069     action.setArguments(args);
0070     QCOMPARE(action.arguments(), args);
0071 
0072     Q_EMIT changeCapabilities(KAuth::AuthBackend::NoCapability);
0073 
0074     action = KAuth::Action(QLatin1String("i.do.not.exist"), detailsMap);
0075 
0076     QVERIFY(action.isValid());
0077     QCOMPARE(action.name(), QLatin1String("i.do.not.exist"));
0078     QCOMPARE(action.detailsV2(), detailsMap);
0079     QVERIFY(!action.hasHelper());
0080     QVERIFY(action.helperId().isEmpty());
0081     QCOMPARE(action.status(), KAuth::Action::InvalidStatus);
0082 }
0083 
0084 void SetupActionTest::testUserAuthorization()
0085 {
0086     KAuth::Action::DetailsMap detailsMap{{KAuth::Action::AuthDetail::DetailOther, QLatin1String("details")}};
0087     KAuth::Action action(QLatin1String("requires.auth"), detailsMap);
0088     QVERIFY(action.isValid());
0089 
0090     QCOMPARE(action.status(), KAuth::Action::AuthRequiredStatus);
0091     KAuth::ExecuteJob *job = action.execute();
0092 
0093     QVERIFY(!job->exec());
0094 
0095     QCOMPARE(job->error(), (int)KAuth::ActionReply::BackendError);
0096 
0097     Q_EMIT changeCapabilities(KAuth::AuthBackend::AuthorizeFromClientCapability);
0098 
0099     QVERIFY(action.isValid());
0100 
0101     QCOMPARE(action.status(), KAuth::Action::AuthRequiredStatus);
0102     job = action.execute();
0103 
0104     QVERIFY(job->exec());
0105 
0106     QVERIFY(!job->error());
0107     QVERIFY(job->data().isEmpty());
0108 }
0109 
0110 void SetupActionTest::testAuthorizationFail()
0111 {
0112     Q_EMIT changeCapabilities(KAuth::AuthBackend::AuthorizeFromClientCapability);
0113 
0114     KAuth::Action::DetailsMap detailsMap{{KAuth::Action::AuthDetail::DetailOther, QLatin1String("details")}};
0115     KAuth::Action action(QLatin1String("doomed.to.fail"), detailsMap);
0116     QVERIFY(action.isValid());
0117 
0118     QCOMPARE(action.status(), KAuth::Action::DeniedStatus);
0119     KAuth::ExecuteJob *job = action.execute();
0120 
0121     QVERIFY(!job->exec());
0122 
0123     QCOMPARE(job->error(), (int)KAuth::ActionReply::AuthorizationDeniedError);
0124     QVERIFY(job->data().isEmpty());
0125 }
0126 
0127 QTEST_MAIN(SetupActionTest)
0128 #include "SetupActionTest.moc"