示例#1
0
 def test_wallet_owners_can_delete(
         self, client_bob, url_permission_link_detail,
         mock_successful_wallet_owner_calls,
         successful_wallet_owner_calls_assertions):
     response = client_bob.delete(url_permission_link_detail)
     AssertionHelper.HTTP_204(response)
     mock_successful_wallet_owner_calls.assert_calls(
         successful_wallet_owner_calls_assertions)
示例#2
0
    def test_agnostic_authentication(self, api_client, client_bob,
                                     client_alice, shipment_alice_with_device,
                                     tracking_data):
        signed_tracking_data = self.sign_tracking(tracking_data, self.device)
        response = api_client.post(self.url_device,
                                   {'payload': signed_tracking_data})
        AssertionHelper.HTTP_204(response)
        assert TrackingData.objects.all().count() == 1

        response = client_bob.post(self.url_device,
                                   {'payload': signed_tracking_data})
        AssertionHelper.HTTP_204(response)
        assert TrackingData.objects.all().count() == 2

        response = client_alice.post(self.url_device,
                                     {'payload': signed_tracking_data})
        AssertionHelper.HTTP_204(response)
        assert TrackingData.objects.all().count() == 3
示例#3
0
    def test_idempotent(self, api_client, document_shipment_alice):
        document_shipment_alice.upload_status = UploadStatus.COMPLETE
        document_shipment_alice.save()

        history_count = document_shipment_alice.history.count()
        self.s3_event["Records"][0]["s3"]["object"]["key"] = f"{random_id()}/{random_id()}/{random_id()}/{document_shipment_alice.id}.png"
        response = api_client.post(self.url, json.dumps(self.s3_event), content_type="application/json",
                                   X_NGINX_SOURCE='internal', X_SSL_CLIENT_VERIFY='SUCCESS',
                                   X_SSL_CLIENT_DN='/CN=document-management-s3-hook.test-internal')
        AssertionHelper.HTTP_204(response)
        document_shipment_alice.refresh_from_db()
        assert document_shipment_alice.upload_status == UploadStatus.COMPLETE
        assert document_shipment_alice.history.count() == history_count
示例#4
0
    def test_requires_shipment_association(self, api_client, shipment_alice,
                                           tracking_data):
        signed_tracking_data = self.sign_tracking(tracking_data, self.device)

        response = api_client.post(self.url_device,
                                   {'payload': signed_tracking_data})
        AssertionHelper.HTTP_403(
            response, error='No shipment/route found associated to device.')
        shipment_alice.device = self.device
        shipment_alice.save()

        response = api_client.post(self.url_device,
                                   {'payload': signed_tracking_data})
        AssertionHelper.HTTP_204(response)
        assert TrackingData.objects.all().count() == 1
示例#5
0
    def test_bulk_shipment_data(self, api_client, shipment_alice_with_device,
                                tracking_data):
        signed_tracking_data = self.sign_tracking(tracking_data, self.device)
        tracking_data['timestamp'] = (datetime.utcnow() +
                                      timedelta(minutes=5)).isoformat()
        signed_tracking_data_two = self.sign_tracking(tracking_data,
                                                      self.device)

        response = api_client.post(self.url_device,
                                   [{
                                       'payload': signed_tracking_data
                                   }, {
                                       'payload': signed_tracking_data_two
                                   }])
        AssertionHelper.HTTP_204(response)
        assert TrackingData.objects.all().count() == 2
示例#6
0
    def test_random_device_id(self, api_client, tracking_data,
                              shipment_alice_with_device):
        signed_tracking_data = self.sign_tracking(tracking_data, self.device)

        response = api_client.post(self.url_random,
                                   {'payload': signed_tracking_data})
        AssertionHelper.HTTP_403(
            response, error='No shipment/route found associated to device.')

        # Does not matter which device id is in the data just the id in the endpoint
        signed_tracking_data = self.sign_tracking(tracking_data,
                                                  self.device,
                                                  device_id=random_id())
        response = api_client.post(self.url_device,
                                   {'payload': signed_tracking_data})
        AssertionHelper.HTTP_204(response)
示例#7
0
def test_event_transfer(client_alice, token_event):
    url = reverse('event-list', kwargs={'version': 'v1'})
    data = {'events': token_event, 'project': 'ShipToken'}
    response = client_alice.post(url, data)
    assert response.status_code == status.HTTP_403_FORBIDDEN

    with patch('apps.eth.views.log_metric') as mocked:
        # Set NGINX headers for engine auth
        response = client_alice.post(
            url,
            data,
            X_NGINX_SOURCE='internal',
            X_SSL_CLIENT_VERIFY='SUCCESS',
            X_SSL_CLIENT_DN='/CN=engine.test-internal')
        AssertionHelper.HTTP_204(response)

        assert mocked.call_count == 2
示例#8
0
    def test_requires_document(self, api_client, document_shipment_alice, mock_s3_buckets):
        self.s3_event["Records"][0]["s3"]["object"]["key"] = f"{random_id()}/{random_id()}/{random_id()}/{random_id()}.png"
        response = api_client.post(self.url, json.dumps(self.s3_event), content_type="application/json",
                                   X_NGINX_SOURCE='internal', X_SSL_CLIENT_VERIFY='SUCCESS',
                                   X_SSL_CLIENT_DN='/CN=document-management-s3-hook.test-internal')
        AssertionHelper.HTTP_400(response, error='Document not found with ID')

        with mock.patch.object(requests.Session, 'post') as mock_method:
            mock_method.return_value = mocked_rpc_response({
                "jsonrpc": "2.0",
                "result": {
                    "success": False
                },
                "id": 0
            })
            self.s3_event["Records"][0]["s3"]["object"]["key"] = f"{random_id()}/{random_id()}/{random_id()}/{document_shipment_alice.id}.png"
            response = api_client.post(self.url, json.dumps(self.s3_event), content_type="application/json",
                                       X_NGINX_SOURCE='internal', X_SSL_CLIENT_VERIFY='SUCCESS',
                                       X_SSL_CLIENT_DN='/CN=document-management-s3-hook.test-internal')
            AssertionHelper.HTTP_500(response, error='Invalid response from Engine')

            mock_method.return_value = mocked_rpc_response({
                "jsonrpc": "2.0",
                "result": {
                    "success": True,
                    "vault_signed": {
                        'hash': 'VAULT_HASH'
                    }
                },
                "id": 0
            })
            self.s3_event["Records"][0]["s3"]["object"]["key"] = f"{random_id()}/{random_id()}/{random_id()}/{document_shipment_alice.id}.png"
            response = api_client.post(self.url, json.dumps(self.s3_event), content_type="application/json",
                                       X_NGINX_SOURCE='internal', X_SSL_CLIENT_VERIFY='SUCCESS',
                                       X_SSL_CLIENT_DN='/CN=document-management-s3-hook.test-internal')
            AssertionHelper.HTTP_204(response)
            document_shipment_alice.refresh_from_db()
            assert document_shipment_alice.upload_status == UploadStatus.COMPLETE
示例#9
0
def test_event_update(client_alice, shipment, contract_event,
                      mock_event_signal):
    url = reverse('event-list', kwargs={'version': 'v1'})
    # Create EthAction so events don't get ignored
    EthAction.objects.create(
        transaction_hash=contract_event['transactionHash'],
        async_job_id=shipment.asyncjob_set.first().id,
        shipment=shipment)

    data = {'events': contract_event, 'project': 'LOAD'}

    data_batched = {
        'events': [contract_event, contract_event],
        'project': 'LOAD'
    }

    response = client_alice.post(url, data)
    assert response.status_code == status.HTTP_403_FORBIDDEN

    # Set NGINX headers for engine auth
    response = client_alice.post(url,
                                 data,
                                 X_NGINX_SOURCE='internal',
                                 X_SSL_CLIENT_VERIFY='SUCCESS',
                                 X_SSL_CLIENT_DN='/CN=engine.test-internal')
    AssertionHelper.HTTP_204(response)

    # Test idempotency of Events
    num_events = Event.objects.count()
    response = client_alice.post(url,
                                 data,
                                 X_NGINX_SOURCE='internal',
                                 X_SSL_CLIENT_VERIFY='SUCCESS',
                                 X_SSL_CLIENT_DN='/CN=engine.test-internal')
    AssertionHelper.HTTP_204(response)
    assert Event.objects.count() == num_events

    response = client_alice.post(url,
                                 data_batched,
                                 X_NGINX_SOURCE='internal',
                                 X_SSL_CLIENT_VERIFY='SUCCESS',
                                 X_SSL_CLIENT_DN='/CN=engine.test-internal')
    AssertionHelper.HTTP_204(response)
    assert Event.objects.count() == num_events

    # Events should be idempotent by transactionHash and logIndex
    data['events']['blockNumber'] = 9999
    response = client_alice.post(url,
                                 data,
                                 X_NGINX_SOURCE='internal',
                                 X_SSL_CLIENT_VERIFY='SUCCESS',
                                 X_SSL_CLIENT_DN='/CN=engine.test-internal')
    AssertionHelper.HTTP_204(response)
    assert Event.objects.count() == num_events

    data['events']['logIndex'] = 1
    response = client_alice.post(url,
                                 data,
                                 X_NGINX_SOURCE='internal',
                                 X_SSL_CLIENT_VERIFY='SUCCESS',
                                 X_SSL_CLIENT_DN='/CN=engine.test-internal')
    AssertionHelper.HTTP_204(response)
    assert Event.objects.count() == (num_events + 1)
示例#10
0
 def test_batch_signed_data_succeeds(self, api_client, create_batch_signed_telemetry_post):
     response = api_client.post(self.telemetry_url, create_batch_signed_telemetry_post)
     AssertionHelper.HTTP_204(response)
     assert TelemetryData.objects.all().count() == 2
示例#11
0
 def test_owner_can_delete_with_permission_link(self, client_alice):
     response = client_alice.delete(self.url_valid_permission)
     AssertionHelper.HTTP_204(response)
示例#12
0
 def test_owner_can_delete(self, client_alice, url_permission_link_detail):
     response = client_alice.delete(url_permission_link_detail)
     AssertionHelper.HTTP_204(response)
示例#13
0
 def test_not_yet_started(self, client_alice):
     response = client_alice.delete(self.url_route)
     AssertionHelper.HTTP_204(response)
示例#14
0
 def test_org_member_can_delete(self, client_carol, new_route):
     response = client_carol.delete(self.url_route)
     AssertionHelper.HTTP_204(response)
     route = Route.objects.filter(pk=new_route.id).first()
     assert not route
示例#15
0
 def test_creator_can_delete(self, client_alice, new_route):
     response = client_alice.delete(self.url_route)
     AssertionHelper.HTTP_204(response)
     route = Route.objects.filter(pk=new_route.id).first()
     assert not route
示例#16
0
    def test_update_shipment_device_certificate(
            self, client_alice, tracking_data, profiles_ids, mocker,
            shipment_alice_with_device, mock_successful_wallet_owner_calls,
            successful_shipment_create_profiles_assertions):
        mock_device_call = mocker.patch.object(settings.REQUESTS_SESSION,
                                               'get')
        mock_device_call.return_value.status_code = 200

        with open('tests/data/cert.pem', 'r') as cert_file:
            cert_pem = cert_file.read()

        map_describe = {}
        principals = []
        for i in range(0, 4):
            describe = {}
            res = self.mocked_iot.create_keys_and_certificate()
            describe['certificateDescription'] = res
            describe['certificateDescription']['status'] = 'INACTIVE'
            if i == 1:
                expired_certificate = res['certificateId']
            if i == 2:
                describe['certificateDescription']['status'] = 'ACTIVE'
                describe['certificateDescription']['certificatePem'] = cert_pem
                new_active_certificate = res['certificateId']
            map_describe[res['certificateId']] = describe
            principals.append(res['certificateArn'])

        self.device.certificate_id = expired_certificate
        self.device.save()

        def side_effects(**kwargs):
            cert = kwargs['certificateId']
            return map_describe[cert]

        with mock.patch('apps.shipments.serializers.boto3.client') as serial_client, \
                mock.patch('apps.shipments.models.boto3.client') as model_client:
            serial_client = serial_client.return_value
            model_client = model_client.return_value
            serial_client.describe_certificate.side_effect = side_effects
            model_client.list_thing_principals.return_value = {
                'principals': principals
            }
            model_client.describe_certificate.side_effect = side_effects

            signed_data = self.sign_tracking(
                tracking_data,
                self.device,
                certificate_id=new_active_certificate)
            response = client_alice.post(self.url_device,
                                         data={'payload': signed_data})
            AssertionHelper.HTTP_204(response)
            self.device.refresh_from_db()

            assert self.device.certificate_id == new_active_certificate
            shipment_alice_with_device.device = None
            shipment_alice_with_device.save()

            self.device.certificate_id = expired_certificate
            self.device.save()
            self.device.refresh_from_db()

            response = client_alice.post(self.create_url,
                                         data={
                                             'device_id': self.device.id,
                                             **profiles_ids
                                         })
            AssertionHelper.HTTP_202(response)

            self.device.refresh_from_db()
            assert self.device.certificate_id == new_active_certificate
 def test_owner_can_update(self, client_alice, org_id_alice):
     response = client_alice.delete(getattr(self, f'url_{self.shipment_tags[0].tag_type}'))
     AssertionHelper.HTTP_204(response)