testpushnotifications.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include <QTest>
  15. #include <QVector>
  16. #include <QWebSocketServer>
  17. #include <QSignalSpy>
  18. #include "accountfwd.h"
  19. #include "pushnotifications.h"
  20. #include "pushnotificationstestutils.h"
  21. #define RETURN_FALSE_ON_FAIL(expr) \
  22. if (!(expr)) { \
  23. return false; \
  24. }
  25. bool verifyCalledOnceWithAccount(QSignalSpy &spy, OCC::AccountPtr account)
  26. {
  27. RETURN_FALSE_ON_FAIL(spy.count() == 1);
  28. auto accountFromSpy = spy.at(0).at(0).value<OCC::Account *>();
  29. RETURN_FALSE_ON_FAIL(accountFromSpy == account.data());
  30. return true;
  31. }
  32. bool failThreeAuthenticationAttempts(FakeWebSocketServer &fakeServer, OCC::AccountPtr account)
  33. {
  34. RETURN_FALSE_ON_FAIL(account);
  35. RETURN_FALSE_ON_FAIL(account->pushNotifications());
  36. account->pushNotifications()->setReconnectTimerInterval(0);
  37. QSignalSpy authenticationFailedSpy(account->pushNotifications(), &OCC::PushNotifications::authenticationFailed);
  38. // Let three authentication attempts fail
  39. for (uint8_t i = 0; i < 3; ++i) {
  40. RETURN_FALSE_ON_FAIL(fakeServer.waitForTextMessages());
  41. RETURN_FALSE_ON_FAIL(fakeServer.textMessagesCount() == 2);
  42. auto socket = fakeServer.socketForTextMessage(0);
  43. fakeServer.clearTextMessages();
  44. socket->sendTextMessage("err: Invalid credentials");
  45. }
  46. // Now the authenticationFailed Signal should be emitted
  47. RETURN_FALSE_ON_FAIL(authenticationFailedSpy.wait());
  48. RETURN_FALSE_ON_FAIL(authenticationFailedSpy.count() == 1);
  49. return true;
  50. }
  51. class TestPushNotifications : public QObject
  52. {
  53. Q_OBJECT
  54. private slots:
  55. void testTryReconnect_capabilitesReportPushNotificationsAvailable_reconnectForEver()
  56. {
  57. FakeWebSocketServer fakeServer;
  58. auto account = FakeWebSocketServer::createAccount();
  59. // Let if fail a few times
  60. QVERIFY(failThreeAuthenticationAttempts(fakeServer, account));
  61. account->pushNotifications()->setup();
  62. QVERIFY(failThreeAuthenticationAttempts(fakeServer, account));
  63. account->setPushNotificationsReconnectInterval(0);
  64. // Push notifications should try to reconnect
  65. QVERIFY(fakeServer.authenticateAccount(account));
  66. account->setPushNotificationsReconnectInterval(1000 * 60 * 2);
  67. }
  68. void testSetup_correctCredentials_authenticateAndEmitReady()
  69. {
  70. FakeWebSocketServer fakeServer;
  71. std::unique_ptr<QSignalSpy> filesChangedSpy;
  72. std::unique_ptr<QSignalSpy> notificationsChangedSpy;
  73. std::unique_ptr<QSignalSpy> activitiesChangedSpy;
  74. auto account = FakeWebSocketServer::createAccount();
  75. QVERIFY(fakeServer.authenticateAccount(
  76. account, [&](OCC::PushNotifications *pushNotifications) {
  77. filesChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::filesChanged));
  78. notificationsChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::notificationsChanged));
  79. activitiesChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::activitiesChanged));
  80. },
  81. [&] {
  82. QVERIFY(verifyCalledOnceWithAccount(*filesChangedSpy, account));
  83. QVERIFY(verifyCalledOnceWithAccount(*notificationsChangedSpy, account));
  84. QVERIFY(verifyCalledOnceWithAccount(*activitiesChangedSpy, account));
  85. }));
  86. }
  87. void testOnWebSocketTextMessageReceived_notifyFileMessage_emitFilesChanged()
  88. {
  89. FakeWebSocketServer fakeServer;
  90. auto account = FakeWebSocketServer::createAccount();
  91. const auto socket = fakeServer.authenticateAccount(account);
  92. QVERIFY(socket);
  93. QSignalSpy filesChangedSpy(account->pushNotifications(), &OCC::PushNotifications::filesChanged);
  94. socket->sendTextMessage("notify_file");
  95. // filesChanged signal should be emitted
  96. QVERIFY(filesChangedSpy.wait());
  97. QVERIFY(verifyCalledOnceWithAccount(filesChangedSpy, account));
  98. }
  99. void testOnWebSocketTextMessageReceived_notifyActivityMessage_emitNotification()
  100. {
  101. FakeWebSocketServer fakeServer;
  102. auto account = FakeWebSocketServer::createAccount();
  103. const auto socket = fakeServer.authenticateAccount(account);
  104. QVERIFY(socket);
  105. QSignalSpy activitySpy(account->pushNotifications(), &OCC::PushNotifications::activitiesChanged);
  106. QVERIFY(activitySpy.isValid());
  107. // Send notify_file push notification
  108. socket->sendTextMessage("notify_activity");
  109. // notification signal should be emitted
  110. QVERIFY(activitySpy.wait());
  111. QVERIFY(verifyCalledOnceWithAccount(activitySpy, account));
  112. }
  113. void testOnWebSocketTextMessageReceived_notifyNotificationMessage_emitNotification()
  114. {
  115. FakeWebSocketServer fakeServer;
  116. auto account = FakeWebSocketServer::createAccount();
  117. const auto socket = fakeServer.authenticateAccount(account);
  118. QVERIFY(socket);
  119. QSignalSpy notificationSpy(account->pushNotifications(), &OCC::PushNotifications::notificationsChanged);
  120. QVERIFY(notificationSpy.isValid());
  121. // Send notify_file push notification
  122. socket->sendTextMessage("notify_notification");
  123. // notification signal should be emitted
  124. QVERIFY(notificationSpy.wait());
  125. QVERIFY(verifyCalledOnceWithAccount(notificationSpy, account));
  126. }
  127. void testOnWebSocketTextMessageReceived_invalidCredentialsMessage_reconnectWebSocket()
  128. {
  129. FakeWebSocketServer fakeServer;
  130. auto account = FakeWebSocketServer::createAccount();
  131. // Need to set reconnect timer interval to zero for tests
  132. account->pushNotifications()->setReconnectTimerInterval(0);
  133. // Wait for authentication attempt and then sent invalid credentials
  134. QVERIFY(fakeServer.waitForTextMessages());
  135. QCOMPARE(fakeServer.textMessagesCount(), 2);
  136. const auto socket = fakeServer.socketForTextMessage(0);
  137. const auto firstPasswordSent = fakeServer.textMessage(1);
  138. QCOMPARE(firstPasswordSent, account->credentials()->password());
  139. fakeServer.clearTextMessages();
  140. socket->sendTextMessage("err: Invalid credentials");
  141. // Wait for a new authentication attempt
  142. QVERIFY(fakeServer.waitForTextMessages());
  143. QCOMPARE(fakeServer.textMessagesCount(), 2);
  144. const auto secondPasswordSent = fakeServer.textMessage(1);
  145. QCOMPARE(secondPasswordSent, account->credentials()->password());
  146. }
  147. void testOnWebSocketError_connectionLost_emitConnectionLost()
  148. {
  149. FakeWebSocketServer fakeServer;
  150. auto account = FakeWebSocketServer::createAccount();
  151. QSignalSpy connectionLostSpy(account->pushNotifications(), &OCC::PushNotifications::connectionLost);
  152. QSignalSpy pushNotificationsDisabledSpy(account.data(), &OCC::Account::pushNotificationsDisabled);
  153. QVERIFY(connectionLostSpy.isValid());
  154. // Wait for authentication and then sent a network error
  155. QVERIFY(fakeServer.waitForTextMessages());
  156. QCOMPARE(fakeServer.textMessagesCount(), 2);
  157. auto socket = fakeServer.socketForTextMessage(0);
  158. socket->abort();
  159. QVERIFY(connectionLostSpy.wait());
  160. // Account handled connectionLost signal and disabled push notifications
  161. QCOMPARE(pushNotificationsDisabledSpy.count(), 1);
  162. }
  163. void testSetup_maxConnectionAttemptsReached_disablePushNotifications()
  164. {
  165. FakeWebSocketServer fakeServer;
  166. auto account = FakeWebSocketServer::createAccount();
  167. QSignalSpy pushNotificationsDisabledSpy(account.data(), &OCC::Account::pushNotificationsDisabled);
  168. QVERIFY(failThreeAuthenticationAttempts(fakeServer, account));
  169. // Account disabled the push notifications
  170. QCOMPARE(pushNotificationsDisabledSpy.count(), 1);
  171. }
  172. void testOnWebSocketSslError_sslError_disablePushNotifications()
  173. {
  174. FakeWebSocketServer fakeServer;
  175. auto account = FakeWebSocketServer::createAccount();
  176. QSignalSpy pushNotificationsDisabledSpy(account.data(), &OCC::Account::pushNotificationsDisabled);
  177. QVERIFY(fakeServer.waitForTextMessages());
  178. // FIXME: This a little bit ugly but I had no better idea how to trigger a error on the websocket client.
  179. // The websocket that is retrived through the server is not connected to the ssl error signal.
  180. auto pushNotificationsWebSocketChildren = account->pushNotifications()->findChildren<QWebSocket *>();
  181. QVERIFY(pushNotificationsWebSocketChildren.size() == 1);
  182. emit pushNotificationsWebSocketChildren[0]->sslErrors(QList<QSslError>());
  183. // Account handled connectionLost signal and the authenticationFailed Signal should be emitted
  184. QCOMPARE(pushNotificationsDisabledSpy.count(), 1);
  185. }
  186. void testAccount_web_socket_connectionLost_emitNotificationsDisabled()
  187. {
  188. FakeWebSocketServer fakeServer;
  189. auto account = FakeWebSocketServer::createAccount();
  190. // Need to set reconnect timer interval to zero for tests
  191. account->pushNotifications()->setReconnectTimerInterval(0);
  192. const auto socket = fakeServer.authenticateAccount(account);
  193. QVERIFY(socket);
  194. QSignalSpy connectionLostSpy(account->pushNotifications(), &OCC::PushNotifications::connectionLost);
  195. QVERIFY(connectionLostSpy.isValid());
  196. QSignalSpy pushNotificationsDisabledSpy(account.data(), &OCC::Account::pushNotificationsDisabled);
  197. QVERIFY(pushNotificationsDisabledSpy.isValid());
  198. // Wait for authentication and then sent a network error
  199. socket->abort();
  200. QVERIFY(pushNotificationsDisabledSpy.wait());
  201. QCOMPARE(pushNotificationsDisabledSpy.count(), 1);
  202. QCOMPARE(connectionLostSpy.count(), 1);
  203. auto accountSent = pushNotificationsDisabledSpy.at(0).at(0).value<OCC::Account *>();
  204. QCOMPARE(accountSent, account.data());
  205. }
  206. void testAccount_web_socket_authenticationFailed_emitNotificationsDisabled()
  207. {
  208. FakeWebSocketServer fakeServer;
  209. auto account = FakeWebSocketServer::createAccount();
  210. QSignalSpy pushNotificationsDisabledSpy(account.data(), &OCC::Account::pushNotificationsDisabled);
  211. QVERIFY(pushNotificationsDisabledSpy.isValid());
  212. QVERIFY(failThreeAuthenticationAttempts(fakeServer, account));
  213. // Now the pushNotificationsDisabled Signal should be emitted
  214. QCOMPARE(pushNotificationsDisabledSpy.count(), 1);
  215. auto accountSent = pushNotificationsDisabledSpy.at(0).at(0).value<OCC::Account *>();
  216. QCOMPARE(accountSent, account.data());
  217. }
  218. void testPingTimeout_pingTimedOut_reconnect()
  219. {
  220. FakeWebSocketServer fakeServer;
  221. std::unique_ptr<QSignalSpy> filesChangedSpy;
  222. std::unique_ptr<QSignalSpy> notificationsChangedSpy;
  223. std::unique_ptr<QSignalSpy> activitiesChangedSpy;
  224. auto account = FakeWebSocketServer::createAccount();
  225. QVERIFY(fakeServer.authenticateAccount(account));
  226. // Set the ping timeout interval to zero and check if the server attemps to authenticate again
  227. fakeServer.clearTextMessages();
  228. account->pushNotifications()->setPingInterval(0);
  229. QVERIFY(fakeServer.authenticateAccount(
  230. account, [&](OCC::PushNotifications *pushNotifications) {
  231. filesChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::filesChanged));
  232. notificationsChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::notificationsChanged));
  233. activitiesChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::activitiesChanged));
  234. },
  235. [&] {
  236. QVERIFY(verifyCalledOnceWithAccount(*filesChangedSpy, account));
  237. QVERIFY(verifyCalledOnceWithAccount(*notificationsChangedSpy, account));
  238. QVERIFY(verifyCalledOnceWithAccount(*activitiesChangedSpy, account));
  239. }));
  240. }
  241. };
  242. QTEST_GUILESS_MAIN(TestPushNotifications)
  243. #include "testpushnotifications.moc"