Commit 2fca1445 authored by Rafael Monnerat's avatar Rafael Monnerat

erp5_certificate_authority: Added simpler tests for the Caucase Connector

   The connector is mostly tests via Person/Certificate Login tests, this tests aims to cover the extra portion.
parent f50870bb
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2023 Nexedi SARL and Contributors. All Rights Reserved.
# Rafael Monnerat <rafael@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from caucase.client import CaucaseHTTPError
class TestCertificateAuthorityCaucaseConnector(ERP5TypeTestCase):
def afterSetUp(self):
self.caucase_connector = self.portal.portal_web_services.caucase_connector
#self.setUpCaucase()
#self.caucase_connector = self.portal.portal_web_services.test_caucase_connector
def getBusinessTemplateList(self):
return ('erp5_base', 'erp5_certificate_authority')
def test_getConnection_no_url(self):
connector_no_url_string = self.portal.portal_web_services.newContent(
portal_type="Caucase Connector"
)
self.assertRaises(ValueError, connector_no_url_string._getConnection)
def test_getConnection(self):
self.assertNotEqual(None, self.caucase_connector._getConnection())
self.assertNotEqual(None, self.caucase_connector._getConnection(mode="service"))
self.assertNotEqual(None, self.caucase_connector._getConnection(mode="user"))
self.assertRaises(ValueError, self.caucase_connector._getConnection, "unknownmode")
def test_getAuthenticatedConnection_no_url(self):
connector_no_url_string = self.portal.portal_web_services.newContent(
portal_type="Caucase Connector"
)
self.assertRaises(ValueError, connector_no_url_string._getAuthenticatedConnection)
def test_getAuthenticatedConnection_with_url(self):
connector_no_url_string = self.portal.portal_web_services.newContent(
portal_type="Caucase Connector",
url_string="https://hasurl.but.no.user_certificate"
)
self.assertRaises(ValueError, connector_no_url_string._getAuthenticatedConnection)
def test(self):
# Simply test
key, csr = self.caucase_connector._createCertificateRequest()
# Only simple test for the order of response dont change
self.assertIn("PRIVATE KEY", key)
self.assertIn("CERTIFICATE REQUEST", csr)
csr_id = self.caucase_connector.createCertificateSigningRequest(csr)
self.caucase_connector.createCertificate(csr_id)
cert_data = self.caucase_connector.getCertificate(csr_id)
cert = x509.load_pem_x509_certificate(cert_data, default_backend())
privkey = serialization.load_pem_private_key(key.encode(), None, default_backend())
cerfificate_pub = cert.public_key().public_bytes(
serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo)
private_key_pub = privkey.public_key().public_bytes(
serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo)
self.assertEqual(cerfificate_pub, private_key_pub)
self.assertEqual(None, self.caucase_connector.revokeCertificate(cert_data, key.encode()))
self.assertRaises(CaucaseHTTPError, self.caucase_connector.revokeCertificate, cert_data, key.encode())
def test_revoke_without_key(self):
key, csr = self.caucase_connector._createCertificateRequest()
# Only simple test for the order of response dont change
self.assertIn("PRIVATE KEY", key)
self.assertIn("CERTIFICATE REQUEST", csr)
csr_id = self.caucase_connector.createCertificateSigningRequest(csr)
self.caucase_connector.createCertificate(csr_id)
cert_data = self.caucase_connector.getCertificate(csr_id)
self.assertEqual(None, self.caucase_connector.revokeCertificate(cert_data))
self.assertRaises(CaucaseHTTPError, self.caucase_connector.revokeCertificate, cert_data)
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Test Component" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>default_reference</string> </key>
<value> <string>testCertificateAuthorityCaucaseConnector</string> </value>
</item>
<item>
<key> <string>default_source_reference</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>test.erp5.testCertificateAuthorityCaucaseConnector</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Test Component</string> </value>
</item>
<item>
<key> <string>sid</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>text_content_error_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>version</string> </key>
<value> <string>erp5</string> </value>
</item>
<item>
<key> <string>workflow_history</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary>
<item>
<key> <string>component_validation_workflow</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="WorkflowHistoryList" module="Products.ERP5Type.Workflow"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_log</string> </key>
<value>
<list>
<dictionary>
<item>
<key> <string>action</string> </key>
<value> <string>validate</string> </value>
</item>
<item>
<key> <string>validation_state</string> </key>
<value> <string>validated</string> </value>
</item>
</dictionary>
</list>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
test.erp5.testCertificateAuthorityCaucaseConnector
test.erp5.testCertificateAuthorityPerson
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment