File indexing completed on 2024-10-06 12:56:09

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 "autotests/helperreply.h"
0006 
0007 MockAccount::MockAccount(QObject *parent)
0008     : AbstractAccount(parent)
0009 {
0010     registerGet(apiUrl("/api/v1/preferences"), new TestReply("preferences.json", this));
0011     m_preferences = new Preferences(this);
0012 
0013     Q_EMIT authenticated(true);
0014 }
0015 
0016 void MockAccount::get(const QUrl &url,
0017                       bool authenticated,
0018                       QObject *parent,
0019                       std::function<void(QNetworkReply *)> callback,
0020                       std::function<void(QNetworkReply *)> errorCallback)
0021 {
0022     Q_UNUSED(authenticated)
0023     Q_UNUSED(parent)
0024     Q_UNUSED(errorCallback)
0025 
0026     if (m_getReplies.contains(url)) {
0027         auto reply = m_getReplies[url];
0028         reply->open(QIODevice::ReadOnly);
0029         callback(reply);
0030     } else {
0031         qWarning() << url << m_getReplies;
0032     }
0033 }
0034 
0035 void MockAccount::post(const QUrl &url,
0036                        const QJsonDocument &doc,
0037                        bool authenticated,
0038                        QObject *parent,
0039                        std::function<void(QNetworkReply *)> callback,
0040                        std::function<void(QNetworkReply *)> errorCallback,
0041                        QHash<QByteArray, QByteArray> headers)
0042 {
0043     Q_UNUSED(doc)
0044     Q_UNUSED(authenticated)
0045     Q_UNUSED(parent)
0046     Q_UNUSED(errorCallback)
0047     Q_UNUSED(headers)
0048 
0049     if (m_postReplies.contains(url)) {
0050         auto reply = m_postReplies[url];
0051         reply->open(QIODevice::ReadOnly);
0052         callback(reply);
0053     }
0054 }
0055 
0056 void MockAccount::post(const QUrl &url, const QUrlQuery &formdata, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback)
0057 {
0058     Q_UNUSED(url)
0059     Q_UNUSED(authenticated)
0060     Q_UNUSED(parent)
0061     Q_UNUSED(callback)
0062     Q_UNUSED(formdata)
0063 }
0064 
0065 QNetworkReply *MockAccount::post(const QUrl &url, QHttpMultiPart *message, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback)
0066 {
0067     Q_UNUSED(url)
0068     Q_UNUSED(authenticated)
0069     Q_UNUSED(parent)
0070     Q_UNUSED(callback)
0071     Q_UNUSED(message)
0072     return nullptr;
0073 }
0074 
0075 void MockAccount::put(const QUrl &url, const QJsonDocument &doc, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback)
0076 {
0077     Q_UNUSED(url)
0078     Q_UNUSED(authenticated)
0079     Q_UNUSED(parent)
0080     Q_UNUSED(callback)
0081     Q_UNUSED(doc)
0082 }
0083 
0084 void MockAccount::patch(const QUrl &url, QHttpMultiPart *multiPart, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback)
0085 {
0086     Q_UNUSED(url)
0087     Q_UNUSED(authenticated)
0088     Q_UNUSED(parent)
0089     Q_UNUSED(callback)
0090     Q_UNUSED(multiPart)
0091 }
0092 
0093 void MockAccount::deleteResource(const QUrl &url, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback)
0094 {
0095     Q_UNUSED(url)
0096     Q_UNUSED(authenticated)
0097     Q_UNUSED(parent)
0098     Q_UNUSED(callback)
0099 }
0100 
0101 QNetworkReply *MockAccount::upload(const QUrl &filename, std::function<void(QNetworkReply *)> callback)
0102 {
0103     Q_UNUSED(callback)
0104     Q_UNUSED(filename)
0105     return nullptr;
0106 }
0107 
0108 void MockAccount::writeToSettings()
0109 {
0110 }
0111 
0112 void MockAccount::buildFromSettings(const AccountConfig &settings)
0113 {
0114     Q_UNUSED(settings)
0115 }
0116 
0117 void MockAccount::validateToken()
0118 {
0119 }
0120 
0121 bool MockAccount::hasFollowRequests() const
0122 {
0123     return false;
0124 }
0125 
0126 void MockAccount::checkForFollowRequests()
0127 {
0128 }
0129 
0130 void MockAccount::registerPost(const QString &url, QNetworkReply *reply)
0131 {
0132     m_postReplies[apiUrl(url)] = reply;
0133 }
0134 
0135 void MockAccount::registerGet(const QUrl &url, QNetworkReply *reply)
0136 {
0137     m_getReplies[url] = reply;
0138 }
0139 
0140 void MockAccount::setFakeIdentity(const QJsonObject &object)
0141 {
0142     m_identity = std::make_shared<Identity>();
0143     m_identity->fromSourceData(object);
0144 }
0145 
0146 void MockAccount::clearFakeIdentity()
0147 {
0148     m_identity.reset();
0149 }