File: pushnotificationstestutils.h

package info (click to toggle)
nextcloud-desktop 4.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,404 kB
  • sloc: cpp: 118,401; objc: 752; python: 606; sh: 395; ansic: 391; ruby: 174; makefile: 44; javascript: 32; xml: 6
file content (80 lines) | stat: -rw-r--r-- 2,242 bytes parent folder | download
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
/*
 * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#pragma once

#include <functional>

#include <QWebSocketServer>
#include <QWebSocket>
#include <QSignalSpy>

#include "creds/abstractcredentials.h"
#include "account.h"

class FakeWebSocketServer : public QObject
{
    Q_OBJECT
public:
    explicit FakeWebSocketServer(quint16 port = 12345, QObject *parent = nullptr);

    ~FakeWebSocketServer() override;

    QWebSocket *authenticateAccount(
        const OCC::AccountPtr account, std::function<void(OCC::PushNotifications *pushNotifications)> beforeAuthentication = [](OCC::PushNotifications *) {}, std::function<void(void)> afterAuthentication = [] {});

    void close();

    [[nodiscard]] bool waitForTextMessages() const;

    [[nodiscard]] uint32_t textMessagesCount() const;

    [[nodiscard]] QString textMessage(int messageNumber) const;

    [[nodiscard]] QWebSocket *socketForTextMessage(int messageNumber) const;

    void clearTextMessages();

    static OCC::AccountPtr createAccount(const QString &username = "user", const QString &password = "password");

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;

    std::unique_ptr<QSignalSpy> _processTextMessageSpy;
};

class CredentialsStub : public OCC::AbstractCredentials
{
    Q_OBJECT

public:
    CredentialsStub(const QString &user, const QString &password);
    [[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;
    void fetchFromKeychain() override;
    void askFromUser() override;

    bool stillValid(QNetworkReply *reply) override;
    void persist() override;
    void invalidateToken() override;
    void forgetSensitiveData() override;

private:
    QString _user;
    QString _password;
};