2021-01-12 11:23:41 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Felix Weilbach <felix.weilbach@nextcloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include <QWebSocketServer>
|
|
|
|
#include <QWebSocket>
|
2021-04-08 10:29:29 +03:00
|
|
|
#include <QSignalSpy>
|
2021-01-12 11:23:41 +03:00
|
|
|
|
|
|
|
#include "creds/abstractcredentials.h"
|
|
|
|
#include "account.h"
|
|
|
|
|
|
|
|
class FakeWebSocketServer : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit FakeWebSocketServer(quint16 port = 12345, QObject *parent = nullptr);
|
|
|
|
|
2021-08-17 13:39:31 +03:00
|
|
|
~FakeWebSocketServer() override;
|
2021-01-12 11:23:41 +03:00
|
|
|
|
2021-04-08 10:29:29 +03:00
|
|
|
QWebSocket *authenticateAccount(
|
|
|
|
const OCC::AccountPtr account, std::function<void(OCC::PushNotifications *pushNotifications)> beforeAuthentication = [](OCC::PushNotifications *) {}, std::function<void(void)> afterAuthentication = [] {});
|
|
|
|
|
2021-01-12 11:23:41 +03:00
|
|
|
void close();
|
|
|
|
|
2022-09-30 20:12:08 +03:00
|
|
|
[[nodiscard]] bool waitForTextMessages() const;
|
2021-04-08 10:29:29 +03:00
|
|
|
|
2022-09-30 20:12:08 +03:00
|
|
|
[[nodiscard]] uint32_t textMessagesCount() const;
|
2021-04-08 10:29:29 +03:00
|
|
|
|
2022-09-30 20:12:08 +03:00
|
|
|
[[nodiscard]] QString textMessage(int messageNumber) const;
|
2021-04-08 10:29:29 +03:00
|
|
|
|
2022-09-30 20:12:08 +03:00
|
|
|
[[nodiscard]] QWebSocket *socketForTextMessage(int messageNumber) const;
|
2021-04-08 10:29:29 +03:00
|
|
|
|
|
|
|
void clearTextMessages();
|
|
|
|
|
|
|
|
static OCC::AccountPtr createAccount(const QString &username = "user", const QString &password = "password");
|
2021-01-12 11:23:41 +03:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void closed();
|
|
|
|
void processTextMessage(QWebSocket *sender, const QString &message);
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void processTextMessageInternal(const QString &message);
|
|
|
|
void onNewConnection();
|
|
|
|
void socketDisconnected();
|
|
|
|
|
|
|
|
private:
|
|
|
|
QWebSocketServer *_webSocketServer;
|
|
|
|
QList<QWebSocket *> _clients;
|
2021-04-08 10:29:29 +03:00
|
|
|
|
|
|
|
std::unique_ptr<QSignalSpy> _processTextMessageSpy;
|
2021-01-12 11:23:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class CredentialsStub : public OCC::AbstractCredentials
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
CredentialsStub(const QString &user, const QString &password);
|
2022-09-30 20:12:08 +03:00
|
|
|
[[nodiscard]] QString authType() const override;
|
|
|
|
[[nodiscard]] QString user() const override;
|
|
|
|
[[nodiscard]] QString password() const override;
|
|
|
|
[[nodiscard]] QNetworkAccessManager *createQNAM() const override;
|
|
|
|
[[nodiscard]] bool ready() const override;
|
2021-08-17 13:39:31 +03:00
|
|
|
void fetchFromKeychain() override;
|
|
|
|
void askFromUser() override;
|
|
|
|
|
|
|
|
bool stillValid(QNetworkReply *reply) override;
|
|
|
|
void persist() override;
|
|
|
|
void invalidateToken() override;
|
|
|
|
void forgetSensitiveData() override;
|
2021-01-12 11:23:41 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
QString _user;
|
|
|
|
QString _password;
|
|
|
|
};
|