]> git.stg.codes - stg.git/blob - tests/test_reconnect_on_tariff_change.cpp
87497f9fc99cc34bb111019a52e0379c47e6f1ba
[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
12 class AFTER_CONNECTED_NOTIFIER : public PROPERTY_NOTIFIER_BASE<bool>,
13                                  private NONCOPYABLE {
14 public:
15     AFTER_CONNECTED_NOTIFIER()
16         : connects(0),
17           disconnects(0)
18     {}
19     void Notify(const bool & oldValue, const bool & newValue);
20
21     size_t GetConnects() const { return connects; }
22     size_t GetDisconnects() const { return disconnects; }
23
24 private:
25     size_t connects;
26     size_t disconnects;
27 };
28
29 namespace tut
30 {
31     struct reconnect_on_tariff_change_data {
32     };
33
34     typedef test_group<reconnect_on_tariff_change_data> tg;
35     tg reconnect_on_tariff_change_test_group("Reconnect on tariff change tests group");
36
37     typedef tg::object testobject;
38
39     class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
40         public:
41             TEST_SETTINGS_LOCAL(bool _reconnectOnTariffChange)
42                 : reconnectOnTariffChange(_reconnectOnTariffChange)
43             {}
44
45             bool GetReconnectOnTariffChange() const { return reconnectOnTariffChange; }
46
47         private:
48             bool reconnectOnTariffChange;
49     };
50
51     template<>
52     template<>
53     void testobject::test<1>()
54     {
55         set_test_name("Check normal behaviour");
56
57         TEST_SETTINGS_LOCAL settings(false);
58         TEST_TARIFFS tariffs;
59         TEST_ADMIN admin;
60         TEST_STORE store;
61         TEST_AUTH auth;
62         USER_IMPL user(&settings, &store, &tariffs, &admin, NULL);
63
64         AFTER_CONNECTED_NOTIFIER connectionNotifier;
65
66         user.AddConnectedAfterNotifier(&connectionNotifier);
67
68         USER_PROPERTY<double> & cash(user.GetProperty().cash);
69         USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
70         USER_PROPERTY<USER_IPS> & ips(user.GetProperty().ips);
71
72         ips = StrToIPS("*");
73
74         ensure_equals("user.connected = false", user.GetConnected(), false);
75         ensure_equals("connects = 0", connectionNotifier.GetConnects(), 0);
76         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
77
78         ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperty().tariffName.ConstData(), NO_TARIFF_NAME);
79
80         tariffName = "test";
81         ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
82
83         user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
84
85         ensure_equals("user.connected = true", user.GetConnected(), true);
86         ensure_equals("connects = 1", connectionNotifier.GetConnects(), 1);
87         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
88     }
89 }
90
91 void AFTER_CONNECTED_NOTIFIER::Notify(const bool & oldValue, const bool & newValue)
92 {
93     if (!oldValue && newValue)
94         ++connects;
95     if (oldValue && !newValue)
96         ++disconnects;
97 }