pushnotificationstestutils.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (C) by Felix Weilbach <felix.weilbach@nextcloud.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #pragma once
  15. #include <functional>
  16. #include <QWebSocketServer>
  17. #include <QWebSocket>
  18. #include <QSignalSpy>
  19. #include "creds/abstractcredentials.h"
  20. #include "account.h"
  21. class FakeWebSocketServer : public QObject
  22. {
  23. Q_OBJECT
  24. public:
  25. explicit FakeWebSocketServer(quint16 port = 12345, QObject *parent = nullptr);
  26. ~FakeWebSocketServer() override;
  27. QWebSocket *authenticateAccount(
  28. const OCC::AccountPtr account, std::function<void(OCC::PushNotifications *pushNotifications)> beforeAuthentication = [](OCC::PushNotifications *) {}, std::function<void(void)> afterAuthentication = [] {});
  29. void close();
  30. [[nodiscard]] bool waitForTextMessages() const;
  31. [[nodiscard]] uint32_t textMessagesCount() const;
  32. [[nodiscard]] QString textMessage(int messageNumber) const;
  33. [[nodiscard]] QWebSocket *socketForTextMessage(int messageNumber) const;
  34. void clearTextMessages();
  35. static OCC::AccountPtr createAccount(const QString &username = "user", const QString &password = "password");
  36. signals:
  37. void closed();
  38. void processTextMessage(QWebSocket *sender, const QString &message);
  39. private slots:
  40. void processTextMessageInternal(const QString &message);
  41. void onNewConnection();
  42. void socketDisconnected();
  43. private:
  44. QWebSocketServer *_webSocketServer;
  45. QList<QWebSocket *> _clients;
  46. std::unique_ptr<QSignalSpy> _processTextMessageSpy;
  47. };
  48. class CredentialsStub : public OCC::AbstractCredentials
  49. {
  50. Q_OBJECT
  51. public:
  52. CredentialsStub(const QString &user, const QString &password);
  53. [[nodiscard]] QString authType() const override;
  54. [[nodiscard]] QString user() const override;
  55. [[nodiscard]] QString password() const override;
  56. [[nodiscard]] QNetworkAccessManager *createQNAM() const override;
  57. [[nodiscard]] bool ready() const override;
  58. void fetchFromKeychain() override;
  59. void askFromUser() override;
  60. bool stillValid(QNetworkReply *reply) override;
  61. void persist() override;
  62. void invalidateToken() override;
  63. void forgetSensitiveData() override;
  64. private:
  65. QString _user;
  66. QString _password;
  67. };