Example #1
0
def test_get_token_and_send_request(client, version, https):

    base_url = '%s://localhost' % ('https' if https else 'http')

    # Get valid token
    response = client.post(
        AUTH_PATH,
        data={'username': USERNAME, 'password': PASSWORD},
        base_url=base_url
    )

    assert response.status_code == 200

    data = json.loads(response.get_data(as_text=True))
    token = data.get('token')

    assert token

    headers = prepare_headers(version, https)
    headers[HTTP_AUTHORIZATION] = 'Bearer %s' % token

    # Get correct response for invalid body
    response = client.post(
        INBOX['address'],
        data='invalid-body',
        headers=headers,
        base_url=base_url
    )

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)

    message = as_tm(version).get_message_from_xml(response.data)
    assert message.status_type == ST_BAD_MESSAGE

    request = as_tm(version).DiscoveryRequest(message_id=MESSAGE_ID)
    headers = prepare_headers(version, https)
    headers[HTTP_AUTHORIZATION] = 'Bearer %s' % token

    # Get correct response for valid request
    response = client.post(
        DISCOVERY['address'],
        data=request.to_xml(),
        headers=headers,
        base_url=base_url
    )

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version=version, https=https)

    message = as_tm(version).get_message_from_xml(response.data)

    assert isinstance(message, as_tm(version).DiscoveryResponse)

    from opentaxii import context
    assert not hasattr(context, 'account')
def test_subscribe(server, version, https):

    service = server.get_service('collection-management-A')
    poll_service = server.get_service('poll-A')

    headers = prepare_headers(version, https)

    params = dict(
        response_type=RT_FULL,
        content_bindings=[CB_STIX_XML_111, CUSTOM_CONTENT_BINDING]
    )

    request = prepare_request(
        collection=COLLECTION_OPEN, action=ACT_SUBSCRIBE,
        version=version, params=params)

    response = service.process(headers, request)

    if version == 11:
        assert isinstance(
            response,
            as_tm(version).ManageCollectionSubscriptionResponse)
        assert response.collection_name == COLLECTION_OPEN
    else:
        assert isinstance(
            response,
            as_tm(version).ManageFeedSubscriptionResponse)
        assert response.feed_name == COLLECTION_OPEN

    assert response.message == SUBSCRIPTION_MESSAGE
    assert len(response.subscription_instances) == 1

    subs = response.subscription_instances[0]

    assert subs.subscription_id

    # 1 poll service * 2 protocol bindings
    assert len(subs.poll_instances) == 2
    assert (
        subs.poll_instances[0].poll_address ==
        poll_service.get_absolute_address(
            subs.poll_instances[0].poll_protocol))

    if version == 11:
        assert subs.status == SS_ACTIVE

        response_bindings = [
            b.binding_id
            for b in subs.subscription_parameters.content_bindings]

        assert response_bindings == params['content_bindings']
        assert (
            subs.subscription_parameters.response_type ==
            params['response_type'])
Example #3
0
def test_discovery_request(server, version, https):

    request = as_tm(version).DiscoveryRequest(message_id=MESSAGE_ID)
    service = server.get_service('discovery-A')

    headers = prepare_headers(version, https)
    response = service.process(headers, request)

    assert len(response.service_instances) == INSTANCES_CONFIGURED
    assert response.in_response_to == MESSAGE_ID

    assert isinstance(response, as_tm(version).DiscoveryResponse)
Example #4
0
def test_content_bindings_present(server, version, https):

    request = as_tm(version).DiscoveryRequest(message_id=MESSAGE_ID)
    service = server.get_service('discovery-A')

    headers = prepare_headers(version, https)
    response = service.process(headers, request)

    assert len(response.service_instances) == INSTANCES_CONFIGURED
    assert response.in_response_to == MESSAGE_ID

    inboxes = [
        s for s in response.service_instances
        if s.service_type == SVC_INBOX]

    assert len(inboxes) == 4

    address_a = INBOX_A['address']
    inboxes_a = [i for i in inboxes if i.service_address.endswith(address_a)]
    # inbox_a accepts everything, so inbox_service_accepted_content is empty
    assert all([len(i.inbox_service_accepted_content) == 0 for i in inboxes_a])

    address_b = INBOX_B['address']
    inboxes_b = [i for i in inboxes if i.service_address.endswith(address_b)]
    bindings = inboxes_b[0].inbox_service_accepted_content

    if version == 10:
        binding_ids = bindings
    else:
        binding_ids = [b.binding_id for b in bindings]

    assert set(binding_ids) == set(INBOX_B['supported_content'])
Example #5
0
def test_poll_empty_response(server, version, https, count_blocks):

    server.config['count_blocks_in_poll_responses'] = count_blocks

    service = server.get_service('poll-A')

    headers = prepare_headers(version, https)
    request = prepare_request(
        collection_name=COLLECTION_OPEN, version=version)

    if version == 11:
        response = service.process(headers, request)

        assert isinstance(response, as_tm(version).PollResponse)

        if count_blocks:
            assert response.record_count.record_count == 0
            assert not response.record_count.partial_count
        else:
            assert response.record_count is None

    else:
        # COLLECTION_OPEN type (SET) is not supported in TAXII 1.0
        with pytest.raises(exceptions.StatusMessageException):
            response = service.process(headers, request)

    server.config['count_blocks_in_poll_responses'] = True
Example #6
0
def test_inbox_request_inbox_valid_content_binding(server, version, https):

    inbox = server.get_service('inbox-B')

    blocks = [
        make_content(
            version,
            content_binding=CUSTOM_CONTENT_BINDING,
            subtype=CONTENT_BINDING_SUBTYPE),
        make_content(
            version,
            content_binding=CB_STIX_XML_111)
    ]

    inbox_message = make_inbox_message(
        version, dest_collection=COLLECTION_OPEN, blocks=blocks)
    headers = prepare_headers(version, https)

    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    # all blocks
    blocks = server.persistence.get_content_blocks(collection_id=None)
    assert len(blocks) == len(blocks)
Example #7
0
def test_inbox_request_all_content(server, version, https):

    inbox_a = server.get_service('inbox-A')

    headers = prepare_headers(version, https)

    blocks = [
        make_content(
            version,
            content_binding=CUSTOM_CONTENT_BINDING,
            subtype=CONTENT_BINDING_SUBTYPE),
        make_content(
            version,
            content_binding=INVALID_CONTENT_BINDING)
    ]
    inbox_message = make_inbox_message(version, blocks=blocks)

    # "inbox-A" accepts all content
    response = inbox_a.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)

    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    blocks = server.persistence.get_content_blocks(None)
    assert len(blocks) == len(blocks)
def test_pause_resume_wrong_id(server, https):

    version = 11

    service = server.get_service('collection-management-A')
    poll_service = server.get_service('poll-A')

    headers = prepare_headers(version, https)

    # Subscribing
    request = prepare_request(collection=COLLECTION_OPEN, action=ACT_SUBSCRIBE,
            version=version)

    response = service.process(headers, request)

    assert isinstance(response, as_tm(version).ManageCollectionSubscriptionResponse)
    assert response.collection_name == COLLECTION_OPEN

    assert len(response.subscription_instances) == 1
    subs = response.subscription_instances[0]

    assert subs.subscription_id
    assert subs.status == SS_ACTIVE

    # Pausing with wrong subscription ID
    with pytest.raises(exceptions.StatusMessageException):
        request = prepare_request(collection=COLLECTION_OPEN, action=ACT_PAUSE,
                subscription_id="RANDOM-WRONG-SUBSCRIPTION", version=version)
        response = service.process(headers, request)

    # Resuming with wrong subscription ID
    with pytest.raises(exceptions.StatusMessageException):
        request = prepare_request(collection=COLLECTION_OPEN, action=ACT_RESUME,
                subscription_id="RANDOM-WRONG-SUBSCRIPTION", version=version)
        response = service.process(headers, request)
Example #9
0
def test_collection_access_private_poll(client, version, https):

    # POLL_CLOSED collection allowed read access
    url, headers = prepare_url_headers(version, https, 'johnny', 'johnny')
    request = prepare_poll_request(
        'collection-1', version, bindings=[CB_STIX_XML_111])

    response = client.post(
        POLL_CLOSED['address'],
        data=request.to_xml(),
        headers=headers,
        base_url=url)
    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)
    message = as_tm(version).get_message_from_xml(response.data)
    assert message.message_type == 'Poll_Response'

    # POLL_CLOSED collection disallowed read access
    url, headers = prepare_url_headers(version, https, 'billy', 'billy')
    request = prepare_poll_request(
        'collection-2', version, bindings=[CB_STIX_XML_111])

    response = client.post(
        POLL_CLOSED['address'],
        data=request.to_xml(),
        headers=headers,
        base_url=url)
    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)
    message = as_tm(version).get_message_from_xml(response.data)
    assert message.status_type == 'NOT_FOUND'

    # POLL_CLOSED collection admin access
    url, headers = prepare_url_headers(version, https, 'wally', 'wally')
    request = prepare_poll_request(
        'collection-2', version, bindings=[CB_STIX_XML_111])

    response = client.post(
        POLL_CLOSED['address'],
        data=request.to_xml(),
        headers=headers,
        base_url=url)
    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)
    message = as_tm(version).get_message_from_xml(response.data)
    assert message.message_type == 'Poll_Response'
Example #10
0
def test_request_with_basic_auth(client, version, https):

    base_url = '%s://localhost' % ('https' if https else 'http')
    basic_auth_header = 'Basic {}'.format(
        basic_auth_token(USERNAME, PASSWORD).decode('utf-8'))

    headers = prepare_headers(version, https)
    headers[HTTP_AUTHORIZATION] = basic_auth_header

    # Get correct response for invalid body
    response = client.post(
        INBOX['address'],
        data='invalid-body',
        headers=headers,
        base_url=base_url
    )

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)

    message = as_tm(version).get_message_from_xml(response.data)
    assert message.status_type == ST_BAD_MESSAGE

    request = as_tm(version).DiscoveryRequest(message_id=MESSAGE_ID)
    headers = prepare_headers(version, https)
    headers[HTTP_AUTHORIZATION] = basic_auth_header

    # Get correct response for valid request
    response = client.post(
        DISCOVERY['address'],
        data=request.to_xml(),
        headers=headers,
        base_url=base_url
    )

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version=version, https=https)

    message = as_tm(version).get_message_from_xml(response.data)

    assert isinstance(message, as_tm(version).DiscoveryResponse)

    from opentaxii import context
    assert not hasattr(context, 'account')
Example #11
0
def test_successful_response(client, version, https):

    request = as_tm(version).DiscoveryRequest(message_id=MESSAGE_ID)
    base_url = '%s://localhost' % ('https' if https else 'http')

    response = client.post(
        DISCOVERY['address'],
        data = request.to_xml(),
        headers = prepare_headers(version=version, https=https),
        base_url = base_url
    )

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version=version, https=https)

    message = as_tm(version).get_message_from_xml(response.data)

    assert isinstance(message, as_tm(version).DiscoveryResponse)
    assert len(message.service_instances) == INSTANCES_CONFIGURED
Example #12
0
def test_invalid_basic_auth_request(client, version, https):

    base_url = '%s://localhost' % ('https' if https else 'http')

    headers = prepare_headers(version, https)
    headers[HTTP_AUTHORIZATION] = 'Basic somevalue'

    request = as_tm(version).DiscoveryRequest(message_id=MESSAGE_ID)
    response = client.post(
        DISCOVERY['address'],
        data=request.to_xml(),
        headers=headers,
        base_url=base_url)

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)

    message = as_tm(version).get_message_from_xml(response.data)
    assert message.status_type == ST_UNAUTHORIZED
Example #13
0
def test_poll_get_content(server, version, https):

    service = server.get_service('poll-A')
    original = persist_content(
        server.persistence, COLLECTION_ONLY_STIX,
        service.id, binding=CB_STIX_XML_111)

    # wrong collection
    headers = prepare_headers(version, https)
    request = prepare_request(
        collection_name=COLLECTION_STIX_AND_CUSTOM,
        version=version)

    response = service.process(headers, request)

    assert isinstance(response, as_tm(version).PollResponse)
    assert len(response.content_blocks) == 0

    # right collection
    headers = prepare_headers(version, https)
    request = prepare_request(
        collection_name=COLLECTION_ONLY_STIX,
        version=version)

    response = service.process(headers, request)

    assert isinstance(response, as_tm(version).PollResponse)
    assert len(response.content_blocks) == 1

    block = response.content_blocks[0]

    assert original.content == block.content.encode('utf-8')
    assert original.timestamp_label == block.timestamp_label

    # right collection and request with wrong content_type
    headers = prepare_headers(version, https)
    request = prepare_request(
        collection_name=COLLECTION_ONLY_STIX,
        version=version, bindings=[CUSTOM_CONTENT_BINDING])

    with pytest.raises(exceptions.StatusMessageException):
        service.process(headers, request)
Example #14
0
def test_services_available(client, version, https):

    headers = prepare_headers(version, https)

    request = as_tm(version).DiscoveryRequest(message_id=MESSAGE_ID)
    base_url = '%s://localhost' % ('https' if https else 'http')

    response = client.post(
        DISCOVERY_NOT_AVAILABLE['address'],
        data = request.to_xml(),
        headers = headers,
        base_url = base_url
    )

    assert response.status_code == 200

    message = as_tm(version).get_message_from_xml(response.data)

    assert isinstance(message, as_tm(version).StatusMessage)
    assert message.status_type == ST_FAILURE
Example #15
0
def test_request_with_basic_auth(client, version, https):

    base_url = '%s://localhost' % ('https' if https else 'http')
    basic_auth_header = 'Basic {}'.format(basic_auth_token(USERNAME, PASSWORD))

    headers = prepare_headers(version, https)
    headers[HTTP_AUTHORIZATION] = basic_auth_header

    # Get correct response for invalid body
    response = client.post(INBOX['address'],
                           data='invalid-body',
                           headers=headers,
                           base_url=base_url)

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)

    message = as_tm(version).get_message_from_xml(response.data)
    assert message.status_type == ST_BAD_MESSAGE

    request = as_tm(version).DiscoveryRequest(message_id=MESSAGE_ID)
    headers = prepare_headers(version, https)
    headers[HTTP_AUTHORIZATION] = basic_auth_header

    # Get correct response for valid request
    response = client.post(DISCOVERY['address'],
                           data=request.to_xml(),
                           headers=headers,
                           base_url=base_url)

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version=version, https=https)

    message = as_tm(version).get_message_from_xml(response.data)

    assert isinstance(message, as_tm(version).DiscoveryResponse)

    from opentaxii import context
    assert not hasattr(context, 'account')
Example #16
0
def test_invalid_auth_header_request(client, version, https):

    base_url = '%s://localhost' % ('https' if https else 'http')

    headers = prepare_headers(version, https)

    basic_auth_header = 'Foo {}'.format(basic_auth_token(USERNAME, PASSWORD))
    headers[HTTP_AUTHORIZATION] = basic_auth_header

    request = as_tm(version).DiscoveryRequest(message_id=MESSAGE_ID)
    response = client.post(
        DISCOVERY['address'],
        data=request.to_xml(),
        headers=headers,
        base_url=base_url
    )

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)

    message = as_tm(version).get_message_from_xml(response.data)
    assert message.status_type == ST_UNAUTHORIZED
Example #17
0
def test_post_parse_verification(client, version, https):

    headers = prepare_headers(version, https)
    headers[HTTP_X_TAXII_SERVICES] = 'invalid-services-spec'

    request = as_tm(version).DiscoveryRequest(message_id=MESSAGE_ID)
    base_url = '%s://localhost' % ('https' if https else 'http')

    response = client.post(
        DISCOVERY['address'],
        data = request.to_xml(),
        headers = headers,
        base_url = base_url
    )

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version=version, https=https)

    message = as_tm(version).get_message_from_xml(response.data)

    assert message.status_type == ST_FAILURE
    assert message.in_response_to == MESSAGE_ID
Example #18
0
def test_poll_get_content(server, version, https):

    service = server.get_service('poll-A')
    original = persist_content(server.persistence, COLLECTION_ONLY_STIX,
            service.id, binding=CB_STIX_XML_111)

    # wrong collection
    headers = prepare_headers(version, https)
    request = prepare_request(collection_name=COLLECTION_STIX_AND_CUSTOM,
            version=version)

    response = service.process(headers, request)

    assert isinstance(response, as_tm(version).PollResponse)
    assert len(response.content_blocks) == 0

    # right collection
    headers = prepare_headers(version, https)
    request = prepare_request(collection_name=COLLECTION_ONLY_STIX,
            version=version)

    response = service.process(headers, request)

    assert isinstance(response, as_tm(version).PollResponse)
    assert len(response.content_blocks) == 1

    block = response.content_blocks[0]

    assert original.content == block.content
    assert original.timestamp_label == block.timestamp_label

    # right collection and request with wrong content_type
    headers = prepare_headers(version, https)
    request = prepare_request(collection_name=COLLECTION_ONLY_STIX,
            version=version, bindings=[CUSTOM_CONTENT_BINDING])

    with pytest.raises(exceptions.StatusMessageException):
        response = service.process(headers, request)
def test_collections(server, version, https):

    service = server.get_service('collection-management-A')

    headers = prepare_headers(version, https)
    request = prepare_request(version)
    response = service.process(headers, request)

    names = [c.name for c in COLLECTIONS_B]
    
    if version == 11:
        assert isinstance(response, as_tm(version).CollectionInformationResponse)
        assert len(response.collection_informations) == len(COLLECTIONS_B)

        for c in response.collection_informations:
            assert c.collection_name in names

    else:
        assert isinstance(response, as_tm(version).FeedInformationResponse)
        assert len(response.feed_informations) == len(COLLECTIONS_B)

        for c in response.feed_informations:
            assert c.feed_name in names
Example #20
0
def test_collections(server, version, https):

    service = server.get_service('collection-management-A')

    headers = prepare_headers(version, https)
    request = prepare_request(version)
    response = service.process(headers, request)

    names = [c.name for c in COLLECTIONS_B]

    if version == 11:
        assert isinstance(
            response, as_tm(version).CollectionInformationResponse)
        assert len(response.collection_informations) == len(COLLECTIONS_B)

        for c in response.collection_informations:
            assert c.collection_name in names

    else:
        assert isinstance(response, as_tm(version).FeedInformationResponse)
        assert len(response.feed_informations) == len(COLLECTIONS_B)

        for c in response.feed_informations:
            assert c.feed_name in names
Example #21
0
def test_unauthorized_request(app, client, version, https):
    base_url = '%s://localhost' % ('https' if https else 'http')
    response = client.post(
        INBOX_OPEN['address'],
        data='invalid-body',
        headers=prepare_headers(version, https),
        base_url=base_url)

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)

    message = as_tm(version).get_message_from_xml(response.data)
    assert message.status_type == ST_UNAUTHORIZED

    from opentaxii import context
    assert not hasattr(context, 'account')
Example #22
0
def test_status_message_response(client, version, https):

    base_url = '%s://localhost' % ('https' if https else 'http')

    response = client.post(
        INBOX['address'],
        data = 'invalid-body',
        headers = prepare_headers(version, https),
        base_url = base_url
    )

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)

    message = as_tm(version).get_message_from_xml(response.data)

    assert message.status_type == ST_BAD_MESSAGE
Example #23
0
def test_inbox_request_destination_collection(server, https):
    version = 11

    inbox_message = make_inbox_message(version, blocks=[make_content(version)], dest_collection=None)
    headers = prepare_headers(version, https)

    inbox = server.get_service('inbox-A')
    # destination collection is not required for inbox-A
    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS

    inbox = server.get_service('inbox-B')
    # destination collection is required for inbox-B
    with pytest.raises(exceptions.StatusMessageException):
        response = inbox.process(headers, inbox_message)
Example #24
0
def test_unauthorized_request(client, version, https):

    base_url = '%s://localhost' % ('https' if https else 'http')

    response = client.post(INBOX['address'],
                           data='invalid-body',
                           headers=prepare_headers(version, https),
                           base_url=base_url)

    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)

    message = as_tm(version).get_message_from_xml(response.data)
    assert message.status_type == ST_UNAUTHORIZED

    from opentaxii import context
    assert not hasattr(context, 'account')
Example #25
0
def test_poll_empty_response(server, version, https):

    service = server.get_service('poll-A')

    headers = prepare_headers(version, https)
    request = prepare_request(collection_name=COLLECTION_OPEN, version=version)

    if version == 11:
        response = service.process(headers, request)

        assert isinstance(response, as_tm(version).PollResponse)

        assert response.record_count.record_count == 0
        assert not response.record_count.partial_count
    else:
        # COLLECTION_OPEN type (SET) is not supported in TAXII 1.0
        with pytest.raises(exceptions.StatusMessageException):
            response = service.process(headers, request)
Example #26
0
def test_inbox_request_destination_collection(server, https):
    version = 11

    inbox_message = make_inbox_message(
        version, blocks=[make_content(version)], dest_collection=None)
    headers = prepare_headers(version, https)

    inbox = server.get_service('inbox-A')
    # destination collection is not required for inbox-A
    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS

    inbox = server.get_service('inbox-B')
    # destination collection is required for inbox-B
    with pytest.raises(exceptions.StatusMessageException):
        response = inbox.process(headers, inbox_message)
Example #27
0
def test_inbox_request_inbox_invalid_inbox_content_binding(server, version, https):

    inbox = server.get_service('inbox-B')

    content = make_content(version, content_binding=INVALID_CONTENT_BINDING)
    inbox_message = make_inbox_message(version, dest_collection=COLLECTION_OPEN, blocks=[content])

    headers = prepare_headers(version, https)

    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    blocks = server.persistence.get_content_blocks(None)

    # Content blocks with invalid content should be ignored
    assert len(blocks) == 0
Example #28
0
def test_inbox_request_inbox_valid_content_binding(server, version, https):

    inbox = server.get_service('inbox-B')

    blocks = [
        make_content(version, content_binding=CUSTOM_CONTENT_BINDING, subtype=CONTENT_BINDING_SUBTYPE),
        make_content(version, content_binding=CB_STIX_XML_111)
    ]

    inbox_message = make_inbox_message(version, dest_collection=COLLECTION_OPEN, blocks=blocks)
    headers = prepare_headers(version, https)

    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    blocks = server.persistence.get_content_blocks(collection_id=None) # all blocks
    assert len(blocks) == len(blocks)
Example #29
0
def test_inbox_req_inbox_invalid_inbox_content_binding(server, version, https):

    inbox = server.get_service('inbox-B')

    content = make_content(version, content_binding=INVALID_CONTENT_BINDING)
    inbox_message = make_inbox_message(
        version, dest_collection=COLLECTION_OPEN, blocks=[content])

    headers = prepare_headers(version, https)

    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    blocks = server.persistence.get_content_blocks(None)

    # Content blocks with invalid content should be ignored
    assert len(blocks) == 0
Example #30
0
def test_inbox_request_all_content(server, version, https):

    inbox_a = server.get_service('inbox-A')

    headers = prepare_headers(version, https)

    blocks = [
        make_content(version, content_binding=CUSTOM_CONTENT_BINDING, subtype=CONTENT_BINDING_SUBTYPE),
        make_content(version, content_binding=INVALID_CONTENT_BINDING)
    ]
    inbox_message = make_inbox_message(version, blocks=blocks)

    # "inbox-A" accepts all content
    response = inbox_a.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)

    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    blocks = server.persistence.get_content_blocks(None)
    assert len(blocks) == len(blocks)
Example #31
0
def test_pause_resume_wrong_id(server, https):

    version = 11
    service = server.get_service('collection-management-A')

    headers = prepare_headers(version, https)

    # Subscribing
    request = prepare_request(collection=COLLECTION_OPEN,
                              action=ACT_SUBSCRIBE,
                              version=version)

    response = service.process(headers, request)

    assert isinstance(response,
                      as_tm(version).ManageCollectionSubscriptionResponse)
    assert response.collection_name == COLLECTION_OPEN

    assert len(response.subscription_instances) == 1
    subs = response.subscription_instances[0]

    assert subs.subscription_id
    assert subs.status == SS_ACTIVE

    # Pausing with wrong subscription ID
    with pytest.raises(exceptions.StatusMessageException):
        request = prepare_request(collection=COLLECTION_OPEN,
                                  action=ACT_PAUSE,
                                  subscription_id="RANDOM-WRONG-SUBSCRIPTION",
                                  version=version)
        response = service.process(headers, request)

    # Resuming with wrong subscription ID
    with pytest.raises(exceptions.StatusMessageException):
        request = prepare_request(collection=COLLECTION_OPEN,
                                  action=ACT_RESUME,
                                  subscription_id="RANDOM-WRONG-SUBSCRIPTION",
                                  version=version)
        response = service.process(headers, request)
Example #32
0
def test_inbox_restricted_inbox_stix111_as_stix111(server, version, https):

    inbox = server.get_service('inbox-B')

    content = make_content(version,
                           content=STIX_111_CONTENT,
                           content_binding=CB_STIX_XML_111)
    inbox_message = make_inbox_message(version,
                                       dest_collection=COLLECTION_ONLY_STIX,
                                       blocks=[content])

    headers = prepare_headers(version, https)

    response = inbox.process(headers, inbox_message)

    assert isinstance(response, as_tm(version).StatusMessage)
    assert response.status_type == ST_SUCCESS
    assert response.in_response_to == MESSAGE_ID

    blocks = server.persistence.get_content_blocks(None)

    # Content blocks with invalid content should be ignored
    assert len(blocks) == 1
Example #33
0
def test_subscribe_pause_resume(server, https):

    version = 11

    service = server.get_service('collection-management-A')

    headers = prepare_headers(version, https)

    params = dict(response_type=RT_FULL,
                  content_bindings=[CB_STIX_XML_111, CUSTOM_CONTENT_BINDING])

    # Subscribing
    request = prepare_request(collection=COLLECTION_OPEN,
                              action=ACT_SUBSCRIBE,
                              version=version,
                              params=params)

    response = service.process(headers, request)

    assert isinstance(response,
                      as_tm(version).ManageCollectionSubscriptionResponse)
    assert response.collection_name == COLLECTION_OPEN

    assert len(response.subscription_instances) == 1

    subs = response.subscription_instances[0]

    assert subs.status == SS_ACTIVE
    assert (server.persistence.get_subscription(
        subs.subscription_id).status == SS_ACTIVE)

    # Pausing
    request = prepare_request(collection=COLLECTION_OPEN,
                              action=ACT_PAUSE,
                              subscription_id=subs.subscription_id,
                              version=version)

    response = service.process(headers, request)

    assert isinstance(response,
                      as_tm(version).ManageCollectionSubscriptionResponse)
    assert response.collection_name == COLLECTION_OPEN

    assert len(response.subscription_instances) == 1

    subs = response.subscription_instances[0]

    assert subs.subscription_id
    assert subs.status == SS_PAUSED
    assert (server.persistence.get_subscription(
        subs.subscription_id).status == SS_PAUSED)

    # Resume
    request = prepare_request(collection=COLLECTION_OPEN,
                              action=ACT_RESUME,
                              subscription_id=subs.subscription_id,
                              version=version)

    response = service.process(headers, request)

    assert isinstance(response,
                      as_tm(version).ManageCollectionSubscriptionResponse)
    assert response.collection_name == COLLECTION_OPEN

    assert len(response.subscription_instances) == 1

    subs = response.subscription_instances[0]

    assert subs.subscription_id
    assert subs.status == SS_ACTIVE
    assert (server.persistence.get_subscription(
        subs.subscription_id).status == SS_ACTIVE)
Example #34
0
def test_collection_access_private_inbox(client, version, https):
    # INBOX read-only collection access
    url, headers = prepare_url_headers(version, https, 'johnny', 'johnny')
    request = prepare_inbox_message(
        version,
        dest_collection='collection-1',
        blocks=[make_inbox_content(version)])

    response = client.post(
        INBOX_CLOSED['address'],
        data=request.to_xml(),
        headers=headers,
        base_url=url)
    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)
    message = as_tm(version).get_message_from_xml(response.data)
    assert message.message_type == 'Status_Message'

    if version == 11:
        assert message.status_type == 'UNAUTHORIZED'
        assert (
            message.message ==
            'User can not write to collection collection-1')
    else:
        # Because in TAXII 1.0 destination collection can not be specified
        # so it impossible to verify access
        assert message.status_type == 'SUCCESS'

    # INBOX modify collection access
    request = prepare_inbox_message(
        version,
        dest_collection='collection-2',
        blocks=[make_inbox_content(version)])

    response = client.post(
        INBOX_CLOSED['address'],
        data=request.to_xml(),
        headers=headers,
        base_url=url)
    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)
    message = as_tm(version).get_message_from_xml(response.data)
    assert message.message_type == 'Status_Message'
    assert message.status_type == 'SUCCESS'

    # INBOX modify collection access
    url, headers = prepare_url_headers(version, https, 'wally', 'wally')
    request = prepare_inbox_message(
        version,
        dest_collection='collection-2',
        blocks=[make_inbox_content(version)])

    response = client.post(
        INBOX_CLOSED['address'],
        data=request.to_xml(),
        headers=headers,
        base_url=url)
    assert response.status_code == 200
    assert is_headers_valid(response.headers, version, https)
    message = as_tm(version).get_message_from_xml(response.data)
    assert message.message_type == 'Status_Message'
    assert message.status_type == 'SUCCESS'
def test_subscribe_pause_resume(server, https):

    version = 11

    service = server.get_service('collection-management-A')

    headers = prepare_headers(version, https)

    params = dict(
        response_type=RT_FULL,
        content_bindings=[CB_STIX_XML_111, CUSTOM_CONTENT_BINDING]
    )

    # Subscribing
    request = prepare_request(
        collection=COLLECTION_OPEN, action=ACT_SUBSCRIBE,
        version=version, params=params)

    response = service.process(headers, request)

    assert isinstance(
        response,
        as_tm(version).ManageCollectionSubscriptionResponse)
    assert response.collection_name == COLLECTION_OPEN

    assert len(response.subscription_instances) == 1

    subs = response.subscription_instances[0]

    assert subs.status == SS_ACTIVE
    assert (
        server.persistence.get_subscription(subs.subscription_id).status ==
        SS_ACTIVE)

    # Pausing
    request = prepare_request(
        collection=COLLECTION_OPEN, action=ACT_PAUSE,
        subscription_id=subs.subscription_id, version=version)

    response = service.process(headers, request)

    assert isinstance(
        response,
        as_tm(version).ManageCollectionSubscriptionResponse)
    assert response.collection_name == COLLECTION_OPEN

    assert len(response.subscription_instances) == 1

    subs = response.subscription_instances[0]

    assert subs.subscription_id
    assert subs.status == SS_PAUSED
    assert (
        server.persistence.get_subscription(subs.subscription_id).status ==
        SS_PAUSED)

    # Resume
    request = prepare_request(
        collection=COLLECTION_OPEN, action=ACT_RESUME,
        subscription_id=subs.subscription_id, version=version)

    response = service.process(headers, request)

    assert isinstance(
        response,
        as_tm(version).ManageCollectionSubscriptionResponse)
    assert response.collection_name == COLLECTION_OPEN

    assert len(response.subscription_instances) == 1

    subs = response.subscription_instances[0]

    assert subs.subscription_id
    assert subs.status == SS_ACTIVE
    assert (
        server.persistence.get_subscription(subs.subscription_id).status ==
        SS_ACTIVE)
Example #36
0
def prepare_request(version):
    module = as_tm(version)
    if version == 11:
        return module.CollectionInformationRequest(message_id=MESSAGE_ID)
    else:
        return module.FeedInformationRequest(message_id=MESSAGE_ID)