Changeset View
Changeset View
Standalone View
Standalone View
src/bugzillaintegration/libbugzilla/autotests/attachmenttest.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Harald Sitter <sitter@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include <QTest> | ||||
22 | | ||||
23 | #include <QDebug> | ||||
24 | | ||||
25 | #include "../clients/attachmentclient.h" | ||||
26 | | ||||
27 | namespace Bugzilla | ||||
28 | { | ||||
29 | | ||||
30 | class JobDouble : public APIJob | ||||
31 | { | ||||
32 | Q_OBJECT | ||||
33 | public: | ||||
34 | using APIJob::APIJob; | ||||
35 | | ||||
36 | JobDouble(QString fixture) | ||||
37 | : m_fixture(fixture) | ||||
38 | { | ||||
39 | } | ||||
40 | | ||||
41 | virtual QByteArray data() const override | ||||
42 | { | ||||
43 | Q_ASSERT(!m_fixture.isEmpty()); | ||||
44 | QFile file(m_fixture); | ||||
45 | Q_ASSERT(file.open(QFile::ReadOnly | QFile::Text)); | ||||
46 | QTextStream in(&file); | ||||
47 | return in.readAll().toUtf8(); | ||||
48 | } | ||||
49 | | ||||
50 | QString m_fixture; | ||||
51 | }; | ||||
52 | | ||||
53 | class ConnectionDouble : public Connection | ||||
54 | { | ||||
55 | public: | ||||
56 | using Connection::Connection; | ||||
57 | | ||||
58 | virtual void setToken(const QString &) override | ||||
59 | { | ||||
60 | Q_UNREACHABLE(); | ||||
61 | } | ||||
62 | | ||||
63 | virtual APIJob *get(const QString &path, | ||||
64 | const QUrlQuery &query = QUrlQuery()) const override | ||||
65 | { | ||||
66 | Q_ASSERT_X(false, "get", | ||||
67 | qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString()))); | ||||
68 | return nullptr; | ||||
69 | } | ||||
70 | | ||||
71 | virtual APIJob *post(const QString &path, | ||||
72 | const QByteArray &data, | ||||
73 | const QUrlQuery &query = QUrlQuery()) const override | ||||
74 | { | ||||
75 | if (path == "/bug/1/attachment") { | ||||
76 | QJsonParseError error; | ||||
77 | QJsonDocument::fromJson(data, &error); | ||||
78 | Q_ASSERT(error.error == QJsonParseError::NoError); | ||||
79 | return new JobDouble { QFINDTESTDATA("data/attachment.new.json") }; | ||||
80 | } | ||||
81 | Q_ASSERT_X(false, "post", | ||||
82 | qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString()))); | ||||
83 | return nullptr; | ||||
84 | } | ||||
85 | | ||||
86 | virtual APIJob *put(const QString &path, | ||||
87 | const QByteArray &, | ||||
88 | const QUrlQuery &query = QUrlQuery()) const override | ||||
89 | { | ||||
90 | Q_ASSERT_X(false, "put", | ||||
91 | qUtf8Printable(QStringLiteral("unmapped: %1; %2").arg(path, query.toString()))); | ||||
92 | } | ||||
93 | }; | ||||
94 | | ||||
95 | class AttachmentTest : public QObject | ||||
96 | { | ||||
97 | Q_OBJECT | ||||
98 | private Q_SLOTS: | ||||
99 | | ||||
100 | void initTestCase() | ||||
101 | { | ||||
102 | Bugzilla::setConnection(m_doubleConnection); | ||||
103 | } | ||||
104 | | ||||
105 | void testCreate() | ||||
106 | { | ||||
107 | Bugzilla::AttachmentClient c; | ||||
108 | NewAttachment attachment; | ||||
109 | attachment.ids = { 1 }; | ||||
110 | attachment.data = "123"; | ||||
111 | attachment.file_name = "filename123"; | ||||
112 | attachment.summary = "summary123"; | ||||
113 | // attachment.content_type = "123"; | ||||
114 | attachment.comment = "comment123"; | ||||
115 | attachment.is_patch = true; | ||||
116 | attachment.is_private = false; | ||||
117 | auto job = c.createAttachment(1, attachment); | ||||
118 | job->start(); | ||||
119 | QList<int> ids = c.createAttachment(job); | ||||
120 | QCOMPARE(ids, QList<int>({1234})); | ||||
121 | } | ||||
122 | | ||||
123 | private: | ||||
124 | Bugzilla::ConnectionDouble *m_doubleConnection = new Bugzilla::ConnectionDouble; | ||||
125 | }; | ||||
126 | | ||||
127 | } // namespace Bugzilla | ||||
128 | | ||||
129 | QTEST_MAIN(Bugzilla::AttachmentTest) | ||||
130 | | ||||
131 | #include "attachmenttest.moc" |