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