diff --git a/pulp_2_tests/tests/platform/api_v2/test_consumer.py b/pulp_2_tests/tests/platform/api_v2/test_consumer.py index f47a312..c4589ae 100644 --- a/pulp_2_tests/tests/platform/api_v2/test_consumer.py +++ b/pulp_2_tests/tests/platform/api_v2/test_consumer.py @@ -4,7 +4,9 @@ .. _consumer: https://docs.pulpproject.org/en/latest/dev-guide/integration/rest-api/consumer/index.html """ +import tempfile import unittest +import os from urllib.parse import urljoin from pulp_smash import api, config, utils @@ -12,6 +14,7 @@ from pulp_2_tests.tests.rpm.api_v2.utils import gen_repo, gen_distributor from pulp_2_tests.tests.platform.utils import set_up_module as setUpModule # pylint:disable=unused-import +from pulp_2_tests.tests.platform.api_v2.utils import make_client_with_cert_auth class BindConsumerTestCase(unittest.TestCase): @@ -67,3 +70,71 @@ def test_all(self): self.assertEqual(result['consumer_id'], consumer['consumer']['id']) self.assertEqual(result['distributor_id'], body['distributor_id']) self.assertEqual(result['repo_id'], body['repo_id']) + + +class RegisterAndUpdateConsumerTestCase(unittest.TestCase): + """Show that one can `register and update a consumer`_. + + The call to ``consumer register api`` should return a x.509 certificate, + that should be useful in updating a consumer and for other actions. + + .. _register and update a consumer: + https://docs.pulpproject.org/dev-guide/integration/rest-api/consumer/cud.html#register-a-consumer + """ + + def test_all(self): + """Register and Update a consumer. + + Do the following: + + 1. Register a consumer with the consumer API + 2. Save the certificate returned in the response + 3. Update the same consumer with new details by using the client certificates + + Assert that: + + * The response for registering a consumer has a x.509 certificate. + * The response for update with certificate has HTTP 200 . + * The response body contains the correct values. + + Refer: + + * `Pulp Smash #1007 `_ + """ + cfg = config.get_config() + client = api.Client(cfg) + + # Step 1 + consumer = client.post(CONSUMERS_PATH, {'id': utils.uuid4()}).json() + self.addCleanup(client.delete, consumer['consumer']['_href']) + self.assertIn('certificate', consumer, 'certificate not found in the response') + + # Step 2 + certificate = consumer['certificate'] + tmp_cert_file = tempfile.NamedTemporaryFile(delete=False) + self.addCleanup(os.unlink, tmp_cert_file.name) + with open(tmp_cert_file.name, 'w') as file_writer: + file_writer.write(certificate) + + # step 3 + new_client = make_client_with_cert_auth(cfg, cert=tmp_cert_file.name) + body = { + 'delta': { + 'display_name': 'Test Consumer', + 'notes': {'arch': 'x86_64'}, + 'description': 'QA automation testing machine' + } + } + + response = new_client.put(consumer['consumer']['_href'], body) + + with self.subTest(comment='check response status code'): + self.assertEqual(response.status_code, 200) + + result = response.json() + with self.subTest(comment='check display name'): + self.assertEqual(result['display_name'], body['delta']['display_name']) + with self.subTest(comment='check notes'): + self.assertEqual(result['notes'], body['delta']['notes']) + with self.subTest(comment='check description'): + self.assertEqual(result['description'], body['delta']['description']) diff --git a/pulp_2_tests/tests/platform/api_v2/utils.py b/pulp_2_tests/tests/platform/api_v2/utils.py new file mode 100644 index 0000000..c3cc31d --- /dev/null +++ b/pulp_2_tests/tests/platform/api_v2/utils.py @@ -0,0 +1,28 @@ +# coding=utf-8 +"""Utility functions for Platform tests.""" +from pulp_smash import api + + +def make_client_with_cert_auth(cfg, cert=None): + """Make an API client configured with client certificate for authentication. + + Create a client that is configured with the client certificates. This will ensure + that the authentication of the urls is through certificates rather than the + basic authentication (i.e using `username` and `password`. + + This is made possible by ensuring the following: + + 1. Create a client object in the normal way ``client = api.Client(cfg)``. + 2. Remove the authentication information from the client object. + 3. Add the client certificates in the client object. + + :param cfg: Information about a Pulp + deployment. + :param cert: Path to the Client Certificate file + that is used for authenticating the client + :returns: An API client configured with client certificates. + """ + client = api.Client(cfg) + del client.request_kwargs['auth'] + client.request_kwargs['cert'] = cert + return client