File indexing completed on 2025-01-12 13:02:45
0001 /* 0002 SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "usertest.h" 0008 #include "ruqola_autotest_helper.h" 0009 #include "user.h" 0010 0011 #include <QJsonObject> 0012 0013 QTEST_GUILESS_MAIN(UserTest) 0014 0015 UserTest::UserTest(QObject *parent) 0016 : QObject(parent) 0017 { 0018 } 0019 0020 void UserTest::shouldHaveDefaultValue() 0021 { 0022 User u; 0023 QVERIFY(u.name().isEmpty()); 0024 QVERIFY(u.userId().isEmpty()); 0025 QCOMPARE(u.status(), User::PresenceStatus::PresenceOffline); 0026 QVERIFY(u.userName().isEmpty()); 0027 QVERIFY(!u.isValid()); 0028 QVERIFY(u.roles().isEmpty()); 0029 QVERIFY(!u.userEmailsInfo().isValid()); 0030 QVERIFY(!u.createdAt().isValid()); 0031 QVERIFY(!u.lastLogin().isValid()); 0032 QVERIFY(u.i18nRoles().isEmpty()); 0033 QVERIFY(u.active()); 0034 QVERIFY(u.bio().isEmpty()); 0035 } 0036 0037 void UserTest::shouldBeValid() 0038 { 0039 User u; 0040 0041 u.setName(QStringLiteral("foo")); 0042 QVERIFY(u.isValid()); 0043 0044 User u2; 0045 u2.setUserId(QStringLiteral("bla")); 0046 QVERIFY(!u2.isValid()); 0047 0048 User u3; 0049 u3.setUserName(QStringLiteral("dd")); 0050 QVERIFY(u3.isValid()); 0051 0052 User u4; 0053 u4.setUserId(QStringLiteral("d")); 0054 u4.setUserName(QStringLiteral("dd")); 0055 u4.setName(QStringLiteral("foo")); 0056 QVERIFY(u4.isValid()); 0057 } 0058 0059 void UserTest::shouldSetAndGetName() 0060 { 0061 User sampleUser; 0062 QString name = QStringLiteral("Maxwell"); 0063 sampleUser.setName(name); 0064 0065 QCOMPARE(sampleUser.name(), name); 0066 0067 name = QStringLiteral("Maxwell_NEW"); 0068 sampleUser.setName(name); 0069 QCOMPARE(sampleUser.name(), name); 0070 0071 name = QStringLiteral("Maxwell_NEW"); 0072 sampleUser.setName(name); 0073 QCOMPARE(sampleUser.name(), name); 0074 } 0075 0076 void UserTest::shouldSetAndGetUserId() 0077 { 0078 User sampleUser; 0079 QString Id = QStringLiteral("RA1511ECE"); 0080 sampleUser.setUserId(Id); 0081 0082 QCOMPARE(sampleUser.userId(), Id); 0083 0084 Id = QStringLiteral("RA1511ECE_NEW"); 0085 sampleUser.setUserId(Id); 0086 QCOMPARE(sampleUser.userId(), Id); 0087 0088 Id = QStringLiteral("RA1511ECE_NEW"); 0089 sampleUser.setUserId(Id); 0090 QCOMPARE(sampleUser.userId(), Id); 0091 } 0092 0093 void UserTest::shouldSetAndGetStatus() 0094 { 0095 User sampleUser; 0096 0097 sampleUser.setStatus(User::PresenceStatus::PresenceOffline); 0098 0099 QCOMPARE(sampleUser.status(), User::PresenceStatus::PresenceOffline); 0100 0101 sampleUser.setStatus(User::PresenceStatus::PresenceOnline); 0102 QCOMPARE(sampleUser.status(), User::PresenceStatus::PresenceOnline); 0103 0104 sampleUser.setStatus(User::PresenceStatus::PresenceOnline); 0105 QCOMPARE(sampleUser.status(), User::PresenceStatus::PresenceOnline); 0106 } 0107 0108 void UserTest::shouldParseUser() 0109 { 0110 User sampleUser; 0111 QJsonObject object; 0112 QJsonObject fields; 0113 const QString name = QStringLiteral("Newton"); 0114 const QString status = QStringLiteral("offline"); 0115 fields.insert(QStringLiteral("name"), QJsonValue(name)); 0116 fields.insert(QStringLiteral("status"), QJsonValue(status)); 0117 object.insert(QStringLiteral("id"), QJsonValue(QLatin1String("RA151100ECE"))); 0118 object.insert(QStringLiteral("fields"), fields); 0119 0120 sampleUser.parseUser(object); 0121 QCOMPARE(sampleUser.name(), name); 0122 QCOMPARE(sampleUser.status(), User::PresenceStatus::PresenceOffline); 0123 } 0124 0125 void UserTest::checkEqualsAndUnequalsOperator() 0126 { 0127 User sampleuser; 0128 User sampleuserOther; 0129 QString Id = QStringLiteral("RA151100ECE"); 0130 QString name = QStringLiteral("Robert Segwick"); 0131 0132 sampleuser.setUserId(Id); 0133 sampleuser.setName(name); 0134 sampleuser.setStatus(User::PresenceStatus::PresenceOffline); 0135 QVERIFY(sampleuser != sampleuserOther); 0136 0137 sampleuserOther.setUserId(Id); 0138 sampleuserOther.setName(name); 0139 sampleuserOther.setStatus(User::PresenceStatus::PresenceOffline); 0140 QVERIFY(sampleuser == sampleuserOther); 0141 0142 sampleuserOther.setName(QStringLiteral("Robert Segwick_NEW")); 0143 QVERIFY(sampleuser != sampleuserOther); 0144 } 0145 0146 void UserTest::shouldParseJson_data() 0147 { 0148 QTest::addColumn<QString>("fileName"); 0149 QTest::addColumn<User>("expectedUser"); 0150 User expected; 0151 expected.setName(QStringLiteral("Laurent M")); 0152 expected.setStatus(User::PresenceStatus::PresenceAway); 0153 expected.setUserId(QStringLiteral("yi2ucvqkdkxiTkyZ5")); 0154 expected.setUserName(QStringLiteral("laurent")); 0155 expected.setUtcOffset(1); 0156 QTest::newRow("user1") << QStringLiteral("adduser") << expected; 0157 } 0158 0159 void UserTest::shouldParseJson() 0160 { 0161 QFETCH(QString, fileName); 0162 QFETCH(User, expectedUser); 0163 const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/json/") + fileName + QLatin1String(".json"); 0164 const QJsonObject fields = AutoTestHelper::loadJsonObject(originalJsonFile); 0165 User user; 0166 user.parseUser(fields); 0167 const bool equal = (user == expectedUser); 0168 if (!equal) { 0169 qDebug() << " current value " << user; 0170 qDebug() << " expected value " << expectedUser; 0171 } 0172 QVERIFY(equal); 0173 } 0174 0175 void UserTest::shouldParseRestApiJson_data() 0176 { 0177 QTest::addColumn<QString>("fileName"); 0178 QTest::addColumn<User>("expectedUser"); 0179 { 0180 User expected; 0181 expected.setName(QStringLiteral("name_user")); 0182 expected.setStatus(User::PresenceStatus::PresenceOffline); 0183 expected.setUserId(QStringLiteral("BDFj6E7Z9RYucn8C")); 0184 expected.setUserName(QStringLiteral("username")); 0185 expected.setUtcOffset(0); 0186 expected.setRoles({QStringLiteral("user")}, {}); 0187 QDateTime createdTime; 0188 createdTime.setDate(QDate(2020, 10, 05)); 0189 createdTime.setTime(QTime(00, 48, 01, 903)); 0190 expected.setCreatedAt(createdTime); 0191 expected.setLastLogin(QDateTime()); 0192 QTest::newRow("userrestapi1") << QStringLiteral("userrestapi") << expected; 0193 } 0194 { 0195 User expected; 0196 expected.setName(QStringLiteral("Bla bla")); 0197 expected.setStatus(User::PresenceStatus::PresenceOnline); 0198 expected.setUserId(QStringLiteral("XQZAk3998f9hSNwh")); 0199 expected.setUserName(QStringLiteral("steffen")); 0200 expected.setUtcOffset(2); 0201 expected.setRoles({QStringLiteral("user"), QStringLiteral("admin")}, {}); 0202 QDateTime createdTime; 0203 createdTime.setDate(QDate(2018, 01, 18)); 0204 createdTime.setTime(QTime(12, 52, 50, 772)); 0205 expected.setCreatedAt(createdTime); 0206 QDateTime lastLogin; 0207 lastLogin.setDate(QDate(2020, 10, 12)); 0208 lastLogin.setTime(QTime(02, 26, 27, 324)); 0209 expected.setLastLogin(lastLogin); 0210 User::UserEmailsInfo info; 0211 info.email = QStringLiteral("bla@kde.com"); 0212 info.verified = true; 0213 expected.setUserEmailsInfo(info); 0214 QTest::newRow("userrestapi2") << QStringLiteral("userrestapi2") << expected; 0215 } 0216 } 0217 0218 void UserTest::shouldParseRestApiJson() 0219 { 0220 QFETCH(QString, fileName); 0221 QFETCH(User, expectedUser); 0222 const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/json/") + fileName + QLatin1String(".json"); 0223 const QJsonObject fields = AutoTestHelper::loadJsonObject(originalJsonFile); 0224 User user; 0225 user.parseUserRestApi(fields, {}); 0226 const bool equal = (user == expectedUser); 0227 if (!equal) { 0228 qDebug() << " current value " << user; 0229 qDebug() << " expected value " << expectedUser; 0230 } 0231 QVERIFY(equal); 0232 } 0233 0234 void UserTest::shouldGetStatusIcon_data() 0235 { 0236 QTest::addColumn<User::PresenceStatus>("status"); 0237 QTest::addColumn<QString>("iconFileName"); 0238 QTest::newRow("online") << User::PresenceStatus::PresenceOnline << QStringLiteral("user-online"); 0239 QTest::newRow("busy") << User::PresenceStatus::PresenceBusy << QStringLiteral("user-busy"); 0240 QTest::newRow("away") << User::PresenceStatus::PresenceAway << QStringLiteral("user-away"); 0241 QTest::newRow("offline") << User::PresenceStatus::PresenceOffline << QStringLiteral("user-offline"); 0242 QTest::newRow("unknown") << User::PresenceStatus::Unknown << QStringLiteral("unknown"); 0243 } 0244 0245 void UserTest::shouldGetStatusIcon() 0246 { 0247 QFETCH(User::PresenceStatus, status); 0248 QFETCH(QString, iconFileName); 0249 User user; 0250 user.setStatus(status); 0251 QCOMPARE(user.iconFromStatus(), iconFileName); 0252 } 0253 0254 #include "moc_usertest.cpp"