[geary/mjog/imap-connection-fixes: 5/34] Add unit test for Geary.Imap.ClientSession



commit 339d8ae26ec5c9255e02679872090ca5426a401a
Author: Michael Gratton <mike vee net>
Date:   Sun Dec 29 12:26:38 2019 +1030

    Add unit test for Geary.Imap.ClientSession

 .../imap/transport/imap-client-session-test.vala   | 157 +++++++++++++++++++++
 test/meson.build                                   |   1 +
 test/test-engine.vala                              |   1 +
 3 files changed, 159 insertions(+)
---
diff --git a/test/engine/imap/transport/imap-client-session-test.vala 
b/test/engine/imap/transport/imap-client-session-test.vala
new file mode 100644
index 00000000..329e9aca
--- /dev/null
+++ b/test/engine/imap/transport/imap-client-session-test.vala
@@ -0,0 +1,157 @@
+/*
+ * Copyright 2019 Michael Gratton <mike vee net>
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later). See the COPYING file in this distribution.
+ */
+
+class Geary.Imap.ClientSessionTest : TestCase {
+
+
+    private TestServer? server = null;
+
+
+    public ClientSessionTest() {
+        base("Geary.Imap.ClientSessionTest");
+        add_test("connect_disconnect", connect_disconnect);
+        add_test("login", login);
+        add_test("logout", logout);
+        add_test("login_logout", login_logout);
+    }
+
+    protected override void set_up() throws GLib.Error {
+        this.server = new TestServer();
+    }
+
+    protected override void tear_down() {
+        this.server.stop();
+        this.server = null;
+    }
+
+    public void connect_disconnect() throws GLib.Error {
+        this.server.add_script_line(
+            SEND_LINE, "* OK localhost test server ready"
+        );
+        this.server.add_script_line(WAIT_FOR_DISCONNECT, "");
+
+        var test_article = new ClientSession(new_endpoint());
+        assert_true(test_article.get_protocol_state(null) == NOT_CONNECTED);
+
+        test_article.connect_async.begin(null, this.async_complete_full);
+        test_article.connect_async.end(async_result());
+        assert_true(test_article.get_protocol_state(null) == UNAUTHORIZED);
+
+        test_article.disconnect_async.begin(null, this.async_complete_full);
+        test_article.disconnect_async.end(async_result());
+        assert_true(test_article.get_protocol_state(null) == NOT_CONNECTED);
+
+        TestServer.Result result = this.server.wait_for_script(this.main_loop);
+        assert_true(
+            result.succeeded,
+            result.error != null ? result.error.message : "Server result failed"
+        );
+    }
+
+    public void login() throws GLib.Error {
+        this.server.add_script_line(
+            SEND_LINE, "* OK localhost test server ready"
+        );
+        this.server.add_script_line(RECEIVE_LINE, "a001 login test password");
+        this.server.add_script_line(SEND_LINE, "a001 OK ohhai");
+        this.server.add_script_line(WAIT_FOR_DISCONNECT, "");
+
+        var test_article = new ClientSession(new_endpoint());
+        assert_true(test_article.get_protocol_state(null) == NOT_CONNECTED);
+
+        test_article.connect_async.begin(null, this.async_complete_full);
+        test_article.connect_async.end(async_result());
+        assert_true(test_article.get_protocol_state(null) == UNAUTHORIZED);
+
+        test_article.login_async.begin(
+            new Credentials(PASSWORD, "test", "password"),
+            null,
+            this.async_complete_full
+        );
+        test_article.login_async.end(async_result());
+        assert_true(test_article.get_protocol_state(null) == AUTHORIZED);
+
+        test_article.disconnect_async.begin(null, this.async_complete_full);
+        test_article.disconnect_async.end(async_result());
+        assert_true(test_article.get_protocol_state(null) == NOT_CONNECTED);
+
+        TestServer.Result result = this.server.wait_for_script(this.main_loop);
+        assert_true(
+            result.succeeded,
+            result.error != null ? result.error.message : "Server result failed"
+        );
+    }
+
+    public void logout() throws GLib.Error {
+        this.server.add_script_line(
+            SEND_LINE, "* OK localhost test server ready"
+        );
+        this.server.add_script_line(RECEIVE_LINE, "a001 logout");
+        this.server.add_script_line(SEND_LINE, "* BYE fine");
+        this.server.add_script_line(SEND_LINE, "a001 OK laters");
+        this.server.add_script_line(DISCONNECT, "");
+
+        var test_article = new ClientSession(new_endpoint());
+        assert_true(test_article.get_protocol_state(null) == NOT_CONNECTED);
+
+        test_article.connect_async.begin(null, this.async_complete_full);
+        test_article.connect_async.end(async_result());
+        assert_true(test_article.get_protocol_state(null) == UNAUTHORIZED);
+
+        test_article.logout_async.begin(null, this.async_complete_full);
+        test_article.logout_async.end(async_result());
+        assert_true(test_article.get_protocol_state(null) == NOT_CONNECTED);
+
+        TestServer.Result result = this.server.wait_for_script(this.main_loop);
+        assert_true(
+            result.succeeded,
+            result.error != null ? result.error.message : "Server result failed"
+        );
+    }
+
+    public void login_logout() throws GLib.Error {
+        this.server.add_script_line(
+            SEND_LINE, "* OK localhost test server ready"
+        );
+        this.server.add_script_line(RECEIVE_LINE, "a001 login test password");
+        this.server.add_script_line(SEND_LINE, "a001 OK ohhai");
+        this.server.add_script_line(RECEIVE_LINE, "a002 logout");
+        this.server.add_script_line(SEND_LINE, "* BYE fine");
+        this.server.add_script_line(SEND_LINE, "a002 OK laters");
+        this.server.add_script_line(DISCONNECT, "");
+
+        var test_article = new ClientSession(new_endpoint());
+        assert_true(test_article.get_protocol_state(null) == NOT_CONNECTED);
+
+        test_article.connect_async.begin(null, this.async_complete_full);
+        test_article.connect_async.end(async_result());
+        assert_true(test_article.get_protocol_state(null) == UNAUTHORIZED);
+
+        test_article.login_async.begin(
+            new Credentials(PASSWORD, "test", "password"),
+            null,
+            this.async_complete_full
+        );
+        test_article.login_async.end(async_result());
+        assert_true(test_article.get_protocol_state(null) == AUTHORIZED);
+
+        test_article.logout_async.begin(null, this.async_complete_full);
+        test_article.logout_async.end(async_result());
+        assert_true(test_article.get_protocol_state(null) == NOT_CONNECTED);
+
+        TestServer.Result result = this.server.wait_for_script(this.main_loop);
+        assert_true(
+            result.succeeded,
+            result.error != null ? result.error.message : "Server result failed"
+        );
+    }
+
+    protected Endpoint new_endpoint() {
+        return new Endpoint(this.server.get_client_address(), NONE, 10);
+    }
+
+}
diff --git a/test/meson.build b/test/meson.build
index 18906688..9cd4717f 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -43,6 +43,7 @@ geary_test_engine_sources = [
   'engine/imap/parameter/imap-list-parameter-test.vala',
   'engine/imap/response/imap-namespace-response-test.vala',
   'engine/imap/transport/imap-client-connection-test.vala',
+  'engine/imap/transport/imap-client-session-test.vala',
   'engine/imap/transport/imap-deserializer-test.vala',
   'engine/imap-db/imap-db-account-test.vala',
   'engine/imap-db/imap-db-attachment-test.vala',
diff --git a/test/test-engine.vala b/test/test-engine.vala
index 85f6961b..a70cb11c 100644
--- a/test/test-engine.vala
+++ b/test/test-engine.vala
@@ -59,6 +59,7 @@ int main(string[] args) {
     // Depends on IMAP commands working
     engine.add_suite(new Geary.Imap.DeserializerTest().get_suite());
     engine.add_suite(new Geary.Imap.ClientConnectionTest().get_suite());
+    engine.add_suite(new Geary.Imap.ClientSessionTest().get_suite());
 
     engine.add_suite(new Geary.ImapDB.AccountTest().get_suite());
     engine.add_suite(new Geary.ImapDB.AttachmentTest().get_suite());


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]