1"""
2    :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
3"""
4
5import pytest
6import salt.states.artifactory as artifactory
7from tests.support.mock import MagicMock, patch
8
9
10@pytest.fixture
11def configure_loader_modules():
12    return {artifactory: {}}
13
14
15def test_downloaded():
16    """
17    Test to ensures that the artifact from artifactory exists at
18    given location.
19    """
20    name = "jboss"
21    arti_url = "http://artifactory.intranet.example.com/artifactory"
22    artifact = {
23        "artifactory_url": arti_url,
24        "artifact_id": "module",
25        "repository": "libs-release-local",
26        "packaging": "jar",
27        "group_id": "com.company.module",
28        "classifier": "sources",
29        "version": "1.0",
30    }
31
32    ret = {"name": name, "result": False, "changes": {}, "comment": ""}
33
34    mck = MagicMock(return_value={"status": False, "changes": {}, "comment": ""})
35    with patch.dict(artifactory.__salt__, {"artifactory.get_release": mck}):
36        assert artifactory.downloaded(name, artifact) == ret
37
38    with patch.object(
39        artifactory,
40        "__fetch_from_artifactory",
41        MagicMock(side_effect=Exception("error")),
42    ):
43        ret = artifactory.downloaded(name, artifact)
44        assert ret["result"] is False
45        assert ret["comment"] == "error"
46
47
48def test_downloaded_test_true():
49    """
50    Test to ensures that the artifact from artifactory exists at
51    given location.
52    """
53    name = "jboss"
54    arti_url = "http://artifactory.intranet.example.com/artifactory"
55    artifact = {
56        "artifactory_url": arti_url,
57        "artifact_id": "module",
58        "repository": "libs-release-local",
59        "packaging": "jar",
60        "group_id": "com.company.module",
61        "classifier": "sources",
62        "version": "1.0",
63    }
64
65    ret = {
66        "name": name,
67        "result": True,
68        "changes": {},
69        "comment": (
70            "Artifact would be downloaded from URL:"
71            " http://artifactory.intranet.example.com/artifactory"
72        ),
73    }
74
75    mck = MagicMock(return_value={"status": False, "changes": {}, "comment": ""})
76    with patch.dict(artifactory.__salt__, {"artifactory.get_release": mck}):
77        with patch.dict(artifactory.__opts__, {"test": True}):
78            assert artifactory.downloaded(name, artifact) == ret
79
80    with patch.object(
81        artifactory,
82        "__fetch_from_artifactory",
83        MagicMock(side_effect=Exception("error")),
84    ):
85        ret = artifactory.downloaded(name, artifact)
86        assert ret["result"] is False
87        assert ret["comment"] == "error"
88