1# -*- coding: utf-8 -*-
2import pytest
3
4from os.path import join
5
6from django_extensions.management.commands.runserver_plus import Command as RunServerCommand
7
8from unittest import mock
9
10
11location = join('some', 'strange', 'path')
12different_path = join('some', 'other', 'path')
13
14
15@pytest.mark.parametrize("cert_option, key_file_option, expected_cert_path, expected_key_path", [
16    ['hello', None, join(location, 'hello.crt'), join(location, 'hello.key')],
17    ['hello.crt', None, join(location, 'hello.crt'), join(location, 'hello.key')],
18    [None, 'hello', join(location, 'hello.crt'), join(location, 'hello.key')],
19    [None, 'hello.key', join(location, 'hello.crt'), join(location, 'hello.key')],
20    ['first', 'second', join(location, 'first.crt'), join(location, 'second.key')],
21    ['first.pem', 'second.pem', join(location, 'first.pem'), join(location, 'second.pem')],
22    ['cert.pem', 'key.pem', join(location, 'cert.pem'), join(location, 'key.pem')],
23    [join(location, 'hello'), None, join(location, 'hello.crt'), join(location, 'hello.key')],
24    [None, join(location, 'hello'), join(location, 'hello.crt'), join(location, 'hello.key')],
25    [join(location, 'hello'), join(location, 'hello'), join(location, 'hello.crt'), join(location, 'hello.key')],
26    [join(location, 'hello.crt'), join(location, 'hello.key'), join(location, 'hello.crt'), join(location, 'hello.key')],
27    [join(location, 'hello.key'), join(location, 'hello.crt'), join(location, 'hello.key'), join(location, 'hello.crt')],
28    [join(location, 'cert.pem'), join(location, 'key.pem'), join(location, 'cert.pem'), join(location, 'key.pem')],
29    [join(location, 'hello.crt'), join(location, 'hello.key'), join(location, 'hello.crt'),
30     join(location, 'hello.key')],
31    [join(location, 'other'), join(location, 'hello.key'), join(location, 'other.crt'),
32     join(location, 'hello.key')],
33    [join(location, 'hello.key'), join(location, 'hello.crt'), join(location, 'hello.key'),
34     join(location, 'hello.crt')],
35    [join(different_path, 'hello'), None, join(different_path, 'hello.crt'), join(different_path, 'hello.key')],
36    [None, join(different_path, 'hello'), join(different_path, 'hello.crt'), join(different_path, 'hello.key')],
37    [join(location, 'hello.crt'), join(different_path, 'hello.key'), join(location, 'hello.crt'), join(different_path, 'hello.key')],
38    [join(different_path, 'hello.crt'), join(location, 'hello.key'), join(different_path, 'hello.crt'), join(location, 'hello.key')],
39])
40def test_determining_paths(cert_option, key_file_option, expected_cert_path, expected_key_path):
41    with mock.patch('django_extensions.management.commands.runserver_plus.os.getcwd', return_value=location):
42        options = {'cert_path': cert_option, 'key_file_path': key_file_option}
43        result_cert_path, result_key_path = RunServerCommand.determine_ssl_files_paths(options)
44        assert expected_cert_path == result_cert_path
45        assert expected_key_path == result_key_path
46