3 #include "stg/user_property.h"
6 #include "testsettings.h"
7 #include "testtariffs.h"
11 #include "testusers.h"
13 class AFTER_CONNECTED_NOTIFIER : public PROPERTY_NOTIFIER_BASE<bool>,
16 AFTER_CONNECTED_NOTIFIER()
20 void Notify(const bool & oldValue, const bool & newValue);
22 size_t GetConnects() const { return connects; }
23 size_t GetDisconnects() const { return disconnects; }
30 class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
32 TEST_SETTINGS_LOCAL(bool _reconnectOnTariffChange)
34 reconnectOnTariffChange(_reconnectOnTariffChange)
37 bool GetReconnectOnTariffChange() const { return reconnectOnTariffChange; }
40 bool reconnectOnTariffChange;
45 struct reconnect_on_tariff_change_data {
48 typedef test_group<reconnect_on_tariff_change_data> tg;
49 tg reconnect_on_tariff_change_test_group("Reconnect on tariff change tests group");
51 typedef tg::object testobject;
55 void testobject::test<1>()
57 set_test_name("Check normal behaviour");
59 TEST_SETTINGS_LOCAL settings(false);
65 USER_IMPL user(&settings, &store, &tariffs, &admin, &users);
67 AFTER_CONNECTED_NOTIFIER connectionNotifier;
69 user.AddConnectedAfterNotifier(&connectionNotifier);
71 USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
72 USER_PROPERTY<USER_IPS> & ips(user.GetProperty().ips);
76 ensure_equals("user.connected = false", user.GetConnected(), false);
77 ensure_equals("connects = 0", connectionNotifier.GetConnects(), static_cast<size_t>(0));
78 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
80 ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperty().tariffName.ConstData(), NO_TARIFF_NAME);
82 user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
85 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
87 ensure_equals("user.connected = true", user.GetConnected(), true);
88 ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
89 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
92 ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
94 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
96 ensure_equals("user.connected = true", user.GetConnected(), true);
97 ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
98 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
104 void testobject::test<2>()
106 set_test_name("Check reconnect on tariff change");
108 TEST_SETTINGS_LOCAL settings(true);
110 TEST_SETTINGS * s1 = &settings;
111 SETTINGS * s2 = &settings;
113 ensure("settings.GetReconnectOnTariffChange() == true", settings.GetReconnectOnTariffChange());
114 ensure("s1->GetReconnectOnTariffChange() == true", s1->GetReconnectOnTariffChange());
115 ensure("s2->GetReconnectOnTariffChange() == true", s2->GetReconnectOnTariffChange());
117 TEST_TARIFFS tariffs;
122 USER_IMPL user(&settings, &store, &tariffs, &admin, &users);
124 AFTER_CONNECTED_NOTIFIER connectionNotifier;
126 user.AddConnectedAfterNotifier(&connectionNotifier);
128 USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
129 USER_PROPERTY<USER_IPS> & ips(user.GetProperty().ips);
133 ensure_equals("user.connected = false", user.GetConnected(), false);
134 ensure_equals("connects = 0", connectionNotifier.GetConnects(), static_cast<size_t>(0));
135 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
137 ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperty().tariffName.ConstData(), NO_TARIFF_NAME);
139 user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
142 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
144 ensure_equals("user.connected = true", user.GetConnected(), true);
145 ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
146 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
149 ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
151 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
153 ensure_equals("user.connected = true", user.GetConnected(), true);
154 ensure_equals("connects = 2", connectionNotifier.GetConnects(), static_cast<size_t>(2));
155 ensure_equals("disconnects = 1", connectionNotifier.GetDisconnects(), static_cast<size_t>(1));
159 void AFTER_CONNECTED_NOTIFIER::Notify(const bool & oldValue, const bool & newValue)
161 if (!oldValue && newValue)
163 if (oldValue && !newValue)