|
7 | 7 | from jupyterlab_git.git import Git |
8 | 8 | from jupyterlab_git.handlers import GitConfigHandler |
9 | 9 |
|
10 | | -from .testutils import FakeContentManager |
| 10 | +from .testutils import FakeContentManager, ServerTest |
11 | 11 |
|
12 | 12 |
|
13 | | -@patch("jupyterlab_git.handlers.GitConfigHandler.__init__", Mock(return_value=None)) |
14 | | -@patch( |
15 | | - "jupyterlab_git.handlers.GitConfigHandler.get_json_body", |
16 | | - Mock(return_value={"path": "test_path"}), |
17 | | -) |
18 | | -@patch("jupyterlab_git.handlers.GitConfigHandler.git", Git(FakeContentManager("/bin"))) |
19 | | -@patch("jupyterlab_git.handlers.GitConfigHandler.finish") |
20 | | -@patch("subprocess.Popen") |
21 | | -def test_git_get_config_success(popen, finish): |
22 | | - # Given |
23 | | - process_mock = Mock() |
24 | | - attrs = { |
25 | | - "communicate": Mock( |
26 | | - return_value=( |
27 | | - b"user.name=John Snow\nuser.email=john.snow@iscoming.com", |
28 | | - b"", |
29 | | - ) |
30 | | - ), |
31 | | - "returncode": 0, |
32 | | - } |
33 | | - process_mock.configure_mock(**attrs) |
34 | | - popen.return_value = process_mock |
35 | | - |
36 | | - # When |
37 | | - GitConfigHandler().post() |
| 13 | +class TestConfig(ServerTest): |
| 14 | + @patch("subprocess.Popen") |
| 15 | + def test_git_get_config_success(self, popen): |
| 16 | + # Given |
| 17 | + process_mock = Mock() |
| 18 | + attrs = { |
| 19 | + "communicate": Mock( |
| 20 | + return_value=( |
| 21 | + b"user.name=John Snow\nuser.email=john.snow@iscoming.com", |
| 22 | + b"", |
| 23 | + ) |
| 24 | + ), |
| 25 | + "returncode": 0, |
| 26 | + } |
| 27 | + process_mock.configure_mock(**attrs) |
| 28 | + popen.return_value = process_mock |
38 | 29 |
|
39 | | - # Then |
40 | | - popen.assert_called_once_with( |
41 | | - ["git", "config", "--list"], |
42 | | - stdout=subprocess.PIPE, |
43 | | - stderr=subprocess.PIPE, |
44 | | - cwd="test_path", |
45 | | - ) |
46 | | - process_mock.communicate.assert_called_once_with() |
| 30 | + # When |
| 31 | + body = {"path": "test_path"} |
| 32 | + response = self.tester.post(["config"], body=body) |
47 | 33 |
|
48 | | - finish.assert_called_once_with( |
49 | | - json.dumps( |
50 | | - { |
51 | | - "code": 0, |
52 | | - "options": { |
53 | | - "user.name": "John Snow", |
54 | | - "user.email": "john.snow@iscoming.com", |
55 | | - }, |
56 | | - } |
| 34 | + # Then |
| 35 | + popen.assert_called_once_with( |
| 36 | + ["git", "config", "--list"], |
| 37 | + stdout=subprocess.PIPE, |
| 38 | + stderr=subprocess.PIPE, |
| 39 | + cwd="test_path", |
57 | 40 | ) |
58 | | - ) |
| 41 | + process_mock.communicate.assert_called_once_with() |
59 | 42 |
|
| 43 | + assert response.status_code == 201 |
| 44 | + payload = response.json() |
| 45 | + assert payload == { |
| 46 | + "code": 0, |
| 47 | + "options": { |
| 48 | + "user.name": "John Snow", |
| 49 | + "user.email": "john.snow@iscoming.com", |
| 50 | + }, |
| 51 | + } |
60 | 52 |
|
61 | | -@patch("jupyterlab_git.handlers.GitConfigHandler.__init__", Mock(return_value=None)) |
62 | | -@patch( |
63 | | - "jupyterlab_git.handlers.GitConfigHandler.get_json_body", |
64 | | - Mock( |
65 | | - return_value={ |
| 53 | + @patch("subprocess.Popen") |
| 54 | + def test_git_set_config_success(self, popen): |
| 55 | + # Given |
| 56 | + process_mock = Mock() |
| 57 | + attrs = {"communicate": Mock(return_value=(b"", b"")), "returncode": 0} |
| 58 | + process_mock.configure_mock(**attrs) |
| 59 | + popen.return_value = process_mock |
| 60 | + |
| 61 | + # When |
| 62 | + body = { |
66 | 63 | "path": "test_path", |
67 | 64 | "options": { |
68 | 65 | "user.name": "John Snow", |
69 | 66 | "user.email": "john.snow@iscoming.com", |
70 | 67 | }, |
71 | 68 | } |
72 | | - ), |
73 | | -) |
74 | | -@patch("jupyterlab_git.handlers.GitConfigHandler.git", Git(FakeContentManager('/bin'))) |
75 | | -@patch("jupyterlab_git.handlers.GitConfigHandler.finish") |
76 | | -@patch("subprocess.Popen") |
77 | | -def test_git_set_config_success(popen, finish): |
78 | | - # Given |
79 | | - process_mock = Mock() |
80 | | - attrs = {"communicate": Mock(return_value=(b"", b"")), "returncode": 0} |
81 | | - process_mock.configure_mock(**attrs) |
82 | | - popen.return_value = process_mock |
83 | | - |
84 | | - # When |
85 | | - GitConfigHandler().post() |
| 69 | + response = self.tester.post(["config"], body=body) |
86 | 70 |
|
87 | | - # Then |
88 | | - assert popen.call_count == 2 |
89 | | - assert ( |
90 | | - call( |
91 | | - ["git", "config", "--add", "user.email", "john.snow@iscoming.com"], |
92 | | - stdout=subprocess.PIPE, |
93 | | - stderr=subprocess.PIPE, |
94 | | - cwd="test_path", |
| 71 | + # Then |
| 72 | + assert popen.call_count == 2 |
| 73 | + assert ( |
| 74 | + call( |
| 75 | + ["git", "config", "--add", "user.email", "john.snow@iscoming.com"], |
| 76 | + stdout=subprocess.PIPE, |
| 77 | + stderr=subprocess.PIPE, |
| 78 | + cwd="test_path", |
| 79 | + ) |
| 80 | + in popen.call_args_list |
95 | 81 | ) |
96 | | - in popen.call_args_list |
97 | | - ) |
98 | | - assert ( |
99 | | - call( |
100 | | - ["git", "config", "--add", "user.name", "John Snow"], |
101 | | - stdout=subprocess.PIPE, |
102 | | - stderr=subprocess.PIPE, |
103 | | - cwd="test_path", |
| 82 | + assert ( |
| 83 | + call( |
| 84 | + ["git", "config", "--add", "user.name", "John Snow"], |
| 85 | + stdout=subprocess.PIPE, |
| 86 | + stderr=subprocess.PIPE, |
| 87 | + cwd="test_path", |
| 88 | + ) |
| 89 | + in popen.call_args_list |
104 | 90 | ) |
105 | | - in popen.call_args_list |
106 | | - ) |
107 | | - assert process_mock.communicate.call_count == 2 |
| 91 | + assert process_mock.communicate.call_count == 2 |
108 | 92 |
|
109 | | - finish.assert_called_once_with(json.dumps({"code": 0, "message": ""})) |
| 93 | + assert response.status_code == 201 |
| 94 | + payload = response.json() |
| 95 | + assert payload == {"code": 0, "message": ""} |
0 commit comments