def test_bad_set(): """ Test that bad values for set raise error. """ with pytest.raises(TypeError): MessageTrustContext()['asdf'] = True with pytest.raises(TypeError): MessageTrustContext()[CONFIDENTIALITY] = 10
def test_cannot_alter_attributes(): """ Test that affirmed and denied attributes can't be altered """ mtc = MessageTrustContext() assert mtc.affirmed == NONE assert mtc.denied == NONE with pytest.raises(AttributeError): mtc.affirmed = CONFIDENTIALITY with pytest.raises(AttributeError): mtc.denied = CONFIDENTIALITY
def test_get_set(): """ Test using __getitem__ and __setitem__ for retrieving and changing contexts. """ mtc = MessageTrustContext() mtc[CONFIDENTIALITY] = True assert mtc.affirmed == CONFIDENTIALITY assert mtc[CONFIDENTIALITY] assert mtc.denied == NONE mtc[SIZE_OK] = True assert mtc.affirmed == CONFIDENTIALITY | SIZE_OK assert mtc[CONFIDENTIALITY | SIZE_OK] assert mtc.denied == NONE mtc[SIZE_OK] = False assert mtc.affirmed == CONFIDENTIALITY assert mtc[CONFIDENTIALITY] assert not mtc[CONFIDENTIALITY | SIZE_OK] assert mtc.denied == SIZE_OK mtc[AUTHENTICATED_ORIGIN] = True assert mtc.affirmed == CONFIDENTIALITY | AUTHENTICATED_ORIGIN assert mtc[CONFIDENTIALITY | AUTHENTICATED_ORIGIN] assert mtc.denied == SIZE_OK mtc[AUTHENTICATED_ORIGIN] = None assert mtc.affirmed == CONFIDENTIALITY assert mtc.denied == SIZE_OK
def test_message_serialization(): """Test deserializing and serializing a message""" msg = Message.deserialize(json.dumps({"@type": TEST_TYPE}), mtc=MessageTrustContext()) assert msg.type == TEST_TYPE assert msg.id is not None assert msg.mtc assert msg.serialize() == json.dumps({"@type": TEST_TYPE, "@id": msg.id})
def test_overlapping_affirm_denied_fail(plus, minus): """ Test overlapping affirm and denied flags raise error """ with pytest.raises(ContextsConflict): MessageTrustContext(plus, minus)
def test_str(plus, minus, expected): """ Test stringifying """ mtc = MessageTrustContext(plus, minus) assert str(mtc) == expected