File indexing completed on 2024-12-01 04:35:26

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "permissionmanagertest.h"
0008 #include "permissions/permissionmanager.h"
0009 #include "ruqola_autotest_helper.h"
0010 
0011 QTEST_GUILESS_MAIN(PermissionManagerTest)
0012 PermissionManagerTest::PermissionManagerTest(QObject *parent)
0013     : QObject(parent)
0014 {
0015 }
0016 
0017 void PermissionManagerTest::shouldHaveDefaultValues()
0018 {
0019     PermissionManager w;
0020     QCOMPARE(w.permissionCount(), 0);
0021 }
0022 
0023 void PermissionManagerTest::shouldHasRoles()
0024 {
0025     PermissionManager w;
0026     QVERIFY(w.roles(QStringLiteral("bla")).isEmpty());
0027 }
0028 
0029 void PermissionManagerTest::shouldLoadPermissions_data()
0030 {
0031     QTest::addColumn<QString>("name");
0032     QTest::addColumn<int>("permissionsCount");
0033     QTest::addColumn<int>("permissionsAdded");
0034     QTest::addColumn<QVector<Permission>>("permissions");
0035 
0036     {
0037         QVector<Permission> permissions;
0038 
0039         {
0040             Permission p;
0041             p.setIdentifier(QStringLiteral("access-permissions"));
0042             p.setRoles({QStringLiteral("admin")});
0043             p.setUpdatedAt(1533550243852);
0044             permissions.append(std::move(p));
0045         }
0046         {
0047             Permission p;
0048             p.setIdentifier(QStringLiteral("add-user-to-joined-room"));
0049             p.setRoles({QStringLiteral("admin"), QStringLiteral("owner"), QStringLiteral("moderator")});
0050             p.setUpdatedAt(1533550243869);
0051             permissions.append(std::move(p));
0052         }
0053         {
0054             Permission p;
0055             p.setIdentifier(QStringLiteral("add-user-to-any-c-room"));
0056             p.setRoles({QStringLiteral("admin")});
0057             p.setUpdatedAt(1533550243881);
0058             permissions.append(std::move(p));
0059         }
0060         {
0061             Permission p;
0062             p.setIdentifier(QStringLiteral("add-user-to-any-p-room"));
0063             p.setUpdatedAt(1533550243889);
0064             permissions.append(std::move(p));
0065         }
0066         QTest::addRow("permissions1") << QStringLiteral("permissions1") << 5 << 4 << permissions;
0067     }
0068 }
0069 
0070 void PermissionManagerTest::shouldLoadPermissions()
0071 {
0072     QFETCH(QString, name);
0073     QFETCH(int, permissionsCount);
0074     QFETCH(int, permissionsAdded);
0075     QFETCH(QVector<Permission>, permissions);
0076     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/permissions/") + name + QLatin1String(".json");
0077     const QJsonArray obj = AutoTestHelper::loadJsonArrayObject(originalJsonFile);
0078     QCOMPARE(obj.count(), permissionsCount);
0079     // qDebug() << " OBJ " << obj;
0080 
0081     PermissionManager r;
0082     r.parseUpdatePermission(obj);
0083     QCOMPARE(r.permissionCount(), permissionsAdded);
0084     QCOMPARE(r.permissionCount(), permissions.count());
0085     for (const Permission &p : permissions) {
0086         const Permission managerPermission = r.permission(p.identifier());
0087         const bool equalPermission = (managerPermission == p);
0088         if (!equalPermission) {
0089             qDebug() << p;
0090             qDebug() << managerPermission;
0091         }
0092         QVERIFY(equalPermission);
0093     }
0094 }
0095 
0096 void PermissionManagerTest::shouldUpdatePermissions_data()
0097 {
0098     QTest::addColumn<QString>("name");
0099     QTest::addColumn<QString>("updateName");
0100     QTest::addColumn<QVector<Permission>>("permissions");
0101     QTest::addColumn<QVector<Permission>>("updatedPermissions");
0102     QTest::addColumn<bool>("permissionUpdated");
0103     {
0104         // No updated permission as this permission can't be store in manager
0105         QVector<Permission> permissions;
0106         {
0107             Permission p;
0108             p.setIdentifier(QStringLiteral("access-permissions"));
0109             p.setRoles({QStringLiteral("admin")});
0110             p.setUpdatedAt(1533550243852);
0111             permissions.append(std::move(p));
0112         }
0113         {
0114             Permission p;
0115             p.setIdentifier(QStringLiteral("add-user-to-joined-room"));
0116             p.setRoles({QStringLiteral("admin"), QStringLiteral("owner"), QStringLiteral("moderator")});
0117             p.setUpdatedAt(1533550243869);
0118             permissions.append(std::move(p));
0119         }
0120         {
0121             Permission p;
0122             p.setIdentifier(QStringLiteral("add-user-to-any-c-room"));
0123             p.setRoles({QStringLiteral("admin")});
0124             p.setUpdatedAt(1533550243881);
0125             permissions.append(std::move(p));
0126         }
0127         {
0128             Permission p;
0129             p.setIdentifier(QStringLiteral("add-user-to-any-p-room"));
0130             p.setUpdatedAt(1533550243889);
0131             permissions.append(std::move(p));
0132         }
0133         QTest::addRow("permissions1") << QStringLiteral("permissions1") << QStringLiteral("update-permissions1") << permissions << permissions << false;
0134     }
0135     {
0136         // No updated permission as this permission can't be store in manager
0137         QVector<Permission> permissions;
0138         QVector<Permission> updatedPermissions;
0139         {
0140             Permission p;
0141             p.setIdentifier(QStringLiteral("access-permissions"));
0142             p.setRoles({QStringLiteral("admin")});
0143             p.setUpdatedAt(1533550243852);
0144             permissions.append(p);
0145             updatedPermissions.append(p);
0146         }
0147         {
0148             Permission p;
0149             p.setIdentifier(QStringLiteral("add-user-to-joined-room"));
0150             p.setRoles({QStringLiteral("admin"), QStringLiteral("owner"), QStringLiteral("moderator")});
0151             p.setUpdatedAt(1533550243869);
0152             permissions.append(p);
0153             updatedPermissions.append(p);
0154         }
0155         {
0156             Permission p;
0157             p.setIdentifier(QStringLiteral("add-user-to-any-c-room"));
0158             p.setRoles({QStringLiteral("admin")});
0159             p.setUpdatedAt(1533550243881);
0160             permissions.append(p);
0161 
0162             p.setRoles({QStringLiteral("admin"), QStringLiteral("vFXCWG9trXLti6xQm")});
0163             p.setUpdatedAt(1634569746270);
0164             updatedPermissions.append(p);
0165         }
0166         {
0167             Permission p;
0168             p.setIdentifier(QStringLiteral("add-user-to-any-p-room"));
0169             p.setUpdatedAt(1533550243889);
0170             permissions.append(p);
0171             updatedPermissions.append(p);
0172         }
0173         // Use permissions1 as ref.
0174         QTest::addRow("permissions2") << QStringLiteral("permissions1") << QStringLiteral("update-permissions2") << permissions << updatedPermissions << true;
0175     }
0176 }
0177 
0178 void PermissionManagerTest::shouldUpdatePermissions()
0179 {
0180     QFETCH(QString, name);
0181     QFETCH(QString, updateName);
0182     QFETCH(QVector<Permission>, permissions);
0183     QFETCH(QVector<Permission>, updatedPermissions);
0184     QFETCH(bool, permissionUpdated);
0185     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/permissions/") + name + QLatin1String(".json");
0186     const QJsonArray obj = AutoTestHelper::loadJsonArrayObject(originalJsonFile);
0187 
0188     const QString originalUpdateJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/permissions/") + updateName + QLatin1String(".json");
0189     const QJsonArray updateArray = AutoTestHelper::loadJsonArrayObject(originalUpdateJsonFile);
0190 
0191     PermissionManager r;
0192     r.parseUpdatePermission(obj);
0193     QCOMPARE(r.permissionCount(), permissions.count());
0194 
0195     for (const Permission &p : permissions) {
0196         const Permission managerPermission = r.permission(p.identifier());
0197         const bool equalPermission = (managerPermission == p);
0198         if (!equalPermission) {
0199             qDebug() << p;
0200             qDebug() << managerPermission;
0201         }
0202         QVERIFY(equalPermission);
0203     }
0204 
0205     QCOMPARE(r.updatePermission(updateArray), permissionUpdated);
0206 
0207     for (const Permission &p : updatedPermissions) {
0208         const Permission managerPermission = r.permission(p.identifier());
0209         const bool equalPermission = (managerPermission == p);
0210         if (!equalPermission) {
0211             qDebug() << p;
0212             qDebug() << managerPermission;
0213         }
0214         QVERIFY(equalPermission);
0215     }
0216 }
0217 
0218 #include "moc_permissionmanagertest.cpp"