Browse Source

add test and fixture for models

Guénaël Muller 7 years ago
parent
commit
48a3bba6ab

+ 37 - 0
tracim/fixtures/__init__.py View File

@@ -0,0 +1,37 @@
1
+import transaction
2
+
3
+class Fixture(object):
4
+
5
+    """ Fixture classes (list) required for this fixtures"""
6
+    require = NotImplemented
7
+
8
+    def __init__(self, session):
9
+        self._session = session
10
+
11
+    def insert(self):
12
+        raise NotImplementedError()
13
+
14
+
15
+class FixturesLoader(object):
16
+    """
17
+    Fixtures loader. Load each fixture once.
18
+    """
19
+
20
+    def __init__(self, session, loaded=None):
21
+        loaded = [] if loaded is None else loaded
22
+        self._loaded = loaded
23
+        self._session = session
24
+
25
+    def loads(self, fixtures_classes):
26
+        for fixture_class in fixtures_classes:
27
+            for required_fixture_class in fixture_class.require:
28
+                self._load(required_fixture_class)
29
+            self._load(fixture_class)
30
+
31
+    def _load(self, fixture_class: Fixture):
32
+        if fixture_class not in self._loaded:
33
+            fixture = fixture_class(self._session)
34
+            fixture.insert()
35
+            self._loaded.append(fixture_class)
36
+            self._session.flush()
37
+            transaction.commit()

+ 137 - 0
tracim/fixtures/content.py View File

@@ -0,0 +1,137 @@
1
+# TODO : Reenable this when API where availables
2
+# # -*- coding: utf-8 -*-
3
+# from depot.io.utils import FileIntent
4
+#
5
+# from tracim import models
6
+# from tracim.fixtures import Fixture
7
+# from tracim.fixtures.users_and_groups import Test
8
+# from tracim.lib.content import ContentApi
9
+# from tracim.lib.userworkspace import RoleApi
10
+# from tracim.lib.workspace import WorkspaceApi
11
+# from tracim.models.data import ContentType
12
+# from tracim.models.data import UserRoleInWorkspace
13
+#
14
+#
15
+# class Content(Fixture):
16
+#     require = [Test]
17
+#
18
+#     def insert(self):
19
+#         admin = self._session.query(models.User) \
20
+#             .filter(models.User.email == 'admin@admin.admin') \
21
+#             .one()
22
+#         bob = self._session.query(models.User) \
23
+#             .filter(models.User.email == 'bob@fsf.local') \
24
+#             .one()
25
+#         admin_workspace_api = WorkspaceApi(admin)
26
+#         bob_workspace_api = WorkspaceApi(bob)
27
+#         content_api = ContentApi(admin)
28
+#         role_api = RoleApi(admin)
29
+#
30
+#         # Workspaces
31
+#         w1 = admin_workspace_api.create_workspace('w1', save_now=True)
32
+#         w2 = bob_workspace_api.create_workspace('w2', save_now=True)
33
+#         w3 = admin_workspace_api.create_workspace('w3', save_now=True)
34
+#
35
+#         # Workspaces roles
36
+#         role_api.create_one(
37
+#             user=bob,
38
+#             workspace=w1,
39
+#             role_level=UserRoleInWorkspace.CONTENT_MANAGER,
40
+#             with_notif=False,
41
+#         )
42
+#
43
+#         # Folders
44
+#         w1f1 = content_api.create(
45
+#             content_type=ContentType.Folder,
46
+#             workspace=w1,
47
+#             label='w1f1',
48
+#             do_save=True,
49
+#         )
50
+#         w1f2 = content_api.create(
51
+#             content_type=ContentType.Folder,
52
+#             workspace=w1,
53
+#             label='w1f2',
54
+#             do_save=True,
55
+#         )
56
+#
57
+#         w2f1 = content_api.create(
58
+#             content_type=ContentType.Folder,
59
+#             workspace=w2,
60
+#             label='w2f1',
61
+#             do_save=True,
62
+#         )
63
+#         w2f2 = content_api.create(
64
+#             content_type=ContentType.Folder,
65
+#             workspace=w2,
66
+#             label='w2f2',
67
+#             do_save=True,
68
+#         )
69
+#
70
+#         w3f1 = content_api.create(
71
+#             content_type=ContentType.Folder,
72
+#             workspace=w3,
73
+#             label='w3f3',
74
+#             do_save=True,
75
+#         )
76
+#
77
+#         # Pages, threads, ..
78
+#         w1f1p1 = content_api.create(
79
+#             content_type=ContentType.Page,
80
+#             workspace=w1,
81
+#             parent=w1f1,
82
+#             label='w1f1p1',
83
+#             do_save=True,
84
+#         )
85
+#         w1f1t1 = content_api.create(
86
+#             content_type=ContentType.Thread,
87
+#             workspace=w1,
88
+#             parent=w1f1,
89
+#             label='w1f1t1',
90
+#             do_save=False,
91
+#         )
92
+#         w1f1t1.description = 'w1f1t1 description'
93
+#         self._session.add(w1f1t1)
94
+#         w1f1d1_txt = content_api.create(
95
+#             content_type=ContentType.File,
96
+#             workspace=w1,
97
+#             parent=w1f1,
98
+#             label='w1f1d1',
99
+#             do_save=False,
100
+#         )
101
+#         w1f1d1_txt.file_extension = '.txt'
102
+#         w1f1d1_txt.depot_file = FileIntent(
103
+#             b'w1f1d1 content',
104
+#             'w1f1d1.txt',
105
+#             'text/plain',
106
+#         )
107
+#         self._session.add(w1f1d1_txt)
108
+#         w1f1d2_html = content_api.create(
109
+#             content_type=ContentType.File,
110
+#             workspace=w1,
111
+#             parent=w1f1,
112
+#             label='w1f1d2',
113
+#             do_save=False,
114
+#         )
115
+#         w1f1d2_html.file_extension = '.html'
116
+#         w1f1d2_html.depot_file = FileIntent(
117
+#             b'<p>w1f1d2 content</p>',
118
+#             'w1f1d2.html',
119
+#             'text/html',
120
+#         )
121
+#         self._session.add(w1f1d2_html)
122
+#         w1f1f1 = content_api.create(
123
+#             content_type=ContentType.Folder,
124
+#             workspace=w1,
125
+#             label='w1f1f1',
126
+#             parent=w1f1,
127
+#             do_save=True,
128
+#         )
129
+#
130
+#         w2f1p1 = content_api.create(
131
+#             content_type=ContentType.Page,
132
+#             workspace=w2,
133
+#             parent=w2f1,
134
+#             label='w2f1p1',
135
+#             do_save=True,
136
+#         )
137
+#         self._session.flush()

+ 58 - 0
tracim/fixtures/ldap.py View File

@@ -0,0 +1,58 @@
1
+ldap_test_server_fixtures = {
2
+    'port': 3333,
3
+    'password': 'toor',
4
+
5
+    'bind_dn': 'cn=admin,dc=directory,dc=fsf,dc=org',
6
+    'base': {
7
+        'objectclass': ['dcObject', 'organization'],
8
+        'dn': 'dc=directory,dc=fsf,dc=org',
9
+        'attributes': {
10
+            'o': 'Free Software Foundation',
11
+            'dc': 'directory'
12
+        }
13
+    },
14
+
15
+    'entries': [
16
+        {
17
+            'objectclass': ['organizationalRole'],
18
+            'dn': 'cn=admin,dc=directory,dc=fsf,dc=org',
19
+            'attributes': {
20
+                'cn': 'admin'
21
+            }
22
+        },
23
+        {
24
+            'objectclass': ['organizationalUnit'],
25
+            'dn': 'ou=people,dc=directory,dc=fsf,dc=org',
26
+            'attributes': {
27
+                'ou': 'people',
28
+            }
29
+        },
30
+        {
31
+            'objectclass': ['organizationalUnit'],
32
+            'dn': 'ou=groups,dc=directory,dc=fsf,dc=org',
33
+            'attributes': {
34
+                'ou': 'groups',
35
+            }
36
+        },
37
+        {
38
+            'objectclass': ['account', 'top'],
39
+            'dn': 'cn=richard-not-real-email@fsf.org,ou=people,dc=directory,dc=fsf,dc=org',
40
+            'attributes': {
41
+                'uid': 'richard-not-real-email@fsf.org',
42
+                'userPassword': 'rms',
43
+                'mail': 'richard-not-real-email@fsf.org',
44
+                'pubname': 'Richard Stallman',
45
+            }
46
+        },
47
+        {
48
+            'objectclass': ['account', 'top'],
49
+            'dn': 'cn=lawrence-not-real-email@fsf.local,ou=people,dc=directory,dc=fsf,dc=org',
50
+            'attributes': {
51
+                'uid': 'lawrence-not-real-email@fsf.local',
52
+                'userPassword': 'foobarbaz',
53
+                'mail': 'lawrence-not-real-email@fsf.local',
54
+                'pubname': 'Lawrence Lessig',
55
+            }
56
+        },
57
+    ]
58
+}

+ 59 - 0
tracim/fixtures/users_and_groups.py View File

@@ -0,0 +1,59 @@
1
+# -*- coding: utf-8 -*-
2
+from tracim import models
3
+from tracim.fixtures import Fixture
4
+#from tracim.lib.user import UserApi
5
+
6
+
7
+class Base(Fixture):
8
+    require = []
9
+
10
+    def insert(self):
11
+        u = models.User()
12
+        u.display_name = 'Global manager'
13
+        u.email = 'admin@admin.admin'
14
+        u.password = 'admin@admin.admin'
15
+        self._session.add(u)
16
+        #uapi = UserApi(u)
17
+        #uapi.execute_created_user_actions(u)
18
+
19
+        g1 = models.Group()
20
+        g1.group_id = 1
21
+        g1.group_name = 'users'
22
+        g1.display_name = 'Users'
23
+        g1.users.append(u)
24
+        self._session.add(g1)
25
+
26
+        g2 = models.Group()
27
+        g2.group_id = 2
28
+        g2.group_name = 'managers'
29
+        g2.display_name = 'Global Managers'
30
+        g2.users.append(u)
31
+        self._session.add(g2)
32
+
33
+        g3 = models.Group()
34
+        g3.group_id = 3
35
+        g3.group_name = 'administrators'
36
+        g3.display_name = 'Administrators'
37
+        g3.users.append(u)
38
+        self._session.add(g3)
39
+
40
+
41
+class Test(Fixture):
42
+    require = [Base, ]
43
+
44
+    def insert(self):
45
+        g2 = self._session.query(models.Group).filter(models.Group.group_name == 'managers').one()
46
+
47
+        lawrence = models.User()
48
+        lawrence.display_name = 'Lawrence L.'
49
+        lawrence.email = 'lawrence-not-real-email@fsf.local'
50
+        lawrence.password = 'foobarbaz'
51
+        self._session.add(lawrence)
52
+        g2.users.append(lawrence)
53
+
54
+        bob = models.User()
55
+        bob.display_name = 'Bob i.'
56
+        bob.email = 'bob@fsf.local'
57
+        bob.password = 'foobarbaz'
58
+        self._session.add(bob)
59
+        g2.users.append(bob)

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

@@ -0,0 +1,85 @@
1
+import unittest
2
+import transaction
3
+from pyramid import testing
4
+
5
+from tracim.models.data import Workspace
6
+from tracim.models.data import Content
7
+from tracim.logger import logger
8
+from tracim.fixtures import FixturesLoader
9
+from tracim.fixtures.users_and_groups import Base as BaseFixture
10
+
11
+
12
+class BaseTest(unittest.TestCase):
13
+    """
14
+    Pyramid default test.
15
+    """
16
+    def setUp(self):
17
+        logger.debug(self, 'Setup Test...')
18
+        self.config = testing.setUp(settings={
19
+            'sqlalchemy.url': 'sqlite:///:memory:'
20
+        })
21
+        self.config.include('tracim.models')
22
+        settings = self.config.get_settings()
23
+
24
+        from tracim.models import (
25
+            get_engine,
26
+            get_session_factory,
27
+            get_tm_session,
28
+            )
29
+
30
+        self.engine = get_engine(settings)
31
+        session_factory = get_session_factory(self.engine)
32
+
33
+        self.session = get_tm_session(session_factory, transaction.manager)
34
+        self.init_database()
35
+
36
+    def init_database(self):
37
+        logger.debug(self, 'Init Database Schema...')
38
+        from tracim.models.meta import DeclarativeBase
39
+        DeclarativeBase.metadata.create_all(self.engine)
40
+
41
+    def tearDown(self):
42
+        logger.debug(self, 'TearDown Test...')
43
+        from tracim.models.meta import DeclarativeBase
44
+
45
+        testing.tearDown()
46
+        transaction.abort()
47
+        DeclarativeBase.metadata.drop_all(self.engine)
48
+
49
+# class DefaultTest(object):
50
+#
51
+#     def _create_workspace_and_test(self, name, user) -> Workspace:
52
+#         """
53
+#         All extra parameters (*args, **kwargs) are for Workspace init
54
+#         :return: Created workspace instance
55
+#         """
56
+#         WorkspaceApi(user).create_workspace(name, save_now=True)
57
+#
58
+#         eq_(1, self.session.query(Workspace).filter(Workspace.label == name).count())
59
+#         return self.session.query(Workspace).filter(Workspace.label == name).one()
60
+#
61
+#     def _create_content_and_test(self, name, workspace, *args, **kwargs) -> Content:
62
+#         """
63
+#         All extra parameters (*args, **kwargs) are for Content init
64
+#         :return: Created Content instance
65
+#         """
66
+#         content = Content(*args, **kwargs)
67
+#         content.label = name
68
+#         content.workspace = workspace
69
+#         self.session.add(content)
70
+#         self.session.flush()
71
+#
72
+#         eq_(1, ContentApi.get_canonical_query().filter(Content.label == name).count())
73
+#         return ContentApi.get_canonical_query().filter(Content.label == name).one()
74
+#
75
+#
76
+# class StandardTest(BaseTest):
77
+#     """
78
+#     BaseTest with default fixtures
79
+#     """
80
+#     fixtures = [BaseFixture]
81
+#
82
+#     def init_database(self):
83
+#         BaseTest.init_database(self)
84
+#         fixtures_loader = FixturesLoader([BaseFixture])
85
+#         fixtures_loader.loads(self.fixtures)

+ 2 - 0
tracim/tests/models/__init__.py View File

@@ -0,0 +1,2 @@
1
+# -*- coding: utf-8 -*-
2
+"""Unit test suite for the models of the application."""

+ 216 - 0
tracim/tests/models/test_content.py View File

@@ -0,0 +1,216 @@
1
+# -*- coding: utf-8 -*-
2
+import time
3
+
4
+from depot.fields.upload import UploadedFile
5
+from nose.tools import ok_
6
+from nose.tools import raises
7
+from sqlalchemy.sql.elements import and_
8
+from sqlalchemy.testing import eq_
9
+
10
+# from tracim.lib.content import ContentApi
11
+from tracim.exceptions import ContentRevisionUpdateError
12
+from tracim.models import Content
13
+from tracim.models.revision_protection import new_revision
14
+from tracim.models import User
15
+from tracim.models.data import ActionDescription
16
+from tracim.models.data import ContentRevisionRO
17
+from tracim.models.data import ContentType
18
+from tracim.models.data import Workspace
19
+from tracim.tests import BaseTest
20
+
21
+
22
+class TestContent(BaseTest):
23
+
24
+    @raises(ContentRevisionUpdateError)
25
+    def test_update_without_prepare(self):
26
+        content1 = self.test_create()
27
+        content1.description = 'FOO'  # Raise ContentRevisionUpdateError because revision can't be updated
28
+
29
+    # TODO - G.M - 28-03-2018 - Reenable this test when libContent is available
30
+    # def test_query(self):
31
+    #     content1 = self.test_create()
32
+    #     with new_revision(content1):
33
+    #         content1.description = 'TEST_CONTENT_DESCRIPTION_1_UPDATED'
34
+    #     DBSession.flush()
35
+    #
36
+    #     content2 = self.test_create(key='2')
37
+    #     with new_revision(content2):
38
+    #         content2.description = 'TEST_CONTENT_DESCRIPTION_2_UPDATED'
39
+    #     DBSession.flush()
40
+    #
41
+    #     workspace1 = DBSession.query(Workspace).filter(Workspace.label == 'TEST_WORKSPACE_1').one()
42
+    #     workspace2 = DBSession.query(Workspace).filter(Workspace.label == 'TEST_WORKSPACE_2').one()
43
+    #
44
+    #     # To get Content in database we have to join Content and ContentRevisionRO with particular condition:
45
+    #     # Join have to be on most recent revision
46
+    #     join_sub_query = DBSession.query(ContentRevisionRO.revision_id)\
47
+    #         .filter(ContentRevisionRO.content_id == Content.id)\
48
+    #         .order_by(ContentRevisionRO.revision_id.desc())\
49
+    #         .limit(1)\
50
+    #         .correlate(Content)
51
+    #
52
+    #     base_query = DBSession.query(Content)\
53
+    #         .join(ContentRevisionRO, and_(Content.id == ContentRevisionRO.content_id,
54
+    #                                       ContentRevisionRO.revision_id == join_sub_query))
55
+    #
56
+    #     pattern = 'TEST_CONTENT_DESCRIPTION_%_UPDATED'
57
+    #     eq_(2, base_query.filter(Content.description.like(pattern)).count())
58
+    #
59
+    #     eq_(1, base_query.filter(Content.workspace == workspace1).count())
60
+    #     eq_(1, base_query.filter(Content.workspace == workspace2).count())
61
+    #
62
+    #     content1_from_query = base_query.filter(Content.workspace == workspace1).one()
63
+    #     eq_(content1.id, content1_from_query.id)
64
+    #     eq_('TEST_CONTENT_DESCRIPTION_1_UPDATED', content1_from_query.description)
65
+    #
66
+    #     user_admin = DBSession.query(User).filter(User.email == 'admin@admin.admin').one()
67
+    #
68
+    #     api = ContentApi(None)
69
+    #
70
+    #     content1_from_api = api.get_one(content1.id, ContentType.Page, workspace1)
71
+
72
+    def test_update(self):
73
+        created_content = self.test_create()
74
+        content = self.session.query(Content).filter(Content.id == created_content.id).one()
75
+        eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'TEST_CONTENT_1').count())
76
+
77
+        with new_revision(content):
78
+            time.sleep(0.00001)
79
+            content.description = 'TEST_CONTENT_DESCRIPTION_1_UPDATED'
80
+        self.session.flush()
81
+
82
+        eq_(2, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'TEST_CONTENT_1').count())
83
+        eq_(1, self.session.query(Content).filter(Content.id == created_content.id).count())
84
+
85
+        with new_revision(content):
86
+            time.sleep(0.00001)
87
+            content.description = 'TEST_CONTENT_DESCRIPTION_1_UPDATED_2'
88
+            content.label = 'TEST_CONTENT_1_UPDATED_2'
89
+        self.session.flush()
90
+
91
+        eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'TEST_CONTENT_1_UPDATED_2').count())
92
+        eq_(1, self.session.query(Content).filter(Content.id == created_content.id).count())
93
+
94
+        revision_1 = self.session.query(ContentRevisionRO)\
95
+            .filter(ContentRevisionRO.description == 'TEST_CONTENT_DESCRIPTION_1').one()
96
+        revision_2 = self.session.query(ContentRevisionRO)\
97
+            .filter(ContentRevisionRO.description == 'TEST_CONTENT_DESCRIPTION_1_UPDATED').one()
98
+        revision_3 = self.session.query(ContentRevisionRO)\
99
+            .filter(ContentRevisionRO.description == 'TEST_CONTENT_DESCRIPTION_1_UPDATED_2').one()
100
+
101
+        # Updated dates must be different
102
+        ok_(revision_1.updated < revision_2.updated < revision_3.updated)
103
+        # Created dates must be equal
104
+        ok_(revision_1.created == revision_2.created == revision_3.created)
105
+
106
+    def test_creates(self):
107
+        eq_(0, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'TEST_CONTENT_1').count())
108
+        eq_(0, self.session.query(Workspace).filter(Workspace.label == 'TEST_WORKSPACE_1').count())
109
+
110
+        user_admin = self.session.query(User).filter(User.email == 'admin@admin.admin').one()
111
+        workspace = Workspace(label="TEST_WORKSPACE_1")
112
+        self.session.add(workspace)
113
+        self.session.flush()
114
+        eq_(1, self.session.query(Workspace).filter(Workspace.label == 'TEST_WORKSPACE_1').count())
115
+
116
+        first_content = self._create_content(
117
+            owner=user_admin,
118
+            workspace=workspace,
119
+            type=ContentType.Page,
120
+            label='TEST_CONTENT_1',
121
+            description='TEST_CONTENT_DESCRIPTION_1',
122
+            revision_type=ActionDescription.CREATION,
123
+            is_deleted=False,
124
+            is_archived=False,
125
+        )
126
+
127
+        eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'TEST_CONTENT_1').count())
128
+
129
+        content = self.session.query(Content).filter(Content.id == first_content.id).one()
130
+        eq_('TEST_CONTENT_1', content.label)
131
+        eq_('TEST_CONTENT_DESCRIPTION_1', content.description)
132
+
133
+        # Create a second content
134
+        second_content = self._create_content(
135
+            owner=user_admin,
136
+            workspace=workspace,
137
+            type=ContentType.Page,
138
+            label='TEST_CONTENT_2',
139
+            description='TEST_CONTENT_DESCRIPTION_2',
140
+            revision_type=ActionDescription.CREATION
141
+        )
142
+
143
+        eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'TEST_CONTENT_2').count())
144
+
145
+        content = self.session.query(Content).filter(Content.id == second_content.id).one()
146
+        eq_('TEST_CONTENT_2', content.label)
147
+        eq_('TEST_CONTENT_DESCRIPTION_2', content.description)
148
+
149
+    def test_create(self, key='1'):
150
+        eq_(0, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'TEST_CONTENT_%s' % key).count())
151
+        eq_(0, self.session.query(Workspace).filter(Workspace.label == 'TEST_WORKSPACE_%s' % key).count())
152
+
153
+        user_admin = self.session.query(User).filter(User.email == 'admin@admin.admin').one()
154
+        workspace = Workspace(label="TEST_WORKSPACE_%s" % key)
155
+        self.session.add(workspace)
156
+        self.session.flush()
157
+        eq_(1, self.session.query(Workspace).filter(Workspace.label == 'TEST_WORKSPACE_%s' % key).count())
158
+
159
+        created_content = self._create_content(
160
+            owner=user_admin,
161
+            workspace=workspace,
162
+            type=ContentType.Page,
163
+            label='TEST_CONTENT_%s' % key,
164
+            description='TEST_CONTENT_DESCRIPTION_%s' % key,
165
+            revision_type=ActionDescription.CREATION
166
+        )
167
+
168
+        eq_(1, self.session.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'TEST_CONTENT_%s' % key).count())
169
+
170
+        content = self.session.query(Content).filter(Content.id == created_content.id).one()
171
+        eq_('TEST_CONTENT_%s' % key, content.label)
172
+        eq_('TEST_CONTENT_DESCRIPTION_%s' % key, content.description)
173
+
174
+        return created_content
175
+
176
+    def _get_user(self):
177
+        email = 'admin@admin.admin'
178
+        user_query = self.session.query(User)
179
+        user_filter = user_query.filter(User.email == email)
180
+        user = user_filter.one()
181
+        return user
182
+
183
+    def _create_content(self, *args, **kwargs):
184
+        content = Content(*args, **kwargs)
185
+        self.session.add(content)
186
+        self.session.flush()
187
+        return content
188
+
189
+    def _create_content_from_nothing(self):
190
+        user_admin = self._get_user()
191
+        workspace = Workspace(label="TEST_WORKSPACE_1")
192
+        content = self._create_content(
193
+            owner=user_admin,
194
+            workspace=workspace,
195
+            type=ContentType.File,
196
+            label='TEST_CONTENT_1',
197
+            description='TEST_CONTENT_DESCRIPTION_1',
198
+            revision_type=ActionDescription.CREATION,
199
+        )
200
+        return content
201
+
202
+    def test_unit__content_depot_file(self):
203
+        """ Depot file access thought content property methods. """
204
+        content = self._create_content_from_nothing()
205
+        # tests uninitialized depot file
206
+        eq_(content.depot_file, None)
207
+        # initializes depot file
208
+        # which is able to behave like a python file object
209
+        content.depot_file = b'test'
210
+        # tests initialized depot file
211
+        ok_(content.depot_file)
212
+        # tests type of initialized depot file
213
+        eq_(type(content.depot_file), UploadedFile)
214
+        # tests content of initialized depot file
215
+        # using depot_file.file of type StoredFile to fetch content back
216
+        eq_(content.depot_file.file.read(), b'test')

+ 64 - 0
tracim/tests/models/test_content_revision.py View File

@@ -0,0 +1,64 @@
1
+# -*- coding: utf-8 -*-
2
+from collections import OrderedDict
3
+
4
+from nose.tools import eq_
5
+from sqlalchemy import inspect
6
+
7
+from tracim.models import ContentRevisionRO
8
+from tracim.models import User
9
+from tracim.models.data import ContentType
10
+from tracim.tests import BaseTest
11
+
12
+
13
+class TestContentRevision(BaseTest):
14
+
15
+    def _new_from(self, revision):
16
+        excluded_columns = (
17
+            'revision_id',
18
+            '_sa_instance_state',
19
+            'depot_file',
20
+            'node',
21
+            'revision_read_statuses',
22
+        )
23
+        revision_columns = [attr.key for attr in inspect(revision).attrs if not attr.key in excluded_columns]
24
+        new_revision = ContentRevisionRO()
25
+
26
+        for revision_column in revision_columns:
27
+            old_revision_column_value = getattr(revision, revision_column)
28
+            setattr(new_revision, revision_column, old_revision_column_value)
29
+
30
+        return new_revision
31
+
32
+    def _get_dict_representation(self, revision):
33
+        keys_to_remove = ('updated', '_sa_instance_state')
34
+        dict_repr = OrderedDict(sorted(revision.__dict__.items()))
35
+        for key_to_remove in keys_to_remove:
36
+            dict_repr.pop(key_to_remove, None)
37
+        return dict_repr
38
+
39
+    # TODO - G.M - 28-03-2018 - Reenable this test
40
+    # def test_new_revision(self):
41
+    #     admin = self.session.query(User).filter(User.email == 'admin@admin.admin').one()
42
+    #     workspace = self._create_workspace_and_test(name='workspace_1', user=admin)
43
+    #     folder = self._create_content_and_test(name='folder_1', workspace=workspace, type=ContentType.Folder)
44
+    #     page = self._create_content_and_test(
45
+    #         workspace=workspace,
46
+    #         parent=folder,
47
+    #         name='file_1',
48
+    #         description='content of file_1',
49
+    #         type=ContentType.Page,
50
+    #         owner=admin
51
+    #     )
52
+    #
53
+    #     self.session.flush()
54
+    #
55
+    #     # Model create a new instance with list of column
56
+    #     new_revision_by_model = ContentRevisionRO.new_from(page.revision)
57
+    #     # Test create a new instance from dynamic listing of model columns mapping
58
+    #     new_revision_by_test = self._new_from(page.revision)
59
+    #
60
+    #     new_revision_by_model_dict = self._get_dict_representation(new_revision_by_model)
61
+    #     new_revision_by_test_dict = self._get_dict_representation(new_revision_by_test)
62
+    #
63
+    #     # They must be identical
64
+    #     eq_(new_revision_by_model_dict, new_revision_by_test_dict)

+ 44 - 0
tracim/tests/models/test_user.py View File

@@ -0,0 +1,44 @@
1
+import transaction
2
+
3
+from nose.tools import eq_
4
+from nose.tools import ok_
5
+
6
+from tracim.tests import BaseTest
7
+
8
+from tracim.models.auth import User
9
+
10
+
11
+class TestUserModel(BaseTest):
12
+
13
+    def test_create(self):
14
+        self.session.flush()
15
+        transaction.commit()
16
+        name = 'Damien'
17
+        email = 'damien@accorsi.info'
18
+
19
+        user = User()
20
+        user.display_name = name
21
+        user.email = email
22
+
23
+        self.session.add(user)
24
+        self.session.flush()
25
+        transaction.commit()
26
+
27
+        new_user = self.session.query(User).filter(User.display_name==name).one()
28
+
29
+        eq_(new_user.display_name, name)
30
+        eq_(new_user.email, email)
31
+        eq_(new_user.email_address, email)
32
+
33
+    def test_null_password(self):
34
+        # Check bug #70 fixed
35
+        # http://tracim.org/workspaces/4/folders/5/threads/70
36
+
37
+        name = 'Damien'
38
+        email = 'tracim@trac.im'
39
+
40
+        user = User()
41
+        user.display_name = name
42
+        user.email = email
43
+
44
+        eq_(False, user.validate_password(None))