# Path: zengine/lib/test_utils.py
# class BaseTestCase:
#     """
#     Base test case.
#     """
#     client = None
# 
#     def setup_method(self, method):
#         """
#         Creates a new user and Role with all Permissions.
#         """
# 
#         # if not '--ignore=fixture' in sys.argv:
#         #     if hasattr(self, 'fixture'):
#         #         print("\nREPORT:: Running test cases own fixture() method")
#         #         self.fixture()
#         #         sleep(2)
#         #
#         #     else:
#         #         fixture_guess = 'fixtures/%s.csv' % method.__self__.__module__.split('.test_')[1]
#         #         if os.path.exists(fixture_guess) and fixture_guess not in sys.LOADED_FIXTURES:
#         #             sys.LOADED_FIXTURES.append(fixture_guess)
#         #             FlushDB(model='all', wait_sync=True,
#         #                     exclude=settings.TEST_FLUSHING_EXCLUDES).run()
#         #             print("\nREPORT:: Test fixture will be loaded: %s" % fixture_guess)
#         #             LoadData(path=fixture_guess, update=True).run()
#         #             sleep(2)
#         #         else:
#         #             print(
#         #                 "\nREPORT:: Test case does not have a fixture file like %s" % fixture_guess)
#         #
#         # else:
#         #     print("\nREPORT:: Fixture loading disabled by user. (by --ignore=fixture)")
#         # clear all caches
#         if not hasattr(sys, 'cache_cleared'):
#             sys.cache_cleared = True
#             print(ClearCache.flush())
#             print("\nREPORT:: Cache cleared")
# 
#     @classmethod
#     def prepare_client(cls, path='', reset=False, user=None, login=None, token='', username=None):
#         """
#         Setups the path, logs in if necessary
# 
#         Args:
#             path: change or set path
#             reset: Create a new client
#             login: Login to system
#             token: Set token
#         """
# 
#         if not cls.client or reset or user:
#             cls.client = TestClient(path)
#             login = True if login is None else login
# 
#         if username:
#             cls.client.username = username
# 
#         if user:
#             cls.client.user = user
#             login = True if login is None else login
# 
#         if login:
#             cls._do_login()
# 
#         cls.client.set_path(path, token)
# 
#     @classmethod
#     def _do_login(self):
#         """
#         logs in the "test_user"
#         """
#         self.client.sess_id = uuid4().hex
#         self.client.set_path("/login/")
#         resp = self.client.post()
#         assert resp.json['forms']['schema']['title'] == 'LoginForm'
#         req_fields = resp.json['forms']['schema']['required']
#         assert all([(field in req_fields) for field in ('username', 'password')])
#         resp = self.client.post(username=self.client.username or self.client.user.username,
#                                 password="123", cmd="do")
#         log.debug("login result :\n%s" % resp.json)
#         assert resp.json['cmd'] == 'upgrade'
# 
#     def get_user_token(self, username):
#         user = User.objects.get(username=username)
#         token = self.client.current.token
#         return token, user


from zengine.lib.test_utils import BaseTestCase
    def test_check_user_task_rerun(self):

        for i in range(3):
            # If pre-task is user task and there is no wf_meta, engine should rerun this user task.
            self.prepare_client('/check_user_task_rerun', username='super_user')
            resp = self.client.post(cmd=i)
            token = resp.token
            assert resp.json['task_name'] == 'user_task_a'
            self.prepare_client('/check_user_task_rerun', username='super_user', token=token)
            resp = self.client.post(wf_meta=False)
            assert resp.json['task_name'] == 'user_task_a'

        for i in range(3, 5):
            self.prepare_client('/check_user_task_rerun', username='super_user')
            resp = self.client.post(cmd=i)
            token = resp.token
            assert resp.json['task_name'] == 'user_task_b'
            resp = self.client.post()
            assert resp.json['task_name'] == 'user_task_a'
            self.prepare_client('/check_user_task_rerun', username='super_user', token=token)
            if i == 3:
                # User Task which is before than EndEvent test.
                # If user task is before than EndEvent, engine should ignore rerun and finish wf.
                self.client.post()
                assert self.client.current.wf_cache.wf_state['finished'] == True
            else:
                # First lane ends with user task test.
                # Generally this user task is message box and doesn't have buttons. If there is
                # lane change and lane ends with user task, engine should ignore rerun and
                # continue to wf execution.

# Path: zengine/lib/test_utils.py
# class BaseTestCase:
#     """
#     Base test case.
#     """
#     client = None
# 
#     def setup_method(self, method):
#         """
#         Creates a new user and Role with all Permissions.
#         """
# 
#         # if not '--ignore=fixture' in sys.argv:
#         #     if hasattr(self, 'fixture'):
#         #         print("\nREPORT:: Running test cases own fixture() method")
#         #         self.fixture()
#         #         sleep(2)
#         #
#         #     else:
#         #         fixture_guess = 'fixtures/%s.csv' % method.__self__.__module__.split('.test_')[1]
#         #         if os.path.exists(fixture_guess) and fixture_guess not in sys.LOADED_FIXTURES:
#         #             sys.LOADED_FIXTURES.append(fixture_guess)
#         #             FlushDB(model='all', wait_sync=True,
#         #                     exclude=settings.TEST_FLUSHING_EXCLUDES).run()
#         #             print("\nREPORT:: Test fixture will be loaded: %s" % fixture_guess)
#         #             LoadData(path=fixture_guess, update=True).run()
#         #             sleep(2)
#         #         else:
#         #             print(
#         #                 "\nREPORT:: Test case does not have a fixture file like %s" % fixture_guess)
#         #
#         # else:
#         #     print("\nREPORT:: Fixture loading disabled by user. (by --ignore=fixture)")
#         # clear all caches
#         if not hasattr(sys, 'cache_cleared'):
#             sys.cache_cleared = True
#             print(ClearCache.flush())
#             print("\nREPORT:: Cache cleared")
# 
#     @classmethod
#     def prepare_client(cls, path='', reset=False, user=None, login=None, token='', username=None):
#         """
#         Setups the path, logs in if necessary
# 
#         Args:
#             path: change or set path
#             reset: Create a new client
#             login: Login to system
#             token: Set token
#         """
# 
#         if not cls.client or reset or user:
#             cls.client = TestClient(path)
#             login = True if login is None else login
# 
#         if username:
#             cls.client.username = username
# 
#         if user:
#             cls.client.user = user
#             login = True if login is None else login
# 
#         if login:
#             cls._do_login()
# 
#         cls.client.set_path(path, token)
# 
#     @classmethod
#     def _do_login(self):
#         """
#         logs in the "test_user"
#         """
#         self.client.sess_id = uuid4().hex
#         self.client.set_path("/login/")
#         resp = self.client.post()
#         assert resp.json['forms']['schema']['title'] == 'LoginForm'
#         req_fields = resp.json['forms']['schema']['required']
#         assert all([(field in req_fields) for field in ('username', 'password')])
#         resp = self.client.post(username=self.client.username or self.client.user.username,
#                                 password="123", cmd="do")
#         log.debug("login result :\n%s" % resp.json)
#         assert resp.json['cmd'] == 'upgrade'
# 
#     def get_user_token(self, username):
#         user = User.objects.get(username=username)
#         token = self.client.current.token
#         return token, user


# Path: tests/test_check_user_task_rerun.py
from zengine.lib.test_utils import BaseTestCase

    def test_check_user_task_rerun(self):

        for i in range(3):
            # If pre-task is user task and there is no wf_meta, engine should rerun this user task.
            self.prepare_client('/check_user_task_rerun', username='super_user')
            resp = self.client.post(cmd=i)
            token = resp.token
            assert resp.json['task_name'] == 'user_task_a'
            self.prepare_client('/check_user_task_rerun', username='super_user', token=token)
            resp = self.client.post(wf_meta=False)
            assert resp.json['task_name'] == 'user_task_a'

        for i in range(3, 5):
            self.prepare_client('/check_user_task_rerun', username='super_user')
            resp = self.client.post(cmd=i)
            token = resp.token
            assert resp.json['task_name'] == 'user_task_b'
            resp = self.client.post()
            assert resp.json['task_name'] == 'user_task_a'
            self.prepare_client('/check_user_task_rerun', username='super_user', token=token)
            if i == 3:
                # User Task which is before than EndEvent test.
                # If user task is before than EndEvent, engine should ignore rerun and finish wf.
                self.client.post()
                assert self.client.current.wf_cache.wf_state['finished'] == True
            else:
                # First lane ends with user task test.
                # Generally this user task is message box and doesn't have buttons. If there is
                # lane change and lane ends with user task, engine should ignore rerun and
                # continue to wf execution.

                resp = self.client.post()