File indexing completed on 2024-05-12 04:46:02

0001 #ifndef TEST_TESTLIBDAVCLIENT
0002 #define TEST_TESTLIBDAVCLIENT
0003 
0004 #include <QCoreApplication>
0005 #include <QList>
0006 #include <QObject>
0007 #include <QTest>
0008 
0009 #include <../CardDAV.hpp>
0010 #include <../WebDAV.hpp>
0011 #include <../dto/WebDAVItem.hpp>
0012 #include <../utils/Environment.hpp>
0013 #include <../utils/WebDAVReply.hpp>
0014 
0015 class TestLibDAVClient : public QObject {
0016   Q_OBJECT
0017 
0018  private:
0019   QCoreApplication *app;
0020   WebDAV *client;
0021   CardDAV *cardDAVClient;
0022   QString uid;
0023 
0024   void listDirOutputHandler(WebDAVReply *reply) {
0025     connect(reply, &WebDAVReply::listDirResponse,
0026             [=](QNetworkReply *listDirReply, QList<WebDAVItem> items) {
0027               qDebug() << "URL :" << listDirReply->url();
0028               qDebug() << "Received List of" << items.length() << "items";
0029               qDebug() << endl << "---------------------------------------";
0030 
0031               for (WebDAVItem item : items) {
0032                 qDebug().noquote() << endl << item.toString();
0033                 qDebug() << endl << "---------------------------------------";
0034               }
0035 
0036               QCoreApplication::exit(0);
0037             });
0038     connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err) {
0039       qDebug() << err;
0040       QCoreApplication::exit(1);
0041     });
0042   }
0043 
0044  private slots:
0045   void initTestCase() {
0046     int argc = 1;
0047     char *argv[] = {"test"};
0048 
0049     this->uid = QUuid::createUuid().toString().mid(1, 36);
0050     this->app = new QCoreApplication(argc, argv);
0051     this->client =
0052         new WebDAV(Environment::get("LIBDAVCLIENT_WEBDAV_TEST_HOST"),
0053                    Environment::get("LIBDAVCLIENT_WEBDAV_TEST_USER"),
0054                    Environment::get("LIBDAVCLIENT_WEBDAV_TEST_PASSWORD"));
0055     this->cardDAVClient =
0056         new CardDAV(Environment::get("LIBDAVCLIENT_CARDDAV_TEST_HOST"),
0057                     Environment::get("LIBDAVCLIENT_CARDDAV_TEST_USER"),
0058                     Environment::get("LIBDAVCLIENT_CARDDAV_TEST_PASSWORD"));
0059   }
0060 
0061   void testWebDAVConnection() {
0062     WebDAVReply *reply = this->client->testConnection();
0063     connect(reply, &WebDAVReply::testConnectionResponse, [=](bool isSuccess) {
0064       qDebug() << "testConnection :" << isSuccess;
0065       QCoreApplication::exit(0);
0066       Q_ASSERT(true);
0067     });
0068     connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err) {
0069       qDebug() << "ERROR :" << err;
0070       QCoreApplication::exit(1);
0071       Q_ASSERT(false);
0072     });
0073 
0074     this->app->exec();
0075   }
0076 
0077   //  void testWebDAVListDir() {
0078   //    this->listDirOutputHandler(this->client->listDir(
0079   //        Environment::get("LIBDAVCLIENT_WEBDAV_TEST_PATH")));
0080   //    this->app->exec();
0081   //  }
0082 
0083   //  void testWebDAVListDirWithDepth() {
0084   //    this->listDirOutputHandler(this->client->listDir(
0085   //        Environment::get("LIBDAVCLIENT_WEBDAV_TEST_PATH"),
0086   //        ListDepthEnum::One));
0087 
0088   //    this->app->exec();
0089   //  }
0090 
0091   //  void testWebDAVDownload() {
0092   //    QString url = "/remote.php/webdav/Nextcloud%20Manual.pdf";
0093 
0094   //    WebDAVReply *reply = this->client->downloadFrom(url);
0095 
0096   //    connect(reply, &WebDAVReply::downloadResponse, [=](QNetworkReply *reply)
0097   //    {
0098   //      if (!reply->error()) {
0099   //        qDebug() << "\nDownload Success"
0100   //                 << "\nURL  :" << reply->url() << "\nSize :" <<
0101   //                 reply->size();
0102   //      } else {
0103   //        qDebug() << "ERROR(DOWNLOAD)" << reply->error();
0104   //      }
0105   //      QCoreApplication::exit(0);
0106   //    });
0107   //    connect(reply, &WebDAVReply::downloadProgressResponse,
0108   //            [=](qint64 bytesReceived, qint64 bytesTotal) {
0109   //              int percent = ((float)bytesReceived / bytesTotal) * 100;
0110 
0111   //              qDebug() << "\nReceived : " << bytesReceived
0112   //                       << "\nTotal    : " << bytesTotal
0113   //                       << "\nPercent  : " << percent;
0114   //            });
0115   //    connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err)
0116   //    {
0117   //      qDebug() << "ERROR" << err;
0118   //      QCoreApplication::exit(1);
0119   //    });
0120 
0121   //    this->app->exec();
0122   //  }
0123 
0124   //  void testWebDAVUpload() {
0125   //    QString url = Environment::get("LIBDAVCLIENT_WEBDAV_TEST_PATH");
0126   //    QFile file("/home/anupam/libwebdav/lib/WebDAVClient.cpp");
0127   //    file.open(QIODevice::ReadOnly);
0128 
0129   //    WebDAVReply *reply = this->client->uploadTo(url, "tttt.cpp", &file);
0130 
0131   //    connect(reply, &WebDAVReply::uploadFinished, [=](QNetworkReply *reply) {
0132   //      if (!reply->error()) {
0133   //        qDebug() << "\nUpload Success"
0134   //                 << "\nURL  :" << reply->url() << "\nSize :" <<
0135   //                 reply->size();
0136   //      } else {
0137   //        qDebug() << "ERROR(UPLOAD)" << reply->error();
0138   //      }
0139   //      QCoreApplication::exit(0);
0140   //    });
0141 
0142   //    connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err)
0143   //    {
0144   //      qDebug() << "ERROR" << err;
0145   //      QCoreApplication::exit(1);
0146   //    });
0147 
0148   //    this->app->exec();
0149   //  }
0150 
0151   //  void testWebDAVCreateDir() {
0152   //    QString dirName =
0153   //    QDate::currentDate().toString(Qt::DateFormat::ISODate); WebDAVReply
0154   //    *reply = this->client->createDir(
0155   //        Environment::get("LIBDAVCLIENT_WEBDAV_TEST_PATH"), dirName);
0156 
0157   //    connect(reply, &WebDAVReply::createDirFinished, [=](QNetworkReply
0158   //    *reply) {
0159   //      if (!reply->error()) {
0160   //        qDebug() << "\nDir Created"
0161   //                 << "\nURL  :" << reply->url();
0162   //      } else {
0163   //        qDebug() << "ERROR(CREATE DIR)" << reply->error();
0164   //      }
0165   //      QCoreApplication::exit(0);
0166   //    });
0167 
0168   //    connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err)
0169   //    {
0170   //      qDebug() << "ERROR" << err;
0171   //      QCoreApplication::exit(1);
0172   //    });
0173 
0174   //    this->app->exec();
0175   //  }
0176 
0177   //  void testWebDAVCopyDir() {
0178   //    QString dirName =
0179   //    QDate::currentDate().toString(Qt::DateFormat::ISODate); WebDAVReply
0180   //    *reply = this->client->copy(
0181   //        Environment::get("LIBDAVCLIENT_WEBDAV_TEST_PATH") + "/tttt.cpp",
0182   //        Environment::get("LIBDAVCLIENT_WEBDAV_TEST_PATH") + "/" + dirName +
0183   //            "/tttt-copy.cpp");
0184 
0185   //    connect(reply, &WebDAVReply::copyFinished, [=](QNetworkReply *reply) {
0186   //      if (!reply->error()) {
0187   //        qDebug() << "\nItem Copied"
0188   //                 << "\nURL  :" << reply->url();
0189   //      } else {
0190   //        qDebug() << "ERROR(COPY)" << reply->error();
0191   //      }
0192   //      QCoreApplication::exit(0);
0193   //    });
0194 
0195   //    connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err)
0196   //    {
0197   //      qDebug() << "ERROR" << err;
0198   //      QCoreApplication::exit(1);
0199   //    });
0200 
0201   //    this->app->exec();
0202   //  }
0203 
0204   //  void testWebDAVMoveDir() {
0205   //    QString dirName =
0206   //    QDate::currentDate().toString(Qt::DateFormat::ISODate); WebDAVReply
0207   //    *reply = this->client->move(
0208   //        Environment::get("LIBDAVCLIENT_WEBDAV_TEST_PATH") + "/tttt.cpp",
0209   //        Environment::get("LIBDAVCLIENT_WEBDAV_TEST_PATH") + "/" + dirName +
0210   //            "/tttt.cpp",
0211   //        true);
0212 
0213   //    connect(reply, &WebDAVReply::moveFinished, [=](QNetworkReply *reply) {
0214   //      if (!reply->error()) {
0215   //        qDebug() << "\nItem Moved"
0216   //                 << "\nURL  :" << reply->url();
0217   //      } else {
0218   //        qDebug() << "ERROR(MOVE DIR)" << reply->error();
0219   //      }
0220   //      QCoreApplication::exit(0);
0221   //    });
0222 
0223   //    connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err)
0224   //    {
0225   //      qDebug() << "ERROR" << err;
0226   //      QCoreApplication::exit(1);
0227   //    });
0228 
0229   //    this->app->exec();
0230   //  }
0231 
0232   //  void testWebDAVRemove() {
0233   //    QString dirName =
0234   //    QDate::currentDate().toString(Qt::DateFormat::ISODate);
0235 
0236   //    WebDAVReply *reply =
0237   //        this->client->remove(Environment::get("LIBDAVCLIENT_WEBDAV_TEST_PATH")
0238   //        +
0239   //                             "/" + dirName + "/tttt.cpp");
0240 
0241   //    connect(reply, &WebDAVReply::removeFinished, [=](QNetworkReply *reply) {
0242   //      if (!reply->error()) {
0243   //        qDebug() << "\nItem Removed"
0244   //                 << "\nURL  :" << reply->url();
0245   //      } else {
0246   //        qDebug() << "ERROR(REMOVE File)" << reply->error();
0247   //      }
0248   //      QCoreApplication::exit(0);
0249   //    });
0250 
0251   //    connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err)
0252   //    {
0253   //      qDebug() << "ERROR" << err;
0254   //      QCoreApplication::exit(1);
0255   //    });
0256 
0257   //    this->app->exec();
0258   //  }
0259 
0260   //  void testWebDAVRemoveDir() {
0261   //    QString dirName =
0262   //    QDate::currentDate().toString(Qt::DateFormat::ISODate); WebDAVReply
0263   //    *reply = this->client->remove(
0264   //        Environment::get("LIBDAVCLIENT_WEBDAV_TEST_PATH") + "/" + dirName);
0265 
0266   //    connect(reply, &WebDAVReply::removeFinished, [=](QNetworkReply *reply) {
0267   //      if (!reply->error()) {
0268   //        qDebug() << "\nItem Removed"
0269   //                 << "\nURL  :" << reply->url();
0270   //      } else {
0271   //        qDebug() << "ERROR(REMOVE Dir)" << reply->error();
0272   //      }
0273   //      QCoreApplication::exit(0);
0274   //    });
0275 
0276   //    connect(reply, &WebDAVReply::error, [=](QNetworkReply::NetworkError err)
0277   //    {
0278   //      qDebug() << "ERROR" << err;
0279   //      QCoreApplication::exit(1);
0280   //    });
0281 
0282   //    this->app->exec();
0283   //  }
0284 
0285   //  ////////////////////////////////////////////////
0286 
0287   //  void testCardDAVConnection() {
0288   //    CardDAVReply *reply = this->cardDAVClient->testConnection();
0289   //    this->connect(reply, &CardDAVReply::testConnectionResponse,
0290   //                  [=](bool isSuccess) {
0291   //                    QCoreApplication::exit(0);
0292   //                    QCOMPARE(isSuccess, true);
0293   //                  });
0294   //    this->app->exec();
0295 
0296   //    CardDAV wrongCardDAVClient(
0297   //        "https://cloud.opendesktop.org/remote.php/dav/addressbooks/"
0298   //        "ab0027/",
0299   //        Environment::get("LIBDAVCLIENT_CARDDAV_TEST_USER"),
0300   //        Environment::get("LIBDAVCLIENT_CARDDAV_TEST_PASSWORD"));
0301   //    reply = wrongCardDAVClient.testConnection();
0302   //    this->connect(reply, &CardDAVReply::testConnectionResponse,
0303   //                  [=](bool isSuccess) {
0304   //                    QCoreApplication::exit(0);
0305   //                    QCOMPARE(isSuccess, false);
0306   //                  });
0307   //    this->app->exec();
0308   //  }
0309 
0310   //  void testCardDAVCreateContact() {
0311   //    QString vCard =
0312   //        "BEGIN:VCARD\nVERSION:3.0\nN:Basak;Anupam;;;Basak\nADR;INTL;PARCEL;"
0313   //        "WORK:;;;;;;India\nEMAIL;INTERNET:anupam.basak27@gmail.com\nTEL;WORK:"
0314   //        "8981861008\nEND:VCARD";
0315   //    CardDAVReply *reply = this->cardDAVClient->createContact(this->uid,
0316   //    vCard); this->connect(reply, &CardDAVReply::createContactResponse,
0317   //                  [=](Contact *contact) {
0318   //                    QCoreApplication::exit(0);
0319 
0320   //                    qDebug() << "\n\n    Contact Created."
0321   //                             << "\n    ETAG :" << contact->getEtag()
0322   //                             << "\n    Href:" << contact->getHref()
0323   //                             << "\n    vCard :" << contact->getVcard() <<
0324   //                             "\n";
0325   //                  });
0326   //    this->connect(reply, &CardDAVReply::error,
0327   //                  [=](QNetworkReply::NetworkError err) {
0328   //                    QCoreApplication::exit(0);
0329   //                    qDebug() << err;
0330   //                    QCOMPARE(err, QNetworkReply::NoError);
0331   //                  });
0332   //    this->app->exec();
0333   //  }
0334 
0335   //  void testCardDAVUpdateContact() {
0336   //    QString vCard =
0337   //        "BEGIN:VCARD\nVERSION:3.0\nN:Basak;Probal;;;Basak\nADR;INTL;PARCEL;"
0338   //        "WORK:;;;;;;India\nEMAIL;INTERNET:probal31@gmail.com\nTEL;WORK:"
0339   //        "8981861008\nEND:VCARD";
0340   //    CardDAVReply *reply = this->cardDAVClient->updateContact(
0341   //        QUrl(Environment::get("LIBDAVCLIENT_CARDDAV_TEST_HOST") + "/" +
0342   //             this->uid + ".vcf"),
0343   //        vCard, "*");
0344   //    this->connect(reply, &CardDAVReply::updateContactResponse,
0345   //                  [=](Contact *contact) {
0346   //                    QCoreApplication::exit(0);
0347 
0348   //                    qDebug() << "\n\n    Contact Updated."
0349   //                             << "\n    ETAG :" << contact->getEtag()
0350   //                             << "\n    Href:" << contact->getHref()
0351   //                             << "\n    vCard:" << contact->getVcard() <<
0352   //                             "\n";
0353   //                  });
0354   //    this->connect(reply, &CardDAVReply::error,
0355   //                  [=](QNetworkReply::NetworkError err) {
0356   //                    QCoreApplication::exit(0);
0357   //                    qDebug() << err;
0358   //                    QCOMPARE(err, QNetworkReply::NoError);
0359   //                  });
0360   //    this->app->exec();
0361   //  }
0362 
0363   //  void testCardDAVDeleteContact() {
0364   //    CardDAVReply *reply = this->cardDAVClient->deleteContact(
0365   //        QUrl(Environment::get("LIBDAVCLIENT_CARDDAV_TEST_HOST") + "/" +
0366   //             this->uid + ".vcf"));
0367   //    this->connect(reply, &CardDAVReply::deleteContactResponse, [=]() {
0368   //      QCoreApplication::exit(0);
0369 
0370   //      qDebug() << "Contact Deleted";
0371   //    });
0372   //    this->connect(reply, &CardDAVReply::error,
0373   //                  [=](QNetworkReply::NetworkError err) {
0374   //                    QCoreApplication::exit(0);
0375   //                    qDebug() << err;
0376   //                    QCOMPARE(err, QNetworkReply::NoError);
0377   //                  });
0378   //    this->app->exec();
0379   //  }
0380 
0381   //  void testCardDAVListAllContacts() {
0382   //    CardDAVReply *reply = this->cardDAVClient->listAllContacts();
0383   //    this->connect(reply, &CardDAVReply::listAllContactsResponse,
0384   //                  [=](QList<Contact *> contacts) {
0385   //                    QCoreApplication::exit(0);
0386 
0387   //                    qDebug() << "Size :" << contacts.length();
0388 
0389   //                    for (Contact *contact : contacts) {
0390   //                      qDebug() << ", ETag :" << contact->getEtag()
0391   //                               << "Contact URL :" << contact->getHref();
0392   //                      //                               << ", vCard :" <<
0393   //                      //                               contact->getVcard();
0394   //                    }
0395   //                  });
0396   //    this->connect(reply, &CardDAVReply::error,
0397   //                  [=](QNetworkReply::NetworkError err) {
0398   //                    QCoreApplication::exit(0);
0399   //                    qDebug() << err;
0400   //                    QCOMPARE(err, QNetworkReply::NoError);
0401   //                  });
0402   //    this->app->exec();
0403   //  }
0404 
0405   void cleanupTestCase() { delete this->app; }
0406 };
0407 
0408 QTEST_MAIN(TestLibDAVClient)
0409 #include "TestLibDAVClient.moc"
0410 
0411 #endif