File indexing completed on 2024-11-24 04:43:51

0001 /*
0002     SPDX-FileCopyrightText: 2015-2017 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QTest>
0008 
0009 #include "fakehttppost.h"
0010 
0011 #include "ewsgetitemrequest.h"
0012 
0013 class UtEwsGetItemRequest : public QObject
0014 {
0015     Q_OBJECT
0016 private Q_SLOTS:
0017     void twoFailures();
0018 
0019 private:
0020     void verifier(FakeTransferJob *job, const QByteArray &req, const QByteArray &expReq, const QByteArray &resp);
0021 
0022     EwsClient mClient;
0023 };
0024 
0025 void UtEwsGetItemRequest::twoFailures()
0026 {
0027     static const QByteArray request =
0028         "<?xml version=\"1.0\"?>"
0029         "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
0030         "xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\" "
0031         "xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"
0032         "<soap:Header>"
0033         "<t:RequestServerVersion Version=\"Exchange2007_SP1\"/></soap:Header>"
0034         "<soap:Body>"
0035         "<m:GetItem>"
0036         "<m:ItemShape><t:BaseShape>IdOnly</t:BaseShape></m:ItemShape>"
0037         "<m:ItemIds>"
0038         "<t:ItemId Id=\"DdBTBAvLHI8OyQ3K\" ChangeKey=\"6yDDqXl+\"/>"
0039         "<t:ItemId Id=\"CgIdfZGT3QJrWZHi\" ChangeKey=\"wPjRsOpg\"/>"
0040         "<t:ItemId Id=\"Enxw15n4imIERH4w\" ChangeKey=\"82pEQQIj\"/>"
0041         "<t:ItemId Id=\"yV1OhxPOinZ7mxpK\" ChangeKey=\"B22tdkME\"/>"
0042         "<t:ItemId Id=\"j1ptydBqXKJLuCiB\" ChangeKey=\"z0u+e6/Z\"/>"
0043         "<t:ItemId Id=\"ogM0ejAHml/og1tZ\" ChangeKey=\"f2t/ou/g\"/>"
0044         "<t:ItemId Id=\"ZqDVkG1gIrUkJDGB\" ChangeKey=\"0LOh2uE+\"/>"
0045         "<t:ItemId Id=\"SFYgXaYm1DK+0TCs\" ChangeKey=\"Zbkp+aB4\"/>"
0046         "<t:ItemId Id=\"UrNr/v4HynI062u/\" ChangeKey=\"WMjq6rUe\"/>"
0047         "</m:ItemIds>"
0048         "</m:GetItem>"
0049         "</soap:Body>"
0050         "</soap:Envelope>\n";
0051     static const QByteArray response =
0052         "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
0053         "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"
0054         "<s:Header>"
0055         "<h:ServerVersionInfo MajorVersion=\"14\" MinorVersion=\"3\" MajorBuildNumber=\"266\" MinorBuildNumber=\"1\" Version=\"Exchange2010_SP2\" "
0056         "xmlns:h=\"http://schemas.microsoft.com/exchange/services/2006/types\" xmlns=\"http://schemas.microsoft.com/exchange/services/2006/types\" "
0057         "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"/>"
0058         "</s:Header>"
0059         "<s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
0060         "<m:GetItemResponse xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\" "
0061         "xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"
0062         "<m:ResponseMessages>"
0063         "<m:GetItemResponseMessage ResponseClass=\"Success\"><m:ResponseCode>NoError</m:ResponseCode><m:Items><t:Message><t:ItemId Id=\"DdBTBAvLHI8OyQ3K\" "
0064         "ChangeKey=\"6yDDqXl+\"/></t:Message></m:Items></m:GetItemResponseMessage>"
0065         "<m:GetItemResponseMessage ResponseClass=\"Success\"><m:ResponseCode>NoError</m:ResponseCode><m:Items><t:Message><t:ItemId Id=\"CgIdfZGT3QJrWZHi\" "
0066         "ChangeKey=\"wPjRsOpg\"/></t:Message></m:Items></m:GetItemResponseMessage>"
0067         "<m:GetItemResponseMessage ResponseClass=\"Success\"><m:ResponseCode>NoError</m:ResponseCode><m:Items><t:Message><t:ItemId Id=\"Enxw15n4imIERH4w\" "
0068         "ChangeKey=\"82pEQQIj\"/></t:Message></m:Items></m:GetItemResponseMessage>"
0069         "<m:GetItemResponseMessage ResponseClass=\"Success\"><m:ResponseCode>NoError</m:ResponseCode><m:Items><t:Message><t:ItemId Id=\"yV1OhxPOinZ7mxpK\" "
0070         "ChangeKey=\"B22tdkME\"/></t:Message></m:Items></m:GetItemResponseMessage>"
0071         "<m:GetItemResponseMessage ResponseClass=\"Success\"><m:ResponseCode>NoError</m:ResponseCode><m:Items><t:Message><t:ItemId Id=\"j1ptydBqXKJLuCiB\" "
0072         "ChangeKey=\"z0u+e6/Z\"/></t:Message></m:Items></m:GetItemResponseMessage>"
0073         "<m:GetItemResponseMessage ResponseClass=\"Success\"><m:ResponseCode>NoError</m:ResponseCode><m:Items><t:Message><t:ItemId Id=\"ogM0ejAHml/og1tZ\" "
0074         "ChangeKey=\"f2t/ou/g\"/></t:Message></m:Items></m:GetItemResponseMessage>"
0075         "<m:GetItemResponseMessage ResponseClass=\"Success\"><m:ResponseCode>NoError</m:ResponseCode><m:Items><t:Message><t:ItemId Id=\"ZqDVkG1gIrUkJDGB\" "
0076         "ChangeKey=\"0LOh2uE+\"/></t:Message></m:Items></m:GetItemResponseMessage>"
0077         "<m:GetItemResponseMessage ResponseClass=\"Error\"><m:MessageText>The specified object was not found in the "
0078         "store.</m:MessageText><m:ResponseCode>ErrorItemNotFound</m:ResponseCode><m:DescriptiveLinkKey>0</m:DescriptiveLinkKey><m:Items/></"
0079         "m:GetItemResponseMessage>"
0080         "<m:GetItemResponseMessage ResponseClass=\"Error\"><m:MessageText>The specified object was not found in the "
0081         "store.</m:MessageText><m:ResponseCode>ErrorItemNotFound</m:ResponseCode><m:DescriptiveLinkKey>0</m:DescriptiveLinkKey><m:Items/></"
0082         "m:GetItemResponseMessage>"
0083         "</m:ResponseMessages></m:GetItemResponse></s:Body></s:Envelope>";
0084 
0085     FakeTransferJob::addVerifier(this, [this](FakeTransferJob *job, const QByteArray &req) {
0086         verifier(job, req, request, response);
0087     });
0088     QScopedPointer<EwsGetItemRequest> req(new EwsGetItemRequest(mClient, this));
0089     EwsId::List ids;
0090     ids << EwsId(QStringLiteral("DdBTBAvLHI8OyQ3K"), QStringLiteral("6yDDqXl+"));
0091     ids << EwsId(QStringLiteral("CgIdfZGT3QJrWZHi"), QStringLiteral("wPjRsOpg"));
0092     ids << EwsId(QStringLiteral("Enxw15n4imIERH4w"), QStringLiteral("82pEQQIj"));
0093     ids << EwsId(QStringLiteral("yV1OhxPOinZ7mxpK"), QStringLiteral("B22tdkME"));
0094     ids << EwsId(QStringLiteral("j1ptydBqXKJLuCiB"), QStringLiteral("z0u+e6/Z"));
0095     ids << EwsId(QStringLiteral("ogM0ejAHml/og1tZ"), QStringLiteral("f2t/ou/g"));
0096     ids << EwsId(QStringLiteral("ZqDVkG1gIrUkJDGB"), QStringLiteral("0LOh2uE+"));
0097     ids << EwsId(QStringLiteral("SFYgXaYm1DK+0TCs"), QStringLiteral("Zbkp+aB4"));
0098     ids << EwsId(QStringLiteral("UrNr/v4HynI062u/"), QStringLiteral("WMjq6rUe"));
0099     req->setItemIds(ids);
0100 
0101     req->setItemShape(EwsItemShape(EwsShapeIdOnly));
0102 
0103     req->exec();
0104 
0105     QCOMPARE(req->error(), 0);
0106     QCOMPARE(req->responses().size(), ids.size());
0107 
0108     static const QList<EwsResponseClass> respClasses = {EwsResponseSuccess,
0109                                                         EwsResponseSuccess,
0110                                                         EwsResponseSuccess,
0111                                                         EwsResponseSuccess,
0112                                                         EwsResponseSuccess,
0113                                                         EwsResponseSuccess,
0114                                                         EwsResponseSuccess,
0115                                                         EwsResponseError,
0116                                                         EwsResponseError};
0117     QList<EwsResponseClass>::const_iterator respClassesIt = respClasses.begin();
0118     EwsId::List::const_iterator idsIt = ids.cbegin();
0119     unsigned i = 0;
0120     const auto responses{req->responses()};
0121     for (const EwsGetItemRequest::Response &resp : responses) {
0122         qDebug() << "Verifying response" << i++;
0123         QCOMPARE(resp.responseClass(), *respClassesIt);
0124         if (resp.isSuccess()) {
0125             auto id = resp.item()[EwsItemFieldItemId].value<EwsId>();
0126             QCOMPARE(id, *idsIt);
0127         }
0128         ++idsIt;
0129         ++respClassesIt;
0130     }
0131 }
0132 
0133 void UtEwsGetItemRequest::verifier(FakeTransferJob *job, const QByteArray &req, const QByteArray &expReq, const QByteArray &response)
0134 {
0135     bool fail = true;
0136     auto f = finally([&fail, &job] {
0137         if (fail) {
0138             job->postResponse("");
0139         }
0140     });
0141     QCOMPARE(req, expReq);
0142     fail = false;
0143     job->postResponse(response);
0144 }
0145 
0146 QTEST_MAIN(UtEwsGetItemRequest)
0147 
0148 #include "ewsgetitemrequest_ut.moc"