]> git.stg.codes - stg.git/blob - tests/test_reconnect_on_tariff_change.cpp
603f60dfe01adbb0b1739ef12957b7b8a1f928cc
[stg.git] / tests / test_reconnect_on_tariff_change.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
13 class AFTER_CONNECTED_NOTIFIER : public PROPERTY_NOTIFIER_BASE<bool>,
14                                  private NONCOPYABLE {
15 public:
16     AFTER_CONNECTED_NOTIFIER()
17         : connects(0),
18           disconnects(0)
19     {}
20     void Notify(const bool & oldValue, const bool & newValue);
21
22     size_t GetConnects() const { return connects; }
23     size_t GetDisconnects() const { return disconnects; }
24
25 private:
26     size_t connects;
27     size_t disconnects;
28 };
29
30 class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
31     public:
32         TEST_SETTINGS_LOCAL(bool _reconnectOnTariffChange)
33             : TEST_SETTINGS(),
34               reconnectOnTariffChange(_reconnectOnTariffChange)
35         {}
36
37         bool GetReconnectOnTariffChange() const { return reconnectOnTariffChange; }
38
39     private:
40         bool reconnectOnTariffChange;
41 };
42
43 namespace tut
44 {
45     struct reconnect_on_tariff_change_data {
46     };
47
48     typedef test_group<reconnect_on_tariff_change_data> tg;
49     tg reconnect_on_tariff_change_test_group("Reconnect on tariff change tests group");
50
51     typedef tg::object testobject;
52
53     template<>
54     template<>
55     void testobject::test<1>()
56     {
57         set_test_name("Check normal behaviour");
58
59         TEST_SETTINGS_LOCAL settings(false);
60         TEST_TARIFFS tariffs;
61         TEST_ADMIN admin;
62         TEST_STORE store;
63         TEST_AUTH auth;
64         TEST_USERS users;
65         USER_IMPL user(&settings, &store, &tariffs, &admin, &users);
66
67         AFTER_CONNECTED_NOTIFIER connectionNotifier;
68
69         user.AddConnectedAfterNotifier(&connectionNotifier);
70
71         USER_PROPERTY<double> & cash(user.GetProperty().cash);
72         USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
73         USER_PROPERTY<USER_IPS> & ips(user.GetProperty().ips);
74
75         ips = StrToIPS("*");
76
77         ensure_equals("user.connected = false", user.GetConnected(), false);
78         ensure_equals("connects = 0", connectionNotifier.GetConnects(), 0);
79         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
80
81         ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperty().tariffName.ConstData(), NO_TARIFF_NAME);
82
83         user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
84         user.Run();
85
86         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
87
88         ensure_equals("user.connected = true", user.GetConnected(), true);
89         ensure_equals("connects = 1", connectionNotifier.GetConnects(), 1);
90         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
91
92         tariffName = "test";
93         ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
94
95         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
96
97         ensure_equals("user.connected = true", user.GetConnected(), true);
98         ensure_equals("connects = 1", connectionNotifier.GetConnects(), 1);
99         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
100     }
101
102
103     template<>
104     template<>
105     void testobject::test<2>()
106     {
107         set_test_name("Check reconnect on tariff change");
108
109         TEST_SETTINGS_LOCAL settings(true);
110
111         TEST_SETTINGS * s1 = &settings;
112         SETTINGS * s2 = &settings;
113
114         ensure("settings.GetReconnectOnTariffChange() == true", settings.GetReconnectOnTariffChange());
115         ensure("s1->GetReconnectOnTariffChange() == true", s1->GetReconnectOnTariffChange());
116         ensure("s2->GetReconnectOnTariffChange() == true", s2->GetReconnectOnTariffChange());
117
118         TEST_TARIFFS tariffs;
119         TEST_ADMIN admin;
120         TEST_STORE store;
121         TEST_AUTH auth;
122         TEST_USERS users;
123         USER_IMPL user(&settings, &store, &tariffs, &admin, &users);
124
125         AFTER_CONNECTED_NOTIFIER connectionNotifier;
126
127         user.AddConnectedAfterNotifier(&connectionNotifier);
128
129         USER_PROPERTY<double> & cash(user.GetProperty().cash);
130         USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
131         USER_PROPERTY<USER_IPS> & ips(user.GetProperty().ips);
132
133         ips = StrToIPS("*");
134
135         ensure_equals("user.connected = false", user.GetConnected(), false);
136         ensure_equals("connects = 0", connectionNotifier.GetConnects(), 0);
137         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
138
139         ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperty().tariffName.ConstData(), NO_TARIFF_NAME);
140
141         user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
142         user.Run();
143
144         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
145
146         ensure_equals("user.connected = true", user.GetConnected(), true);
147         ensure_equals("connects = 1", connectionNotifier.GetConnects(), 1);
148         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
149
150         tariffName = "test";
151         ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
152
153         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
154
155         ensure_equals("user.connected = true", user.GetConnected(), true);
156         ensure_equals("connects = 2", connectionNotifier.GetConnects(), 2);
157         ensure_equals("disconnects = 1", connectionNotifier.GetDisconnects(), 1);
158     }
159 }
160
161 void AFTER_CONNECTED_NOTIFIER::Notify(const bool & oldValue, const bool & newValue)
162 {
163     if (!oldValue && newValue)
164         ++connects;
165     if (oldValue && !newValue)
166         ++disconnects;
167 }