示例#1
0
def test_swagger_keyfile():
    keyfile = KeyFileFactory()
    as_dict = keyfile.to_dict()

    reconstructed = KeyFile.from_dict(as_dict)
    assert reconstructed.pseudonym_template == keyfile.pseudonym_template
    assert reconstructed.description == keyfile.description
    assert reconstructed.name == keyfile.name
    assert reconstructed.key == keyfile.key
示例#2
0
def test_pimsconnection(mock_pims_session):
    connection = PIMSConnection(session=mock_pims_session)
    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.DEIDENTIFY_CREATE_JSONOUTPUT_TRUE)
    connection.pseudonymize(key_file=KeyFileFactory(),
                            identifiers=[IdentifierFactory()])

    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.KEYFILES_PSEUDONYMS_REIDENTIFY_RESPONSE)
    connection.reidentify(key_file=KeyFileFactory(),
                          pseudonyms=[PseudonymFactory()])
示例#3
0
def test_deidentify(mock_pims_session):
    connection = PIMSConnection(session=mock_pims_session)
    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.DEIDENTIFY_CREATE_JSONOUTPUT_TRUE)
    identifiers = [IdentifierFactory(), IdentifierFactory()]
    keys = connection.pseudonymize(key_file=KeyFileFactory(),
                                   identifiers=identifiers)
    assert len(keys) == 2
    assert str(keys[0].identifier) == str(identifiers[0])

    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.DEIDENTIFY_CREATE_JSONOUTPUT_TRUE_INVALID)

    with pytest.raises(PIMSSwaggerException) as e:
        connection.pseudonymize(key_file=KeyFileFactory(),
                                identifiers=identifiers)
示例#4
0
def test_keyfiles_get_users(mock_pims_session):
    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.GET_USERS_FOR_KEYFILE_RESPONSE)
    entrypoint = KeyFiles(mock_pims_session)

    users = entrypoint.get_users(key_file=KeyFileFactory())

    assert len(users) == 2
    assert users[1].key == 22
示例#5
0
def test_set_keys(mock_pims_session, some_patient_id_keys):
    """Add an pseudonym-identifier pair"""
    connection = PIMSConnection(session=mock_pims_session)
    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.
        KEYFILES_PSEUDONYMS_REIDENTIFY_NON_EXISTENT_RESPONSE)
    # keys need to be patientID to match mocked response above

    connection.set_keys(key_file=KeyFileFactory(), keys=some_patient_id_keys)
示例#6
0
def test_keyfiles_reidentify_chunked(mock_pims_session):
    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.KEYFILES_PSEUDONYMS_REIDENTIFY_RESPONSE
    )
    entrypoint = KeyFiles(mock_pims_session)

    _ = entrypoint.reidentify(
        key_file=KeyFileFactory(), pseudonyms=[PseudoPatientIDFactory() for _ in range(2000)]
    )
    reidentified_items = mock_pims_session.session.requests_mock.post.mock_calls[0][2]['params']['items']
    assert len(reidentified_items) <= 501
示例#7
0
def test_set_keys_existing_pseudonym(mock_pims_session, some_patient_id_keys):
    """Setting a pseudonym that is already used for a different identifier should
    fail
    """
    connection = PIMSConnection(session=mock_pims_session)
    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.DEIDENTIFY_FAILED_TO_INSERT)
    # keys need to be patientID to match mocked response above
    with pytest.raises(PIMSServerException):
        connection.set_keys(key_file=KeyFileFactory(),
                            keys=some_patient_id_keys)
示例#8
0
def test_set_keys_existing_identity(mock_pims_session, some_patient_id_keys):
    """I want to add an pseudonym-identifier pair, but the identifier already
    exists
    """
    connection = PIMSConnection(session=mock_pims_session)
    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.
        KEYFILES_PSEUDONYMS_REIDENTIFY_EXISTENT_RESPONSE)
    # keys need to be patientID to match mocked response above
    with pytest.raises(PIMSServerException):
        connection.set_keys(key_file=KeyFileFactory(),
                            keys=some_patient_id_keys)
示例#9
0
def mock_project_requests(requests_mock):
    """Mock project that simulates actual requests.post calls. Will
    raise PIMSServerException for invalid return values, but allows checking what
    is sent to server
    """
    mock_session = Mock()
    project = Project(
        key_file_id=1,
        connection=PIMSConnection(
            session=PIMSSession(mock_session, base_url="/test")),
    )
    project.get_key_file = lambda: KeyFileFactory()  # make sure this passes
    return project
示例#10
0
def test_keyfiles_pseudonymize(mock_pims_session):
    """Get a pseudonym, mock server response"""
    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.DEIDENTIFY_CREATE_JSONOUTPUT_TRUE)
    entrypoint = KeyFiles(mock_pims_session)
    keys = entrypoint.pseudonymize(
        key_file=KeyFileFactory(),
        identifiers=[
            Identifier(value="Jack de Boer", source="Test"),
            Identifier(value="Chris van Os", source="Test"),
        ],
    )
    assert len(keys) == 2
    assert keys[0].identifier.value == "Jack de Boer"
示例#11
0
def test_keyfiles_pseudonymize_chunk_size(mock_pims_session):
    """In live testing the PIMS server was found not to be able to handle API requests with more then 1000 items
    Check the chunking system to work around this"""

    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.DEIDENTIFY_CREATE_JSONOUTPUT_TRUE
    )
    entrypoint = KeyFiles(mock_pims_session)
    _ = entrypoint.pseudonymize(
        key_file=KeyFileFactory(),
        identifiers=[PatientIDFactory() for _ in range(2000)],
    )
    values_posted = mock_pims_session.session.requests_mock.post.mock_calls[0][2]['json'][0]['values']
    assert len(values_posted) <= 1001
示例#12
0
def test_keyfiles_pseudonymize_different_sources(mock_pims_session):
    """Get a pseudonym, mock server response. Two different sources should yield separate calls for each source"""
    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.DEIDENTIFY_CREATE_JSONOUTPUT_TRUE
    )
    entrypoint = KeyFiles(mock_pims_session)
    keys = entrypoint.pseudonymize(
        key_file=KeyFileFactory(),
        identifiers=[
            Identifier(value="Jack de Boer", source="Test"),
            Identifier(value="Sarah Toh", source="Test_2"),
        ],
    )
    assert mock_pims_session.session.requests_mock.post.call_count == 2
示例#13
0
def test_keyfiles_reidentify(mock_pims_session):
    mock_pims_session.session.set_response_tuple(
        RequestsMockResponseExamples.KEYFILES_PSEUDONYMS_REIDENTIFY_RESPONSE
    )
    entrypoint = KeyFiles(mock_pims_session)

    keys = entrypoint.reidentify(
        key_file=KeyFileFactory(), pseudonyms=[
            PseudoPatientID(value='test'),
            PseudoPatientID(value='test2')]
    )

    assert len(keys) == 2
    assert keys[0].identifier.source == "PatientID"