endtoendtestutils.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) by Claudio Cambra <claudio.cambra@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 "endtoendtestutils.h"
  15. #include <QNetworkProxy>
  16. #include <QJsonObject>
  17. #include <QSignalSpy>
  18. #include "cmd/simplesslerrorhandler.h"
  19. #include "creds/httpcredentials.h"
  20. #include "gui/accountmanager.h"
  21. #include "libsync/theme.h"
  22. #include "accessmanager.h"
  23. #include "httplogger.h"
  24. #include "syncenginetestutils.h"
  25. #include "testhelper.h"
  26. constexpr auto serverUrl = "https://server";
  27. Q_LOGGING_CATEGORY(lcEndToEndTestUtils, "nextcloud.gui.endtoendtestutils", QtInfoMsg)
  28. /** End to end test credentials access manager class **/
  29. class EndToEndTestCredentialsAccessManager : public OCC::AccessManager
  30. {
  31. public:
  32. EndToEndTestCredentialsAccessManager(const EndToEndTestCredentials *cred, QObject *parent = nullptr)
  33. : OCC::AccessManager(parent)
  34. , _cred(cred)
  35. {
  36. }
  37. protected:
  38. QNetworkReply *createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData) override
  39. {
  40. if(!_cred) {
  41. qCWarning(lcEndToEndTestUtils) << "Could not create request -- null creds!";
  42. return {};
  43. }
  44. QNetworkRequest req(request);
  45. QByteArray credHash = QByteArray(_cred->user().toUtf8() + ":" + _cred->password().toUtf8()).toBase64();
  46. req.setRawHeader("Authorization", "Basic " + credHash);
  47. return OCC::AccessManager::createRequest(op, req, outgoingData);
  48. }
  49. private:
  50. // The credentials object dies along with the account, while the QNAM might
  51. // outlive both.
  52. QPointer<const EndToEndTestCredentials> _cred;
  53. };
  54. /** End to end test credentials class **/
  55. QNetworkAccessManager *EndToEndTestCredentials::createQNAM() const
  56. {
  57. return new EndToEndTestCredentialsAccessManager(this);
  58. }
  59. /** End to end test helper class **/
  60. EndToEndTestHelper::~EndToEndTestHelper()
  61. {
  62. removeConfiguredSyncFolder();
  63. removeConfiguredAccount();
  64. OCC::AccountManager::instance()->shutdown();
  65. }
  66. void EndToEndTestHelper::startAccountConfig()
  67. {
  68. const auto accountManager = OCC::AccountManager::instance();
  69. _account = accountManager->createAccount();
  70. _account->setCredentials(new EndToEndTestCredentials);
  71. _account->setUrl(OCC::Theme::instance()->overrideServerUrl());
  72. const auto serverUrlString = QString(serverUrl);
  73. _account->setUrl(serverUrlString);
  74. _account->networkAccessManager()->setProxy(QNetworkProxy(QNetworkProxy::NoProxy));
  75. _account->setSslConfiguration(QSslConfiguration::defaultConfiguration());
  76. _account->setSslErrorHandler(new OCC::SimpleSslErrorHandler);
  77. _account->setTrustCertificates(true);
  78. slotConnectToNCUrl(serverUrlString);
  79. }
  80. void EndToEndTestHelper::slotConnectToNCUrl(const QString &url)
  81. {
  82. qCDebug(lcEndToEndTestUtils) << "Connect to url: " << url;
  83. const auto fetchUserNameJob = new OCC::JsonApiJob(_account->sharedFromThis(), QStringLiteral("/ocs/v1.php/cloud/user"));
  84. connect(fetchUserNameJob, &OCC::JsonApiJob::jsonReceived, this, [this, url](const QJsonDocument &json, const int statusCode) {
  85. if (statusCode != 100) {
  86. qCDebug(lcEndToEndTestUtils) << "Could not fetch username.";
  87. }
  88. const auto objData = json.object().value("ocs").toObject().value("data").toObject();
  89. const auto userId = objData.value("id").toString("");
  90. const auto displayName = objData.value("display-name").toString("");
  91. _account->setDavUser(userId);
  92. _account->setDavDisplayName(displayName);
  93. _accountState = new OCC::AccountState(_account);
  94. emit accountReady(_account);
  95. });
  96. fetchUserNameJob->start();
  97. }
  98. void EndToEndTestHelper::removeConfiguredAccount()
  99. {
  100. OCC::AccountManager::instance()->deleteAccount(_accountState.data());
  101. }
  102. OCC::Folder *EndToEndTestHelper::configureSyncFolder(const QString &targetPath)
  103. {
  104. if(_syncFolder) {
  105. removeConfiguredSyncFolder();
  106. }
  107. qCDebug(lcEndToEndTestUtils) << "Creating temp end-to-end test folder.";
  108. Q_ASSERT(_tempDir.isValid());
  109. OCC::FileSystem::setFolderMinimumPermissions(_tempDir.path());
  110. qCDebug(lcEndToEndTestUtils) << "Created temp end-to-end test folder at:" << _tempDir.path();
  111. setupFolderMan();
  112. OCC::FolderDefinition definition;
  113. definition.localPath = _tempDir.path();
  114. definition.targetPath = targetPath;
  115. _syncFolder = _folderMan->addFolder(_accountState.data(), definition);
  116. return _syncFolder;
  117. }
  118. void EndToEndTestHelper::removeConfiguredSyncFolder()
  119. {
  120. if(!_syncFolder || !_folderMan) {
  121. return;
  122. }
  123. QSignalSpy folderSyncFinished(_syncFolder, &OCC::Folder::syncFinished);
  124. _folderMan->forceSyncForFolder(_syncFolder);
  125. Q_ASSERT(folderSyncFinished.wait(3000));
  126. _folderMan->unloadAndDeleteAllFolders();
  127. _syncFolder = nullptr;
  128. }
  129. void EndToEndTestHelper::setupFolderMan()
  130. {
  131. if(_folderMan) {
  132. return;
  133. }
  134. auto folderMan = new OCC::FolderMan;
  135. Q_ASSERT(folderMan);
  136. folderMan->setSyncEnabled(true);
  137. _folderMan.reset(folderMan);
  138. }