Browse Source

add better test for commandes

Guénaël Muller 6 years ago
parent
commit
f32874ffca
3 changed files with 90 additions and 9 deletions
  1. 8 0
      tests_configs.ini
  2. 15 7
      tracim/tests/__init__.py
  3. 67 2
      tracim/tests/commands/test_commands.py

+ 8 - 0
tests_configs.ini View File

@@ -5,6 +5,14 @@ depot_storage_dir = /tmp/test/depot
5 5
 user.auth_token.validity = 604800
6 6
 preview_cache_dir = /tmp/test/preview_cache
7 7
 
8
+[app:command_test]
9
+use = egg:tracim_backend
10
+sqlalchemy.url = sqlite:///tracim_test.sqlite
11
+depot_storage_name = test
12
+depot_storage_dir = /tmp/test/depot
13
+user.auth_token.validity = 604800
14
+preview_cache_dir = /tmp/test/preview_cache
15
+
8 16
 [mail_test]
9 17
 sqlalchemy.url = sqlite:///:memory:
10 18
 depot_storage_name = test

+ 15 - 7
tracim/tests/__init__.py View File

@@ -36,7 +36,7 @@ class FunctionalTest(unittest.TestCase):
36 36
 
37 37
     def setUp(self):
38 38
         DepotManager._clear()
39
-        settings = {
39
+        self.settings = {
40 40
             'sqlalchemy.url': self.sqlalchemy_url,
41 41
             'user.auth_token.validity': '604800',
42 42
             'depot_storage_dir': '/tmp/test/depot',
@@ -45,19 +45,22 @@ class FunctionalTest(unittest.TestCase):
45 45
 
46 46
         }
47 47
         hapic.reset_context()
48
-        app = main({}, **settings)
49
-        self.init_database(settings)
48
+        self.init_database(self.settings)
49
+        self.run_app()
50
+
51
+    def run_app(self):
52
+        app = main({}, **self.settings)
50 53
         self.testapp = TestApp(app)
51 54
 
52 55
     def init_database(self, settings):
53 56
         self.engine = get_engine(settings)
54 57
         DeclarativeBase.metadata.create_all(self.engine)
55
-        session_factory = get_session_factory(self.engine)
56
-        app_config = CFG(settings)
58
+        self.session_factory = get_session_factory(self.engine)
59
+        self.app_config = CFG(settings)
57 60
         with transaction.manager:
58
-            dbsession = get_tm_session(session_factory, transaction.manager)
61
+            dbsession = get_tm_session(self.session_factory, transaction.manager)
59 62
             try:
60
-                fixtures_loader = FixturesLoader(dbsession, app_config)
63
+                fixtures_loader = FixturesLoader(dbsession, self.app_config)
61 64
                 fixtures_loader.loads(self.fixtures)
62 65
                 transaction.commit()
63 66
                 print("Database initialized.")
@@ -90,6 +93,11 @@ class FunctionalTestNoDB(FunctionalTest):
90 93
         self.engine = get_engine(settings)
91 94
 
92 95
 
96
+class CommandFunctionalTest(FunctionalTest):
97
+
98
+    def run_app(self):
99
+        self.session = get_tm_session(self.session_factory, transaction.manager)
100
+
93 101
 class BaseTest(unittest.TestCase):
94 102
     """
95 103
     Pyramid default test.

+ 67 - 2
tracim/tests/commands/test_commands.py View File

@@ -2,11 +2,27 @@
2 2
 import os
3 3
 import subprocess
4 4
 
5
+import pytest
6
+import transaction
7
+from pkg_resources import load_entry_point
8
+from sqlalchemy.orm.exc import NoResultFound
9
+
5 10
 import tracim
11
+from tracim.command import TracimCLI
12
+from tracim.command.user import UserCommand
13
+from tracim.exceptions import UserNotExist
14
+from tracim.lib.core.user import UserApi
15
+from tracim.tests import CommandFunctionalTest
16
+
17
+
18
+class TestCommands(CommandFunctionalTest):
19
+    """
20
+    Test tracimcli command line ui.
21
+    """
6 22
 
23
+    config_section = 'app:command_test'
7 24
 
8
-class TestCommands(object):
9
-    def test_commands(self):
25
+    def test_func__check_commands_list__ok__nominal_case(self):
10 26
         """
11 27
         Test listing of tracimcli command: Tracim commands must be listed
12 28
         :return:
@@ -20,3 +36,52 @@ class TestCommands(object):
20 36
         assert output.find('user update') > 0
21 37
         assert output.find('db init') > 0
22 38
         assert output.find('db delete') > 0
39
+        assert output.find('webdav start') > 0
40
+
41
+    def test_func__user_create_command__ok__nominal_case(self):
42
+        """
43
+        Test User creation
44
+        """
45
+        api = UserApi(
46
+            current_user=None,
47
+            session=self.session,
48
+            config=self.app_config,
49
+        )
50
+        with pytest.raises(NoResultFound):
51
+            api.get_one_by_email('command_test@user')
52
+        app = TracimCLI()
53
+        result = app.run([
54
+            'user', 'create',
55
+            '-c', 'tests_configs.ini#command_test',
56
+            '-l', 'command_test@user',
57
+            '-p', 'new_password'
58
+        ])
59
+        new_user = api.get_one_by_email('command_test@user')
60
+        assert new_user.email == 'command_test@user'
61
+        assert new_user.validate_password('new_password')
62
+
63
+    def test_func__user_update_command__ok__nominal_case(self):
64
+        """
65
+        Test user password update
66
+        """
67
+        api = UserApi(
68
+            current_user=None,
69
+            session=self.session,
70
+            config=self.app_config,
71
+        )
72
+        user = api.get_one_by_email('admin@admin.admin')
73
+        assert user.email == 'admin@admin.admin'
74
+        assert user.validate_password('admin@admin.admin')
75
+        assert not user.validate_password('new_password')
76
+
77
+        app = TracimCLI()
78
+        result = app.run([
79
+            'user', 'update',
80
+            '-c', 'tests_configs.ini#command_test',
81
+            '-l', 'admin@admin.admin',
82
+            '-p', 'new_password'
83
+        ])
84
+        new_user = api.get_one_by_email('admin@admin.admin')
85
+        assert new_user.email == 'admin@admin.admin'
86
+        assert new_user.validate_password('new_password')
87
+        assert not new_user.validate_password('admin@admin.admin')