Ejemplo n.º 1
0
def test_get_certificate_thing_exception(mock_api):
    '''
    Assert that an exception is thrown if the request fails and
    is not a ResourceNotFoundExeption
    '''
    mock_api.side_effect = ClientError(
        {
            'Error': {
                'Code': 'InvalidRequestException',
                'Message': 'something went wrong',
            }
        }, 'list_thing_principals')
    with pytest.raises(ClientError):
        get_certificate(thing='my-test-core')
Ejemplo n.º 2
0
def test_get_thing_exists_certificate_creates_monkeypatch():
    '''
    Assert that a new certificate is generated if the thing exists
    but no certificates are attached
    '''
    client = botocore.session.get_session().create_client('iot')
    stubber = Stubber(client)
    response = {
        'principals': [
            'arn:aws:iot:region:account_id:cert/foobar',
            'arn:aws:iot:region:account_id:cert/baz',
        ],
    }
    expected_params = {'thingName': 'my-test-core'}
    stubber.add_response('list_thing_principals', response, expected_params)
    stubber.activate()

    # We're going to patch the boto3 import in src.example
    with mock.patch('src.example.boto3') as mock_boto3:
        # Tell our mock object to return our own client when boto3.client is called
        mock_boto3.client.return_value = client
        cert = get_certificate(thing='my-test-core', )
        assert cert == [
            'arn:aws:iot:region:account_id:cert/foobar',
            'arn:aws:iot:region:account_id:cert/baz',
        ]
Ejemplo n.º 3
0
def test_get_thing_exists_certificate_creates():
    '''
    Assert that a new certificate is generated if the thing exists
    but no certificates are attached
    '''
    client = botocore.session.get_session().create_client('iot')
    stubber = Stubber(client)
    response_list = {
        'principals': [],
    }
    response_create = {
        'certificateArn':
        'arn:aws:iot:region:account_id:cert/0000000000000000000000000000000000000000000000000000000000000000',
        'certificateId':
        '0000000000000000000000000000000000000000000000000000000000000000',
        'certificatePem': 'foo',
        'keyPair': {
            'PublicKey': 'bar',
            'PrivateKey': 'baz',
        },
    }
    stubber.add_response('list_thing_principals', response_list,
                         {'thingName': ANY})
    stubber.add_response('create_keys_and_certificate', response_create,
                         {'setAsActive': ANY})
    stubber.activate()

    cert = get_certificate(
        thing='my-test-core',
        client=client,
    )
    assert cert == [
        'arn:aws:iot:region:account_id:cert/0000000000000000000000000000000000000000000000000000000000000000'
    ]
Ejemplo n.º 4
0
def test_get_thing_and_certificate_exists(placebo_session):
    '''
    Test getting a thing with an existing certificate
    '''
    cert = get_certificate(thing='my-test-core')
    print(cert)
    assert cert == [
        'arn:aws:iot:region:account_id:cert/0000000000000000000000000000000000000000000000000000000000000000',
    ]
Ejemplo n.º 5
0
def test_get_thing_and_certificate_exists():
    '''
    Test getting a thing with an existing certificate
    '''
    cert = get_certificate(thing='my-test-core')
    assert cert

    iot_client = boto3.client('iot')
    response = iot_client.list_thing_principals(
        thingName='my-test-core',
    )
    # Expected to fail because of missing iot implementation
    assert response['principals']
Ejemplo n.º 6
0
def test_get_thing_and_certificate_exists(mock_api):
    '''
    Test getting a thing with an existing certificate
    '''
    mock_api.return_value = MockResponse(
        200,
        {
            'principals': [
                'arn:aws:iot:region:account_id:cert/foobar',
                'arn:aws:iot:region:account_id:cert/baz',
            ],
        },
    )
    cert = get_certificate(thing='my-test-core')
    assert cert == [
        'arn:aws:iot:region:account_id:cert/foobar',
        'arn:aws:iot:region:account_id:cert/baz',
    ]
Ejemplo n.º 7
0
def test_get_certificate_no_thing_cert_creates(mock_api):
    '''
    Test that a certificate is created if no thing exists and returns the cert
    '''
    mock_api.side_effect = [
        boto_client_error('ResourceNotFoundException',
                          'list_thing_principals'),
        MockResponse(
            200,
            {
                'certificateArn': 'arn:aws:iot:region:account_id:cert/foobar',
                'certificateId': 'barbaz',
                'certificatePem': 'foo',
                'keyPair': {
                    'PublicKey': 'bar',
                    'PrivateKey': 'baz',
                },
            },
        ),
    ]
    cert = get_certificate(thing='my-test-core')
    assert cert == ['arn:aws:iot:region:account_id:cert/foobar']
Ejemplo n.º 8
0
def test_get_thing_exists_certificate_creates(mock_api):
    '''
    Assert that a new certificate is generated if the thing exists
    but no certificates are attached
    '''
    mock_api.side_effect = [
        MockResponse(200, {
            'principals': [],
        }),
        MockResponse(
            200,
            {
                'certificateArn': 'arn:aws:iot:region:account_id:cert/foobar',
                'certificateId': 'barbaz',
                'certificatePem': 'foo',
                'keyPair': {
                    'PublicKey': 'bar',
                    'PrivateKey': 'baz',
                },
            },
        ),
    ]
    cert = get_certificate(thing='my-test-core')
    assert cert == ['arn:aws:iot:region:account_id:cert/foobar']
Ejemplo n.º 9
0
def test_get_thing_and_certificate_exists():
    '''
    Test getting a thing with an existing certificate
    '''
    client = botocore.session.get_session().create_client('iot')
    stubber = Stubber(client)
    response = {
        'principals': [
            'arn:aws:iot:region:account_id:cert/foobar',
            'arn:aws:iot:region:account_id:cert/baz',
        ],
    }
    expected_params = {'thingName': 'my-test-core'}
    stubber.add_response('list_thing_principals', response, expected_params)
    stubber.activate()

    cert = get_certificate(
        thing='my-test-core',
        client=client,
    )
    assert cert == [
        'arn:aws:iot:region:account_id:cert/foobar',
        'arn:aws:iot:region:account_id:cert/baz',
    ]