|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +import requests |
| 5 | +import redis |
| 6 | +import time |
| 7 | + |
| 8 | +from tests.acceptance.helpers import ENDPOINT_DECIDE |
| 9 | +from tests.acceptance.helpers import create_and_validate_request_and_response |
| 10 | +from tests.acceptance.helpers import sort_response |
| 11 | + |
| 12 | +expected_redis_save = { |
| 13 | + "variationKey": "variation_b", |
| 14 | + "enabled": True, |
| 15 | + "ruleKey": "ab_experiment", |
| 16 | + "flagKey": "flag1", |
| 17 | + "userContext": { |
| 18 | + "userId": "matjaz-user-1", |
| 19 | + "attributes": {} |
| 20 | + }, |
| 21 | + "reasons": ['User "matjaz-user-1" was previously bucketed into variation "variation_b" of experiment "ab_experiment".'] |
| 22 | +} |
| 23 | + |
| 24 | +expected_redis_lookup = { |
| 25 | + "variationKey": "variation_a", |
| 26 | + "enabled": True, |
| 27 | + "ruleKey": "ab_experiment", |
| 28 | + "flagKey": "flag1", |
| 29 | + "userContext": { |
| 30 | + "userId": "matjaz-user-2", |
| 31 | + "attributes": {} |
| 32 | + }, |
| 33 | + "reasons": ["Audiences for experiment ab_experiment collectively evaluated to true."] |
| 34 | +} |
| 35 | + |
| 36 | +expected_redis_reset = { |
| 37 | + "variationKey": "variation_b", |
| 38 | + "enabled": True, |
| 39 | + "ruleKey": "ab_experiment", |
| 40 | + "flagKey": "flag1", |
| 41 | + "userContext": { |
| 42 | + "userId": "matjaz-user-4", |
| 43 | + "attributes": {} |
| 44 | + }, |
| 45 | + "reasons": ["Audiences for experiment ab_experiment collectively evaluated to true."] |
| 46 | +} |
| 47 | + |
| 48 | +def test_redis_save(session_override_sdk_key_odp): |
| 49 | + """ |
| 50 | + Test that first fetch call saves segments in redis successfuly. |
| 51 | + :param session_override_sdk_key_odp: sdk key to override the session using odp key |
| 52 | + """ |
| 53 | + |
| 54 | + expected_segments = ["atsbugbashsegmenthaspurchased", "atsbugbashsegmentdob"] |
| 55 | + uId = "fs_user_id-$-matjaz-user-1" |
| 56 | + r = redis.Redis(host='localhost', port=6379, db=0) |
| 57 | + # clean redis before testing since several tests use same user_id |
| 58 | + r.flushdb() |
| 59 | + |
| 60 | + payload = { |
| 61 | + "userId": "matjaz-user-1", |
| 62 | + "decideOptions": [ |
| 63 | + "ENABLED_FLAGS_ONLY", |
| 64 | + "INCLUDE_REASONS" |
| 65 | + ], |
| 66 | + "userAttributes": {}, |
| 67 | + "fetchSegments": True, |
| 68 | + } |
| 69 | + |
| 70 | + params = {"keys": "flag1"} |
| 71 | + resp = create_and_validate_request_and_response(ENDPOINT_DECIDE, "post", session_override_sdk_key_odp, payload=json.dumps(payload), |
| 72 | + params=params) |
| 73 | + |
| 74 | + # Check saved segments |
| 75 | + assert json.loads(json.dumps(expected_segments)) == json.loads(r.get(uId)) |
| 76 | + |
| 77 | + assert json.loads(json.dumps(expected_redis_save)) == resp.json() |
| 78 | + assert resp.status_code == 200, resp.text |
| 79 | + resp.raise_for_status() |
| 80 | + |
| 81 | + |
| 82 | +def test_redis_lookup(session_override_sdk_key_odp): |
| 83 | + """ |
| 84 | + Test that saved segments in redis are used successfuly by segment manager using lookup. |
| 85 | + :param session_override_sdk_key_odp: sdk key to override the session using odp key |
| 86 | + """ |
| 87 | + |
| 88 | + expected_segments = ["atsbugbashsegmenthaspurchased", "atsbugbashsegmentdob"] |
| 89 | + uId = "fs_user_id-$-matjaz-user-2" |
| 90 | + r = redis.Redis(host='localhost', port=6379, db=0) |
| 91 | + r.set(uId, json.dumps(expected_segments)) |
| 92 | + |
| 93 | + payload = { |
| 94 | + "userId": "matjaz-user-2", |
| 95 | + "decideOptions": [ |
| 96 | + "ENABLED_FLAGS_ONLY", |
| 97 | + "INCLUDE_REASONS" |
| 98 | + ], |
| 99 | + "userAttributes": {}, |
| 100 | + "fetchSegments": True, |
| 101 | + } |
| 102 | + |
| 103 | + params = {"keys": "flag1"} |
| 104 | + resp = create_and_validate_request_and_response(ENDPOINT_DECIDE, "post", session_override_sdk_key_odp, payload=json.dumps(payload), |
| 105 | + params=params) |
| 106 | + |
| 107 | + # Check saved segments |
| 108 | + assert json.loads(json.dumps(expected_segments)) == json.loads(r.get(uId)) |
| 109 | + |
| 110 | + assert json.loads(json.dumps(expected_redis_lookup)) == resp.json() |
| 111 | + assert resp.status_code == 200, resp.text |
| 112 | + resp.raise_for_status() |
| 113 | + |
| 114 | + |
| 115 | +def test_redis_reset(session_override_sdk_key_odp): |
| 116 | + """ |
| 117 | + Test that reset option resets segments in redis. |
| 118 | + :param session_override_sdk_key_odp: sdk key to override the session using odp key |
| 119 | + """ |
| 120 | + |
| 121 | + expected_segments_after_reset = ['atsbugbashsegmentdob', 'atsbugbashsegmentgender'] |
| 122 | + uId = "fs_user_id-$-matjaz-user-4" |
| 123 | + r = redis.Redis(host='localhost', port=6379, db=0) |
| 124 | + |
| 125 | + # Manually add invalid segments for matjaz-user-4 in redis |
| 126 | + r.set(uId, json.dumps(["invalid_segments"])) |
| 127 | + # Check invalid segments were saved |
| 128 | + assert json.loads(json.dumps(["invalid_segments"])) == json.loads(r.get(uId)) |
| 129 | + |
| 130 | + payload = { |
| 131 | + "userId": "matjaz-user-4", |
| 132 | + "decideOptions": [ |
| 133 | + "ENABLED_FLAGS_ONLY", |
| 134 | + "INCLUDE_REASONS" |
| 135 | + ], |
| 136 | + "userAttributes": {}, |
| 137 | + "fetchSegments": True, |
| 138 | + "fetchSegmentsOptions": ["RESET_CACHE"], |
| 139 | + } |
| 140 | + |
| 141 | + params = {"keys": "flag1"} |
| 142 | + resp = create_and_validate_request_and_response(ENDPOINT_DECIDE, "post", session_override_sdk_key_odp, payload=json.dumps(payload), |
| 143 | + params=params) |
| 144 | + |
| 145 | + # Check old segments were removed and newly fetched were saved |
| 146 | + assert json.loads(json.dumps(expected_segments_after_reset)) == json.loads(r.get(uId)) |
| 147 | + |
| 148 | + assert json.loads(json.dumps(expected_redis_reset)) == resp.json() |
| 149 | + assert resp.status_code == 200, resp.text |
| 150 | + resp.raise_for_status() |
0 commit comments