]> git.stg.codes - stg.git/blob - tests/test_filter_params_log.cpp
ab7a30977c54deb33a02bd50b563e72f48f7788c
[stg.git] / tests / test_filter_params_log.cpp
1 #include "tut/tut.hpp"
2
3 #include "stg/user_property.h"
4 #include "user_impl.h"
5
6 #include "testsettings.h"
7 #include "testtariffs.h"
8 #include "testadmin.h"
9 #include "teststore.h"
10 #include "testauth.h"
11 #include "testusers.h"
12 #include "testservices.h"
13
14 namespace
15 {
16
17 class TEST_STORE_LOCAL : public TEST_STORE,
18                          private NONCOPYABLE {
19 public:
20     TEST_STORE_LOCAL()
21         : entries(0)
22     {}
23
24     int WriteUserChgLog(const std::string & /*login*/,
25                         const std::string & /*admLogin*/,
26                         uint32_t /*admIP*/,
27                         const std::string & /*paramName*/,
28                         const std::string & /*oldValue*/,
29                         const std::string & /*newValue*/,
30                         const std::string & /*message*/) const { ++entries; return 0; }
31
32     size_t GetEntries() const { return entries; }
33
34 private:
35     mutable size_t entries;
36 };
37
38 class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
39     public:
40         void addFilter(const std::string& field) { filter.push_back(field); }
41
42         const std::vector<std::string>& GetFilterParamsLog() const { return filter; }
43
44     private:
45         std::vector<std::string> filter;
46 };
47
48 }
49
50 namespace tut
51 {
52     struct filter_params_log_data {
53     };
54
55     typedef test_group<filter_params_log_data> tg;
56     tg filter_params_log_test_group("Filter params log tests group");
57
58     typedef tg::object testobject;
59
60     template<>
61     template<>
62     void testobject::test<1>()
63     {
64         set_test_name("Check normal behaviour");
65
66         TEST_SETTINGS_LOCAL settings;
67         settings.addFilter("*"); // Allow everything by default.
68         TEST_TARIFFS tariffs;
69         TEST_ADMIN admin;
70         TEST_STORE_LOCAL store;
71         TEST_AUTH auth;
72         TEST_USERS users;
73         TEST_SERVICES services;
74         USER_IMPL user(&settings, &store, &tariffs, &admin, &users, services);
75
76         USER_PROPERTY_LOGGED<std::string> & address(user.GetProperty().address);
77         USER_PROPERTY_LOGGED<std::string> & note(user.GetProperty().note);
78         USER_PROPERTY_LOGGED<std::string> & group(user.GetProperty().group);
79
80         address.Set("address", &admin, "", &store, "");
81         note.Set("note", &admin, "", &store, "");
82         group.Set("group", &admin, "", &store, "");
83
84         ensure_equals("entries = 3", store.GetEntries(), 3);
85
86         note.Set("another note", &admin, "", &store, "");
87
88         ensure_equals("entries = 4", store.GetEntries(), 4);
89     }
90
91
92     template<>
93     template<>
94     void testobject::test<2>()
95     {
96         set_test_name("Check single filter entry.");
97
98         TEST_SETTINGS_LOCAL settings;
99         settings.addFilter("address"); // Allow everything by default.
100         TEST_TARIFFS tariffs;
101         TEST_ADMIN admin;
102         TEST_STORE_LOCAL store;
103         TEST_AUTH auth;
104         TEST_USERS users;
105         TEST_SERVICES services;
106         USER_IMPL user(&settings, &store, &tariffs, &admin, &users, services);
107
108         USER_PROPERTY_LOGGED<std::string> & address(user.GetProperty().address);
109         USER_PROPERTY_LOGGED<std::string> & note(user.GetProperty().note);
110         USER_PROPERTY_LOGGED<std::string> & group(user.GetProperty().group);
111
112         address.Set("address", &admin, "", &store, "");
113         note.Set("note", &admin, "", &store, "");
114         group.Set("group", &admin, "", &store, "");
115
116         ensure_equals("entries = 1", store.GetEntries(), 1);
117
118         note.Set("another note", &admin, "", &store, "");
119
120         ensure_equals("entries = 1", store.GetEntries(), 1);
121
122         address.Set("new address", &admin, "", &store, "");
123
124         ensure_equals("entries = 2", store.GetEntries(), 2);
125     }
126
127     template<>
128     template<>
129     void testobject::test<3>()
130     {
131         set_test_name("Check multiple filter entries.");
132
133         TEST_SETTINGS_LOCAL settings;
134         settings.addFilter("address"); // Allow everything by default.
135         settings.addFilter("group"); // Allow everything by default.
136         TEST_TARIFFS tariffs;
137         TEST_ADMIN admin;
138         TEST_STORE_LOCAL store;
139         TEST_AUTH auth;
140         TEST_USERS users;
141         TEST_SERVICES services;
142         USER_IMPL user(&settings, &store, &tariffs, &admin, &users, services);
143
144         USER_PROPERTY_LOGGED<std::string> & address(user.GetProperty().address);
145         USER_PROPERTY_LOGGED<std::string> & note(user.GetProperty().note);
146         USER_PROPERTY_LOGGED<std::string> & group(user.GetProperty().group);
147
148         address.Set("address", &admin, "", &store, "");
149         note.Set("note", &admin, "", &store, "");
150         group.Set("group", &admin, "", &store, "");
151
152         ensure_equals("entries = 2", store.GetEntries(), 2);
153
154         note.Set("another note", &admin, "", &store, "");
155
156         ensure_equals("entries = 2", store.GetEntries(), 2);
157
158         address.Set("new address", &admin, "", &store, "");
159
160         ensure_equals("entries = 3", store.GetEntries(), 3);
161
162         group.Set("administrative group", &admin, "", &store, "");
163
164         ensure_equals("entries = 4", store.GetEntries(), 4);
165     }
166 }