2021-01-12 11:23:41 +03:00
|
|
|
#include <QTest>
|
|
|
|
#include <QVector>
|
|
|
|
#include <QWebSocketServer>
|
|
|
|
#include <QSignalSpy>
|
|
|
|
|
2021-04-08 10:29:29 +03:00
|
|
|
#include "accountfwd.h"
|
2021-01-12 11:23:41 +03:00
|
|
|
#include "pushnotifications.h"
|
|
|
|
#include "pushnotificationstestutils.h"
|
|
|
|
|
2021-04-08 10:29:29 +03:00
|
|
|
bool verifyCalledOnceWithAccount(QSignalSpy &spy, OCC::AccountPtr account)
|
|
|
|
{
|
|
|
|
if (spy.count() != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto accountFromSpy = spy.at(0).at(0).value<OCC::Account *>();
|
|
|
|
if (accountFromSpy != account.data()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:23:41 +03:00
|
|
|
class TestPushNotifications : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void testSetup_correctCredentials_authenticateAndEmitReady()
|
|
|
|
{
|
|
|
|
FakeWebSocketServer fakeServer;
|
2021-04-08 10:29:29 +03:00
|
|
|
std::unique_ptr<QSignalSpy> filesChangedSpy;
|
|
|
|
std::unique_ptr<QSignalSpy> notificationsChangedSpy;
|
|
|
|
std::unique_ptr<QSignalSpy> activitiesChangedSpy;
|
2021-01-12 11:23:41 +03:00
|
|
|
auto account = FakeWebSocketServer::createAccount();
|
|
|
|
|
2021-04-08 10:29:29 +03:00
|
|
|
QVERIFY(fakeServer.authenticateAccount(
|
|
|
|
account, [&](OCC::PushNotifications *pushNotifications) {
|
|
|
|
filesChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::filesChanged));
|
|
|
|
notificationsChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::notificationsChanged));
|
|
|
|
activitiesChangedSpy.reset(new QSignalSpy(pushNotifications, &OCC::PushNotifications::activitiesChanged));
|
|
|
|
},
|
|
|
|
[&] {
|
|
|
|
QVERIFY(verifyCalledOnceWithAccount(*filesChangedSpy, account));
|
|
|
|
QVERIFY(verifyCalledOnceWithAccount(*notificationsChangedSpy, account));
|
|
|
|
QVERIFY(verifyCalledOnceWithAccount(*activitiesChangedSpy, account));
|
|
|
|
}));
|
2021-01-12 11:23:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void testOnWebSocketTextMessageReceived_notifyFileMessage_emitFilesChanged()
|
|
|
|
{
|
|
|
|
FakeWebSocketServer fakeServer;
|
|
|
|
auto account = FakeWebSocketServer::createAccount();
|
2021-04-08 10:29:29 +03:00
|
|
|
const auto socket = fakeServer.authenticateAccount(account);
|
|
|
|
QVERIFY(socket);
|
2021-01-12 11:23:41 +03:00
|
|
|
QSignalSpy filesChangedSpy(account->pushNotifications(), &OCC::PushNotifications::filesChanged);
|
|
|
|
|
|
|
|
socket->sendTextMessage("notify_file");
|
|
|
|
|
|
|
|
// filesChanged signal should be emitted
|
|
|
|
QVERIFY(filesChangedSpy.wait());
|
2021-04-08 10:29:29 +03:00
|
|
|
QVERIFY(verifyCalledOnceWithAccount(filesChangedSpy, account));
|
2021-01-12 11:23:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void testOnWebSocketTextMessageReceived_notifyActivityMessage_emitNotification()
|
|
|
|
{
|
|
|
|
FakeWebSocketServer fakeServer;
|
|
|
|
auto account = FakeWebSocketServer::createAccount();
|
2021-04-08 10:29:29 +03:00
|
|
|
const auto socket = fakeServer.authenticateAccount(account);
|
|
|
|
QVERIFY(socket);
|
2021-01-26 17:57:46 +03:00
|
|
|
QSignalSpy activitySpy(account->pushNotifications(), &OCC::PushNotifications::activitiesChanged);
|
|
|
|
QVERIFY(activitySpy.isValid());
|
2021-01-12 11:23:41 +03:00
|
|
|
|
2021-04-08 10:29:29 +03:00
|
|
|
// Send notify_file push notification
|
2021-01-12 11:23:41 +03:00
|
|
|
socket->sendTextMessage("notify_activity");
|
|
|
|
|
|
|
|
// notification signal should be emitted
|
2021-01-26 17:57:46 +03:00
|
|
|
QVERIFY(activitySpy.wait());
|
2021-04-08 10:29:29 +03:00
|
|
|
QVERIFY(verifyCalledOnceWithAccount(activitySpy, account));
|
2021-01-12 11:23:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void testOnWebSocketTextMessageReceived_notifyNotificationMessage_emitNotification()
|
|
|
|
{
|
|
|
|
FakeWebSocketServer fakeServer;
|
|
|
|
auto account = FakeWebSocketServer::createAccount();
|
2021-04-08 10:29:29 +03:00
|
|
|
const auto socket = fakeServer.authenticateAccount(account);
|
|
|
|
QVERIFY(socket);
|
2021-01-26 17:57:46 +03:00
|
|
|
QSignalSpy notificationSpy(account->pushNotifications(), &OCC::PushNotifications::notificationsChanged);
|
2021-01-12 11:23:41 +03:00
|
|
|
QVERIFY(notificationSpy.isValid());
|
|
|
|
|
2021-04-08 10:29:29 +03:00
|
|
|
// Send notify_file push notification
|
2021-01-12 11:23:41 +03:00
|
|
|
socket->sendTextMessage("notify_notification");
|
|
|
|
|
|
|
|
// notification signal should be emitted
|
|
|
|
QVERIFY(notificationSpy.wait());
|
2021-04-08 10:29:29 +03:00
|
|
|
QVERIFY(verifyCalledOnceWithAccount(notificationSpy, account));
|
2021-01-12 11:23:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void testOnWebSocketTextMessageReceived_invalidCredentialsMessage_reconnectWebSocket()
|
|
|
|
{
|
|
|
|
FakeWebSocketServer fakeServer;
|
|
|
|
auto account = FakeWebSocketServer::createAccount();
|
|
|
|
// Need to set reconnect timer interval to zero for tests
|
|
|
|
account->pushNotifications()->setReconnectTimerInterval(0);
|
|
|
|
|
|
|
|
// Wait for authentication attempt and then sent invalid credentials
|
2021-04-08 10:29:29 +03:00
|
|
|
QVERIFY(fakeServer.waitForTextMessages());
|
|
|
|
QCOMPARE(fakeServer.textMessagesCount(), 2);
|
|
|
|
const auto socket = fakeServer.socketForTextMessage(0);
|
|
|
|
const auto firstPasswordSent = fakeServer.textMessage(1);
|
|
|
|
QCOMPARE(firstPasswordSent, account->credentials()->password());
|
|
|
|
fakeServer.clearTextMessages();
|
2021-01-12 11:23:41 +03:00
|
|
|
socket->sendTextMessage("err: Invalid credentials");
|
|
|
|
|
|
|
|
// Wait for a new authentication attempt
|
2021-04-08 10:29:29 +03:00
|
|
|
QVERIFY(fakeServer.waitForTextMessages());
|
|
|
|
QCOMPARE(fakeServer.textMessagesCount(), 2);
|
|
|
|
const auto secondPasswordSent = fakeServer.textMessage(1);
|
|
|
|
QCOMPARE(secondPasswordSent, account->credentials()->password());
|
2021-01-12 11:23:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void testOnWebSocketError_connectionLost_emitConnectionLost()
|
|
|
|
{
|
|
|
|
FakeWebSocketServer fakeServer;
|
|
|
|
auto account = FakeWebSocketServer::createAccount();
|
|
|
|
QSignalSpy connectionLostSpy(account->pushNotifications(), &OCC::PushNotifications::connectionLost);
|
|
|
|
QVERIFY(connectionLostSpy.isValid());
|
|
|
|
|
|
|
|
// Wait for authentication and then sent a network error
|
2021-04-08 10:29:29 +03:00
|
|
|
QVERIFY(fakeServer.waitForTextMessages());
|
|
|
|
QCOMPARE(fakeServer.textMessagesCount(), 2);
|
|
|
|
auto socket = fakeServer.socketForTextMessage(0);
|
2021-01-12 11:23:41 +03:00
|
|
|
socket->abort();
|
|
|
|
|
|
|
|
QVERIFY(connectionLostSpy.wait());
|
|
|
|
// Account handled connectionLost signal and deleted PushNotifications
|
|
|
|
QCOMPARE(account->pushNotifications(), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testSetup_maxConnectionAttemptsReached_deletePushNotifications()
|
|
|
|
{
|
|
|
|
FakeWebSocketServer fakeServer;
|
|
|
|
auto account = FakeWebSocketServer::createAccount();
|
|
|
|
account->pushNotifications()->setReconnectTimerInterval(0);
|
|
|
|
QSignalSpy authenticationFailedSpy(account->pushNotifications(), &OCC::PushNotifications::authenticationFailed);
|
|
|
|
QVERIFY(authenticationFailedSpy.isValid());
|
|
|
|
|
|
|
|
// Let three authentication attempts fail
|
2021-04-08 10:29:29 +03:00
|
|
|
for (uint8_t i = 0; i < 3; ++i) {
|
|
|
|
QVERIFY(fakeServer.waitForTextMessages());
|
|
|
|
QCOMPARE(fakeServer.textMessagesCount(), 2);
|
|
|
|
auto socket = fakeServer.socketForTextMessage(0);
|
|
|
|
fakeServer.clearTextMessages();
|
|
|
|
socket->sendTextMessage("err: Invalid credentials");
|
|
|
|
}
|
2021-01-12 11:23:41 +03:00
|
|
|
|
|
|
|
// Now the authenticationFailed Signal should be emitted
|
|
|
|
QVERIFY(authenticationFailedSpy.wait());
|
|
|
|
QCOMPARE(authenticationFailedSpy.count(), 1);
|
|
|
|
// Account deleted the push notifications
|
|
|
|
QCOMPARE(account->pushNotifications(), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testOnWebSocketSslError_sslError_deletePushNotifications()
|
|
|
|
{
|
|
|
|
FakeWebSocketServer fakeServer;
|
|
|
|
auto account = FakeWebSocketServer::createAccount();
|
|
|
|
|
2021-04-08 10:29:29 +03:00
|
|
|
QVERIFY(fakeServer.waitForTextMessages());
|
2021-01-12 11:23:41 +03:00
|
|
|
// FIXME: This a little bit ugly but I had no better idea how to trigger a error on the websocket client.
|
|
|
|
// The websocket that is retrived through the server is not connected to the ssl error signal.
|
|
|
|
auto pushNotificationsWebSocketChildren = account->pushNotifications()->findChildren<QWebSocket *>();
|
|
|
|
QVERIFY(pushNotificationsWebSocketChildren.size() == 1);
|
|
|
|
emit pushNotificationsWebSocketChildren[0]->sslErrors(QList<QSslError>());
|
|
|
|
|
|
|
|
// Account handled connectionLost signal and deleted PushNotifications
|
|
|
|
QCOMPARE(account->pushNotifications(), nullptr);
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:57:46 +03:00
|
|
|
void testAccount_web_socket_connectionLost_emitNotificationsDisabled()
|
|
|
|
{
|
|
|
|
FakeWebSocketServer fakeServer;
|
|
|
|
auto account = FakeWebSocketServer::createAccount();
|
|
|
|
// Need to set reconnect timer interval to zero for tests
|
|
|
|
account->pushNotifications()->setReconnectTimerInterval(0);
|
2021-04-08 10:29:29 +03:00
|
|
|
const auto socket = fakeServer.authenticateAccount(account);
|
|
|
|
QVERIFY(socket);
|
2021-01-26 17:57:46 +03:00
|
|
|
|
|
|
|
QSignalSpy connectionLostSpy(account->pushNotifications(), &OCC::PushNotifications::connectionLost);
|
|
|
|
QVERIFY(connectionLostSpy.isValid());
|
|
|
|
|
|
|
|
QSignalSpy pushNotificationsDisabledSpy(account.data(), &OCC::Account::pushNotificationsDisabled);
|
|
|
|
QVERIFY(pushNotificationsDisabledSpy.isValid());
|
|
|
|
|
|
|
|
// Wait for authentication and then sent a network error
|
|
|
|
socket->abort();
|
|
|
|
|
|
|
|
QVERIFY(pushNotificationsDisabledSpy.wait());
|
|
|
|
QCOMPARE(pushNotificationsDisabledSpy.count(), 1);
|
|
|
|
|
|
|
|
QCOMPARE(connectionLostSpy.count(), 1);
|
|
|
|
|
|
|
|
auto accountSent = pushNotificationsDisabledSpy.at(0).at(0).value<OCC::Account *>();
|
|
|
|
QCOMPARE(accountSent, account.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
void testAccount_web_socket_authenticationFailed_emitNotificationsDisabled()
|
|
|
|
{
|
|
|
|
FakeWebSocketServer fakeServer;
|
|
|
|
auto account = FakeWebSocketServer::createAccount();
|
|
|
|
account->pushNotifications()->setReconnectTimerInterval(0);
|
|
|
|
QSignalSpy authenticationFailedSpy(account->pushNotifications(), &OCC::PushNotifications::authenticationFailed);
|
|
|
|
QVERIFY(authenticationFailedSpy.isValid());
|
|
|
|
QSignalSpy pushNotificationsDisabledSpy(account.data(), &OCC::Account::pushNotificationsDisabled);
|
|
|
|
QVERIFY(pushNotificationsDisabledSpy.isValid());
|
|
|
|
|
|
|
|
// Let three authentication attempts fail
|
2021-04-08 10:29:29 +03:00
|
|
|
for (uint8_t i = 0; i < 3; ++i) {
|
|
|
|
QVERIFY(fakeServer.waitForTextMessages());
|
|
|
|
QCOMPARE(fakeServer.textMessagesCount(), 2);
|
|
|
|
auto socket = fakeServer.socketForTextMessage(0);
|
|
|
|
fakeServer.clearTextMessages();
|
|
|
|
socket->sendTextMessage("err: Invalid credentials");
|
|
|
|
}
|
2021-01-26 17:57:46 +03:00
|
|
|
|
|
|
|
// Now the authenticationFailed and pushNotificationsDisabled Signals should be emitted
|
|
|
|
QVERIFY(pushNotificationsDisabledSpy.wait());
|
|
|
|
QCOMPARE(pushNotificationsDisabledSpy.count(), 1);
|
|
|
|
QCOMPARE(authenticationFailedSpy.count(), 1);
|
|
|
|
auto accountSent = pushNotificationsDisabledSpy.at(0).at(0).value<OCC::Account *>();
|
|
|
|
QCOMPARE(accountSent, account.data());
|
|
|
|
}
|
2021-01-12 11:23:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(TestPushNotifications)
|
|
|
|
#include "testpushnotifications.moc"
|