File indexing completed on 2025-02-02 04:51:34
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 "userscreatejobtest.h" 0008 #include "ruqola_restapi_helper.h" 0009 #include "users/userscreatejob.h" 0010 QTEST_GUILESS_MAIN(UsersCreateJobTest) 0011 using namespace RocketChatRestApi; 0012 UsersCreateJobTest::UsersCreateJobTest(QObject *parent) 0013 : QObject(parent) 0014 { 0015 } 0016 0017 void UsersCreateJobTest::shouldHaveDefaultValue() 0018 { 0019 UsersCreateJob job; 0020 verifyDefaultValue(&job); 0021 QVERIFY(job.requireHttpAuthentication()); 0022 QVERIFY(!job.hasQueryParameterSupport()); 0023 } 0024 0025 void UsersCreateJobTest::shouldGenerateRequest() 0026 { 0027 UsersCreateJob job; 0028 QNetworkRequest request = QNetworkRequest(QUrl()); 0029 verifyAuthentication(&job, request); 0030 QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/users.create"))); 0031 QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), QStringLiteral("application/json")); 0032 } 0033 0034 void UsersCreateJobTest::shouldGenerateJson() 0035 { 0036 UsersCreateJob job; 0037 CreateUpdateUserInfo info; 0038 const QString password{QStringLiteral("ccc")}; 0039 info.mPassword = password; 0040 const QString email{QStringLiteral("bla@kde.org")}; 0041 info.mEmail = email; 0042 job.setCreateInfo(info); 0043 QCOMPARE( 0044 job.json().toJson(QJsonDocument::Compact), 0045 QStringLiteral( 0046 R"({"email":"%1","joinDefaultChannels":false,"password":"%2","requirePasswordChange":false,"sendWelcomeEmail":false,"setRandomPassword":false,"verified":false})") 0047 .arg(email, password) 0048 .toLatin1()); 0049 0050 const QString nickame{QStringLiteral("blu")}; 0051 info.mNickName = nickame; 0052 job.setCreateInfo(info); 0053 QCOMPARE( 0054 job.json().toJson(QJsonDocument::Compact), 0055 QStringLiteral( 0056 R"({"email":"%1","joinDefaultChannels":false,"nickname":"%3","password":"%2","requirePasswordChange":false,"sendWelcomeEmail":false,"setRandomPassword":false,"verified":false})") 0057 .arg(email, password, nickame) 0058 .toLatin1()); 0059 0060 const QStringList roles{QStringLiteral("cd"), QStringLiteral("ssc")}; 0061 info.mRoles = roles; 0062 job.setCreateInfo(info); 0063 QCOMPARE( 0064 job.json().toJson(QJsonDocument::Compact), 0065 QStringLiteral( 0066 R"({"email":"%1","joinDefaultChannels":false,"nickname":"%3","password":"%2","requirePasswordChange":false,"roles":["cd","ssc"],"sendWelcomeEmail":false,"setRandomPassword":false,"verified":false})") 0067 .arg(email, password, nickame) 0068 .toLatin1()); 0069 info.mRequirePasswordChange = true; 0070 job.setCreateInfo(info); 0071 QCOMPARE( 0072 job.json().toJson(QJsonDocument::Compact), 0073 QStringLiteral( 0074 R"({"email":"%1","joinDefaultChannels":false,"nickname":"%3","password":"%2","requirePasswordChange":true,"roles":["cd","ssc"],"sendWelcomeEmail":false,"setRandomPassword":false,"verified":false})") 0075 .arg(email, password, nickame) 0076 .toLatin1()); 0077 0078 info.mSendWelcomeEmail = true; 0079 job.setCreateInfo(info); 0080 QCOMPARE( 0081 job.json().toJson(QJsonDocument::Compact), 0082 QStringLiteral( 0083 R"({"email":"%1","joinDefaultChannels":false,"nickname":"%3","password":"%2","requirePasswordChange":true,"roles":["cd","ssc"],"sendWelcomeEmail":true,"setRandomPassword":false,"verified":false})") 0084 .arg(email, password, nickame) 0085 .toLatin1()); 0086 0087 info.mVerified = true; 0088 job.setCreateInfo(info); 0089 QCOMPARE( 0090 job.json().toJson(QJsonDocument::Compact), 0091 QStringLiteral( 0092 R"({"email":"%1","joinDefaultChannels":false,"nickname":"%3","password":"%2","requirePasswordChange":true,"roles":["cd","ssc"],"sendWelcomeEmail":true,"setRandomPassword":false,"verified":true})") 0093 .arg(email, password, nickame) 0094 .toLatin1()); 0095 0096 info.mSetRandomPassword = false; 0097 info.mPassword = QStringLiteral("ccc"); 0098 job.setCreateInfo(info); 0099 QCOMPARE( 0100 job.json().toJson(QJsonDocument::Compact), 0101 QStringLiteral( 0102 R"({"email":"%1","joinDefaultChannels":false,"nickname":"%3","password":"%2","requirePasswordChange":true,"roles":["cd","ssc"],"sendWelcomeEmail":true,"setRandomPassword":false,"verified":true})") 0103 .arg(email, password, nickame) 0104 .toLatin1()); 0105 } 0106 0107 void UsersCreateJobTest::shouldNotStarting() 0108 { 0109 UsersCreateJob job; 0110 0111 RestApiMethod method; 0112 method.setServerUrl(QStringLiteral("http://www.kde.org")); 0113 job.setRestApiMethod(&method); 0114 0115 QNetworkAccessManager mNetworkAccessManager; 0116 job.setNetworkAccessManager(&mNetworkAccessManager); 0117 QVERIFY(!job.canStart()); 0118 const QString auth = QStringLiteral("foo"); 0119 const QString userId = QStringLiteral("foo"); 0120 job.setAuthToken(auth); 0121 QVERIFY(!job.canStart()); 0122 job.setUserId(userId); 0123 0124 CreateUpdateUserInfo info; 0125 info.mPassword = QStringLiteral("ccc"); 0126 job.setCreateInfo(info); 0127 0128 QVERIFY(!job.canStart()); 0129 info.mEmail = QStringLiteral("ccc"); 0130 job.setCreateInfo(info); 0131 0132 QVERIFY(!job.canStart()); 0133 info.mName = QStringLiteral("777"); 0134 job.setCreateInfo(info); 0135 0136 QVERIFY(job.canStart()); 0137 } 0138 0139 #include "moc_userscreatejobtest.cpp"