Browse Source

add functional test for session-related API endpoint

Guénaël Muller 6 years ago
parent
commit
49ea30cc5c

+ 53 - 1
tracim/tests/__init__.py View File

@@ -3,16 +3,21 @@ import unittest
3 3
 import transaction
4 4
 from depot.manager import DepotManager
5 5
 from pyramid import testing
6
+from sqlalchemy.exc import IntegrityError
6 7
 
7
-
8
+from tracim.command.initializedb import InitializeDBCommand
8 9
 from tracim.lib.core.content import ContentApi
9 10
 from tracim.lib.core.workspace import WorkspaceApi
11
+from tracim.models import get_engine, DeclarativeBase, get_session_factory, \
12
+    get_tm_session
10 13
 from tracim.models.data import Workspace, ContentType
11 14
 from tracim.models.data import Content
12 15
 from tracim.lib.utils.logger import logger
13 16
 from tracim.fixtures import FixturesLoader
14 17
 from tracim.fixtures.users_and_groups import Base as BaseFixture
15 18
 from tracim.config import CFG
19
+from tracim import main
20
+from webtest import TestApp
16 21
 
17 22
 
18 23
 def eq_(a, b, msg=None):
@@ -20,6 +25,53 @@ def eq_(a, b, msg=None):
20 25
     assert a == b, msg or "%r != %r" % (a, b)
21 26
 
22 27
 
28
+class FunctionalTest(unittest.TestCase):
29
+    def setUp(self):
30
+        DepotManager._clear()
31
+        settings = {
32
+            'sqlalchemy.url': 'sqlite:///tracim_test.sqlite',
33
+            'user.auth_token.validity': '604800',
34
+            'depot_storage_dir': '/tmp/test/depot',
35
+            'depot_storage_name': 'test',
36
+            'preview_cache_dir': '/tmp/test/preview_cache',
37
+
38
+        }
39
+        from tracim.extensions import hapic
40
+        hapic._context = None
41
+        app = main({}, **settings)
42
+        self.init_database(settings)
43
+        self.testapp = TestApp(app)
44
+
45
+    def init_database(self, settings):
46
+        self.engine = get_engine(settings)
47
+        DeclarativeBase.metadata.create_all(self.engine)
48
+        session_factory = get_session_factory(self.engine)
49
+        app_config = CFG(settings)
50
+        with transaction.manager:
51
+            dbsession = get_tm_session(session_factory, transaction.manager)
52
+            try:
53
+                fixtures_loader = FixturesLoader(dbsession, app_config)
54
+                fixtures_loader.loads([BaseFixture])
55
+                transaction.commit()
56
+                print("Database initialized.")
57
+            except IntegrityError:
58
+                print('Warning, there was a problem when adding default data'
59
+                      ', it may have already been added:')
60
+                import traceback
61
+                print(traceback.format_exc())
62
+                transaction.abort()
63
+                print('Database initialization failed')
64
+
65
+    def tearDown(self):
66
+        logger.debug(self, 'TearDown Test...')
67
+        from tracim.models.meta import DeclarativeBase
68
+
69
+        testing.tearDown()
70
+        transaction.abort()
71
+        DeclarativeBase.metadata.drop_all(self.engine)
72
+        DepotManager._clear()
73
+
74
+
23 75
 class BaseTest(unittest.TestCase):
24 76
     """
25 77
     Pyramid default test.

+ 0 - 0
tracim/tests/functional/__init__.py View File


+ 79 - 0
tracim/tests/functional/test_session.py View File

@@ -0,0 +1,79 @@
1
+# coding=utf-8
2
+from tracim.tests import FunctionalTest
3
+
4
+
5
+class TestLogoutEndpoint(FunctionalTest):
6
+
7
+    def test_logout(self):
8
+        res = self.testapp.get('/api/v2/sessions/logout', status=200)
9
+        assert res.json_body == {'message': 'ok'}
10
+
11
+
12
+class TestLoginEndpoint(FunctionalTest):
13
+
14
+    def test_login_ok(self):
15
+        params = {
16
+            'email': 'admin@admin.admin',
17
+            'password': 'admin@admin.admin',
18
+        }
19
+        res = self.testapp.get(
20
+            '/api/v2/sessions/login',
21
+            status=200,
22
+            params=params
23
+        )
24
+        assert res.json_body == {'message': 'ok'}
25
+
26
+    def test_bad_password(self):
27
+        params = {
28
+            'email': 'admin@admin.admin',
29
+            'password': 'bad_password',
30
+        }
31
+        res = self.testapp.get(
32
+            '/api/v2/sessions/login',
33
+            status=400,
34
+            params=params,
35
+        )
36
+
37
+    def test_bad_user(self):
38
+        params = {
39
+            'email': 'unknown_user@unknown.unknown',
40
+            'password': 'bad_password',
41
+        }
42
+        res = self.testapp.get(
43
+            '/api/v2/sessions/login',
44
+            status=400,
45
+            params=params,
46
+        )
47
+
48
+    def test_uncomplete(self):
49
+        res = self.testapp.get('/api/v2/sessions/login', status=400)
50
+
51
+
52
+class TestWhoamiEndpoint(FunctionalTest):
53
+
54
+    def test_login_ok(self):
55
+        self.testapp.authorization = (
56
+            'Basic',
57
+            (
58
+                'admin@admin.admin',
59
+                'admin@admin.admin'
60
+            )
61
+        )
62
+        res = self.testapp.get('/api/v2/sessions/whoami', status=200)
63
+        assert res.json_body['display_name'] == 'Global manager'
64
+        assert res.json_body['email'] == 'admin@admin.admin'
65
+        assert res.json_body['created']
66
+        assert res.json_body['is_active']
67
+        assert res.json_body['profile']
68
+        assert isinstance(res.json_body['profile']['id'], int)
69
+        assert res.json_body['profile']['name'] == 'administrators'
70
+
71
+    def test_unauthenticated(self):
72
+        self.testapp.authorization = (
73
+            'Basic',
74
+            (
75
+                'john@doe.doe',
76
+                'lapin'
77
+            )
78
+        )
79
+        res = self.testapp.get('/api/v2/sessions/whoami', status=401)