]> git.stg.codes - stg.git/blob - tests/test_disable_session_log.cpp
Merge pull request #2 from bobr-kun/MySQL_custom_port
[stg.git] / tests / test_disable_session_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         : connects(0),
22           disconnects(0)
23     {}
24     int WriteUserConnect(const std::string & /*login*/, uint32_t /*ip*/) const { ++connects; return 0; }
25
26     int WriteUserDisconnect(const std::string & /*login*/,
27                             const DIR_TRAFF & /*up*/,
28                             const DIR_TRAFF & /*down*/,
29                             const DIR_TRAFF & /*sessionUp*/,
30                             const DIR_TRAFF & /*sessionDown*/,
31                             double /*cash*/,
32                             double /*freeMb*/,
33                             const std::string & /*reason*/) const { ++disconnects; return 0; }
34
35     size_t GetConnects() const { return connects; }
36     size_t GetDisconnects() const { return disconnects; }
37
38 private:
39     mutable size_t connects;
40     mutable size_t disconnects;
41 };
42
43 class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
44     public:
45         TEST_SETTINGS_LOCAL(bool _disableSessionLog)
46             : TEST_SETTINGS(),
47               disableSessionLog(_disableSessionLog)
48         {}
49
50         bool GetDisableSessionLog() const { return disableSessionLog; }
51
52     private:
53         bool disableSessionLog;
54 };
55
56 }
57
58 namespace tut
59 {
60     struct disable_session_log_data {
61     };
62
63     typedef test_group<disable_session_log_data> tg;
64     tg disable_session_log_test_group("Disable session log tests group");
65
66     typedef tg::object testobject;
67
68     template<>
69     template<>
70     void testobject::test<1>()
71     {
72         set_test_name("Check normal behaviour");
73
74         TEST_SETTINGS_LOCAL settings(false);
75         TEST_TARIFFS tariffs;
76         TEST_ADMIN admin;
77         TEST_STORE_LOCAL store;
78         TEST_AUTH auth;
79         TEST_USERS users;
80         TEST_SERVICES services;
81         USER_IMPL user(&settings, &store, &tariffs, &admin, &users, services);
82
83         USER_PROPERTY<USER_IPS> & ips(user.GetProperty().ips);
84
85         ips = StrToIPS("*");
86
87         ensure_equals("user.connected = false", user.GetConnected(), false);
88         ensure_equals("connects = 0", store.GetConnects(), static_cast<size_t>(0));
89         ensure_equals("disconnects = 0", store.GetDisconnects(), static_cast<size_t>(0));
90
91         user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
92         user.Run();
93
94         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
95
96         ensure_equals("user.connected = true", user.GetConnected(), true);
97         ensure_equals("connects = 1", store.GetConnects(), static_cast<size_t>(1));
98         ensure_equals("disconnects = 0", store.GetDisconnects(), static_cast<size_t>(0));
99
100         user.Unauthorize(&auth);
101         user.Run();
102
103         ensure_equals("user.authorised_by = false", user.IsAuthorizedBy(&auth), false);
104
105         ensure_equals("user.connected = false", user.GetConnected(), false);
106         ensure_equals("connects = 1", store.GetConnects(), static_cast<size_t>(1));
107         ensure_equals("disconnects = 1", store.GetDisconnects(), static_cast<size_t>(1));
108     }
109
110
111     template<>
112     template<>
113     void testobject::test<2>()
114     {
115         set_test_name("Check disabled session log");
116
117         TEST_SETTINGS_LOCAL settings(true);
118         TEST_TARIFFS tariffs;
119         TEST_ADMIN admin;
120         TEST_STORE_LOCAL store;
121         TEST_AUTH auth;
122         TEST_USERS users;
123         TEST_SERVICES services;
124         USER_IMPL user(&settings, &store, &tariffs, &admin, &users, services);
125
126         USER_PROPERTY<USER_IPS> & ips(user.GetProperty().ips);
127
128         ips = StrToIPS("*");
129
130         ensure_equals("user.connected = false", user.GetConnected(), false);
131         ensure_equals("connects = 0", store.GetConnects(), static_cast<size_t>(0));
132         ensure_equals("disconnects = 0", store.GetDisconnects(), static_cast<size_t>(0));
133
134         user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
135         user.Run();
136
137         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
138
139         ensure_equals("user.connected = true", user.GetConnected(), true);
140         ensure_equals("connects = 0", store.GetConnects(), static_cast<size_t>(0));
141         ensure_equals("disconnects = 0", store.GetDisconnects(), static_cast<size_t>(0));
142
143         user.Unauthorize(&auth);
144         user.Run();
145
146         ensure_equals("user.authorised_by = false", user.IsAuthorizedBy(&auth), false);
147
148         ensure_equals("user.connected = false", user.GetConnected(), false);
149         ensure_equals("connects = 0", store.GetConnects(), static_cast<size_t>(0));
150         ensure_equals("disconnects = 0", store.GetDisconnects(), static_cast<size_t>(0));
151     }
152 }