class SesssonStoreTest(TestCase):
    def setUp(self):
        # create session and key
        self.session = SessionStore()
        self.session.create()
        self.session_key = self.session.session_key

    def test_flush(self):
        # check that folder and key exists
        self.assertTrue(os.path.exists(os.path.join(settings.UPLOADED_IMAGES_ROOT, self.session_key)))
        Session.objects.get(session_key=self.session_key)

        # flush the session
        self.session.flush()

        # check that session doesnt exist and folder is deleted
        with self.assertRaises(Session.DoesNotExist):
            Session.objects.get(session_key=self.session_key)
        self.assertFalse(os.path.exists(os.path.join(settings.UPLOADED_IMAGES_ROOT, self.session_key)))

    def test_create(self):
        self.assertTrue(self.session.exists(self.session_key))
        root = os.path.join(settings.UPLOADED_IMAGES_ROOT, self.session_key)
        self.assertTrue(os.path.exists(root))
        self.assertTrue(os.path.exists(os.path.join(root, "undo")))
        self.assertTrue(os.path.exists(os.path.join(root, "redo")))

    def tearDown(self):
        if self.session.exists(self.session_key):
            self.session.flush()
class TestSessionClean(TestCase):
    def setUp(self):
        self.s = SessionStore()

    def test_clear_inactive(self):
        # set expiry to 1 second
        self.s.set_expiry(1)
        # create session
        self.s.create()
        # wait 3 seconds
        time.sleep(3)
        # call the cleaner with False testing argument as, with testing=True, function returns instantly
        session_cleaner.session_clean(False)
        # check expired session is cleared
        self.assertTrue([s for s in Session.objects.get_queryset()] == [])

    def test_max_time(self):
        # set start to 0, should be expired
        self.s["start"] = 0
        # set to high value, shouldn't be cleared from activity expiration
        self.s.set_expiry(1000)
        self.s.create()

        # call the cleaner with False testing argument as, with testing=True, function returns instantly
        session_cleaner.session_clean(False)

        # check session deleted
        self.assertTrue([s for s in Session.objects.get_queryset()] == [])

    def test_no_session_key(self):
        # make a folder in uploaded images directory
        path = os.path.join(settings.UPLOADED_IMAGES_ROOT, "test_should_be_deleted")
        os.mkdir(path)

        # call the cleaner with False testing argument as, with testing=True, function returns instantly
        session_cleaner.session_clean(False)

        # check folder is deleted
        self.assertFalse(os.path.exists(path))

    def test_no_folder(self):
        # create session
        self.s["start"] = time.time()
        self.s.set_expiry(1000)
        self.s.create()

        # remove folder
        shutil.rmtree(os.path.join(settings.UPLOADED_IMAGES_ROOT, self.s.session_key))

        # call the cleaner with False testing argument as, with testing=True, function returns instantly
        session_cleaner.session_clean(False)

        # check session is cleared
        self.assertTrue([s for s in Session.objects.get_queryset()] == [])

    def test_valid(self):
        # no expiry, with folder, nothing should be removed
        self.s["start"] = time.time()
        self.s.set_expiry(1000)
        self.s.create()

        # call the cleaner with False testing argument as, with testing=True, function returns instantly
        session_cleaner.session_clean(False)

        # check sessions in database is a list with the session key
        self.assertTrue([s.session_key for s in Session.objects.get_queryset()] == [self.s.session_key])

    def tearDown(self):
        if self.s.exists(self.s.session_key):
            self.s.flush()