Move activitylistmodel testing QNAM override to dedicated convenience method in ActivityTestUtils

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2023-03-01 01:52:22 +01:00
parent 6364c6226c
commit e42f5cf33e
No known key found for this signature in database
GPG key ID: C839200C384636B0
3 changed files with 77 additions and 36 deletions

View file

@ -13,12 +13,65 @@
*/
#include "activitylistmodeltestutils.h"
#include "syncenginetestutils.h"
#include <QString>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
namespace {
static QByteArray fake404Response = R"(
{"ocs":{"meta":{"status":"failure","statuscode":404,"message":"Invalid query, please check the syntax. API specifications are here: http:\/\/www.freedesktop.org\/wiki\/Specifications\/open-collaboration-services.\n"},"data":[]}}
)";
static QByteArray fake400Response = R"(
{"ocs":{"meta":{"status":"failure","statuscode":400,"message":"Parameter is incorrect.\n"},"data":[]}}
)";
static QByteArray fake500Response = R"(
{"ocs":{"meta":{"status":"failure","statuscode":500,"message":"Internal Server Error.\n"},"data":[]}}
)";
}
namespace ActivityListModelTestUtils
{
QNetworkReply *almTestQnamOverride(FakeQNAM * const fakeQnam,
const QNetworkAccessManager::Operation op,
const QNetworkRequest &req,
const QString &accountUrl,
QObject * const parent,
const int searchResultsReplyDelay,
QIODevice * const device)
{
Q_UNUSED(device);
QNetworkReply *reply = nullptr;
const auto urlQuery = QUrlQuery(req.url());
const auto format = urlQuery.queryItemValue(QStringLiteral("format"));
const auto since = urlQuery.queryItemValue(QStringLiteral("since")).toInt();
const auto limit = urlQuery.queryItemValue(QStringLiteral("limit")).toInt();
const auto path = req.url().path();
if (!req.url().toString().startsWith(accountUrl)) {
reply = new FakeErrorReply(op, req, parent, 404, fake404Response);
}
if (format != QStringLiteral("json")) {
reply = new FakeErrorReply(op, req, parent, 400, fake400Response);
}
if (path.startsWith(QStringLiteral("/ocs/v2.php/apps/activity/api/v2/activity"))) {
reply = new FakePayloadReply(op, req, FakeRemoteActivityStorage::instance()->activityJsonData(since, limit), searchResultsReplyDelay, fakeQnam);
}
if (!reply) {
return qobject_cast<QNetworkReply*>(new FakeErrorReply(op, req, parent, 404, QByteArrayLiteral("{error: \"Not found!\"}")));
}
return reply;
}
FakeRemoteActivityStorage *FakeRemoteActivityStorage::_instance = nullptr;
FakeRemoteActivityStorage* FakeRemoteActivityStorage::instance()
@ -370,3 +423,5 @@ void TestingALM::slotProcessReceivedActivities()
setAndRefreshCurrentlyFetching(false);
emit activitiesProcessed();
}
}

View file

@ -23,9 +23,21 @@
#pragma once
class FakeQNAM;
class QByteArray;
class QJsonValue;
namespace ActivityListModelTestUtils
{
[[nodiscard]] QNetworkReply *almTestQnamOverride(FakeQNAM * const fakeQnam,
const QNetworkAccessManager::Operation op,
const QNetworkRequest &req,
const QString &accountUrl,
QObject * const parent = nullptr,
const int searchResultsReplyDelay = 0,
QIODevice * const device = nullptr);
class FakeRemoteActivityStorage
{
FakeRemoteActivityStorage() = default;
@ -71,3 +83,5 @@ signals:
private:
int _numRowsPrev = 0;
};
}

View file

@ -21,17 +21,7 @@
#include <QSignalSpy>
#include <QTest>
static QByteArray fake404Response = R"(
{"ocs":{"meta":{"status":"failure","statuscode":404,"message":"Invalid query, please check the syntax. API specifications are here: http:\/\/www.freedesktop.org\/wiki\/Specifications\/open-collaboration-services.\n"},"data":[]}}
)";
static QByteArray fake400Response = R"(
{"ocs":{"meta":{"status":"failure","statuscode":400,"message":"Parameter is incorrect.\n"},"data":[]}}
)";
static QByteArray fake500Response = R"(
{"ocs":{"meta":{"status":"failure","statuscode":500,"message":"Internal Server Error.\n"},"data":[]}}
)";
using namespace ActivityListModelTestUtils;
class TestActivityListModel : public QObject
{
@ -107,31 +97,13 @@ private slots:
accountState.reset(new OCC::AccountState(account));
fakeQnam->setOverride([this](QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *device) {
Q_UNUSED(device);
QNetworkReply *reply = nullptr;
const auto urlQuery = QUrlQuery(req.url());
const auto format = urlQuery.queryItemValue(QStringLiteral("format"));
const auto since = urlQuery.queryItemValue(QStringLiteral("since")).toInt();
const auto limit = urlQuery.queryItemValue(QStringLiteral("limit")).toInt();
const auto path = req.url().path();
if (!req.url().toString().startsWith(accountState->account()->url().toString())) {
reply = new FakeErrorReply(op, req, this, 404, fake404Response);
}
if (format != QStringLiteral("json")) {
reply = new FakeErrorReply(op, req, this, 400, fake400Response);
}
if (path.startsWith(QStringLiteral("/ocs/v2.php/apps/activity/api/v2/activity"))) {
reply = new FakePayloadReply(op, req, FakeRemoteActivityStorage::instance()->activityJsonData(since, limit), searchResultsReplyDelay, fakeQnam.data());
}
if (!reply) {
return qobject_cast<QNetworkReply*>(new FakeErrorReply(op, req, this, 404, QByteArrayLiteral("{error: \"Not found!\"}")));
}
return reply;
Q_UNUSED(device)
return almTestQnamOverride(fakeQnam.data(),
op,
req,
accountState->account()->url().toString(),
this,
searchResultsReplyDelay);
});
OCC::AccountManager::instance()->addAccount(account);