1"""
2    :codeauthor: Rahul Handay <rahulha@saltstack.com>
3"""
4
5import os
6
7import pytest
8import salt.states.svn as svn
9from tests.support.mock import MagicMock, patch
10
11
12@pytest.fixture
13def configure_loader_modules():
14    return {svn: {}}
15
16
17def test_latest():
18    """
19    Checkout or update the working directory to
20    the latest revision from the remote repository.
21    """
22    mock = MagicMock(return_value=True)
23    with patch.object(svn, "_fail", mock):
24        assert svn.latest("salt")
25
26    mock = MagicMock(side_effect=[True, False, False, False])
27    with patch.object(os.path, "exists", mock):
28        mock = MagicMock(return_value=False)
29        with patch.object(os.path, "isdir", mock):
30            with patch.object(svn, "_fail", mock):
31                assert not svn.latest("salt", "c://salt")
32
33        with patch.dict(svn.__opts__, {"test": True}):
34            mock = MagicMock(return_value=["salt"])
35            with patch.object(svn, "_neutral_test", mock):
36                assert svn.latest("salt", "c://salt") == ["salt"]
37
38            mock = MagicMock(side_effect=[False, True])
39            with patch.object(os.path, "exists", mock):
40                mock = MagicMock(return_value=True)
41                info_mock = MagicMock(return_value=[{"Revision": "mocked"}])
42                with patch.dict(
43                    svn.__salt__, {"svn.diff": mock, "svn.info": info_mock}
44                ):
45                    mock = MagicMock(return_value=["Dude"])
46                    with patch.object(svn, "_neutral_test", mock):
47                        assert svn.latest("salt", "c://salt") == ["Dude"]
48
49        with patch.dict(svn.__opts__, {"test": False}):
50            mock = MagicMock(return_value=[{"Revision": "a"}])
51            with patch.dict(svn.__salt__, {"svn.info": mock}):
52                mock = MagicMock(return_value=True)
53                with patch.dict(svn.__salt__, {"svn.update": mock}):
54                    assert svn.latest("salt", "c://salt") == {
55                        "changes": {},
56                        "comment": True,
57                        "name": "salt",
58                        "result": True,
59                    }
60
61
62def test_latest_trust_failures():
63    """
64    Test that checks that the trust_failures option is handled
65    correctly when running svn.latest in test mode. This tests for the
66    bug reported as #59069.
67    """
68    os_path_exists_mock = MagicMock(side_effect=[False, True])
69    svn_info_mock = MagicMock(return_value=[{"Revision": "42"}])
70    svn_diff_mock = MagicMock()
71    svn_neutral_test_mock = MagicMock()
72    with patch.object(os.path, "exists", os_path_exists_mock), patch.dict(
73        svn.__opts__, {"test": True}
74    ), patch.dict(
75        svn.__salt__, {"svn.diff": svn_diff_mock, "svn.info": svn_info_mock}
76    ), patch.object(
77        svn, "_neutral_test", svn_neutral_test_mock
78    ):
79        svn.latest("salt", "/my/test/dir", trust_failures="unknown-ca")
80        svn_diff_mock.assert_called_with(
81            "/my/test",
82            "/my/test/dir",
83            None,
84            None,
85            None,
86            "-r",
87            "42:HEAD",
88            "--trust-server-cert-failures",
89            "unknown-ca",
90        )
91
92
93def test_export():
94    """
95    Test to export a file or directory from an SVN repository
96    """
97    mock = MagicMock(return_value=True)
98    with patch.object(svn, "_fail", mock):
99        assert svn.export("salt")
100
101    mock = MagicMock(side_effect=[True, False, False, False])
102    with patch.object(os.path, "exists", mock):
103        mock = MagicMock(return_value=False)
104        with patch.object(os.path, "isdir", mock):
105            with patch.object(svn, "_fail", mock):
106                assert not svn.export("salt", "c://salt")
107
108        with patch.dict(svn.__opts__, {"test": True}):
109            mock = MagicMock(return_value=["salt"])
110            with patch.object(svn, "_neutral_test", mock):
111                assert svn.export("salt", "c://salt") == ["salt"]
112
113            mock = MagicMock(side_effect=[False, True])
114            with patch.object(os.path, "exists", mock):
115                mock = MagicMock(return_value=True)
116                with patch.dict(svn.__salt__, {"svn.list": mock}):
117                    mock = MagicMock(return_value=["Dude"])
118                    with patch.object(svn, "_neutral_test", mock):
119                        assert svn.export("salt", "c://salt") == ["Dude"]
120
121        with patch.dict(svn.__opts__, {"test": False}):
122            mock = MagicMock(return_value=True)
123            with patch.dict(svn.__salt__, {"svn.export": mock}):
124                assert svn.export("salt", "c://salt") == {
125                    "changes": {
126                        "new": "salt",
127                        "comment": "salt was Exported to c://salt",
128                    },
129                    "comment": True,
130                    "name": "salt",
131                    "result": True,
132                }
133
134
135def test_dirty():
136    """
137    Test to determine if the working directory has been changed.
138    """
139    mock = MagicMock(return_value=True)
140    with patch.object(svn, "_fail", mock):
141        assert svn.dirty("salt", "c://salt")
142