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

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ddpauthenticationmanagerutilstest.h"
0008 #include "ddpapi/ddpauthenticationmanagerutils.h"
0009 #include <QJsonDocument>
0010 #include <QJsonObject>
0011 #include <QTest>
0012 QTEST_GUILESS_MAIN(DDPAuthenticationManagerUtilsTest)
0013 DDPAuthenticationManagerUtilsTest::DDPAuthenticationManagerUtilsTest(QObject *parent)
0014     : QObject{parent}
0015 {
0016 }
0017 
0018 void DDPAuthenticationManagerUtilsTest::shouldTestLoginResume()
0019 {
0020     {
0021         const QString token = QStringLiteral(R"(dsdf##56;)");
0022         QCOMPARE(QJsonDocument(DDPAuthenticationManagerUtils::loginResume(token)).toJson(QJsonDocument::Compact), QByteArray("[{\"resume\":\"dsdf##56;\"}]"));
0023     }
0024 
0025     {
0026         const QString token = QStringLiteral(R"(foo42";)");
0027         QCOMPARE(QJsonDocument(DDPAuthenticationManagerUtils::loginResume(token)).toJson(QJsonDocument::Compact), QByteArray("[{\"resume\":\"foo42\\\";\"}]"));
0028     }
0029 }
0030 
0031 void DDPAuthenticationManagerUtilsTest::shouldTestLdapLogin()
0032 {
0033     {
0034         const QString user = QStringLiteral(R"(username42)");
0035         const QString password = QStringLiteral(R"(blafoo4)");
0036         QCOMPARE(QJsonDocument(DDPAuthenticationManagerUtils::loginLdap(user, password)).toJson(QJsonDocument::Compact),
0037                  QByteArray("[{\"ldap\":true,\"ldapOptions\":{},\"ldapPass\":\"blafoo4\",\"username\":\"username42\"}]"));
0038     }
0039     {
0040         const QString user = QStringLiteral(R"(username42)");
0041         const QString password = QStringLiteral(R"(45p";)");
0042         QCOMPARE(QJsonDocument(DDPAuthenticationManagerUtils::loginLdap(user, password)).toJson(QJsonDocument::Compact),
0043                  QByteArray("[{\"ldap\":true,\"ldapOptions\":{},\"ldapPass\":\"45p\\\";\",\"username\":\"username42\"}]"));
0044     }
0045 }
0046 
0047 void DDPAuthenticationManagerUtilsTest::shouldTestloginOAuth()
0048 {
0049     {
0050         const QString credentialToken = QStringLiteral(R"(username42)");
0051         const QString credentialSecret = QStringLiteral(R"(blafoo4)");
0052         QCOMPARE(QJsonDocument(DDPAuthenticationManagerUtils::loginOAuth(credentialToken, credentialSecret)).toJson(QJsonDocument::Compact),
0053                  QByteArray("[{\"oauth\":{\"credentialSecret\":\"blafoo4\",\"credentialToken\":\"username42\"}}]"));
0054     }
0055 }
0056 
0057 void DDPAuthenticationManagerUtilsTest::shouldTestlogin()
0058 {
0059     {
0060         const QString user = QStringLiteral(R"(username42)");
0061         const QString password = QStringLiteral(R"(45p";)");
0062         QCOMPARE(QJsonDocument(DDPAuthenticationManagerUtils::login(user, password)).toJson(QJsonDocument::Compact),
0063                  QByteArray("[{\"password\":{\"algorithm\":\"sha-256\",\"digest\":\"465906e4251bcc0e47e97be030e468a3dcc011eb422b12c2a6ddba35d76f9df8\"},"
0064                             "\"user\":{\"username\":\"username42\"}}]"));
0065     }
0066     {
0067         // Use email as login
0068         const QString user = QStringLiteral(R"(foo@kde.org)");
0069         const QString password = QStringLiteral(R"(45p";)");
0070         QCOMPARE(QJsonDocument(DDPAuthenticationManagerUtils::login(user, password)).toJson(QJsonDocument::Compact),
0071                  QByteArray("[{\"password\":{\"algorithm\":\"sha-256\",\"digest\":\"465906e4251bcc0e47e97be030e468a3dcc011eb422b12c2a6ddba35d76f9df8\"},"
0072                             "\"user\":{\"email\":\"foo@kde.org\"}}]"));
0073     }
0074 }
0075 
0076 void DDPAuthenticationManagerUtilsTest::shouldTestSendOtp()
0077 {
0078     {
0079         const QString codeOtp = QStringLiteral(R"(foo42)");
0080         QJsonObject lastLoginPayLoad;
0081         lastLoginPayLoad[QLatin1String("test")] = QStringLiteral("test");
0082         QCOMPARE(QJsonDocument(DDPAuthenticationManagerUtils::sendOTP(codeOtp, lastLoginPayLoad)).toJson(QJsonDocument::Compact),
0083                  QByteArray("[{\"totp\":{\"code\":\"foo42\",\"login\":{\"test\":\"test\"}}}]"));
0084     }
0085 }
0086 
0087 #include "moc_ddpauthenticationmanagerutilstest.cpp"