Source

ziggurat_foundations / ziggurat_foundations / tests.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# -*- coding: utf-8 -*-
import unittest

from sqlalchemy.ext.declarative import declarative_base

import ziggurat_foundations
from ziggurat_foundations.models import UserMixin
from ziggurat_foundations.models import GroupMixin
from ziggurat_foundations.models import GroupPermissionMixin
from ziggurat_foundations.models import UserPermissionMixin
from ziggurat_foundations.models import UserGroupMixin
from ziggurat_foundations.models import UserResourcePermissionMixin
from ziggurat_foundations.models import GroupResourcePermissionMixin
from ziggurat_foundations.models import ResourceMixin
from ziggurat_foundations.models import get_db_session
from ziggurat_foundations import ziggurat_model_init

from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import create_engine

Base = declarative_base()

class Group(GroupMixin, Base):
    pass

class GroupPermission(GroupPermissionMixin, Base):
    pass

class UserGroup(UserGroupMixin, Base):
    pass

class GroupResourcePermission(GroupResourcePermissionMixin, Base):
    pass

class Resource(ResourceMixin, Base):
    pass

class TestResource(Resource):
    __mapper_args__ = {'polymorphic_identity': u'test_resource'}

class UserPermission(UserPermissionMixin, Base):
    pass

class UserResourcePermission(UserResourcePermissionMixin, Base):
    pass
        
class User(UserMixin, Base):
    pass

ziggurat_model_init(User, Group, UserGroup, GroupPermission, UserPermission,
                   UserResourcePermission, GroupResourcePermission, Resource)

def _initTestingDB():
    engine = create_engine('sqlite://')
    # pyramid way
    maker = sessionmaker(bind=engine)
    Base.metadata.bind = engine
    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)
    return maker()

    # pylons/akhet monkeypatching way
#    DBSession = scoped_session(sessionmaker())
#    dbsession = DBSession()
#    dbsession.configure(bind=engine)
#    ziggurat_foundations.models.DBSession = DBSession
#    Base.metadata.bind = engine
#    Base.metadata.create_all(engine)
#    return dbsession




class BaseTestCase(unittest.TestCase):
    def setUp(self):
        self.session = _initTestingDB()

    def _addUser(self, user_name=u'username', email=u'email'):
        user = User(user_name=user_name, email=email, status=0)
        user.set_password('password')
        root_perm = UserPermission(perm_name=u'root')
        users_perm = UserPermission(perm_name=u'alter_users')
        user.user_permissions.append(root_perm)
        user.user_permissions.append(users_perm)
        self.session.add(user)
        self.session.flush()
        return user
    
    def _addResource(self, resource_id, resource_name=u'test_resource',):
        Resource.__possible_permissions__ = [u'test_perm', u'test_perm1',
                                             u'test_perm2',u'foo_perm',
                                             u'group_perm']
        resource = TestResource(resource_id=resource_id,
                            resource_name=resource_name)
        self.session.add(resource)
        self.session.flush()
        return resource

    def _addGroup(self, group_name=u'group', description=u'desc',):
        group = Group(
            group_name=group_name,
            description=description
        )
        self.session.add(group)
        self.session.flush()
        return group

class ModelTestCase(BaseTestCase):
    
    def test_get_keys(self):
        keys = User._get_keys()
        self.assertEqual(len(keys), 7)
        
    def test_get_dict(self):
        created_user = self._addUser()
        dict_ = created_user.get_dict()
        self.assertEqual(len(dict_), 7)
        
    def test_appstruct(self):
        created_user = self._addUser()
        appstruct = created_user.get_appstruct()
        self.assertEqual(len(appstruct), 7)

    def test_populate_obj_appstruct(self):
        created_user = self._addUser()
        app_struct = {'user_name':u'new_name'}
        created_user.populate_obj(app_struct)
        self.assertEqual(created_user.user_name, u'new_name')

#    def test_session(self):
#        session = get_db_session(self._addUser())
#        from sqlalchemy.orm import ScopedSession
#        self.assertIsInstance(session, ScopedSession)

class UserTestCase(BaseTestCase):

    def test_add_user(self):
        user = User(user_name=u'username', email=u'email', status=0)
        self.session.add(user)
        self.session.flush()

        user = self.session.query(User).filter(User.user_name == u'username')
        user = user.first()
        self.assertEqual(user.user_name, u'username')
        self.assertEqual(user.email, u'email')
        self.assertEqual(user.status, 0)
    
    def test_delete_user(self):
        self._addUser()
        to_delete = User.by_user_name(u'username', db_session=self.session)
        self.session.delete(to_delete)
        self.session.commit()

    def test_user_repr(self):
        user = self._addUser()
        self.assertEqual(repr(user), '<User: username>')

    def test_by_user_name_existing(self):
        created_user = self._addUser()
        queried_user = User.by_user_name(u'username', db_session=self.session)

        self.assertEqual(created_user, queried_user)

    def test_by_user_name_not_existing(self):
        self._addUser()
        queried_user = User.by_user_name(u'not_existing_user',
                                         db_session=self.session)

        self.assertEqual(queried_user, None)

    def test_by_username_andsecurity_code_existing(self):
        created_user = self._addUser()
        security_code = created_user.security_code
        queried_user = User.by_user_name_and_security_code(
            user_name=u'username',
            security_code=security_code,
            db_session=self.session
        )

        self.assertEqual(created_user, queried_user)

    def test_by_username_andsecurity_code_not_existing(self):
        created_user = self._addUser()
        security_code = created_user.security_code
        queried_user = User.by_user_name_and_security_code(
            user_name=u'not_existing_user',
            security_code=security_code,
            db_session=self.session
        )

        self.assertEqual(queried_user, None)

    def test_by_username_andsecurity_code_wrong_code(self):
        self._addUser()
        queried_user = User.by_user_name_and_security_code(
            user_name=u'username',
            security_code=u'wrong_code',
            db_session=self.session
        )

        self.assertEqual(queried_user, None)

    def test_by_user_names(self):
        user1 = self._addUser(u'user1', u'email1')
        self._addUser(u'user2', u'email2')
        user3 = self._addUser(u'user3', u'email3')

        queried_users = User.by_user_names([u'user1', u'user3'],
                                           db_session=self.session).all()

        self.assertEqual(len(queried_users), 2)
        self.assertEqual(user1, queried_users[0])
        self.assertEqual(user3, queried_users[1])

    def test_by_user_names_like(self):
        user1 = self._addUser(u'user1', u'email1')
        self._addUser(u'luser2', u'email2')
        self._addUser(u'noname', u'email3')

        queried_users = User.user_names_like(u'user%',
                                             db_session=self.session).all()
        self.assertEqual(len(queried_users), 1)
        self.assertEqual(user1, queried_users[0])

    def test_by_email(self):
        created_user = self._addUser()
        queried_user = User.by_email(u'email',db_session=self.session)

        self.assertEqual(created_user, queried_user)

    def test_by_email_wrong_email(self):
        self._addUser()
        queried_user = User.by_email(u'wrong_email',db_session=self.session)

        self.assertEqual(queried_user, None)

    def test_by_mail_and_username(self):
        created_user = self._addUser()
        queried_user = User.by_email_and_username(u'email', u'username',
                                                  db_session=self.session)

        self.assertEqual(created_user, queried_user)

    def test_by_mail_and_username_wrong_mail(self):
        self._addUser()
        queried_user = User.by_email_and_username(u'wrong_email', u'username',
                                                  db_session=self.session)

        self.assertEqual(queried_user, None)

    def test_by_mail_and_username_wrong_username(self):
        self._addUser()
        queried_user = User.by_email_and_username(u'email', u'wrong_username',
                                                  db_session=self.session)

        self.assertEqual(queried_user, None)

    def test_gravatar_url(self):
        user = self._addUser()
        self.assertEqual(user.gravatar_url(),
                         'https://secure.gravatar.com/avatar/'
                         '0c83f57c786a0b4a39efab23731c7ebc?d=mm')
        
    def test_generate_random_string(self):
        rand_str = User.generate_random_string()
        
        self.assertEqual(len(rand_str), 7)
        self.assertIsInstance(rand_str, unicode)
        
    def test_generate_random_pass(self):
        rand_str = User.generate_random_pass()
        
        self.assertEqual(len(rand_str), 7)
        self.assertIsInstance(rand_str, unicode)
        
        rand_str = User.generate_random_pass(20)
        self.assertEqual(len(rand_str), 20)
        
    def test_regenerate_security_code(self):
        created_user = self._addUser()
        old_code = created_user.security_code
        created_user.regenerate_security_code()
        new_code = created_user.security_code
        
        self.assertNotEqual(old_code, new_code)
        self.assertEqual(len(new_code), 32)
        
    def test_user_permissions(self):
        created_user = self._addUser()
        permissions = created_user.permissions
        self.assertEqual(permissions, [(u'username', u'alter_users'),
                                       (u'username', u'root')])
        
    def test_resources_with_perm(self):
        created_user = self._addUser()
        resource = self._addResource(1, u'test_resource')
        permission = UserResourcePermission(perm_name=u'test_perm',
                                            user_name=created_user.user_name,
                                            resource_id=resource.resource_id)
        resource.user_permissions.append(permission)
        self.session.flush()
        resources = created_user.resources_with_perms([u'test_perm'],
                                        db_session=self.session).all()
        self.assertEqual(resources[0], resource)
        
    def test_resources_with_wrong_perm(self):
        with self.assertRaises(AssertionError):
            created_user = self._addUser()
            resource = self._addResource(1, u'test_resource')
            permission = UserResourcePermission(perm_name=u'test_perm_BAD',
                                        user_name=created_user.user_name,
                                        resource_id = resource.resource_id
                                                    )
            resource.user_permissions.append(permission)

    def test_multiple_resources_with_perm(self):
        created_user = self._addUser()
        resource = self._addResource(1, u'test_resource')
        permission = UserResourcePermission(perm_name=u'test_perm',
                                            user_name=created_user.user_name,
                                            resource_id = resource.resource_id
                                                )
        resource.user_permissions.append(permission)
        resource2 = self._addResource(2, u'test_resource2')
        permission2 = UserResourcePermission(perm_name=u'test_perm',
                                            user_name=created_user.user_name,
                                            resource_id = resource2.resource_id
                                                )
        resource2.user_permissions.append(permission2)
        resources = created_user.resources_with_perms([u'test_perm'],
                                        db_session=self.session).all()
        self.assertEqual(resources, [resource,resource2])
    
    def test_resources_ids_with_perm(self):
        created_user = self._addUser()
        resource1 = self._addResource(1, u'test_resource1')
        resource2 = self._addResource(2, u'test_resource2')
        resource3 = self._addResource(3, u'test_resource3')

        permission1 = UserResourcePermission(perm_name=u'test_perm',
                                            user_name=created_user.user_name,
                                            resource_id=resource1.resource_id)
        permission2 = UserResourcePermission(perm_name=u'test_perm',
                                            user_name=created_user.user_name,
                                            resource_id=resource2.resource_id)
        permission3 = UserResourcePermission(perm_name=u'test_perm',
                                            user_name=created_user.user_name,
                                            resource_id=resource3.resource_id)

        resource1.user_permissions.append(permission1)
        resource2.user_permissions.append(permission2)
        resource3.user_permissions.append(permission3)

        self.session.flush()
        resources = created_user.resources_with_perms([u'test_perm'],
                                                resource_ids=[1, 3],
                                                db_session=self.session).all()
        self.assertEqual(resources, [resource1, resource3])

    def test_resources_with_wrong_group_permission(self):
        with self.assertRaises(AssertionError):
            created_user = self._addUser()
            resource = self._addResource(1, u'test_resource')
            group = self._addGroup()
            group.users.append(created_user)
            group_permission = GroupResourcePermission(
                                        perm_name=u'test_perm_BAD',
                                        group_name=u'group',
                                        resource_id = resource.resource_id
                                                       )
            resource.group_permissions.append(group_permission)
        
    def test_resources_with_group_permission(self):
        created_user = self._addUser()
        resource = self._addResource(1, u'test_resource')
        resource2 = self._addResource(2, u'test_resource2')
        self._addResource(3, u'test_resource3')
        group = self._addGroup()
        group.users.append(created_user)
        group_permission = GroupResourcePermission(
                                    perm_name=u'test_perm',
                                    group_name=u'group',
                                    resource_id = resource.resource_id
                                                   )
        group_permission2 = GroupResourcePermission(
                                    perm_name=u'foo_perm',
                                    group_name=u'group',
                                    resource_id = resource2.resource_id
                                                   )
        resource.group_permissions.append(group_permission)
        resource2.group_permissions.append(group_permission2)
        self.session.flush()
        resources = created_user.resources_with_perms([u'foo_perm'],
                                        db_session=self.session).all()
        self.assertEqual(resources[0],resource2)
        
    
    def __set_up_user_group_and_perms(self):
        created_user = self._addUser()
        created_user2 = self._addUser(user_name=u'foouser',email=u'new_email')
        created_user3 = self._addUser(user_name=u'foouser2',email=u'new_email2')
        created_user4 = self._addUser(user_name=u'foouser3',email=u'new_email3')
        resource = self._addResource(1, u'test_resource')
        group = self._addGroup()
        group2 = self._addGroup(group_name=u'group2')
        group.users.append(created_user)
        group2.users.append(created_user4)
        group_permission = GroupResourcePermission(
                                    perm_name=u'group_perm',
                                    group_name=u'group',
                                    resource_id = resource.resource_id
                                                   )
        group_permission2 = GroupResourcePermission(
                                    perm_name=u'group_perm',
                                    group_name=u'group2',
                                    resource_id = resource.resource_id
                                                   )
        user_permission = UserResourcePermission(
                                    perm_name=u'test_perm2',
                                    user_name=u'username',
                                    resource_id = resource.resource_id
                                                   )
        user_permission2 = UserResourcePermission(
                                    perm_name=u'foo_perm',
                                    user_name=u'username',
                                    resource_id = resource.resource_id
                                                   )
        user_permission3 = UserResourcePermission(
                                    perm_name=u'foo_perm',
                                    user_name=u'foouser',
                                    resource_id = resource.resource_id
                                                   )
        user_permission4 = UserResourcePermission(
                                    perm_name=u'test_perm',
                                    user_name=u'foouser2',
                                    resource_id = resource.resource_id
                                                   )
        resource.group_permissions.append(group_permission)
        resource.group_permissions.append(group_permission2)
        resource.user_permissions.append(user_permission)
        resource.user_permissions.append(user_permission2)
        resource.user_permissions.append(user_permission3)
        resource.user_permissions.append(user_permission4)
        self.session.flush()
        self.resource = resource
        self.user = created_user
        self.user2 = created_user2
        self.user3 = created_user3
        self.user4 = created_user4
    
    def test_resources_with_direct_user_perms(self):
        self.__set_up_user_group_and_perms()
        # test_perm1 from group perms should be ignored
        self.assertEqual(self.resource.direct_perms_for_user(self.user,
                                                    db_session=self.session),
                [(u'username', u'foo_perm'), (u'username', u'test_perm2')]
                         )

    def test_resources_with_direct_group_perms(self):
        self.__set_up_user_group_and_perms()
        # test_perm1 from group perms should be ignored
        self.assertEqual(self.resource.group_perms_for_user(self.user,
                                                db_session=self.session),
                [(u'group:group', u'group_perm')]
                )
        
    def test_resources_with_user_perms(self):
        self.__set_up_user_group_and_perms()
        self.assertEqual(
                sorted(self.resource.perms_for_user(self.user,
                                                    db_session=self.session)),
                sorted([(u'username', u'foo_perm'),
                        (u'group:group', u'group_perm'), 
                        (u'username', u'test_perm2')])
                        )
    
    def test_users_for_perm(self):
        self.__set_up_user_group_and_perms()
        self.assertEqual(
                sorted(self.resource.users_for_perm(u'foo_perm',
                                                    db_session=self.session)),
                sorted([(self.user, u'foo_perm',),(self.user2, u'foo_perm',)])
                        )

    def test_users_for_any_perm(self):
        self.__set_up_user_group_and_perms()
        self.assertEqual(
                sorted(self.resource.users_for_perm('__any_permission__',
                                                    db_session=self.session)),
                sorted([
                        (self.user, u'group_perm',),
                        (self.user, u'test_perm2',),
                        (self.user, u'foo_perm',),
                        (self.user2, u'foo_perm',),
                        (self.user3, u'test_perm',),
                        (self.user4, u'group_perm',),
                        ])
                )
        

class GroupTestCase(BaseTestCase):
    def test_add_group(self):
        group = Group(
            group_name=u'example group',
            description=u'example description'
        )
        self.session.add(group)
        self.session.flush()

        group = self.session.query(Group)
        group = group.filter(Group.group_name == u'example group').first()
        
        self.assertEqual(group.group_name, u'example group')
        self.assertEqual(group.description, u'example description')
        self.assertEqual(group.member_count, 0)

    def test_by_group_name(self):
        added_group = self._addGroup()
        queried_group = Group.by_group_name(u'group',
                                            db_session=self.session)

        self.assertEqual(added_group, queried_group)

    def test_by_group_name_wrong_groupname(self):
        self._addGroup()
        queried_group = Group.by_group_name(u'not existing group',
                                            db_session=self.session)

        self.assertEqual(queried_group, None)

    def test_users(self):
        user1 = self._addUser(u'user1', u'email1')
        user2 = self._addUser(u'user2', u'email2')

        group = self._addGroup()
        group.users.append(user1)
        group.users.append(user2)

        self.assertEqual(group.users[0], user1)
        self.assertEqual(group.users[1], user2)


    def test_users_dynamic(self):
        user1 = self._addUser(u'user1', u'email1')
        user2 = self._addUser(u'user2', u'email2')

        group = self._addGroup()
        group.users.append(user1)
        group.users.append(user2)
        group_users = group.users_dynamic.all()

        self.assertEqual(group_users[0], user1)
        self.assertEqual(group_users[1], user2)

    def test_all(self):
        group1 = self._addGroup(u'group1')
        group2 = self._addGroup(u'group2')

        all_groups = Group.all(db_session=self.session).all()

        self.assertEqual(len(all_groups), 2)
        self.assertEqual(all_groups[0], group1)
        self.assertEqual(all_groups[1], group2)

if __name__ == '__main__':
    unittest.main()  # pragma: nocover
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.