File indexing completed on 2024-05-05 05:04:13

0001 // SPDX-FileCopyrightText: 2021 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "mockaccount.h"
0005 #include "account/notificationhandler.h"
0006 #include "autotests/helperreply.h"
0007 
0008 using namespace Qt::Literals::StringLiterals;
0009 
0010 MockAccount::MockAccount(QObject *parent)
0011     : AbstractAccount(parent)
0012 {
0013     registerGet(apiUrl(QStringLiteral("/api/v1/preferences")), new TestReply(QStringLiteral("preferences.json"), this));
0014     m_preferences = new Preferences(this);
0015     auto notificationHandler = new NotificationHandler(new QNetworkAccessManager, this);
0016     connect(this, &MockAccount::notification, notificationHandler, [this, notificationHandler](std::shared_ptr<Notification> notification) {
0017         notificationHandler->handle(notification, this);
0018     });
0019 
0020     Q_EMIT authenticated(true, {});
0021 }
0022 
0023 void MockAccount::get(const QUrl &url,
0024                       bool authenticated,
0025                       QObject *parent,
0026                       std::function<void(QNetworkReply *)> callback,
0027                       std::function<void(QNetworkReply *)> errorCallback)
0028 {
0029     Q_UNUSED(authenticated)
0030     Q_UNUSED(parent)
0031     Q_UNUSED(errorCallback)
0032 
0033     if (m_getReplies.contains(url)) {
0034         auto reply = m_getReplies[url];
0035         reply->open(QIODevice::ReadOnly);
0036         callback(reply);
0037         reply->seek(0);
0038     } else {
0039         qWarning() << url << m_getReplies;
0040         if (errorCallback)
0041             errorCallback(nullptr);
0042     }
0043 }
0044 
0045 void MockAccount::post(const QUrl &url,
0046                        const QJsonDocument &doc,
0047                        bool authenticated,
0048                        QObject *parent,
0049                        std::function<void(QNetworkReply *)> callback,
0050                        std::function<void(QNetworkReply *)> errorCallback,
0051                        QHash<QByteArray, QByteArray> headers)
0052 {
0053     Q_UNUSED(doc)
0054     Q_UNUSED(authenticated)
0055     Q_UNUSED(parent)
0056     Q_UNUSED(errorCallback)
0057     Q_UNUSED(headers)
0058 
0059     if (m_postReplies.contains(url)) {
0060         auto reply = m_postReplies[url];
0061         reply->open(QIODevice::ReadOnly);
0062         callback(reply);
0063     }
0064 }
0065 
0066 void MockAccount::post(const QUrl &url,
0067                        const QUrlQuery &formdata,
0068                        bool authenticated,
0069                        QObject *parent,
0070                        std::function<void(QNetworkReply *)> callback,
0071                        std::function<void(QNetworkReply *)> errorCallback)
0072 {
0073     Q_UNUSED(url)
0074     Q_UNUSED(authenticated)
0075     Q_UNUSED(parent)
0076     Q_UNUSED(callback)
0077     Q_UNUSED(formdata)
0078     Q_UNUSED(errorCallback)
0079 }
0080 
0081 QNetworkReply *MockAccount::post(const QUrl &url, QHttpMultiPart *message, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback)
0082 {
0083     Q_UNUSED(url)
0084     Q_UNUSED(authenticated)
0085     Q_UNUSED(parent)
0086     Q_UNUSED(callback)
0087     Q_UNUSED(message)
0088     return nullptr;
0089 }
0090 
0091 void MockAccount::put(const QUrl &url, const QJsonDocument &doc, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback)
0092 {
0093     Q_UNUSED(url)
0094     Q_UNUSED(authenticated)
0095     Q_UNUSED(parent)
0096     Q_UNUSED(callback)
0097     Q_UNUSED(doc)
0098 }
0099 
0100 void MockAccount::put(const QUrl &url, const QUrlQuery &doc, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback)
0101 {
0102     Q_UNUSED(url)
0103     Q_UNUSED(authenticated)
0104     Q_UNUSED(parent)
0105     Q_UNUSED(callback)
0106     Q_UNUSED(doc)
0107 }
0108 
0109 void MockAccount::patch(const QUrl &url, QHttpMultiPart *multiPart, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback)
0110 {
0111     Q_UNUSED(url)
0112     Q_UNUSED(authenticated)
0113     Q_UNUSED(parent)
0114     Q_UNUSED(callback)
0115     Q_UNUSED(multiPart)
0116 }
0117 
0118 void MockAccount::deleteResource(const QUrl &url, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback)
0119 {
0120     Q_UNUSED(url)
0121     Q_UNUSED(authenticated)
0122     Q_UNUSED(parent)
0123     Q_UNUSED(callback)
0124 }
0125 
0126 QNetworkReply *MockAccount::upload(const QUrl &filename, std::function<void(QNetworkReply *)> callback)
0127 {
0128     Q_UNUSED(callback)
0129     Q_UNUSED(filename)
0130     return nullptr;
0131 }
0132 
0133 void MockAccount::writeToSettings()
0134 {
0135 }
0136 
0137 void MockAccount::buildFromSettings()
0138 {
0139 }
0140 
0141 void MockAccount::validateToken(bool newAccount)
0142 {
0143     Q_UNUSED(newAccount)
0144 }
0145 
0146 bool MockAccount::hasFollowRequests() const
0147 {
0148     return false;
0149 }
0150 
0151 void MockAccount::checkForFollowRequests()
0152 {
0153 }
0154 
0155 void MockAccount::registerPost(const QString &url, QNetworkReply *reply)
0156 {
0157     m_postReplies[apiUrl(url)] = reply;
0158 }
0159 
0160 void MockAccount::registerGet(const QUrl &url, QNetworkReply *reply)
0161 {
0162     m_getReplies[url] = reply;
0163 }
0164 
0165 void MockAccount::setFakeIdentity(const QJsonObject &object)
0166 {
0167     m_identity = std::make_shared<Identity>();
0168     m_identity->fromSourceData(object);
0169 }
0170 
0171 void MockAccount::clearFakeIdentity()
0172 {
0173     m_identity.reset();
0174 }
0175 
0176 void MockAccount::mentionNotification()
0177 {
0178     readNotificationFromFile("notification_mention.json"_L1);
0179 }
0180 
0181 void MockAccount::favoriteNotification()
0182 {
0183     readNotificationFromFile("notification_favorite.json"_L1);
0184 }
0185 
0186 void MockAccount::boostNotification()
0187 {
0188     readNotificationFromFile("notification_boost.json"_L1);
0189 }
0190 
0191 void MockAccount::followNotification()
0192 {
0193     readNotificationFromFile("notification_follow.json"_L1);
0194 }
0195 
0196 void MockAccount::followRequestNotification()
0197 {
0198     readNotificationFromFile("notification_request.json"_L1);
0199 }
0200 
0201 void MockAccount::statusNotification()
0202 {
0203     readNotificationFromFile("notification_status.json"_L1);
0204 }
0205 
0206 void MockAccount::updateNotification()
0207 {
0208     readNotificationFromFile("notification_update.json"_L1);
0209 }
0210 
0211 void MockAccount::pollNotification()
0212 {
0213     readNotificationFromFile("notification_poll.json"_L1);
0214 }
0215 
0216 void MockAccount::readNotificationFromFile(QLatin1String filename)
0217 {
0218     QFile statusExampleApi;
0219     statusExampleApi.setFileName(QLatin1String(DATA_DIR) + QLatin1Char('/') + filename);
0220     statusExampleApi.open(QIODevice::ReadOnly);
0221 
0222     handleNotification(QJsonDocument::fromJson(statusExampleApi.readAll()));
0223 }