from __future__ import unicode_literals
import unittest
from user.userdecorators import has_permissions
from  config import noreply_email
from web.webapi import SeeOther
import web
from user.model import User, Role, Action

class TestUserDecorators(unittest.TestCase):
    def setUp(self):
        self.anonymous_user = web.ctx.orm.query(User).filter_by(name='anonymous').one()
        self.test_role = Role('test_role', [self.anonymous_user])
        self.test_action = Action('test_action', [self.test_role])
        web.ctx.orm.add(self.test_role)
        web.ctx.orm.add(self.test_action)

    def tearDown(self):
        web.ctx.orm.delete(self.test_role)
        web.ctx.orm.delete(self.test_action)

    def test_has_permissions(self):
        self.check_perms()

        self.test_role.users = []
        web.ctx.orm.commit()
        try:
            self.check_perms()
            raise Exception
        except SeeOther:
            pass

    @has_permissions('test_action')
    def check_perms(self):
        pass

def suite():
   suite = unittest.TestSuite()
   suite.addTest(unittest.makeSuite(TestUserDecorators))
   return suite
