Skip to content

Commit

Permalink
Increase code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Anja Strunk <[email protected]>
  • Loading branch information
anjastrunk committed Aug 20, 2024
1 parent d22cc18 commit fb57350
Showing 1 changed file with 59 additions and 23 deletions.
82 changes: 59 additions & 23 deletions tests/test_csp_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,86 @@


class CspPGeneratorTestCase(unittest.TestCase):
@patch("generator.discovery.gxdch_services.ComplianceService.request_compliance_vc")
@patch("generator.discovery.gxdch_services.NotaryService.request_reg_number_vc")
@patch("generator.discovery.gxdch_services.RegistryService.get_gx_tandc")
@patch("generator.common.crypto.load_jwk_from_file")
def test_generate(self, load_jwk, get_tandc, request_req_number, request_comp_vc):
load_jwk.return_value = JWK.from_pem(rsa.generate_private_key(

@classmethod
def setUpClass(cls):
with open(get_absolute_path(const.CONFIG_FILE), "r") as config_file:
cls.conf = config.Config(yaml.safe_load(config_file))

cls.private_key = JWK.from_pem(rsa.generate_private_key(
public_exponent=65537,
key_size=2048).private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()))
get_tandc.return_value = {"version": "22.10", "text": "foo"}
request_req_number.return_value = {"id": "cred_id", "credentialSubject": {"type": "gx:legalRegistrationNumber", "id": "cred_sub_id"}}
request_comp_vc.return_value = "{\"credentialSubject\": {\"type\": \"gx:ComplianceCredential\"}}"
cls.tanbc = {"version": "22.10", "text": "foo"}
cls.lrn = {"id": "cred_id", "credentialSubject": {"type": "gx:legalRegistrationNumber",
"id": "cred_sub_id"}}
cls.cs = "{\"credentialSubject\": {\"type\": \"gx:ComplianceCredential\"}}"

with open(get_absolute_path(const.CONFIG_FILE), "r") as config_file:
conf = config.Config(yaml.safe_load(config_file))
def setUp(self):
self.csp_gen = CspGenerator(self.conf)

@patch("generator.discovery.gxdch_services.ComplianceService.request_compliance_vc")
@patch("generator.discovery.gxdch_services.NotaryService.request_reg_number_vc")
@patch("generator.discovery.gxdch_services.RegistryService.get_gx_tandc")
@patch("generator.common.crypto.load_jwk_from_file")
def test_generate_auto_sign(self, load_jwk, get_tandc, request_req_number, request_comp_vc):
self._prepare_test(load_jwk, get_tandc, request_req_number, request_comp_vc)
csp_vcs = self.csp_gen.generate(auto_sign=True)
self._verify_csp_gen(csp_vcs, get_tandc, request_req_number, request_comp_vc)

@patch("generator.discovery.gxdch_services.ComplianceService.request_compliance_vc")
@patch("generator.discovery.gxdch_services.NotaryService.request_reg_number_vc")
@patch("generator.discovery.gxdch_services.RegistryService.get_gx_tandc")
@patch("generator.common.crypto.load_jwk_from_file")
def test_generate(self, load_jwk, get_tandc, request_req_number, request_comp_vc):
self._prepare_test(load_jwk, get_tandc, request_req_number, request_comp_vc)

# sign terms and conditions
with patch('builtins.input', return_value="y"):
csp_vcs = self.csp_gen.generate()
self._verify_csp_gen(csp_vcs, get_tandc, request_req_number, request_comp_vc)

# do not sign terms and conditions
with patch('builtins.input', return_value="n"):
csp_vcs = self.csp_gen.generate()
self.assertIsNone(csp_vcs)

csp_gen = CspGenerator(conf)
csp_vcs = csp_gen.generate(auto_sign=True)
def _prepare_test(self, load_jwk, get_tandc, request_req_number, request_comp_vc):
load_jwk.return_value = self.private_key
get_tandc.return_value = self.tanbc
request_req_number.return_value = self.lrn
request_comp_vc.return_value = self.cs

def _verify_csp_gen(self, csp_vcs, get_tandc, request_req_number, request_comp_vc):
get_tandc.assert_called_once()
request_req_number.assert_called_once()
request_req_number.assert_called_with(
csp=conf.get_value([const.CONFIG_CSP]),
cred_id=conf.get_value([const.CONFIG_CRED, const.CONFIG_CRED_BASE_CRED_URL]) + "/lrn.json",
cred_subject_id=conf.get_value([const.CONFIG_CRED, const.CONFIG_CRED_BASE_CRED_URL]) + "/lrn_cs.json"
csp=self.conf.get_value([const.CONFIG_CSP]),
cred_id=self.conf.get_value([const.CONFIG_CRED, const.CONFIG_CRED_BASE_CRED_URL]) + "/lrn.json",
cred_subject_id=self.conf.get_value([const.CONFIG_CRED, const.CONFIG_CRED_BASE_CRED_URL]) + "/lrn_cs.json"
)
request_comp_vc.assert_called_once()

self.assertEqual(conf.get_value([const.CONFIG_CRED, const.CONFIG_CRED_BASE_CRED_URL]) + "/tandc.json",
self.assertEqual(self.conf.get_value([const.CONFIG_CRED, const.CONFIG_CRED_BASE_CRED_URL]) + "/tandc.json",
csp_vcs['tandc']['id'])
self.assertEqual(conf.get_value([const.CONFIG_CRED, const.CONFIG_CRED_BASE_CRED_URL]) + "/tandc_cs.json", csp_vcs['tandc']['credentialSubject']['id'])
self.assertEqual(self.conf.get_value([const.CONFIG_CRED, const.CONFIG_CRED_BASE_CRED_URL]) + "/tandc_cs.json",
csp_vcs['tandc']['credentialSubject']['id'])
self.assertEqual("gx:GaiaXTermsAndConditions", csp_vcs['tandc']['credentialSubject']['type'])
self.assertEqual("foo", csp_vcs['tandc']['credentialSubject']['gx:termsAndConditions'])
self.assertEqual(conf.get_value([const.CONFIG_CSP, const.CONFIG_DID]), csp_vcs['tandc']['issuer'])
self.assertEqual(self.conf.get_value([const.CONFIG_CSP, const.CONFIG_DID]), csp_vcs['tandc']['issuer'])
self.assertIsNotNone(csp_vcs['tandc']['proof'])
self.assertEqual("cred_id", csp_vcs['lrn']['id'])
self.assertEqual("cred_sub_id", csp_vcs['lrn']['credentialSubject']['id'])
self.assertEqual("gx:legalRegistrationNumber", csp_vcs['lrn']['credentialSubject']['type'])
self.assertEqual(conf.get_value([const.CONFIG_CRED, const.CONFIG_CRED_BASE_CRED_URL]) + "/legal_person.json",
csp_vcs['lp']['id'])
self.assertEqual(conf.get_value([const.CONFIG_CRED, const.CONFIG_CRED_BASE_CRED_URL]) + "/legal_person_cs.json", csp_vcs['lp']['credentialSubject']['id'])
self.assertEqual(conf.get_value([const.CONFIG_CSP, const.CONFIG_DID]), csp_vcs['lp']['issuer'])
self.assertEqual(
self.conf.get_value([const.CONFIG_CRED, const.CONFIG_CRED_BASE_CRED_URL]) + "/legal_person.json",
csp_vcs['lp']['id'])
self.assertEqual(
self.conf.get_value([const.CONFIG_CRED, const.CONFIG_CRED_BASE_CRED_URL]) + "/legal_person_cs.json",
csp_vcs['lp']['credentialSubject']['id'])
self.assertEqual(self.conf.get_value([const.CONFIG_CSP, const.CONFIG_DID]), csp_vcs['lp']['issuer'])
self.assertIsNotNone(csp_vcs['lp']['proof'])
self.assertEqual("gx:LegalParticipant", csp_vcs['lp']['credentialSubject']['type'])
self.assertEqual("gx:ComplianceCredential", csp_vcs['cs']['credentialSubject']['type'])
Expand Down

0 comments on commit fb57350

Please sign in to comment.