|
1 | 1 | """Tests that CRUD maven remotes."""
|
2 |
| -import unittest |
3 |
| - |
4 |
| -from requests.exceptions import HTTPError |
5 |
| - |
6 |
| -from pulp_smash import api, config, utils |
7 |
| - |
8 |
| -from pulp_maven.tests.functional.constants import MAVEN_REMOTE_PATH |
9 |
| -from pulp_maven.tests.functional.utils import skip_if, gen_maven_remote |
10 |
| -from pulp_maven.tests.functional.utils import set_up_module as setUpModule # noqa:F401 |
11 |
| - |
12 |
| - |
13 |
| -class CRUDRemotesTestCase(unittest.TestCase): |
14 |
| - """CRUD remotes.""" |
15 |
| - |
16 |
| - @classmethod |
17 |
| - def setUpClass(cls): |
18 |
| - """Create class-wide variables.""" |
19 |
| - cls.cfg = config.get_config() |
20 |
| - cls.client = api.Client(cls.cfg, api.json_handler) |
21 |
| - |
22 |
| - def test_01_create_remote(self): |
23 |
| - """Create a remote.""" |
24 |
| - body = _gen_verbose_remote() |
25 |
| - type(self).remote = self.client.post(MAVEN_REMOTE_PATH, body) |
26 |
| - for key in ("username", "password"): |
27 |
| - del body[key] |
28 |
| - for key, val in body.items(): |
29 |
| - with self.subTest(key=key): |
30 |
| - self.assertEqual(self.remote[key], val) |
31 |
| - |
32 |
| - @skip_if(bool, "remote", False) |
33 |
| - def test_02_create_same_name(self): |
34 |
| - """Try to create a second remote with an identical name. |
35 |
| -
|
36 |
| - See: `Pulp Smash #1055 |
37 |
| - <https://github.com/PulpQE/pulp-smash/issues/1055>`_. |
38 |
| - """ |
39 |
| - body = gen_maven_remote() |
40 |
| - body["name"] = self.remote["name"] |
41 |
| - with self.assertRaises(HTTPError): |
42 |
| - self.client.post(MAVEN_REMOTE_PATH, body) |
43 |
| - |
44 |
| - @skip_if(bool, "remote", False) |
45 |
| - def test_02_read_remote(self): |
46 |
| - """Read a remote by its href.""" |
47 |
| - remote = self.client.get(self.remote["pulp_href"]) |
48 |
| - for key, val in self.remote.items(): |
49 |
| - with self.subTest(key=key): |
50 |
| - self.assertEqual(remote[key], val) |
51 |
| - |
52 |
| - @skip_if(bool, "remote", False) |
53 |
| - def test_02_read_remotes(self): |
54 |
| - """Read a remote by its name.""" |
55 |
| - page = self.client.get(MAVEN_REMOTE_PATH, params={"name": self.remote["name"]}) |
56 |
| - self.assertEqual(len(page["results"]), 1) |
57 |
| - for key, val in self.remote.items(): |
58 |
| - with self.subTest(key=key): |
59 |
| - self.assertEqual(page["results"][0][key], val) |
60 |
| - |
61 |
| - @skip_if(bool, "remote", False) |
62 |
| - def test_03_partially_update(self): |
63 |
| - """Update a remote using HTTP PATCH.""" |
64 |
| - body = _gen_verbose_remote() |
65 |
| - self.client.patch(self.remote["pulp_href"], body) |
66 |
| - for key in ("username", "password"): |
67 |
| - del body[key] |
68 |
| - type(self).remote = self.client.get(self.remote["pulp_href"]) |
69 |
| - for key, val in body.items(): |
70 |
| - with self.subTest(key=key): |
71 |
| - self.assertEqual(self.remote[key], val) |
72 |
| - |
73 |
| - @skip_if(bool, "remote", False) |
74 |
| - def test_04_fully_update(self): |
75 |
| - """Update a remote using HTTP PUT.""" |
76 |
| - body = _gen_verbose_remote() |
77 |
| - self.client.put(self.remote["pulp_href"], body) |
78 |
| - for key in ("username", "password"): |
79 |
| - del body[key] |
80 |
| - type(self).remote = self.client.get(self.remote["pulp_href"]) |
81 |
| - for key, val in body.items(): |
82 |
| - with self.subTest(key=key): |
83 |
| - self.assertEqual(self.remote[key], val) |
84 |
| - |
85 |
| - @skip_if(bool, "remote", False) |
86 |
| - def test_05_delete(self): |
87 |
| - """Delete a remote.""" |
88 |
| - self.client.delete(self.remote["pulp_href"]) |
89 |
| - with self.assertRaises(HTTPError): |
90 |
| - self.client.get(self.remote["pulp_href"]) |
91 |
| - |
92 |
| - |
93 |
| -class CreateRemoteNoURLTestCase(unittest.TestCase): |
94 |
| - """Verify whether is possible to create a remote without a URL.""" |
95 |
| - |
96 |
| - def test_all(self): |
97 |
| - """Verify whether is possible to create a remote without a URL. |
98 |
| -
|
99 |
| - This test targets the following issues: |
100 |
| -
|
101 |
| - * `Pulp #3395 <https://pulp.plan.io/issues/3395>`_ |
102 |
| - * `Pulp Smash #984 <https://github.com/PulpQE/pulp-smash/issues/984>`_ |
103 |
| - """ |
104 |
| - body = gen_maven_remote() |
105 |
| - del body["url"] |
106 |
| - with self.assertRaises(HTTPError): |
107 |
| - api.Client(config.get_config()).post(MAVEN_REMOTE_PATH, body) |
108 |
| - |
109 |
| - |
110 |
| -def _gen_verbose_remote(): |
111 |
| - """Return a semi-random dict for use in defining a remote. |
112 |
| -
|
113 |
| - For most tests, it's desirable to create remotes with as few attributes |
114 |
| - as possible, so that the tests can specifically target and attempt to break |
115 |
| - specific features. This module specifically targets remotes, so it makes |
116 |
| - sense to provide as many attributes as possible. |
117 |
| -
|
118 |
| - Note that 'username' and 'password' are write-only attributes. |
119 |
| - """ |
120 |
| - attrs = gen_maven_remote() |
121 |
| - attrs.update({"password": utils.uuid4(), "username": utils.uuid4()}) |
122 |
| - return attrs |
| 2 | +import json |
| 3 | +import uuid |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pulpcore.client.pulp_maven.exceptions import ApiException |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parallel |
| 11 | +def test_remote_crud_workflow(maven_remote_api_client, gen_object_with_cleanup, monitor_task): |
| 12 | + remote_data = {"name": str(uuid.uuid4()), "url": "http://example.com"} |
| 13 | + remote = gen_object_with_cleanup(maven_remote_api_client, remote_data) |
| 14 | + assert remote.url == remote_data["url"] |
| 15 | + assert remote.name == remote_data["name"] |
| 16 | + |
| 17 | + with pytest.raises(ApiException) as exc: |
| 18 | + gen_object_with_cleanup(maven_remote_api_client, remote_data) |
| 19 | + assert exc.value.status == 400 |
| 20 | + assert json.loads(exc.value.body) == {"name": ["This field must be unique."]} |
| 21 | + |
| 22 | + update_response = maven_remote_api_client.partial_update( |
| 23 | + remote.pulp_href, {"url": "https://example.com"} |
| 24 | + ) |
| 25 | + task = monitor_task(update_response.task) |
| 26 | + assert task.created_resources == [] |
| 27 | + |
| 28 | + remote = maven_remote_api_client.read(remote.pulp_href) |
| 29 | + assert remote.url == "https://example.com" |
| 30 | + |
| 31 | + all_new_remote_data = {"name": str(uuid.uuid4()), "url": "http://example.com"} |
| 32 | + update_response = maven_remote_api_client.update(remote.pulp_href, all_new_remote_data) |
| 33 | + task = monitor_task(update_response.task) |
| 34 | + assert task.created_resources == [] |
| 35 | + |
| 36 | + remote = maven_remote_api_client.read(remote.pulp_href) |
| 37 | + assert remote.name == all_new_remote_data["name"] |
| 38 | + assert remote.url == all_new_remote_data["url"] |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parallel |
| 42 | +def test_create_maven_remote_with_invalid_parameter( |
| 43 | + maven_remote_api_client, gen_object_with_cleanup |
| 44 | +): |
| 45 | + unexpected_field_remote_data = { |
| 46 | + "name": str(uuid.uuid4()), |
| 47 | + "url": "http://example.com", |
| 48 | + "foo": "bar", |
| 49 | + } |
| 50 | + |
| 51 | + with pytest.raises(ApiException) as exc: |
| 52 | + gen_object_with_cleanup(maven_remote_api_client, unexpected_field_remote_data) |
| 53 | + assert exc.value.status == 400 |
| 54 | + assert json.loads(exc.value.body) == {"foo": ["Unexpected field"]} |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parallel |
| 58 | +def test_create_maven_remote_without_url(maven_remote_api_client, gen_object_with_cleanup): |
| 59 | + with pytest.raises(ApiException) as exc: |
| 60 | + gen_object_with_cleanup(maven_remote_api_client, {"name": str(uuid.uuid4())}) |
| 61 | + assert exc.value.status == 400 |
| 62 | + assert json.loads(exc.value.body) == {"url": ["This field is required."]} |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parallel |
| 66 | +def test_default_remote_policy_immediate(maven_remote_api_client, gen_object_with_cleanup): |
| 67 | + remote_data = {"name": str(uuid.uuid4()), "url": "http://example.com"} |
| 68 | + remote = gen_object_with_cleanup(maven_remote_api_client, remote_data) |
| 69 | + assert remote.policy == "immediate" |
0 commit comments