]> git.stg.codes - stg.git/blobdiff - tests/test_reconnect_on_tariff_change.cpp
Use std::jthread and C++17.
[stg.git] / tests / test_reconnect_on_tariff_change.cpp
index e60e59890c10c22ad9d2c1b6d84b15846b4d810c..60e36ffa64df3d0c15836d8dc7b7fb125741a786 100644 (file)
@@ -1,12 +1,49 @@
 #include "tut/tut.hpp"
 
 #include "tut/tut.hpp"
 
+#include "stg/admin.h"
 #include "stg/user_property.h"
 #include "user_impl.h"
 
 #include "testsettings.h"
 #include "testtariffs.h"
 #include "stg/user_property.h"
 #include "user_impl.h"
 
 #include "testsettings.h"
 #include "testtariffs.h"
-#include "testadmin.h"
 #include "teststore.h"
 #include "teststore.h"
+#include "testauth.h"
+#include "testusers.h"
+#include "testservices.h"
+
+namespace
+{
+
+class AFTER_CONNECTED_NOTIFIER : public STG::PropertyNotifierBase<bool> {
+public:
+    AFTER_CONNECTED_NOTIFIER()
+        : connects(0),
+          disconnects(0)
+    {}
+    void Notify(const bool & oldValue, const bool & newValue);
+
+    size_t GetConnects() const { return connects; }
+    size_t GetDisconnects() const { return disconnects; }
+
+private:
+    size_t connects;
+    size_t disconnects;
+};
+
+class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
+    public:
+        TEST_SETTINGS_LOCAL(bool _reconnectOnTariffChange)
+            : TEST_SETTINGS(),
+              reconnectOnTariffChange(_reconnectOnTariffChange)
+        {}
+
+        bool GetReconnectOnTariffChange() const { return reconnectOnTariffChange; }
+
+    private:
+        bool reconnectOnTariffChange;
+};
+
+}
 
 namespace tut
 {
 
 namespace tut
 {
@@ -18,18 +55,6 @@ namespace tut
 
     typedef tg::object testobject;
 
 
     typedef tg::object testobject;
 
-    class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
-        public:
-            TEST_SETTINGS_LOCAL(bool _reconnectOnTariffChange)
-                : reconnectOnTariffChange(_reconnectOnTariffChange)
-            {}
-
-            bool GetReconnectOnTariffChange() const { return reconnectOnTariffChange; }
-
-        private:
-            bool reconnectOnTariffChange;
-    };
-
     template<>
     template<>
     void testobject::test<1>()
     template<>
     template<>
     void testobject::test<1>()
@@ -38,14 +63,111 @@ namespace tut
 
         TEST_SETTINGS_LOCAL settings(false);
         TEST_TARIFFS tariffs;
 
         TEST_SETTINGS_LOCAL settings(false);
         TEST_TARIFFS tariffs;
-        TEST_ADMIN admin;
+        tariffs.ReadTariffs();
+        STG::Admin admin(STG::Priv(0xFFFF), {}, {});
+        TEST_STORE store;
+        TEST_AUTH auth;
+        TEST_USERS users;
+        TEST_SERVICES services;
+        STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
+
+        AFTER_CONNECTED_NOTIFIER connectionNotifier;
+
+        user.AddConnectedAfterNotifier(&connectionNotifier);
+
+        STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
+        STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
+
+        ips = STG::UserIPs::parse("*");
+
+        ensure_equals("user.connected = false", user.GetConnected(), false);
+        ensure_equals("connects = 0", connectionNotifier.GetConnects(), static_cast<size_t>(0));
+        ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
+
+        ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
+
+        user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
+        user.Run();
+
+        ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
+
+        ensure_equals("user.connected = true", user.GetConnected(), true);
+        ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
+        ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
+
+        tariffName = "test";
+        ensure_equals("user.tariffName == 'test'", user.GetProperties().tariffName.ConstData(), "test");
+
+        ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
+
+        ensure_equals("user.connected = true", user.GetConnected(), true);
+        ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
+        ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
+    }
+
+
+    template<>
+    template<>
+    void testobject::test<2>()
+    {
+        set_test_name("Check reconnect on tariff change");
+
+        TEST_SETTINGS_LOCAL settings(true);
+
+        TEST_SETTINGS * s1 = &settings;
+        STG::Settings * s2 = &settings;
+
+        ensure("settings.GetReconnectOnTariffChange() == true", settings.GetReconnectOnTariffChange());
+        ensure("s1->GetReconnectOnTariffChange() == true", s1->GetReconnectOnTariffChange());
+        ensure("s2->GetReconnectOnTariffChange() == true", s2->GetReconnectOnTariffChange());
+
+        TEST_TARIFFS tariffs;
+        STG::Admin admin(STG::Priv(0xFFFF), {}, {});
         TEST_STORE store;
         TEST_STORE store;
-        USER_IMPL user(&settings, &store, &tariffs, &admin, NULL);
+        TEST_AUTH auth;
+        TEST_USERS users;
+        TEST_SERVICES services;
+        STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
 
 
-        USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
+        AFTER_CONNECTED_NOTIFIER connectionNotifier;
+
+        user.AddConnectedAfterNotifier(&connectionNotifier);
+
+        STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
+        STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
+
+        ips = STG::UserIPs::parse("*");
+
+        ensure_equals("user.connected = false", user.GetConnected(), false);
+        ensure_equals("connects = 0", connectionNotifier.GetConnects(), static_cast<size_t>(0));
+        ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
+
+        ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
+
+        user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
+        user.Run();
+
+        ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
+
+        ensure_equals("user.connected = true", user.GetConnected(), true);
+        ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
+        ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
 
 
-        ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperty().tariffName.ConstData(), NO_TARIFF_NAME);
         tariffName = "test";
         tariffName = "test";
-        ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
+        ensure_equals("user.tariffName == 'test'", user.GetProperties().tariffName.ConstData(), "test");
+
+        ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
+
+        ensure_equals("user.connected = true", user.GetConnected(), true);
+        ensure_equals("connects = 2", connectionNotifier.GetConnects(), static_cast<size_t>(2));
+        ensure_equals("disconnects = 1", connectionNotifier.GetDisconnects(), static_cast<size_t>(1));
     }
 }
     }
 }
+
+void AFTER_CONNECTED_NOTIFIER::Notify(const bool & oldValue, const bool & newValue)
+{
+    if (!oldValue && newValue)
+        ++connects;
+    if (oldValue && !newValue)
+        ++disconnects;
+}