Ejemplo n.º 1
0
    def test_many_to_one_relationship(self):
        self.init_test_role_table()
        test_user1 = User(username=u'tester1', email='*****@*****.**', password='******',
                         role_id=Role.query.filter_by(name=u"Superuser").first().id)
        test_user1.save()

        self.assertEqual(1, test_user1.role.level)
Ejemplo n.º 2
0
 def test_permission_of_someone_to_edit_self_profile(self):
     # a superuser
     test_normal_user = User(username=u'normal_user', email='*****@*****.**', password='******',
                      role_id=Role.query.filter_by(name=u"auth").first().id, confirmed=True)
     test_normal_user.save()
     login_user(test_normal_user)
     identity_changed.send(self.app, identity=Identity(test_normal_user.id))
     self.assertTrue(self.edit_user_profile_route(id=test_normal_user.id))
Ejemplo n.º 3
0
    def init_all_user_tables_with_auth_user(self):
        try:
            self.init_test_role_table()
            self.init_test_code_table()
            self.init_test_code_to_role_table()

            test_auth_user = User(username=u'tester', email='*****@*****.**', password='******',
                        confirmed=True, role_id=Role.query.filter_by(name=u"Auth").first().id)
            test_auth_user.save()

            yield
        finally:
            pass
Ejemplo n.º 4
0
 def init_test_user(self):
     self.test_user = User(username=u'user',
                           email='*****@*****.**',
                           password='******',
                           confirmed=True,
                           )
     self.test_user.save()
Ejemplo n.º 5
0
    def test_find_all_users_of_a_role(self):
        self.init_test_role_table()
        test_user1 = User(username=u'tester1', email='*****@*****.**', password='******',
                         role_id=Role.query.filter_by(name=u"Superuser").first().id)
        test_user1.save()

        test_user2 = User(username=u'tester2', email='*****@*****.**', password='******',
                         role_id=Role.query.filter_by(name=u"Superuser").first().id)
        test_user2.save()

        self.assertEqual(Role.query.filter_by(name=u"Superuser").first().users[0].username, u'tester1')
Ejemplo n.º 6
0
 def test_permission_of_superuser_to_edit_profile(self):
     """Confirm superuser can edit anyone's profile"""
     one_user = User(username=u'normal_user', email='*****@*****.**', password='******',
                       role_id=Role.query.filter_by(name=u"auth").first().id, confirmed=True)
     test_superuser = User(username=u'superuser', email='*****@*****.**', password='******',
                      role_id=Role.query.filter_by(name=u"Superuser").first().id, confirmed=True)
     one_user.save()
     test_superuser.save()
     login_user(test_superuser)
     identity_changed.send(self.app, identity=Identity(test_superuser.id))
     try:
         self.assertTrue(self.edit_user_profile_route(id=one_user.id))
     except HTTPException, e:
         self.assertFalse(True, "Catch an Error, superuser has no Permission!")
Ejemplo n.º 7
0
    def test_normal_user_no_permission_to_edit_profile_throws_HTTPException(self):
        # a superuser
        one_user = User(username=u'normal_user', email='*****@*****.**', password='******',
                          role_id=Role.query.filter_by(name=u"auth").first().id, confirmed=True)

        test_normal_user = User(username=u'test_normal_user', email='*****@*****.**',
                                password='******',
                                role_id=Role.query.filter_by(name=u"auth").first().id,
                                confirmed=True)
        one_user.save()
        test_normal_user.save()
        login_user(test_normal_user)
        identity_changed.send(self.app, identity=Identity(test_normal_user.id))
        self.assertRaises(HTTPException, self.edit_user_profile_route, id=one_user.id)
Ejemplo n.º 8
0
    def test_one_to_many_relationship(self):
        self.init_test_role_table()
        test_user1 = User(username=u'tester1', email='*****@*****.**', password='******',
                         role_id=Role.query.filter_by(name=u"Superuser").first().id)
        test_user1.save()

        test_user2 = User(username=u'tester2', email='*****@*****.**', password='******',
                         role_id=Role.query.filter_by(name=u"Superuser").first().id)
        test_user2.save()

        users = Role.query.filter_by(name=u"Superuser").first().users
        username_list = list()
        for user in users:
            username_list.append(user.username)
        self.assertIn(u'tester2', username_list)
Ejemplo n.º 9
0
 def test_check_user_is_moderator(self):
     self.init_test_role_table()
     test_user = User(username=u'tester', email='*****@*****.**', password='******',
                      role_id=Role.query.filter_by(name=u"Moderator").first().id)
     test_user.save()
     self.assertTrue(test_user.is_moderator)
Ejemplo n.º 10
0
 def test_save_existed_email_fail(self):
     test_user = User(username=u'tester', email='*****@*****.**', password='******')
     self.assertFalse(test_user.save())
Ejemplo n.º 11
0
class BaseTestCase(TestCase):
    def create_app(self):
        test_app = create_app('test')
        test_app.config['TESTING'] = True
        return test_app

    def setUp(self):
        db.create_all()
        self.init_test_user()

    def tearDown(self):
        db.session.remove()
        db.drop_all()

    def init_test_user(self):
        self.test_user = User(username=u'user',
                              email='*****@*****.**',
                              password='******',
                              confirmed=True,
                              )
        self.test_user.save()

    # init test tables
    def init_test_role_table(self):
        Role.init_data()
        self.assertIsNotNone(Role.query.filter_by(name=u"Auth").first())
        self.assertIsNotNone(Role.query.filter_by(name=u"Moderator").first())
        self.assertIsNotNone(Role.query.filter_by(name=u"Admin").first())
        self.assertIsNotNone(Role.query.filter_by(name=u"Superuser").first())

    def init_test_code_table(self):
        PermissionCode.init_data()
        self.assertIsNotNone(PermissionCode.query.filter_by(code=u'100').first())
        self.assertIsNotNone(PermissionCode.query.filter_by(code=u'300').first())

    def init_test_code_to_role_table(self):
        CodeToRole.init_data()
        self.assertIsNotNone(CodeToRole.query.filter_by(permission_code=u'101').first())
        self.assertIsNotNone(CodeToRole.query.filter_by(permission_code=u'341').first())

    # context
    @contextmanager
    def init_all_user_tables_with_auth_user(self):
        try:
            self.init_test_role_table()
            self.init_test_code_table()
            self.init_test_code_to_role_table()

            test_auth_user = User(username=u'tester', email='*****@*****.**', password='******',
                        confirmed=True, role_id=Role.query.filter_by(name=u"Auth").first().id)
            test_auth_user.save()

            yield
        finally:
            pass

    @contextmanager
    def fake_test(self):
        try:
            self.app.config['IN_FAKE_TEST'] = True
            yield
        finally:
            self.app.config['IN_FAKE_TEST'] = False