|
| 1 | +import pytest |
| 2 | +import json |
| 3 | +import logging |
| 4 | + |
| 5 | +from aries_cloudcontroller.models.proof_request import ProofRequest |
| 6 | + |
| 7 | +LOGGER = logging.getLogger(__name__) |
| 8 | + |
| 9 | +pytest.proof_keys = [ |
| 10 | + "connection_id", |
| 11 | + "version", |
| 12 | + "name", |
| 13 | + "requested_attributes", |
| 14 | + "requested_predicates", |
| 15 | +] |
| 16 | + |
| 17 | +# From sample in https://github.com/hyperledger/aries-cloudagent-python/issues/175 |
| 18 | +pytest.valid_proof_request = json.dumps( |
| 19 | + { |
| 20 | + "connection_id": "d965bd54-affb-4e11-ba8e-ea54a3214123", |
| 21 | + "version": "1.0.0", |
| 22 | + "name": "Proof of Name", |
| 23 | + "requested_attributes": [ |
| 24 | + { |
| 25 | + "name": "name", |
| 26 | + "restrictions": [ |
| 27 | + {"cred_def_id": "3avoBCqDMFHFaKUHug9s8W:3:CL:13:default"}, |
| 28 | + {"value": "Alice Jones"}, |
| 29 | + ], |
| 30 | + } |
| 31 | + ], |
| 32 | + "requested_predicates": [], |
| 33 | + } |
| 34 | +) |
| 35 | + |
| 36 | +pytest.multi_attrs = json.dumps( |
| 37 | + { |
| 38 | + "connection_id": "d965bd54-affb-4e11-ba8e-ea54a3214123", |
| 39 | + "version": "1.0.0", |
| 40 | + "name": "Proof of Name", |
| 41 | + "requested_attributes": [ |
| 42 | + { |
| 43 | + "name": "name", |
| 44 | + "restrictions": [ |
| 45 | + {"cred_def_id": "3avoBCqDMFHFaKUHug9s8W:3:CL:13:default"}, |
| 46 | + {"value": "Alice Jones"}, |
| 47 | + ], |
| 48 | + }, |
| 49 | + { |
| 50 | + "name": "name", |
| 51 | + "restrictions": [ |
| 52 | + {"cred_def_id": "3avoBCqDMFHFaKUHug9s8W:3:CL:13:default"}, |
| 53 | + {"value": "Alice Jones"}, |
| 54 | + ], |
| 55 | + }, |
| 56 | + ], |
| 57 | + "requested_predicates": [], |
| 58 | + } |
| 59 | +) |
| 60 | + |
| 61 | +pytest.multi_predicates = json.dumps( |
| 62 | + { |
| 63 | + "connection_id": "d965bd54-affb-4e11-ba8e-ea54a3214123", |
| 64 | + "version": "1.0.0", |
| 65 | + "name": "Proof of Name", |
| 66 | + "requested_attributes": [], |
| 67 | + "requested_predicates": [ |
| 68 | + { |
| 69 | + "name": "name", |
| 70 | + "restrictions": [ |
| 71 | + {"cred_def_id": "3avoBCqDMFHFaKUHug9s8W:3:CL:13:default"}, |
| 72 | + {"value": "Alice Jones"}, |
| 73 | + ], |
| 74 | + }, |
| 75 | + { |
| 76 | + "name": "name", |
| 77 | + "restrictions": [ |
| 78 | + {"cred_def_id": "3avoBCqDMFHFaKUHug9s8W:3:CL:13:default"}, |
| 79 | + {"value": "Alice Jones"}, |
| 80 | + ], |
| 81 | + }, |
| 82 | + ], |
| 83 | + } |
| 84 | +) |
| 85 | + |
| 86 | +pytest.not_json = 123456 |
| 87 | + |
| 88 | +pytest.invalid_proof_request = json.dumps({"abc": "1234"}) |
| 89 | + |
| 90 | +pytest.no_requested_attrs = json.dumps( |
| 91 | + { |
| 92 | + "connection_id": "d965bd54-affb-4e11-ba8e-ea54a3214123", |
| 93 | + "version": "1.0.0", |
| 94 | + "name": "Proof of Name", |
| 95 | + "requested_attributes": [], |
| 96 | + "requested_predicates": [], |
| 97 | + } |
| 98 | +) |
| 99 | + |
| 100 | + |
| 101 | +def test_init_valid(): |
| 102 | + proof = ProofRequest(pytest.valid_proof_request) |
| 103 | + assert proof.json_data == json.loads(pytest.valid_proof_request) |
| 104 | + |
| 105 | + |
| 106 | +def test_init_invalid(): |
| 107 | + with pytest.raises(AssertionError, match="Missing key"): |
| 108 | + ProofRequest(pytest.invalid_proof_request) |
| 109 | + |
| 110 | + |
| 111 | +def test_init_not_json(caplog): |
| 112 | + error_msg = "the JSON object must be str, bytes or bytearray, not int" |
| 113 | + with pytest.raises(TypeError, match=error_msg): |
| 114 | + ProofRequest(pytest.not_json) |
| 115 | + assert "Failed to load proof request JSON" in caplog.text |
| 116 | + |
| 117 | + |
| 118 | +# check for each possibly missing key whether the correct error is raised |
| 119 | +@pytest.mark.parametrize("key", pytest.proof_keys) |
| 120 | +def test_validate_presentation_json(key): |
| 121 | + proof = pytest.valid_proof_request |
| 122 | + proof_obj = ProofRequest(proof) |
| 123 | + with pytest.raises(AssertionError, match=f"Missing key {key}"): |
| 124 | + del proof_obj.json_data[key] |
| 125 | + proof_obj.validate_proof_request_json(json.dumps(proof_obj.json_data)) |
| 126 | + |
| 127 | + |
| 128 | +def test_requested_attributes_empty(caplog): |
| 129 | + requested_attrs = ProofRequest(pytest.no_requested_attrs).get_requested_attributes() |
| 130 | + assert requested_attrs == [] |
| 131 | + |
| 132 | + |
| 133 | +def test_requested_predicates_empty(caplog): |
| 134 | + requested_predicates = ProofRequest( |
| 135 | + pytest.valid_proof_request |
| 136 | + ).get_requested_predicates() |
| 137 | + assert requested_predicates == [] |
| 138 | + |
| 139 | + |
| 140 | +def test_requested_attributes(caplog): |
| 141 | + expected = [ |
| 142 | + { |
| 143 | + "name": "name", |
| 144 | + "restrictions": [ |
| 145 | + {"cred_def_id": "3avoBCqDMFHFaKUHug9s8W:3:CL:13:default"}, |
| 146 | + {"value": "Alice Jones"}, |
| 147 | + ], |
| 148 | + } |
| 149 | + ] |
| 150 | + requested_attrs = ProofRequest( |
| 151 | + pytest.valid_proof_request |
| 152 | + ).get_requested_attributes() |
| 153 | + assert requested_attrs == expected |
| 154 | + |
| 155 | + |
| 156 | +def test_multi_attributes(caplog): |
| 157 | + expected = [ |
| 158 | + { |
| 159 | + "name": "name", |
| 160 | + "restrictions": [ |
| 161 | + {"cred_def_id": "3avoBCqDMFHFaKUHug9s8W:3:CL:13:default"}, |
| 162 | + {"value": "Alice Jones"}, |
| 163 | + ], |
| 164 | + }, |
| 165 | + { |
| 166 | + "name": "name", |
| 167 | + "restrictions": [ |
| 168 | + {"cred_def_id": "3avoBCqDMFHFaKUHug9s8W:3:CL:13:default"}, |
| 169 | + {"value": "Alice Jones"}, |
| 170 | + ], |
| 171 | + }, |
| 172 | + ] |
| 173 | + requested_attrs = ProofRequest(pytest.multi_attrs).get_requested_attributes() |
| 174 | + assert requested_attrs == expected |
| 175 | + |
| 176 | + |
| 177 | +def test_multi_preds(caplog): |
| 178 | + expected = [ |
| 179 | + { |
| 180 | + "name": "name", |
| 181 | + "restrictions": [ |
| 182 | + {"cred_def_id": "3avoBCqDMFHFaKUHug9s8W:3:CL:13:default"}, |
| 183 | + {"value": "Alice Jones"}, |
| 184 | + ], |
| 185 | + }, |
| 186 | + { |
| 187 | + "name": "name", |
| 188 | + "restrictions": [ |
| 189 | + {"cred_def_id": "3avoBCqDMFHFaKUHug9s8W:3:CL:13:default"}, |
| 190 | + {"value": "Alice Jones"}, |
| 191 | + ], |
| 192 | + }, |
| 193 | + ] |
| 194 | + requested_attrs = ProofRequest(pytest.multi_predicates).get_requested_predicates() |
| 195 | + assert requested_attrs == expected |
| 196 | + |
| 197 | + |
| 198 | +def test_get_connection_id(): |
| 199 | + expected = "d965bd54-affb-4e11-ba8e-ea54a3214123" |
| 200 | + connection_id = ProofRequest(pytest.valid_proof_request).get_connection_id() |
| 201 | + assert connection_id == expected |
| 202 | + |
| 203 | + |
| 204 | +def test_get_version(): |
| 205 | + expected = "1.0.0" |
| 206 | + version = ProofRequest(pytest.valid_proof_request).get_version() |
| 207 | + assert version == expected |
| 208 | + |
| 209 | + |
| 210 | +def test_get_name(): |
| 211 | + expected = "Proof of Name" |
| 212 | + name = ProofRequest(pytest.valid_proof_request).get_name() |
| 213 | + assert name == expected |
0 commit comments