]> git.stg.codes - stg.git/blob - tests/test_filter_params_log.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / tests / test_filter_params_log.cpp
1 #define BOOST_TEST_MODULE STGFilterParamsLog
2
3 #include "stg/admin.h"
4 #include "stg/user_property.h"
5 #include "user_impl.h"
6
7 #include "testsettings.h"
8 #include "testtariffs.h"
9 #include "teststore.h"
10 #include "testauth.h"
11 #include "testusers.h"
12 #include "testservices.h"
13
14 #pragma GCC diagnostic push
15 #pragma GCC diagnostic ignored "-Wold-style-cast"
16 #pragma GCC diagnostic ignored "-Wunused-parameter"
17 #pragma GCC diagnostic ignored "-Wsign-compare"
18 #pragma GCC diagnostic ignored "-Wparentheses"
19 #include <boost/test/unit_test.hpp>
20 #pragma GCC diagnostic pop
21
22 volatile time_t stgTime = 0;
23
24 namespace
25 {
26
27 class Store : public TestStore
28 {
29     public:
30         Store()
31             : m_entries(0)
32         {}
33
34         int WriteUserChgLog(const std::string& /*login*/,
35                             const std::string& /*admLogin*/,
36                             uint32_t /*admIP*/,
37                             const std::string& /*paramName*/,
38                             const std::string& /*oldValue*/,
39                             const std::string& /*newValue*/,
40                             const std::string& /*message*/) const override { ++m_entries; return 0; }
41
42         size_t GetEntries() const { return m_entries; }
43
44     private:
45         mutable size_t m_entries;
46 };
47
48 class Settings : public TestSettings
49 {
50     public:
51         void addFilter(const std::string& field) { m_filter.push_back(field); }
52
53         const std::vector<std::string>& GetFilterParamsLog() const { return m_filter; }
54
55     private:
56         std::vector<std::string> m_filter;
57 };
58
59 }
60
61 BOOST_AUTO_TEST_SUITE(FilterParamsLog)
62
63 BOOST_AUTO_TEST_CASE(NormalBehavior)
64 {
65     Settings settings;
66     settings.addFilter("*"); // Allow everything by default.
67     TestTariffs tariffs;
68     tariffs.ReadTariffs();
69     STG::Admin admin(STG::Priv(0xFFFF), {}, {});
70     Store store;
71     TestAuth auth;
72     TestUsers users;
73     TestServices services;
74     STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
75
76     auto & address = user.GetProperties().address;
77     auto & note = user.GetProperties().note;
78     auto & group = user.GetProperties().group;
79
80     address.Set("address", admin, "", store, "");
81     note.Set("note", admin, "", store, "");
82     group.Set("group", admin, "", store, "");
83
84     BOOST_CHECK_EQUAL(store.GetEntries(), 3);
85
86     note.Set("another note", admin, "", store, "");
87
88     BOOST_CHECK_EQUAL(store.GetEntries(), 4);
89
90     address.Set("new address", admin, "", store, "");
91
92     BOOST_CHECK_EQUAL(store.GetEntries(), 5);
93
94     group.Set("administrative group", admin, "", store, "");
95
96     BOOST_CHECK_EQUAL(store.GetEntries(), 6);
97 }
98
99 BOOST_AUTO_TEST_CASE(SingleFilterEntry)
100 {
101     Settings settings;
102     settings.addFilter("address"); // Allow everything by default.
103     TestTariffs tariffs;
104     STG::Admin admin(STG::Priv(0xFFFF), {}, {});
105     Store store;
106     TestAuth auth;
107     TestUsers users;
108     TestServices services;
109     STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
110
111     auto & address = user.GetProperties().address;
112     auto & note = user.GetProperties().note;
113     auto & group = user.GetProperties().group;
114
115     address.Set("address", admin, "", store, "");
116     note.Set("note", admin, "", store, "");
117     group.Set("group", admin, "", store, "");
118
119     BOOST_CHECK_EQUAL(store.GetEntries(), 1);
120
121     note.Set("another note", admin, "", store, "");
122
123     BOOST_CHECK_EQUAL(store.GetEntries(), 1);
124
125     address.Set("new address", admin, "", store, "");
126
127     BOOST_CHECK_EQUAL(store.GetEntries(), 2);
128
129     group.Set("administrative group", admin, "", store, "");
130
131     BOOST_CHECK_EQUAL(store.GetEntries(), 2);
132 }
133
134 BOOST_AUTO_TEST_CASE(MultipleFilterEntries)
135 {
136     Settings settings;
137     settings.addFilter("address"); // Allow everything by default.
138     settings.addFilter("group"); // Allow everything by default.
139     TestTariffs tariffs;
140     STG::Admin admin(STG::Priv(0xFFFF), {}, {});
141     Store store;
142     TestAuth auth;
143     TestUsers users;
144     TestServices services;
145     STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
146
147     auto & address = user.GetProperties().address;
148     auto & note = user.GetProperties().note;
149     auto & group = user.GetProperties().group;
150
151     address.Set("address", admin, "", store, "");
152     note.Set("note", admin, "", store, "");
153     group.Set("group", admin, "", store, "");
154
155     BOOST_CHECK_EQUAL(store.GetEntries(), 2);
156
157     note.Set("another note", admin, "", store, "");
158
159     BOOST_CHECK_EQUAL(store.GetEntries(), 2);
160
161     address.Set("new address", admin, "", store, "");
162
163     BOOST_CHECK_EQUAL(store.GetEntries(), 3);
164
165     group.Set("administrative group", admin, "", store, "");
166
167     BOOST_CHECK_EQUAL(store.GetEntries(), 4);
168 }
169
170 BOOST_AUTO_TEST_CASE(EmptyFilter)
171 {
172     Settings settings;
173     TestTariffs tariffs;
174     STG::Admin admin(STG::Priv(0xFFFF), {}, {});
175     Store store;
176     TestAuth auth;
177     TestUsers users;
178     TestServices services;
179     STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
180
181     auto & address = user.GetProperties().address;
182     auto & note = user.GetProperties().note;
183     auto & group = user.GetProperties().group;
184
185     address.Set("address", admin, "", store, "");
186     note.Set("note", admin, "", store, "");
187     group.Set("group", admin, "", store, "");
188
189     BOOST_CHECK_EQUAL(store.GetEntries(), 0);
190
191     note.Set("another note", admin, "", store, "");
192
193     BOOST_CHECK_EQUAL(store.GetEntries(), 0);
194
195     address.Set("new address", admin, "", store, "");
196
197     BOOST_CHECK_EQUAL(store.GetEntries(), 0);
198
199     group.Set("administrative group", admin, "", store, "");
200
201     BOOST_CHECK_EQUAL(store.GetEntries(), 0);
202 }
203
204 BOOST_AUTO_TEST_SUITE_END()