Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pushnotificationstestutils.cpp « test - github.com/nextcloud/desktop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 60727592f975964914bb8decd80e00b420915452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <QLoggingCategory>
#include <QSignalSpy>
#include <QTest>
#include <functional>

#include "pushnotificationstestutils.h"
#include "pushnotifications.h"

Q_LOGGING_CATEGORY(lcFakeWebSocketServer, "nextcloud.test.fakewebserver", QtInfoMsg)

FakeWebSocketServer::FakeWebSocketServer(quint16 port, QObject *parent)
    : QObject(parent)
    , _webSocketServer(new QWebSocketServer(QStringLiteral("Fake Server"), QWebSocketServer::NonSecureMode, this))
{
    if (!_webSocketServer->listen(QHostAddress::Any, port)) {
        Q_UNREACHABLE();
    }
    connect(_webSocketServer, &QWebSocketServer::newConnection, this, &FakeWebSocketServer::onNewConnection);
    connect(_webSocketServer, &QWebSocketServer::closed, this, &FakeWebSocketServer::closed);
    qCInfo(lcFakeWebSocketServer) << "Open fake websocket server on port:" << port;
    _processTextMessageSpy = std::make_unique<QSignalSpy>(this, &FakeWebSocketServer::processTextMessage);
}

FakeWebSocketServer::~FakeWebSocketServer()
{
    close();
}

QWebSocket *FakeWebSocketServer::authenticateAccount(const OCC::AccountPtr account, std::function<void(OCC::PushNotifications *pushNotifications)> beforeAuthentication, std::function<void(void)> afterAuthentication)
{
    const auto pushNotifications = account->pushNotifications();
    Q_ASSERT(pushNotifications);
    QSignalSpy readySpy(pushNotifications, &OCC::PushNotifications::ready);

    beforeAuthentication(pushNotifications);

    // Wait for authentication
    if (!waitForTextMessages()) {
        return nullptr;
    }

    // Right authentication data should be sent
    if (textMessagesCount() != 2) {
        return nullptr;
    }

    const auto socket = socketForTextMessage(0);
    const auto userSent = textMessage(0);
    const auto passwordSent = textMessage(1);

    if (userSent != account->credentials()->user() || passwordSent != account->credentials()->password()) {
        return nullptr;
    }

    // Sent authenticated
    socket->sendTextMessage("authenticated");

    // Wait for ready signal
    readySpy.wait();
    if (readySpy.count() != 1 || !account->pushNotifications()->isReady()) {
        return nullptr;
    }

    afterAuthentication();

    return socket;
}

void FakeWebSocketServer::close()
{
    if (_webSocketServer->isListening()) {
        qCInfo(lcFakeWebSocketServer) << "Close fake websocket server";

        _webSocketServer->close();
        qDeleteAll(_clients.begin(), _clients.end());
    }
}

void FakeWebSocketServer::processTextMessageInternal(const QString &message)
{
    auto client = qobject_cast<QWebSocket *>(sender());
    emit processTextMessage(client, message);
}

void FakeWebSocketServer::onNewConnection()
{
    qCInfo(lcFakeWebSocketServer) << "New connection on fake websocket server";

    auto socket = _webSocketServer->nextPendingConnection();

    connect(socket, &QWebSocket::textMessageReceived, this, &FakeWebSocketServer::processTextMessageInternal);
    connect(socket, &QWebSocket::disconnected, this, &FakeWebSocketServer::socketDisconnected);

    _clients << socket;
}

void FakeWebSocketServer::socketDisconnected()
{
    qCInfo(lcFakeWebSocketServer) << "Socket disconnected";

    auto client = qobject_cast<QWebSocket *>(sender());

    if (client) {
        _clients.removeAll(client);
        client->deleteLater();
    }
}

bool FakeWebSocketServer::waitForTextMessages() const
{
    return _processTextMessageSpy->wait();
}

uint32_t FakeWebSocketServer::textMessagesCount() const
{
    return _processTextMessageSpy->count();
}

QString FakeWebSocketServer::textMessage(uint32_t messageNumber) const
{
    Q_ASSERT(messageNumber < _processTextMessageSpy->count());
    return _processTextMessageSpy->at(messageNumber).at(1).toString();
}

QWebSocket *FakeWebSocketServer::socketForTextMessage(uint32_t messageNumber) const
{
    Q_ASSERT(messageNumber < _processTextMessageSpy->count());
    return _processTextMessageSpy->at(messageNumber).at(0).value<QWebSocket *>();
}

void FakeWebSocketServer::clearTextMessages()
{
    _processTextMessageSpy->clear();
}

OCC::AccountPtr FakeWebSocketServer::createAccount(const QString &username, const QString &password)
{
    auto account = OCC::Account::create();

    QStringList typeList;
    typeList.append("files");
    typeList.append("activities");
    typeList.append("notifications");

    QString websocketUrl("ws://localhost:12345");

    QVariantMap endpointsMap;
    endpointsMap["websocket"] = websocketUrl;

    QVariantMap notifyPushMap;
    notifyPushMap["type"] = typeList;
    notifyPushMap["endpoints"] = endpointsMap;

    QVariantMap capabilitiesMap;
    capabilitiesMap["notify_push"] = notifyPushMap;

    account->setCapabilities(capabilitiesMap);

    auto credentials = new CredentialsStub(username, password);
    account->setCredentials(credentials);

    return account;
}

CredentialsStub::CredentialsStub(const QString &user, const QString &password)
    : _user(user)
    , _password(password)
{
}

QString CredentialsStub::authType() const
{
    return "";
}

QString CredentialsStub::user() const
{
    return _user;
}

QString CredentialsStub::password() const
{
    return _password;
}

QNetworkAccessManager *CredentialsStub::createQNAM() const
{
    return nullptr;
}

bool CredentialsStub::ready() const
{
    return false;
}

void CredentialsStub::fetchFromKeychain() { }

void CredentialsStub::askFromUser() { }

bool CredentialsStub::stillValid(QNetworkReply * /*reply*/)
{
    return false;
}

void CredentialsStub::persist() { }

void CredentialsStub::invalidateToken() { }

void CredentialsStub::forgetSensitiveData() { }