Changeset 744:3a967f693570 in hatta-dev


Ignore:
Timestamp:
01/24/10 15:19:13 (2 years ago)
Author:
sheep@…
Branch:
default
Message:

implemet the subdirectory storage, more tests

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • hatta.py

    r743 r744  
    682682    """ 
    683683 
    684     # XXX Override required methods here. 
     684    periods_re = re.compile(r'^[.]|(?<=/)[.]') 
     685    slashes_re = re.compile(r'^[/]|(?<=/)[/]') 
     686 
     687    def _title_to_file(self, title): 
     688        """Modified escaping to allow slashes and spaces.""" 
     689 
     690        title = unicode(title).strip() 
     691        escaped = werkzeug.url_quote(title, safe='/ ') 
     692        path = os.path.join(self.repo_prefix, escaped) 
     693        path = self.periods_re.sub('%2E', path) 
     694        path = self.slashes_re.sub('%2F', path) 
     695        return path 
     696 
     697    def save_file(self, title, file_name, author=u'', comment=u'', parent=None): 
     698        """Make the subdirectories if needed.""" 
     699 
     700        file_path = self._file_path(title) 
     701        self._check_path(file_path) 
     702        dir_path = os.path.dirname(file_path) 
     703        try: 
     704            os.makedirs(dir_path) 
     705        except OSError: 
     706            pass 
     707        super(WikiSubdirectoryStorage, self).save_file(title, file_name, 
     708                                                       author, comment, parent) 
    685709 
    686710 
     
    19331957            comment = _(u'created') 
    19341958            rev = -1 
    1935         except werkzeug.exceptions.Forbidden: 
     1959        except werkzeug.exceptions.Forbidden, e: 
    19361960            yield werkzeug.html.p( 
    1937                 werkzeug.html(_(u"Can't edit symbolic links or directories"))) 
     1961                werkzeug.html(_(unicode(e)))) 
    19381962            return 
    19391963        if preview: 
  • tests/test_repo.py

    r741 r744  
    6868    comment = u'test comment' 
    6969 
    70     @py.test.mark.xfail 
    71     def test_filename(self, repo): 
     70    title_encodings = { 
     71        u'test title': 'test title', 
     72        u'.test title': '%2Etest title', 
     73        u'../test title': '%2E./test title', 
     74        u'test/./title': 'test/%2E/title', 
     75        u'test/../title': 'test/%2E./title', 
     76        u'test//title': 'test/%2Ftitle', 
     77        u'/test/title': '%2Ftest/title', 
     78    } 
     79 
     80 
     81    def test_title_to_file(self, subdir_repo): 
     82        for title, filename in self.title_encodings.iteritems(): 
     83            escaped = subdir_repo._title_to_file(title) 
     84            assert escaped == filename 
     85 
     86    def test_filename(self, subdir_repo): 
    7287        """ 
    7388        Check if the page's file is named properly. 
    7489        """ 
    7590 
    76         title = u'some/page.txt' 
    77         filename = 'some/page.txt' 
    78         filepath = os.path.join(repo.path, filename) 
    79         repo.save_text(title, self.text, self.author, self.comment, parent=-1) 
    80         exists = os.path.exists(filepath) 
    81         assert exists 
    82  
    83     # XXX Put your tests here. 
     91        for title, filename in self.title_encodings.iteritems(): 
     92            filepath = os.path.join(subdir_repo.path, filename) 
     93            subdir_repo.save_text(title, self.text, self.author, self.comment, 
     94                                  parent=-1) 
     95            exists = os.path.exists(filepath) 
     96            assert exists 
     97 
    8498 
    8599class TestMercurialStorage(object): 
     
    105119        exists = os.path.exists(filepath) 
    106120        assert exists 
     121 
     122    def test_check_path(self, repo): 
     123        py.test.raises(werkzeug.exceptions.Forbidden, repo._check_path, "/") 
     124        py.test.raises(werkzeug.exceptions.Forbidden, repo._check_path, "..") 
     125        py.test.raises(werkzeug.exceptions.Forbidden, repo._check_path, 
     126                       repo.path+"/..") 
     127        path = os.path.join(repo.path, 'aaa') 
     128        os.symlink('/', path) 
     129        py.test.raises(werkzeug.exceptions.Forbidden, repo._check_path, path) 
     130        path = os.path.join(repo.path, 'bbb') 
     131        os.mkdir(path) 
     132        py.test.raises(werkzeug.exceptions.Forbidden, repo._check_path, path) 
    107133 
    108134    @py.test.mark.skipif("sys.platform == 'win32'") 
Note: See TracChangeset for help on using the changeset viewer.