예제 #1
0
def test_get_uas_zones_subscriptionby_id(test_user):
    subscription1 = make_uas_zones_subscription(user=test_user)
    subscription2 = make_uas_zones_subscription(user=test_user)
    subscription1.save()

    assert get_uas_zones_subscription_by_id(subscription2.id) is None
    assert subscription1 == get_uas_zones_subscription_by_id(subscription1.id)
def test_get_relevant_uas_zones_subscriptions(test_user):
    uas_zone_basilique = make_uas_zone(BASILIQUE_POLYGON)
    db_create_uas_zone(uas_zone_basilique)

    uas_zone_intersecting_basilique = make_uas_zone(
        INTERSECTING_BASILIQUE_POLYGON)
    intersecting_uas_zone_filter = make_uas_zones_filter_from_db_uas_zone(
        uas_zone_intersecting_basilique)

    intersecting_uas_zones_subscription = make_uas_zones_subscription()
    intersecting_uas_zones_subscription.uas_zones_filter = intersecting_uas_zone_filter
    intersecting_uas_zones_subscription.save()

    uas_zone_non_intersecting_basilique = make_uas_zone(
        NON_INTERSECTING_BASILIQUE_POLYGON)
    non_intersecting_uas_zone_filter = make_uas_zones_filter_from_db_uas_zone(
        uas_zone_non_intersecting_basilique)

    non_intersecting_uas_zones_subscription = make_uas_zones_subscription()
    non_intersecting_uas_zones_subscription.uas_zones_filter = non_intersecting_uas_zone_filter
    non_intersecting_uas_zones_subscription.save()

    context = UASZoneContext(uas_zone=uas_zone_basilique, user=test_user)
    get_relevant_uas_zones_subscriptions(context=context)

    assert intersecting_uas_zones_subscription in context.uas_zones_subscriptions
    assert non_intersecting_uas_zones_subscription not in context.uas_zones_subscriptions
def test_uas_zone_matches_subscription():
    uas_zone_basilique = make_uas_zone(BASILIQUE_POLYGON)
    db_create_uas_zone(uas_zone_basilique)
    uas_zone_intersecting_basilique = make_uas_zone(
        INTERSECTING_BASILIQUE_POLYGON)
    uas_zone_non_intersecting_basilique = make_uas_zone(
        NON_INTERSECTING_BASILIQUE_POLYGON)

    intersecting_uas_zone_filter = make_uas_zones_filter_from_db_uas_zone(
        uas_zone_intersecting_basilique)

    intersecting_uas_zones_subscription = make_uas_zones_subscription()
    intersecting_uas_zones_subscription.uas_zones_filter = intersecting_uas_zone_filter
    intersecting_uas_zones_subscription.save()

    assert _uas_zone_matches_subscription_uas_zones_filter(
        uas_zone_basilique, intersecting_uas_zones_subscription) is True

    non_intersecting_uas_zone_filter = make_uas_zones_filter_from_db_uas_zone(
        uas_zone_non_intersecting_basilique)

    non_intersecting_uas_zones_subscription = make_uas_zones_subscription()
    non_intersecting_uas_zones_subscription.uas_zones_filter = non_intersecting_uas_zone_filter
    non_intersecting_uas_zones_subscription.save()

    assert _uas_zone_matches_subscription_uas_zones_filter(
        uas_zone_basilique, non_intersecting_uas_zones_subscription) is False
예제 #4
0
def test_update_subscription_to_uas_zones_updates__is_updated__returns_ok_200(
        mock_sm_client, test_client, test_user):

    uas_zones_subscription = make_uas_zones_subscription(user=test_user)
    uas_zones_subscription.save()

    sm_subscription = Mock()
    sm_subscription.id = uas_zones_subscription.sm_subscription.id
    mock_sm_client.get_subscription_by_id = Mock(return_value=sm_subscription)
    mock_sm_client.put_subscription = Mock()

    data = {"active": not uas_zones_subscription.sm_subscription.active}

    response = test_client.put(URL + uas_zones_subscription.id,
                               data=json.dumps(data),
                               content_type='application/json',
                               headers=make_basic_auth_header(
                                   test_user.username, DEFAULT_LOGIN_PASS))

    assert 200 == response.status_code
    response_data = json.loads(response.data)
    assert "OK" == response_data['genericReply']['RequestStatus']

    updated_subscription = UASZonesSubscription.objects.get(
        id=uas_zones_subscription.id)

    assert updated_subscription.sm_subscription.active == \
           (not uas_zones_subscription.sm_subscription.active)
    mock_sm_client.put_subscription.assert_called_once_with(
        sm_subscription.id, data)
예제 #5
0
def test_get_uas_zones_subscriptions(test_user):
    subscription = make_uas_zones_subscription(user=test_user)
    subscription.save()

    db_subscriptions = get_uas_zones_subscriptions()
    assert 1 == len(db_subscriptions)
    assert subscription in db_subscriptions
예제 #6
0
def test_delete_uas_zones_subscription():
    subscription = make_uas_zones_subscription()
    subscription.save()

    delete_uas_zones_subscription(subscription)

    with pytest.raises(DoesNotExist):
        UASZonesSubscription.objects.get(id=subscription.id)
예제 #7
0
def test_get_subscriptions_to_uas_zones_updates__returns_subscriptions_data_200(
        test_client, test_user):
    uas_zones_subscription1 = make_uas_zones_subscription(user=test_user)
    uas_zones_subscription2 = make_uas_zones_subscription(user=test_user)
    uas_zones_subscription1.save()
    uas_zones_subscription2.save()

    response = test_client.get(URL,
                               headers=make_basic_auth_header(
                                   test_user.username, DEFAULT_LOGIN_PASS))

    assert 200 == response.status_code
    response_data = json.loads(response.data)['subscriptions']
    assert 2 == len(response_data)
    assert response_data[0]['subscriptionID'] == uas_zones_subscription1.id
    assert response_data[0][
        'publicationLocation'] == uas_zones_subscription1.sm_subscription.queue
    assert response_data[0][
        'active'] == uas_zones_subscription1.sm_subscription.active
    assert 'UASZonesFilter' in response_data[0]
예제 #8
0
def test_update_uas_zones_subscription():
    subscription = make_uas_zones_subscription()
    subscription.save()

    subscription.topic_name = 'new_name'

    update_uas_zones_subscription(subscription)

    db_subscription = UASZonesSubscription.objects.get(id=subscription.id)

    assert subscription.sm_subscription.topic_name == db_subscription.sm_subscription.topic_name
예제 #9
0
def test_get_subscriptions_to_uas_zones_updates__invalid_user__returns_nok_401(
        test_client):
    uas_zones_subscription = make_uas_zones_subscription()
    uas_zones_subscription.save()

    response = test_client.get(URL,
                               headers=make_basic_auth_header(
                                   'fake_username', 'fake_password'))

    assert 401 == response.status_code
    response_data = json.loads(response.data)
    assert "NOK" == response_data['genericReply']['RequestStatus']
    assert "Invalid credentials" == response_data['genericReply'][
        "RequestExceptionDescription"]
예제 #10
0
def test_update_subscription_to_uas_zones_updates__invalid_data__returns_nok_400(
        test_client, test_user, invalid_data):
    uas_zones_subscription = make_uas_zones_subscription(user=test_user)
    uas_zones_subscription.save()

    response = test_client.put(URL + uas_zones_subscription.id,
                               data=json.dumps(invalid_data),
                               content_type='application/json',
                               headers=make_basic_auth_header(
                                   test_user.username, DEFAULT_LOGIN_PASS))

    assert 400 == response.status_code
    response_data = json.loads(response.data)
    assert "NOK" == response_data['genericReply']['RequestStatus']
예제 #11
0
def test_delete_subscription_to_uas_zones_updates__is_deleted__returns_ok_204(
        mock_sm_client, test_client, test_user):
    mock_sm_client.delete_subscription_by_id = Mock()

    uas_zones_subscription = make_uas_zones_subscription(user=test_user)
    uas_zones_subscription.save()

    response = test_client.delete(URL + uas_zones_subscription.id,
                                  headers=make_basic_auth_header(
                                      test_user.username, DEFAULT_LOGIN_PASS))

    assert 204 == response.status_code

    with pytest.raises(DoesNotExist):
        UASZonesSubscription.objects.get(id=uas_zones_subscription.id)

    mock_sm_client.delete_subscription_by_id.assert_called_once_with(
        uas_zones_subscription.sm_subscription.id)
예제 #12
0
def test_create_subscription_to_uas_zones_updates__valid_input__returns_ok_and_creates_subscription__200(
        test_client, test_user):
    data = {
        "airspaceVolume": {
            "lowerLimit": 0,
            "lowerVerticalReference": "AMSL",
            "horizontalProjection": {
                "type": "Polygon",
                "coordinates": [[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0],
                                 [1.0, 2.0]]]
            },
            "upperLimit": 0,
            "upperVerticalReference": "AMSL"
        },
        "endDateTime": "2019-11-05T13:10:39.315Z",
        "regions": [0],
        "startDateTime": "2019-11-05T13:10:39.315Z"
    }

    context = UASZonesSubscriptionCreateContext(
        uas_zones_filter=UASZonesFilterSchema().load(data), user=test_user)
    context.uas_zones_subscription = make_uas_zones_subscription()

    with mock.patch(
            'geofencing_service.events.events.create_uas_zones_subscription_event.handle',
            return_value=context):
        response = test_client.post(URL,
                                    data=json.dumps(data),
                                    content_type='application/json',
                                    headers=make_basic_auth_header(
                                        test_user.username,
                                        DEFAULT_LOGIN_PASS))

        assert 201 == response.status_code
        response_data = json.loads(response.data)
        assert "OK" == response_data['genericReply']['RequestStatus']
        assert context.uas_zones_subscription.id == response_data[
            'subscriptionID']
        assert context.uas_zones_subscription.sm_subscription.queue == \
               response_data['publicationLocation']
예제 #13
0
def test_create_uas_zones_subscription():
    subscription = make_uas_zones_subscription()

    create_uas_zones_subscription(subscription)

    assert subscription == UASZonesSubscription.objects.get(id=subscription.id)