aboutsummaryrefslogtreecommitdiff
path: root/src/app_test.cpp
diff options
context:
space:
mode:
authorMetroWind <chris.corsair@gmail.com>2025-09-07 09:42:33 -0700
committerMetroWind <chris.corsair@gmail.com>2025-09-07 09:42:33 -0700
commitea0d3220db995018335c48eb06b9794235ff436b (patch)
tree892564cdd4946c6ee9c1051bc31ff5c7bba6ddf1 /src/app_test.cpp
Initial commit, mostly just copied from shrt.
Diffstat (limited to 'src/app_test.cpp')
-rw-r--r--src/app_test.cpp205
1 files changed, 205 insertions, 0 deletions
diff --git a/src/app_test.cpp b/src/app_test.cpp
new file mode 100644
index 0000000..a022611
--- /dev/null
+++ b/src/app_test.cpp
@@ -0,0 +1,205 @@
1#include <httplib.h>
2#include <memory>
3#include <iostream>
4
5#include <gmock/gmock.h>
6#include <gtest/gtest.h>
7#include <mw/test_utils.hpp>
8#include <mw/http_client.hpp>
9#include <mw/auth_mock.hpp>
10
11#include "app.hpp"
12#include "config.hpp"
13#include "data.hpp"
14#include "data_mock.hpp"
15
16using ::testing::_;
17using ::testing::Return;
18using ::testing::HasSubstr;
19using ::testing::FieldsAre;
20using ::testing::ContainsRegex;
21
22void PrintTo(const ShortLink& link, std::ostream* os)
23{
24 *os << "ShortLink(id: " << link.id
25 << ", shortcut: " << link.shortcut
26 << ", original_url: " << link.original_url
27 << ", type: " << link.type
28 << ", user_id: " << link.user_id
29 << ", user_name: " << link.user_name
30 << ", visits: " << link.visits
31 << ", time_creation: " << link.time_creation << ")";
32}
33
34class UserAppTest : public testing::Test
35{
36protected:
37 UserAppTest()
38 {
39 config.base_url = "http://localhost:8080/";
40 config.listen_address = "localhost";
41 config.listen_port = 8080;
42 config.data_dir = ".";
43
44 auto auth = std::make_unique<mw::AuthMock>();
45
46 mw::UserInfo expected_user;
47 expected_user.name = "mw";
48 expected_user.id = "mw";
49 mw::Tokens token;
50 token.access_token = "aaa";
51 EXPECT_CALL(*auth, getUser(std::move(token)))
52 .Times(::testing::AtLeast(0))
53 .WillRepeatedly(Return(expected_user));
54 auto data = std::make_unique<DataSourceMock>();
55 data_source = data.get();
56
57 app = std::make_unique<App>(config, std::move(data), std::move(auth));
58 }
59
60 Configuration config;
61 std::unique_ptr<App> app;
62 const DataSourceMock* data_source;
63};
64
65TEST_F(UserAppTest, CanDenyAccessToLinkList)
66{
67 EXPECT_TRUE(mw::isExpected(app->start()));
68 {
69 mw::HTTPSession client;
70 ASSIGN_OR_FAIL(const mw::HTTPResponse* res, client.get(
71 mw::HTTPRequest("http://localhost:8080/_/links")));
72 EXPECT_EQ(res->status, 401);
73 }
74 app->stop();
75 app->wait();
76}
77
78TEST_F(UserAppTest, CanHandleLinkList)
79{
80 std::vector<ShortLink> links;
81 ShortLink link0;
82 link0.shortcut = "link0";
83 link0.original_url = "a";
84 link0.id = 1;
85 link0.user_id = "mw";
86 link0.user_name = "mw";
87 link0.type = ShortLink::NORMAL;
88 ShortLink link1;
89 link1.shortcut = "link1";
90 link1.original_url = "b";
91 link1.id = 2;
92 link1.user_id = "mw";
93 link1.user_name = "mw";
94 link1.type = ShortLink::REGEXP;
95 links.push_back(std::move(link0));
96 links.push_back(std::move(link1));
97
98 EXPECT_CALL(*data_source, getAllLinks("mw"))
99 .WillOnce(Return(std::move(links)));
100
101 EXPECT_TRUE(mw::isExpected(app->start()));
102 {
103 mw::HTTPSession client;
104 ASSIGN_OR_FAIL(const mw::HTTPResponse* res, client.get(
105 mw::HTTPRequest("http://localhost:8080/_/links")
106 .addHeader("Cookie", "shrt-access-token=aaa")));
107 EXPECT_EQ(res->status, 200) << "Response body: " << res->payloadAsStr();
108 EXPECT_THAT(res->payloadAsStr(), ContainsRegex("<td>a</td>[[:space:]]*<td>-</td>"));
109 EXPECT_THAT(res->payloadAsStr(), ContainsRegex("<td>b</td>[[:space:]]*<td>✅</td>"));
110 }
111 app->stop();
112 app->wait();
113}
114
115TEST_F(UserAppTest, CanDenyAccessToNewLink)
116{
117 EXPECT_TRUE(mw::isExpected(app->start()));
118 {
119 mw::HTTPSession client;
120 ASSIGN_OR_FAIL(const mw::HTTPResponse* res, client.get(
121 mw::HTTPRequest("http://localhost:8080/_/new-link")));
122 EXPECT_EQ(res->status, 401);
123 }
124 app->stop();
125 app->wait();
126}
127
128TEST_F(UserAppTest, CanHandleNewLink)
129{
130 EXPECT_TRUE(mw::isExpected(app->start()));
131 {
132 mw::HTTPSession client;
133 ASSIGN_OR_FAIL(const mw::HTTPResponse* res, client.get(
134 mw::HTTPRequest("http://localhost:8080/_/new-link")
135 .addHeader("Cookie", "shrt-access-token=aaa")));
136 EXPECT_EQ(res->status, 200);
137 EXPECT_THAT(res->payloadAsStr(), HasSubstr("Create a New Link"));
138 }
139 app->stop();
140 app->wait();
141}
142
143TEST_F(UserAppTest, CanDenyAccessToCreateLink)
144{
145 EXPECT_TRUE(mw::isExpected(app->start()));
146 {
147 mw::HTTPSession client;
148 ASSIGN_OR_FAIL(const mw::HTTPResponse* res, client.post(
149 mw::HTTPRequest("http://localhost:8080/_/create-link")));
150 EXPECT_EQ(res->status, 401);
151 }
152 app->stop();
153 app->wait();
154}
155
156TEST_F(UserAppTest, CanDenyHandleCreateLink)
157{
158 EXPECT_CALL(*data_source, addLink(
159 FieldsAre(
160 _, // id
161 "abc", // shortcut
162 "http://darksair.org", // original_url
163 ShortLink::NORMAL, // type
164 "mw", // user_id
165 "", // user_name
166 _, // visits
167 _))) // time_creation
168 .WillOnce(Return(mw::E<void>()));
169
170 EXPECT_CALL(*data_source, addLink(
171 FieldsAre(
172 _, // id
173 "xyz", // shortcut
174 "http://mws.rocks", // original_url
175 ShortLink::REGEXP, // type
176 "mw", // user_id
177 "", // user_name
178 _, // visits
179 _))) // time_creation
180 .WillOnce(Return(mw::E<void>()));
181
182 EXPECT_TRUE(mw::isExpected(app->start()));
183 {
184 mw::HTTPSession client;
185 ASSIGN_OR_FAIL(const mw::HTTPResponse* res1, client.post(
186 mw::HTTPRequest("http://localhost:8080/_/create-link")
187 .setPayload("shortcut=abc&original_url=http%3A%2F%2Fdarksair%2Eorg"
188 "&regexp=off")
189 .addHeader("Cookie", "shrt-access-token=aaa")
190 .setContentType("application/x-www-form-urlencoded")));
191 EXPECT_EQ(res1->status, 302);
192 EXPECT_EQ(res1->header.at("Location"), "http://localhost:8080/");
193
194 ASSIGN_OR_FAIL(const mw::HTTPResponse* res2, client.post(
195 mw::HTTPRequest("http://localhost:8080/_/create-link")
196 .setPayload("shortcut=xyz&original_url=http%3A%2F%2Fmws%2Erocks"
197 "&regexp=on")
198 .addHeader("Cookie", "shrt-access-token=aaa")
199 .setContentType("application/x-www-form-urlencoded")));
200 EXPECT_EQ(res2->status, 302);
201 EXPECT_EQ(res2->header.at("Location"), "http://localhost:8080/");
202 }
203 app->stop();
204 app->wait();
205}