#coding: UTF-8
"""
Test file/dir operations.
"""

import random
import re
from urllib import urlencode, quote

from tests.common.utils import randstring, urljoin
from tests.api.urls import DEFAULT_REPO_URL, REPOS_URL
from tests.api.apitestbase import ApiTestBase, USERNAME

class FilesApiTest(ApiTestBase):
    def test_rename_file(self):
        with self.get_tmp_repo() as repo:
            name, furl = self.create_file(repo)
            data = {
                'operation': 'rename',
                'newname': name + randstring(),
            }
            res = self.post(furl, data=data)
            self.assertRegexpMatches(res.text, r'"http(.*)"')

    def test_remove_file(self):
        with self.get_tmp_repo() as repo:
            _, furl = self.create_file(repo)
            res = self.delete(furl)
            self.assertEqual(res.text, '"success"')

    def test_move_file(self):
        with self.get_tmp_repo() as repo:
            _, furl = self.create_file(repo)
            # TODO: create another repo here, and use it as dst_repo
            data = {
                'operation': 'move',
                'dst_repo': repo.repo_id,
                'dst_dir': '/',
            }
            res = self.post(furl, data=data)
            self.assertEqual(res.text, '"success"')

    def test_copy_file(self):
        with self.get_tmp_repo() as repo:
            fname, _ = self.create_file(repo)
            # TODO: create another repo here, and use it as dst_repo
            dpath, _ = self.create_dir(repo)
            fopurl = urljoin(repo.repo_url, 'fileops/copy/') + '?p=/'
            data = {
                'file_names': fname,