# Path: nappingcat/response.py
# class Response(object):
#     def __init__(self, status_code, content):
#         self.status_code, self.content = status_code, content
# 
#     def __str__(self):
#         return simplejson.dumps({"status_code":self.status_code, "content":self.content}) 
# 
# Path: nappingcat/request.py
# class Request(object):
#     def __init__(self, user, command, settings, streams, root_patterns):
#         self.user = user
#         self.command = command
#         self.settings = settings
#         self.stdin, self.stdout, self.stderr = streams
#         self.root_patterns = root_patterns 
#         self.auth_backend = auth.get_auth_backend_from_settings(settings)
# 
# Path: nappingcat/exceptions.py
# class NappingCatException(Exception):
#     pass
# 
# Path: nappingcat/contrib/git/handlers.py
# def get_settings(request):
# def delete_repo(request, repo_name):
# def list_repos(request, username):
# def fork_repo(request, repo):
# def create_repo(request, repo_name, template_dir=None):
# def handle_git(request, action, permission_prefix='kittygit'):
# 
# Path: nappingcat/contrib/git/operations.py
# def fork_repository(git, stdin, stdout, stderr, from_directory, to_directory):
# def create_repository(git, stdin, stdout, stderr, directory, template_dir=None, bare=True):
# def git_shell(git, stdin, stdout, stderr, action, directory):
# 
# Path: nappingcat/contrib/git/utils.py
# DEFAULT_USER_NAME = '<nappingcat-user>'
# def get_full_repo_dir(settings, user, repo):
# def get_clone_base_url(settings):
# 
# Path: nappingcat/contrib/git/exceptions.py
# class KittyGitUnauthorized(NappingCatRejected):
#     pass
# 
# class KittyGitBadParameter(NappingCatRejected):
#     pass


from unittest import TestCase
from nappingcat.response import Response 
from nappingcat.request import Request
from nappingcat.exceptions import NappingCatException
from nappingcat.contrib.git import handlers
from nappingcat.contrib.git import operations
from nappingcat.contrib.git import utils 
from nappingcat.contrib.git.exceptions import KittyGitUnauthorized, KittyGitBadParameter
from tests.gittests.test_utils import fake_settings 
import random
import os
import shutil
import sys
import subprocess
import mox
        import time; time.sleep(1)      # pass the time for a bit for things to 
                request = Request(other_user, 'kitty-git fork', test_settings, streams, random_root_patterns)
                expected_dir = os.path.expanduser('%s/%s/%s.git' % (test_dir, other_user, repo_name))
                result = handlers.fork_repo(request, full_repo_name)

        self.assertTrue(isinstance(result, Response))
        self.assertTrue(repo_name in result.content['message'])
        self.assertTrue(os.path.isdir(directory))
        self.assertTrue(os.path.isdir(expected_dir))

    def test_operation_failed(self):
        test_dir = self.test_dir
        settings_str = """
[kittyconfig]
auth = tests.gittests.test_utils.AllAuth
[kittygit]
repo_dir = %s 
        """.strip() % test_dir
        test_settings = fake_settings(settings_str)

        repo_name = 'something-%d' % random.randint(0, 100)
        full_repo_name = 'random-user-%d/%s' % (random.randint(0, 100), repo_name)
        directory = '%s/%s.git' % (test_dir, full_repo_name)
        other_user = 'random-user-%d' % random.randint(0, 100)
        with open('/dev/null', 'w') as out_stream:
            with open('/dev/null', 'w') as err_stream:
                streams = (sys.stdin, out_stream, err_stream)
                random_root_patterns = random.randint(1, 100)
                request = Request(other_user, 'kitty-git fork', test_settings, streams, random_root_patterns)
                expected_dir = os.path.expanduser('%s/%s/%s.git' % (test_dir, other_user, repo_name))


# Path: nappingcat/response.py
# class Response(object):
#     def __init__(self, status_code, content):
#         self.status_code, self.content = status_code, content
# 
#     def __str__(self):
#         return simplejson.dumps({"status_code":self.status_code, "content":self.content}) 
# 
# Path: nappingcat/request.py
# class Request(object):
#     def __init__(self, user, command, settings, streams, root_patterns):
#         self.user = user
#         self.command = command
#         self.settings = settings
#         self.stdin, self.stdout, self.stderr = streams
#         self.root_patterns = root_patterns 
#         self.auth_backend = auth.get_auth_backend_from_settings(settings)
# 
# Path: nappingcat/exceptions.py
# class NappingCatException(Exception):
#     pass
# 
# Path: nappingcat/contrib/git/handlers.py
# def get_settings(request):
# def delete_repo(request, repo_name):
# def list_repos(request, username):
# def fork_repo(request, repo):
# def create_repo(request, repo_name, template_dir=None):
# def handle_git(request, action, permission_prefix='kittygit'):
# 
# Path: nappingcat/contrib/git/operations.py
# def fork_repository(git, stdin, stdout, stderr, from_directory, to_directory):
# def create_repository(git, stdin, stdout, stderr, directory, template_dir=None, bare=True):
# def git_shell(git, stdin, stdout, stderr, action, directory):
# 
# Path: nappingcat/contrib/git/utils.py
# DEFAULT_USER_NAME = '<nappingcat-user>'
# def get_full_repo_dir(settings, user, repo):
# def get_clone_base_url(settings):
# 
# Path: nappingcat/contrib/git/exceptions.py
# class KittyGitUnauthorized(NappingCatRejected):
#     pass
# 
# class KittyGitBadParameter(NappingCatRejected):
#     pass


# Path: tests/gittests/handlers.py
from unittest import TestCase
from nappingcat.response import Response 
from nappingcat.request import Request
from nappingcat.exceptions import NappingCatException
from nappingcat.contrib.git import handlers
from nappingcat.contrib.git import operations
from nappingcat.contrib.git import utils 
from nappingcat.contrib.git.exceptions import KittyGitUnauthorized, KittyGitBadParameter
from tests.gittests.test_utils import fake_settings 
import random
import os
import shutil
import sys
import subprocess
import mox
        import time; time.sleep(1)      # pass the time for a bit for things to 

                request = Request(other_user, 'kitty-git fork', test_settings, streams, random_root_patterns)
                expected_dir = os.path.expanduser('%s/%s/%s.git' % (test_dir, other_user, repo_name))
                result = handlers.fork_repo(request, full_repo_name)

        self.assertTrue(isinstance(result, Response))
        self.assertTrue(repo_name in result.content['message'])
        self.assertTrue(os.path.isdir(directory))
        self.assertTrue(os.path.isdir(expected_dir))

    def test_operation_failed(self):
        test_dir = self.test_dir
        settings_str = """
[kittyconfig]
auth = tests.gittests.test_utils.AllAuth
[kittygit]
repo_dir = %s 
        """.strip() % test_dir
        test_settings = fake_settings(settings_str)

        repo_name = 'something-%d' % random.randint(0, 100)
        full_repo_name = 'random-user-%d/%s' % (random.randint(0, 100), repo_name)
        directory = '%s/%s.git' % (test_dir, full_repo_name)
        other_user = 'random-user-%d' % random.randint(0, 100)
        with open('/dev/null', 'w') as out_stream:
            with open('/dev/null', 'w') as err_stream:
                streams = (sys.stdin, out_stream, err_stream)
                random_root_patterns = random.randint(1, 100)
                request = Request(other_user, 'kitty-git fork', test_settings, streams, random_root_patterns)
                expected_dir = os.path.expanduser('%s/%s/%s.git' % (test_dir, other_user, repo_name))


                self.assertRaises(NappingCatException, handlers.fork_repo, request, full_repo_name)