#!/www/python/bin/python
"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/test_stored_file.py $
$Id: test_stored_file.py 23441 2004-02-05 22:00:46Z dbinger $

Test script for the StoredFile and related classes.
"""

import os, shutil
from mx.DateTime import now
from sancho.unittest import TestScenario, parse_args, run_scenarios
from dulcinea.stored_file import StoredFile, path_check, FileDatabase
from dulcinea.user import DulcineaUser

tested_modules = ["dulcinea.stored_file"]

TEST_ROOT_DIR = '/tmp/storedfiletest'
TEST_CONTENT = "Contents of a file go here."

class StoredFileTest (TestScenario):

    def setup (self):
        if os.path.exists(TEST_ROOT_DIR):
            shutil.rmtree(TEST_ROOT_DIR)
        os.mkdir(TEST_ROOT_DIR)
        self.user = DulcineaUser('a')
        self.content_filename = os.path.join(TEST_ROOT_DIR, 'test_content')
        content = open(self.content_filename, 'w')
        content.write(TEST_CONTENT)
        content.close()

    def shutdown (self):
        shutil.rmtree(TEST_ROOT_DIR)
        del self.content_filename

    def check_file (self):
        self.test_stmt("self.file_db = FileDatabase(root_directory=%r)" %
                       TEST_ROOT_DIR)
        self.test_exc(
            'sf = self.file_db.new_file(source_path=self.content_filename,'
            'allowed_mime_types=[1])', TypeError)
        self.test_stmt(
            'sf = self.file_db.new_file(source_path=self.content_filename)')

        self.test_stmt('self.f = sf.open("rt")')
        self.test_val('self.f.read()', TEST_CONTENT)
        self.f.close()

        self.test_stmt('self.f = sf.open("wb")')
        self.content = 'New binary content goes here...\n'
        self.test_stmt("self.f.write(self.content)")
        self.f.close()
        self.test_val('sf.get_size()', len(self.content))
        self.test_stmt('self.f = sf.open("r")')
        self.test_val('self.f.read()', self.content)
        self.f.close()
        self.test_exc('sf.set_mime_type("textplain")', ValueError)
        self.test_stmt('sf.set_mime_type("text/plain")')
        self.test_val('sf.guess_mime_type()', 'text/plain')
        self.test_stmt('sf.MIME_TYPES = []')
        self.test_val('sf.guess_mime_type()', 'application/octet-stream')
        self.test_val('sf.get_mime_type()', 'text/plain')
        self.test_stmt('self.file_db.get_file(sf.path)==sf')
        self.test_stmt('sf.set_filename("foo")')
        self.test_val('sf.get_filename()', 'foo')
        self.test_stmt('sf.set_description("boo")')
        self.test_val('sf.get_description()', 'boo')
        u = DulcineaUser()
        self.test_stmt('sf.set_owner(u)')
        self.test_val('sf.get_owner()', u)
        self.test_true('sf.has_manage_access(u)')
        adate = now()
        self.test_stmt('sf.set_date(adate)')
        self.test_val('sf.get_date()', adate)

        self.test_stmt('sf.chgrp("mxweb")')
        self.test_stmt('sf.chgrp(4130)')
        self.test_stmt('self.file_db.remove_file(sf, unlink=True)')
        self.test_false('os.path.exists(sf.get_full_path())')
        self.test_val('sf.get_size()', None)
        self.test_stmt('repr(sf)')
        self.test_stmt('sf.get_path()')


    def check_path_check (self):
        self.test_val('path_check("Abc")', 'abc')
        self.test_val('path_check("abc/../../../etc/passwd")', None)
        self.test_val('path_check("/special/characters/-_@.cif")',
                      "/special/characters/-_@.cif")
        self.test_val('path_check("/illegal/characters\001/")', None)


if __name__ == "__main__":
    (scenarios, options) = parse_args()
    run_scenarios(scenarios, options)
