aboutsummaryrefslogtreecommitdiff
path: root/src/app_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/app_test.cpp')
-rw-r--r--src/app_test.cpp217
1 files changed, 38 insertions, 179 deletions
diff --git a/src/app_test.cpp b/src/app_test.cpp
index a022611..06f2084 100644
--- a/src/app_test.cpp
+++ b/src/app_test.cpp
@@ -19,187 +19,46 @@ using ::testing::HasSubstr;
19using ::testing::FieldsAre; 19using ::testing::FieldsAre;
20using ::testing::ContainsRegex; 20using ::testing::ContainsRegex;
21 21
22void PrintTo(const ShortLink& link, std::ostream* os) 22// class UserAppTest : public testing::Test
23{ 23// {
24 *os << "ShortLink(id: " << link.id 24// protected:
25 << ", shortcut: " << link.shortcut 25// UserAppTest()
26 << ", original_url: " << link.original_url 26// {
27 << ", type: " << link.type 27// config.base_url = "http://localhost:8080/";
28 << ", user_id: " << link.user_id 28// config.listen_address = "localhost";
29 << ", user_name: " << link.user_name 29// config.listen_port = 8080;
30 << ", visits: " << link.visits 30// config.data_dir = ".";
31 << ", time_creation: " << link.time_creation << ")";
32}
33 31
34class UserAppTest : public testing::Test 32// auto auth = std::make_unique<mw::AuthMock>();
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 33
44 auto auth = std::make_unique<mw::AuthMock>(); 34// mw::UserInfo expected_user;
35// expected_user.name = "mw";
36// expected_user.id = "mw";
37// mw::Tokens token;
38// token.access_token = "aaa";
39// EXPECT_CALL(*auth, getUser(std::move(token)))
40// .Times(::testing::AtLeast(0))
41// .WillRepeatedly(Return(expected_user));
42// auto data = std::make_unique<DataSourceMock>();
43// data_source = data.get();
45 44
46 mw::UserInfo expected_user; 45// app = std::make_unique<App>(config, std::move(data), std::move(auth));
47 expected_user.name = "mw"; 46// }
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 47
57 app = std::make_unique<App>(config, std::move(data), std::move(auth)); 48// Configuration config;
58 } 49// std::unique_ptr<App> app;
50// const DataSourceMock* data_source;
51// };
59 52
60 Configuration config; 53// TEST_F(UserAppTest, CanDenyAccessToLinkList)
61 std::unique_ptr<App> app; 54// {
62 const DataSourceMock* data_source; 55// EXPECT_TRUE(mw::isExpected(app->start()));
63}; 56// {
64 57// mw::HTTPSession client;
65TEST_F(UserAppTest, CanDenyAccessToLinkList) 58// ASSIGN_OR_FAIL(const mw::HTTPResponse* res, client.get(
66{ 59// mw::HTTPRequest("http://localhost:8080/_/links")));
67 EXPECT_TRUE(mw::isExpected(app->start())); 60// EXPECT_EQ(res->status, 401);
68 { 61// }
69 mw::HTTPSession client; 62// app->stop();
70 ASSIGN_OR_FAIL(const mw::HTTPResponse* res, client.get( 63// app->wait();
71 mw::HTTPRequest("http://localhost:8080/_/links"))); 64// }
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}