|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +import requests |
| 5 | + |
| 6 | +from tests.acceptance.helpers import ENDPOINT_DECIDE |
| 7 | +from tests.acceptance.helpers import create_and_validate_request_and_response |
| 8 | +from tests.acceptance.helpers import sort_response |
| 9 | + |
| 10 | +expected_fetch_disabled = { |
| 11 | + "variationKey": "off", |
| 12 | + "enabled": False, |
| 13 | + "ruleKey": "default-rollout-52207-23726430538", |
| 14 | + "flagKey": "flag1", |
| 15 | + "userContext": { |
| 16 | + "userId": "fs-id-1", |
| 17 | + "attributes": {} |
| 18 | + }, |
| 19 | + "reasons": ['an error occurred while evaluating nested tree for audience ID "23783030150"', |
| 20 | + 'Audiences for experiment ab_experiment collectively evaluated to false.', |
| 21 | + 'User "fs-id-1" does not meet conditions to be in experiment "ab_experiment".', |
| 22 | + 'Audiences for experiment default-rollout-52207-23726430538 collectively evaluated to true.', |
| 23 | + 'User "fs-id-1" meets conditions for targeting rule "Everyone Else".'] |
| 24 | +} |
| 25 | + |
| 26 | +expected_no_segments_fetched = { |
| 27 | + "variationKey": "off", |
| 28 | + "enabled": False, |
| 29 | + "ruleKey": "default-rollout-52207-23726430538", |
| 30 | + "flagKey": "flag1", |
| 31 | + "userContext": { |
| 32 | + "userId": "test_user", |
| 33 | + "attributes": {} |
| 34 | + }, |
| 35 | + "reasons": ['an error occurred while evaluating nested tree for audience ID "23783030150"', |
| 36 | + 'Audiences for experiment ab_experiment collectively evaluated to false.', |
| 37 | + 'User "test_user" does not meet conditions to be in experiment "ab_experiment".', |
| 38 | + 'Audiences for experiment default-rollout-52207-23726430538 collectively evaluated to true.', |
| 39 | + 'User "test_user" meets conditions for targeting rule "Everyone Else".'] |
| 40 | +} |
| 41 | + |
| 42 | +expected_fetch_enabled = { |
| 43 | + "variationKey": "variation_b", |
| 44 | + "enabled": True, |
| 45 | + "ruleKey": "ab_experiment", |
| 46 | + "flagKey": "flag1", |
| 47 | + "userContext": { |
| 48 | + "userId": "fs-id-1", |
| 49 | + "attributes": {} |
| 50 | + }, |
| 51 | + "reasons": ["Audiences for experiment ab_experiment collectively evaluated to true."] |
| 52 | +} |
| 53 | + |
| 54 | +expected_fetch_enabled_default_rollout = { |
| 55 | + "variationKey": "off", |
| 56 | + "enabled": False, |
| 57 | + "ruleKey": "default-rollout-52231-23726430538", |
| 58 | + "flagKey": "flag2", |
| 59 | + "userContext": { |
| 60 | + "userId": "fs-id-1", |
| 61 | + "attributes": {} |
| 62 | + }, |
| 63 | + "reasons": ['Audiences for experiment default-rollout-52231-23726430538 collectively evaluated to true.', |
| 64 | + 'User "fs-id-1" meets conditions for targeting rule "Everyone Else".'] |
| 65 | +} |
| 66 | + |
| 67 | +expected_fetch_failed = { |
| 68 | + 'error': 'failed to fetch qualified segments' |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize( |
| 73 | + "flag_key, expected_response, expected_status_code, fetch_segments, user_id", [ |
| 74 | + ("flag1", expected_fetch_disabled, 200, False, 'fs-id-1'), |
| 75 | + ("flag1", expected_fetch_enabled, 200, True, 'fs-id-1'), |
| 76 | + ("flag2", expected_fetch_enabled_default_rollout, 200, True, 'fs-id-1'), |
| 77 | + ("flag1", expected_no_segments_fetched, 200, True, 'test_user'), |
| 78 | + ]) |
| 79 | +def test_decide_fetch_qualified_segments(session_override_sdk_key_odp, flag_key, expected_response, expected_status_code, fetch_segments, user_id): |
| 80 | + """ |
| 81 | + Test validates: |
| 82 | + Correct response with fetch_segments enabled and disabled. |
| 83 | + ... |
| 84 | + :param session_override_sdk_key_odp: sdk key to override the session using odp key |
| 85 | + :param flag_key: |
| 86 | + :param expected_response: |
| 87 | + :param expected_status_code: |
| 88 | + :param fetch_segments: |
| 89 | + :param user_id: |
| 90 | + """ |
| 91 | + |
| 92 | + payload = { |
| 93 | + "userId": user_id, |
| 94 | + "decideOptions": [ |
| 95 | + "ENABLED_FLAGS_ONLY", |
| 96 | + "INCLUDE_REASONS" |
| 97 | + ], |
| 98 | + "userAttributes": {}, |
| 99 | + "fetchSegments": fetch_segments, |
| 100 | + } |
| 101 | + |
| 102 | + params = {"keys": flag_key} |
| 103 | + resp = create_and_validate_request_and_response(ENDPOINT_DECIDE, "post", session_override_sdk_key_odp, payload=json.dumps(payload), |
| 104 | + params=params) |
| 105 | + |
| 106 | + assert json.loads(json.dumps(expected_response)) == resp.json() |
| 107 | + assert resp.status_code == expected_status_code, resp.text |
| 108 | + resp.raise_for_status() |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.parametrize( |
| 112 | + "flag_key, expected_response, expected_status_code", [ |
| 113 | + ("flag1", expected_fetch_failed, 500), |
| 114 | + ]) |
| 115 | +def test_decide_fetch_qualified_segments_odp_not_integrated(session_obj, flag_key, expected_response, expected_status_code): |
| 116 | + """ |
| 117 | + Test validates: |
| 118 | + Correct response when odp not integrated. |
| 119 | + ... |
| 120 | + :param session_override_sdk_key_odp: sdk key to override the session using odp key |
| 121 | + :param flag_key: |
| 122 | + :param expected_response: |
| 123 | + :param expected_status_code: |
| 124 | + """ |
| 125 | + |
| 126 | + payload = { |
| 127 | + "userId": "fs-id-1", |
| 128 | + "decideOptions": [ |
| 129 | + "ENABLED_FLAGS_ONLY", |
| 130 | + "INCLUDE_REASONS" |
| 131 | + ], |
| 132 | + "userAttributes": {}, |
| 133 | + "fetchSegments": True, |
| 134 | + } |
| 135 | + |
| 136 | + params = {"keys": flag_key} |
| 137 | + |
| 138 | + with pytest.raises(requests.exceptions.HTTPError): |
| 139 | + resp = create_and_validate_request_and_response(ENDPOINT_DECIDE, "post", session_obj, payload=json.dumps(payload), |
| 140 | + params=params) |
| 141 | + |
| 142 | + assert json.loads(json.dumps(expected_response)) == resp.json() |
| 143 | + assert resp.status_code == expected_status_code, resp.text |
| 144 | + resp.raise_for_status() |
0 commit comments