Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Commit

Permalink
Adding logic for registering and updating consumers with client certs
Browse files Browse the repository at this point in the history
This commit adds a new testcase for registering and updating a consumer.
The Registering happens with basic auth url and the update is through
the client certificate that is in the previous response.

See pulp/pulp-smash#1007
  • Loading branch information
ragabala committed Aug 15, 2018
1 parent 3a0928b commit 830e266
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
71 changes: 71 additions & 0 deletions pulp_2_tests/tests/platform/api_v2/test_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
.. _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
from pulp_smash.pulp2.constants import CONSUMERS_PATH, REPOSITORY_PATH

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):
Expand Down Expand Up @@ -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 <https://github.com/PulpQE/pulp-smash/issues/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'])
28 changes: 28 additions & 0 deletions pulp_2_tests/tests/platform/api_v2/utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 830e266

Please sign in to comment.