File indexing completed on 2025-02-02 04:51:25
0001 /* 0002 SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "googleauthjobtest.h" 0008 #include "authentication/googleauthjob.h" 0009 #include "ruqola_restapi_helper.h" 0010 #include <QJsonDocument> 0011 QTEST_GUILESS_MAIN(GoogleAuthJobTest) 0012 using namespace RocketChatRestApi; 0013 GoogleAuthJobTest::GoogleAuthJobTest(QObject *parent) 0014 : QObject(parent) 0015 { 0016 } 0017 0018 void GoogleAuthJobTest::shouldHaveDefaultValue() 0019 { 0020 GoogleAuthJob job; 0021 verifyDefaultValue(&job); 0022 QVERIFY(!job.requireHttpAuthentication()); 0023 QVERIFY(job.idToken().isEmpty()); 0024 QVERIFY(job.accessToken().isEmpty()); 0025 QCOMPARE(job.expireTokenInSeconds(), -1); 0026 QVERIFY(!job.hasQueryParameterSupport()); 0027 } 0028 0029 void GoogleAuthJobTest::shouldGenerateRequest() 0030 { 0031 GoogleAuthJob job; 0032 0033 RestApiMethod method; 0034 method.setServerUrl(QStringLiteral("http://www.kde.org")); 0035 job.setRestApiMethod(&method); 0036 const QNetworkRequest request = job.request(); 0037 QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/login"))); 0038 QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), QStringLiteral("application/json")); 0039 } 0040 0041 void GoogleAuthJobTest::shouldGenerateJson() 0042 { 0043 GoogleAuthJob job; 0044 const QString secret = QStringLiteral("secret:"); 0045 job.setIdToken(secret); 0046 0047 const QString accessToken = QStringLiteral("accessToken"); 0048 job.setAccessToken(accessToken); 0049 0050 const int expireToken = 300; 0051 job.setExpireTokenInSeconds(expireToken); 0052 QCOMPARE(job.json().toJson(QJsonDocument::Compact), 0053 QStringLiteral(R"({"accessToken":"%1","expiresIn":300,"idToken":"%2","serviceName":"google"})").arg(accessToken, secret).toLatin1()); 0054 } 0055 0056 void GoogleAuthJobTest::shouldNotStarting() 0057 { 0058 GoogleAuthJob job; 0059 0060 RestApiMethod method; 0061 method.setServerUrl(QStringLiteral("http://www.kde.org")); 0062 job.setRestApiMethod(&method); 0063 0064 QNetworkAccessManager mNetworkAccessManager; 0065 job.setNetworkAccessManager(&mNetworkAccessManager); 0066 QVERIFY(!job.canStart()); 0067 const QString auth = QStringLiteral("foo"); 0068 const QString userId = QStringLiteral("foo"); 0069 job.setAuthToken(auth); 0070 QVERIFY(!job.canStart()); 0071 job.setUserId(userId); 0072 QVERIFY(!job.canStart()); 0073 const QString secret = QStringLiteral("secret:"); 0074 job.setIdToken(secret); 0075 QVERIFY(!job.canStart()); 0076 0077 const QString accessToken = QStringLiteral("accessToken"); 0078 job.setAccessToken(accessToken); 0079 QVERIFY(!job.canStart()); 0080 0081 const int expireToken = 300; 0082 job.setExpireTokenInSeconds(expireToken); 0083 QVERIFY(job.canStart()); 0084 } 0085 0086 #include "moc_googleauthjobtest.cpp"