<commit_before>from unittest.mock import call, patch
import getpass
import json
import os
import pytest
import responses
import subprocess
import tempfile
from textwrap import dedent
from urllib.parse import urlencode

import new_django_project_in_virtualenv
from new_django_project_in_virtualenv import (
    API_ENDPOINT,
    PYTHON_VERSIONS,
    create_virtualenv,
    create_webapp,
    main,
    start_django_project,
    update_settings_file,
    update_wsgi_file,
    reload_webapp,
)


class TestMain:

    def test_calls_create_virtualenv(self, mock_main_functions):
        main('domain', 'django.version', 'python.version')
        assert mock_main_functions.create_virtualenv.call_args == call(
            'domain', 'python.version', 'django.version'
        )


    def test_domain_defaults_to_using_current_username(self, mock_main_functions):
        username = getpass.getuser()
        main('your-username.pythonanywhere.com', 'django.version', 'python.version')
        assert mock_main_functions.create_virtualenv.call_args == call(
            username + '.pythonanywhere.com', 'python.version', 'django.version'
        )


    def test_calls_start_django_project_with_virtualenv(self, mock_main_functions):
        main('domain', 'django.version', 'python.version')
        assert mock_main_functions.start_django_project.call_args == call(
            'domain', mock_main_functions.create_virtualenv.return_value
        )


    def test_calls_create_webapp_with_virtualenv_and_python_version(self, mock_main_functions):
        main('domain', 'django.version', 'python.version')
        assert mock_main_functions.create_webapp.call_args == call(
            'domain',
            'python.version',
            mock_main_functions.create_virtualenv.return_value,
            mock_main_functions.start_django_project.return_value
        )


    def test_calls_update_wsgi_file(self, mock_main_functions):
        main('www.domain.com', 'django.version', 'python.version')
        assert mock_main_functions.update_wsgi_file.call_args == call(
            '/var/www/www_domain_com_wsgi.py',
            mock_main_functions.start_django_project.return_value
        )


    def test_calls_reload_webapp(self, mock_main_functions):
        main('domain', 'django.version', 'python.version')
        assert mock_main_functions.reload_webapp.call_args == call(
            'domain',
        )



class TestCreateVirtualenv:

    def test_uses_bash_and_sources_virtualenvwrapper(self, mock_subprocess):
        create_virtualenv('domain.com', '2.7', 'latest')
        args, kwargs = mock_subprocess.check_call.call_args
        command_list = args[0]
        assert command_list[:2] == ['bash', '-c']
        assert command_list[2].startswith('source virtualenvwrapper.sh && mkvirtualenv')


    def test_calls_mkvirtualenv_with_python_version_and_domain(self, mock_subprocess):
        create_virtualenv('domain.com', '2.7', 'latest')
        args, kwargs = mock_subprocess.check_call.call_args
        command_list = args[0]
        bash_command = command_list[2]
        assert 'mkvirtualenv --python=/usr/bin/python2.7 domain.com' in bash_command


    def test_django_version_for_latest(self, mock_subprocess):
        create_virtualenv('domain.com', '2.7', 'latest')
        args, kwargs = mock_subprocess.check_call.call_args
        command_list = args[0]
        assert command_list[2].endswith('pip install django')


    def test_returns_virtualenv_path(self, mock_subprocess, virtualenvs_folder):
        response = create_virtualenv('domain.com', '2.7', 'latest')
        assert response == os.path.join(virtualenvs_folder, 'domain.com')


    @pytest.mark.slowtest
    def test_actually_creates_a_virtualenv_with_right_django_version_in(self, virtualenvs_folder):
        domain = 'mydomain.com'
        create_virtualenv(domain, '2.7', '1.9')

        assert domain in os.listdir(virtualenvs_folder)
        django_version = subprocess.check_output([
            os.path.join(virtualenvs_folder, domain, 'bin/python'),
            '-c'
            'import django; print(django.get_version())'
        ]).decode().strip()
        assert django_version == '1.9'



class TestStartDjangoProject:

    @pytest.mark.slowtest
    def test_actually_creates_a_django_project(self, test_virtualenv, fake_home):
        start_django_project('mydomain.com', test_virtualenv)
        assert 'mydomain.com' in os.listdir(fake_home)
        assert 'settings.py' in os.listdir(os.path.join(fake_home, 'mydomain.com/mysite'))


    @pytest.mark.slowtest
    def test_adds_STATIC_and_MEDIA_config_to_settings(self, test_virtualenv, fake_home):
        start_django_project('mydomain.com', test_virtualenv)
        with open(os.path.join(fake_home, 'mydomain.com/mysite/settings.py')) as f:
            contents = f.read()

        print(contents)
        lines = contents.split('\n')
        assert "STATIC_URL = '/static/'" in lines
        assert "MEDIA_URL = '/media/'" in lines
        assert "STATIC_ROOT = os.path.join(BASE_DIR, 'static')" in lines
        assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" in lines
        assert "ALLOWED_HOSTS = ['mydomain.com']" in lines


    @pytest.mark.slowtest
    def test_has_run_collectstatic(self, test_virtualenv, fake_home):
        start_django_project('mydomain.com', test_virtualenv)
        assert 'base.css' in os.listdir(os.path.join(fake_home, 'mydomain.com/static/admin/css'))


    def test_calls_update_settings_file(self, mock_subprocess, fake_home):
        with patch('new_django_project_in_virtualenv.update_settings_file') as mock_update_settings_file:
            start_django_project('mydomain.com', '/path/to/virtualenv')
        expected_path = os.path.join(fake_home, 'mydomain.com')
        assert mock_update_settings_file.call_args == call(
            'mydomain.com', expected_path
        )

<commit_msg>move all slow tests into one semifunctional thing<commit_after>from unittest.mock import call, patch
import getpass
import json
import os
import pytest
import responses
import subprocess
import tempfile
from textwrap import dedent
from urllib.parse import urlencode

import new_django_project_in_virtualenv
from new_django_project_in_virtualenv import (
    API_ENDPOINT,
    PYTHON_VERSIONS,
    create_virtualenv,
    create_webapp,
    main,
    start_django_project,
    update_settings_file,
    update_wsgi_file,
    reload_webapp,
)


class TestMain:

    def test_calls_create_virtualenv(self, mock_main_functions):
        main('domain', 'django.version', 'python.version')
        assert mock_main_functions.create_virtualenv.call_args == call(
            'domain', 'python.version', 'django.version'
        )


    def test_domain_defaults_to_using_current_username(self, mock_main_functions):
        username = getpass.getuser()
        main('your-username.pythonanywhere.com', 'django.version', 'python.version')
        assert mock_main_functions.create_virtualenv.call_args == call(
            username + '.pythonanywhere.com', 'python.version', 'django.version'
        )


    def test_calls_start_django_project_with_virtualenv(self, mock_main_functions):
        main('domain', 'django.version', 'python.version')
        assert mock_main_functions.start_django_project.call_args == call(
            'domain', mock_main_functions.create_virtualenv.return_value
        )


    def test_calls_create_webapp_with_virtualenv_and_python_version(self, mock_main_functions):
        main('domain', 'django.version', 'python.version')
        assert mock_main_functions.create_webapp.call_args == call(
            'domain',
            'python.version',
            mock_main_functions.create_virtualenv.return_value,
            mock_main_functions.start_django_project.return_value
        )


    def test_calls_update_wsgi_file(self, mock_main_functions):
        main('www.domain.com', 'django.version', 'python.version')
        assert mock_main_functions.update_wsgi_file.call_args == call(
            '/var/www/www_domain_com_wsgi.py',
            mock_main_functions.start_django_project.return_value
        )


    def test_calls_reload_webapp(self, mock_main_functions):
        main('domain', 'django.version', 'python.version')
        assert mock_main_functions.reload_webapp.call_args == call(
            'domain',
        )


class TestMainSemiFunctional:

    @pytest.mark.slowtest
    def test_creates_django_project_in_virtualenv_with_hacked_settings_and_static_files(
        self, fake_home, virtualenvs_folder, api_responses, api_token
    ):

        webapps_url = API_ENDPOINT.format(username=getpass.getuser())
        webapp_url = API_ENDPOINT.format(username=getpass.getuser()) + 'mydomain.com/'
        reload_url = webapp_url + 'reload'
        api_responses.add(responses.POST, webapps_url, status=201, body=json.dumps({'status': 'OK'}))
        api_responses.add(responses.PATCH, webapp_url, status=200)
        api_responses.add(responses.POST, reload_url, status=200, body=json.dumps({'status': 'OK'}))

        with patch('new_django_project_in_virtualenv.update_wsgi_file'):
            main('mydomain.com', '1.9.2', '2.7')

        django_version = subprocess.check_output([
            os.path.join(virtualenvs_folder, 'mydomain.com/bin/python'),
            '-c'
            'import django; print(django.get_version())'
        ]).decode().strip()
        assert django_version == '1.9.2'

        with open(os.path.join(fake_home, 'mydomain.com/mysite/settings.py')) as f:
            lines = f.read().split('\n')
        assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" in lines
        assert "ALLOWED_HOSTS = ['mydomain.com']" in lines

        assert 'base.css' in os.listdir(os.path.join(fake_home, 'mydomain.com/static/admin/css'))



class TestCreateVirtualenv:

    def test_uses_bash_and_sources_virtualenvwrapper(self, mock_subprocess):
        create_virtualenv('domain.com', '2.7', 'latest')
        args, kwargs = mock_subprocess.check_call.call_args
        command_list = args[0]
        assert command_list[:2] == ['bash', '-c']
        assert command_list[2].startswith('source virtualenvwrapper.sh && mkvirtualenv')


    def test_calls_mkvirtualenv_with_python_version_and_domain(self, mock_subprocess):
        create_virtualenv('domain.com', '2.7', 'latest')
        args, kwargs = mock_subprocess.check_call.call_args
        command_list = args[0]
        bash_command = command_list[2]
        assert 'mkvirtualenv --python=/usr/bin/python2.7 domain.com' in bash_command


    def test_django_version_for_latest(self, mock_subprocess):
        create_virtualenv('domain.com', '2.7', 'latest')
        args, kwargs = mock_subprocess.check_call.call_args
        command_list = args[0]
        assert command_list[2].endswith('pip install django')


    def test_returns_virtualenv_path(self, mock_subprocess, virtualenvs_folder):
        response = create_virtualenv('domain.com', '2.7', 'latest')
        assert response == os.path.join(virtualenvs_folder, 'domain.com')





class TestStartDjangoProject:


    def test_calls_update_settings_file(self, mock_subprocess, fake_home):
        with patch('new_django_project_in_virtualenv.update_settings_file') as mock_update_settings_file:
            start_django_project('mydomain.com', '/path/to/virtualenv')
        expected_path = os.path.join(fake_home, 'mydomain.com')
        assert mock_update_settings_file.call_args == call(
            'mydomain.com', expected_path
        )


{'fraction_non_alphanumeric': 0.10380712860983436, 'fraction_numerical': 0.004943196600468303, 'mean_word_length': 3.5509076558800317, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5135851', 'n_tokens_mistral': 3631, 'n_tokens_neox': 3549, 'n_words': 580}