Exemple #1
0
def test_activate__enabled(session_obj, enabled, experimentKey, featureKey,
                           expected_response, expected_status_code, bypass_validation):
    """
    Filter the activation response to return only enabled decisions.
    Value for enabled key needs to be a string: "true" or "false"

    - feature_1 feature is enabled - should not appear in response when enabled is set to False
    - featur_3 feature is not enabled in the project - should not appear in the project when enabled is True
    :param session_obj: session fixture
    """
    payload = '{"userId": "matjaz", "userAttributes": {"attr_1": "hola"}}'
    params = {
        "experimentKey": experimentKey,
        "featureKey": featureKey,
        "enabled": enabled
    }

    resp = create_and_validate_request_and_response(ENDPOINT_ACTIVATE, 'post', session_obj, bypass_validation, payload=payload, params=params)

    actual_response = sort_response(resp.json(), 'experimentKey', 'featureKey')
    expected_response = sort_response(json.loads(expected_response), 'experimentKey',
                                      'featureKey')
    assert actual_response == expected_response
    assert resp.status_code == expected_status_code
    resp.raise_for_status()
Exemple #2
0
def test_activate__type(session_obj, decision_type, expected_response,
                        expected_status_code, bypass_validation):
    """
    Test cases:
    1. Get decisions with "experiment" type
    2. Get decisions with "feature" type
    3. Get empty list when non-existent decision type -> bug OASIS-6031
    :param session_obj: session object
    :param decision_type: parameterized decision type
    :param expected_response: expected response
    """
    payload = '{"userId": "matjaz", "userAttributes": {"attr_1": "hola"}}'
    params = {"type": decision_type}

    resp = create_and_validate_request_and_response(ENDPOINT_ACTIVATE, 'post', session_obj, bypass_validation, payload=payload, params=params)

    if decision_type in ['experiment', 'feature']:
        sorted_actual = sort_response(
            resp.json(), 'experimentKey', 'featureKey')
        sorted_expected = sort_response(json.loads(expected_response), 'experimentKey',
                                        'featureKey')
        assert sorted_actual == sorted_expected
    elif resp.json()['error']:
        with pytest.raises(requests.exceptions.HTTPError):
            assert resp.json() == expected_response
            resp.raise_for_status()
Exemple #3
0
def test_activate_with_config(session_obj):
    """
    Tests experimentKeys, featureKeys, variables and variations because it
    validates against the whole response body.

    In "activate"
    Request payload defines the “who” (user id and attributes)
    while the query parameters define the “what” (feature, experiment, etc)

    Request parameter is a list of experiment keys or feature keys.
    If you want both add both and separate them with comma.
    Example:
    params = {
        "featureKey": <list of feature keys>,
        "experimentKey": <list of experiment keys>
    }

    Need to sort the response (list of dictionaries). And the sorting needs to be primary
    and secondary, because we are getting response for two params - experimentKey and
    featureKey and they have different responses. experimentKey has experimentKey field
    always populated and it has featureKey empty.
    Whereas featureKey response has featureKey field populated and experimentKey empty.
    When we sort on both then the responses are properly sorted and ready for being
    asserted on.
    :param session_obj: session object
    """
    # config
    resp = session_obj.get(BASE_URL + ENDPOINT_CONFIG)
    resp_config = resp.json()

    # activate
    feat = [key for key in resp_config['featuresMap']]
    exp = [key for key in resp_config['experimentsMap']]

    payload = '{"userId": "matjaz", "userAttributes": {"attr_1": "hola"}}'
    params = {
        "featureKey": feat,
        "experimentKey": exp
    }

    resp_activate = session_obj.post(BASE_URL + ENDPOINT_ACTIVATE, params=params,
                                     json=json.loads(payload))

    resp_activate.raise_for_status()

    sorted_actual = sort_response(
        resp_activate.json(), 'experimentKey', 'featureKey')
    sorted_expected = sort_response(json.loads(expected_activate_with_config),
                                    'experimentKey',
                                    'featureKey')

    assert sorted_actual == sorted_expected
Exemple #4
0
def test_decide__flag_key_parameter(session_obj, parameters, expected_response, expected_status_code,
                                    bypass_validation_request,
                                    bypass_validation_response):
    """
    Test validates:
    That no required parameter, empty param and all parameters return identical response.
    Openapi spec specifies 400 for missing flagKey parameter. But We keep 400 status code in the openapi spec
    for missing reuired parameter, even though when no flagKey parameter is supplied to the request,
    Agent still responds with all decisions and status 200.
    That is consistent with the behavior of activate and other api-s
    :param session_obj: session obj
    :param parameters:  sesison obj, params, expected, expected status code
    :param expected_response: expected_flag_keys
    :param expected_status_code: 200
    """
    payload = """
        {
          "userId": "matjaz",
          "decideOptions": [
              "ENABLED_FLAGS_ONLY",
              "INCLUDE_REASONS"
          ],
          "userAttributes": {"attr_1": "hola"}
        }
    """

    params = parameters
    resp = create_and_validate_request_and_response(ENDPOINT_DECIDE, 'post', session_obj, bypass_validation_request,
                                                    bypass_validation_response,
                                                    payload=payload,
                                                    params=params)

    sorted_actual = sort_response(resp.json(), 'flagKey')
    sorted_expected = sort_response(json.loads(expected_response), 'flagKey')

    assert sorted_actual == sorted_expected
Exemple #5
0
def test_batch_valid_response__multiple_operations(session_obj):
    """
    Verify that operations with different sdk keys can be sent in a batch.
    :param agent_server: starts agent server with default config
    :param session_obj: session object
    """
    payload = """{
    "operations": [
    {
      "body": {
        "status": "subscribed",
        "email_address": "*****@*****.**"
      },
      "method": "GET",
      "operationID": "1",
      "url": "/v1/config",
      "params": {},
      "headers": {
        "X-Optimizely-SDK-Key": "%s",
        "X-Request-Id": "matjaz 1"
      }
    },
    {
      "body": {
        "status": "subscribed",
        "email_address": "*****@*****.**"
      },
      "method": "GET",
      "operationID": "2",
      "url": "/v1/config",
      "params": {},
      "headers": {
        "X-Optimizely-SDK-Key": "TkB2xhu8WEAHa4LphN3xZ2"
      }
    },
    {
      "body": {
        "userId": "user1"
      },
      "method": "POST",
      "operationID": "3",
      "url": "/v1/activate",
      "params": {
        "type": "feature",
        "experimentKey": "ab_test1"
      },
      "headers": {
        "X-Optimizely-SDK-Key": "%s",
        "Content-Type": "application/json"
      }
    }]
    }""" % (sdk_key, sdk_key)

    resp = create_and_validate_request_and_response(ENDPOINT_BATCH,
                                                    'post',
                                                    session_obj,
                                                    payload=payload,
                                                    bypass_validation=False)

    actual_response = resp.json()

    assert 200 == resp.status_code
    assert 0 == actual_response['errorCount']
    responses = len(actual_response['response'])
    assert 3 == responses
    for operation in range(responses):
        assert 200 == actual_response['response'][operation]['status']

    sorted_actual = sort_response(actual_response['response'], 'operationID',
                                  'status')

    assert json.loads(
        expected_body_of_operationid_2) == sorted_actual[1]['body']
    assert '/v1/config' == sorted_actual[0]['url']
    assert '/v1/config' == sorted_actual[1]['url']
    assert '/v1/activate' == sorted_actual[2]['url']
    resp.raise_for_status()