3 #include "stg/user_property.h"
6 #include "testsettings.h"
7 #include "testtariffs.h"
11 #include "testusers.h"
12 #include "testservices.h"
17 class AFTER_CONNECTED_NOTIFIER : public STG::PropertyNotifierBase<bool> {
19 AFTER_CONNECTED_NOTIFIER()
23 void Notify(const bool & oldValue, const bool & newValue);
25 size_t GetConnects() const { return connects; }
26 size_t GetDisconnects() const { return disconnects; }
33 class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
35 TEST_SETTINGS_LOCAL(bool _reconnectOnTariffChange)
37 reconnectOnTariffChange(_reconnectOnTariffChange)
40 bool GetReconnectOnTariffChange() const { return reconnectOnTariffChange; }
43 bool reconnectOnTariffChange;
50 struct reconnect_on_tariff_change_data {
53 typedef test_group<reconnect_on_tariff_change_data> tg;
54 tg reconnect_on_tariff_change_test_group("Reconnect on tariff change tests group");
56 typedef tg::object testobject;
60 void testobject::test<1>()
62 set_test_name("Check normal behaviour");
64 TEST_SETTINGS_LOCAL settings(false);
70 TEST_SERVICES services;
71 STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
73 AFTER_CONNECTED_NOTIFIER connectionNotifier;
75 user.AddConnectedAfterNotifier(&connectionNotifier);
77 STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
78 STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
80 ips = STG::UserIPs::parse("*");
82 ensure_equals("user.connected = false", user.GetConnected(), false);
83 ensure_equals("connects = 0", connectionNotifier.GetConnects(), static_cast<size_t>(0));
84 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
86 ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
88 user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
91 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
93 ensure_equals("user.connected = true", user.GetConnected(), true);
94 ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
95 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
98 ensure_equals("user.tariffName == 'test'", user.GetProperties().tariffName.ConstData(), "test");
100 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
102 ensure_equals("user.connected = true", user.GetConnected(), true);
103 ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
104 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
110 void testobject::test<2>()
112 set_test_name("Check reconnect on tariff change");
114 TEST_SETTINGS_LOCAL settings(true);
116 TEST_SETTINGS * s1 = &settings;
117 STG::Settings * s2 = &settings;
119 ensure("settings.GetReconnectOnTariffChange() == true", settings.GetReconnectOnTariffChange());
120 ensure("s1->GetReconnectOnTariffChange() == true", s1->GetReconnectOnTariffChange());
121 ensure("s2->GetReconnectOnTariffChange() == true", s2->GetReconnectOnTariffChange());
123 TEST_TARIFFS tariffs;
128 TEST_SERVICES services;
129 STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
131 AFTER_CONNECTED_NOTIFIER connectionNotifier;
133 user.AddConnectedAfterNotifier(&connectionNotifier);
135 STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
136 STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
138 ips = STG::UserIPs::parse("*");
140 ensure_equals("user.connected = false", user.GetConnected(), false);
141 ensure_equals("connects = 0", connectionNotifier.GetConnects(), static_cast<size_t>(0));
142 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
144 ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
146 user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
149 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
151 ensure_equals("user.connected = true", user.GetConnected(), true);
152 ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
153 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
156 ensure_equals("user.tariffName == 'test'", user.GetProperties().tariffName.ConstData(), "test");
158 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
160 ensure_equals("user.connected = true", user.GetConnected(), true);
161 ensure_equals("connects = 2", connectionNotifier.GetConnects(), static_cast<size_t>(2));
162 ensure_equals("disconnects = 1", connectionNotifier.GetDisconnects(), static_cast<size_t>(1));
166 void AFTER_CONNECTED_NOTIFIER::Notify(const bool & oldValue, const bool & newValue)
168 if (!oldValue && newValue)
170 if (oldValue && !newValue)