Debian Codegraph @

mtxclient/tests/media_api.cpp

raw symbols SCIP
1 #include <chrono>
2 #include <fstream>
3 #include <streambuf>
4 #include <thread>
5
6 #include <gtest/gtest.h>
7
8 #include <mtx/requests.hpp>
9 #include <mtx/responses.hpp>
10 #include <mtxclient/http/client.hpp>
11
12 #include "test_helpers.hpp"
13
14 using namespace mtx::http;
15 using namespace mtx::identifiers;
16
17 using namespace std;
18
19 string
20 read_file(const string &file_path)
21 {
22 ifstream file(file_path);
23 string data((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
24
25 return data;
26 }
27
28 void
29 validate_upload(const mtx::responses::ContentURI &res, RequestErr err)
30 {
31 if (err) {
32 if (err->status_code)
33 cout << err->status_code << "\n";
34 if (!err->matrix_error.error.empty())
35 cout << err->matrix_error.error << "\n";
36 if (err->error_code)
37 cout << err->error_code << "\n";
38 }
39
40 ASSERT_FALSE(err);
41 ASSERT_TRUE(res.content_uri.size() > 10);
42 }
43
44 TEST(MediaAPI, UploadTextFile)
45 {
46 std::shared_ptr<Client> alice = make_test_client();
47
48 alice->login("alice", "secret", [alice](const mtx::responses::Login &, RequestErr err) {
49 ASSERT_FALSE(err);
50
51 const auto text = "This is some random text";
52
53 alice->upload(text,
54 "text/plain",
55 "doc.txt",
56 [alice, text](const mtx::responses::ContentURI &res, RequestErr err) {
57 validate_upload(res, err);
58
59 alice->download(res.content_uri,
60 [text](const string &data,
61 const string &content_type,
62 const string &original_filename,
63 RequestErr err) {
64 ASSERT_FALSE(err);
65 EXPECT_EQ(data, text);
66 EXPECT_EQ(
67 content_type.substr(0, std::size("text/plain") - 1),
68 "text/plain");
69 EXPECT_EQ(original_filename, "doc.txt");
70 });
71 });
72 });
73
74 alice->close();
75 }
76
77 TEST(MediaAPI, UploadAudio)
78 {
79 std::shared_ptr<Client> bob = make_test_client();
80
81 bob->login("bob", "secret", [bob](const mtx::responses::Login &, RequestErr err) {
82 ASSERT_FALSE(err);
83
84 const auto audio = read_file(fixture_prefix() + "/fixtures/sound.mp3");
85
86 bob->upload(audio,
87 "audio/mp3",
88 "sound.mp3",
89 [bob, audio](const mtx::responses::ContentURI &res, RequestErr err) {
90 validate_upload(res, err);
91
92 bob->download(res.content_uri,
93 [audio](const string &data,
94 const string &content_type,
95 const string &original_filename,
96 RequestErr err) {
97 ASSERT_FALSE(err);
98 EXPECT_EQ(data, audio);
99 EXPECT_EQ(content_type, "audio/mp3");
100 EXPECT_EQ(original_filename, "sound.mp3");
101 });
102 });
103 });
104
105 bob->close();
106 }
107
108 TEST(MediaAPI, UploadImage)
109 {
110 std::shared_ptr<Client> carl = make_test_client();
111
112 carl->login("carl", "secret", [carl](const mtx::responses::Login &, RequestErr err) {
113 ASSERT_FALSE(err);
114
115 const auto img = read_file(fixture_prefix() + "/fixtures/test.jpeg");
116
117 carl->upload(img,
118 "image/jpeg",
119 "a name that needs to be encode/d.jpeg",
120 [carl, img](const mtx::responses::ContentURI &res, RequestErr err) {
121 validate_upload(res, err);
122 });
123
124 carl->upload(img,
125 "image/jpeg",
126 "test.jpeg",
127 [carl, img](const mtx::responses::ContentURI &res, RequestErr err) {
128 validate_upload(res, err);
129
130 ThumbOpts opts;
131 opts.mxc_url = res.content_uri;
132 carl->get_thumbnail(opts, [](const std::string &res, RequestErr err) {
133 ASSERT_FALSE(err);
134 ASSERT_FALSE(res.empty());
135 });
136
137 carl->download(res.content_uri,
138 [img](const string &data,
139 const string &content_type,
140 const string &original_filename,
141 RequestErr err) {
142 ASSERT_FALSE(err);
143 EXPECT_EQ(data, img);
144 EXPECT_EQ(content_type, "image/jpeg");
145 EXPECT_EQ(original_filename, "test.jpeg");
146 });
147 });
148 });
149
150 carl->close();
151 }
152
153 TEST(MediaAPI, UploadSVG)
154 {
155 std::shared_ptr<Client> carl = make_test_client();
156
157 carl->login("carl", "secret", [carl](const mtx::responses::Login &, RequestErr err) {
158 ASSERT_FALSE(err);
159
160 const auto img = read_file(fixture_prefix() + "/fixtures/kiwi.svg");
161
162 carl->upload(img,
163 "image/svg",
164 "kiwi.svg",
165 [carl, img](const mtx::responses::ContentURI &res, RequestErr err) {
166 validate_upload(res, err);
167
168 ThumbOpts opts;
169 opts.mxc_url = res.content_uri;
170 carl->get_thumbnail(opts, [img](const std::string &res, RequestErr err) {
171 ASSERT_FALSE(err);
172 EXPECT_EQ(res, img);
173 });
174
175 carl->download(res.content_uri,
176 [img](const string &data,
177 const string &content_type,
178 const string &original_filename,
179 RequestErr err) {
180 ASSERT_FALSE(err);
181 EXPECT_EQ(data, img);
182 EXPECT_EQ(content_type, "image/svg");
183 EXPECT_EQ(original_filename, "kiwi.svg");
184 });
185 });
186 });
187
188 carl->close();
189 }