Beispiel #1
0
def test_flagship_singleton():
    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key", Config())
    fs2 = Flagship.instance()
    fs2.start("my_env_id2", "my_api_key2", Config())
    assert fs._config is not None and fs2._config is not None
    assert fs == fs2
    assert fs._config == fs2._config
    assert fs._config.env_id == "my_env_id2" and fs2._config.env_id == "my_env_id2"
    assert fs._config.api_key == "my_api_key2" and fs2._config.api_key == "my_api_key2"
Beispiel #2
0
def test_bucketing_alloc2():
    try:
        os.remove("bucketing.json")
    except Exception as e:
        print("No Bucketing file")

    json_response = '{"campaigns":[{"id":"bs8qvmo4nlr01fl9aaaa","type":"ab","variationGroups":[{"id":"bs8qvmo4nlr01fl9bbbb","targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","key":"fs_all_users","value":""}]}]},"variations":[{"id":"bs8qvmo4nlr01fl9cccc","modifications":{"type":"JSON","value":{"variation":null}},"reference":true},{"id":"bs8qvmo4nlr01fl9dddd","modifications":{"type":"JSON","value":{"variation":1}},"allocation":25},{"id":"bs8r09g4nlr01c77eeee","modifications":{"type":"JSON","value":{"variation":2}},"allocation":25},{"id":"bs8r09g4nlr01cdkffff","modifications":{"type":"JSON","value":{"variation":3}},"allocation":25},{"id":"bs8r09hsbs4011lbgggg","modifications":{"type":"JSON","value":{"variation":4}},"allocation":25}]}]},{"id":"bs8r119sbs4016mehhhh","type":"ab","variationGroups":[{"id":"bs8r119sbs4016meiiii","targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","key":"fs_all_users","value":""}]}]},"variations":[{"id":"bs8r119sbs4016mejjjj","modifications":{"type":"JSON","value":{"variation50":null}},"reference":true},{"id":"bs8r119sbs4016mekkkk","modifications":{"type":"JSON","value":{"variation50":1}},"allocation":50},{"id":"bs8r119sbs4016mellll","modifications":{"type":"JSON","value":{"variation50":2}},"allocation":50}]}]}]}'
    responses.reset()
    headers = {
        "Last-Modified": "Fri,  05 Jun 2020 12:20:40 GMT"
    }
    responses.add(responses.GET,
                  'https://cdn.flagship.io/my_env_id/bucketing.json',
                  json=json.loads(json_response), status=200, headers=headers)
    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/my_env_id/events',
                  json=json.loads('{}'), status=200)

    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key", Config(mode=Config.Mode.BUCKETING, polling_interval=-1))
    ids = ["202072017183814142",
           "202072017183860649",
           "202072017183828850",
           "202072017183818733",
           "202072017183823773",
           "202072017183894922",
           "202072017183829817",
           "202072017183842202",
           "202072017233645009",
           "202072017233690230",
           "202072017183886606",
           "202072017183877657",
           "202072017183860380",
           "202072017183972690",
           "202072017183912618",
           "202072017183951364",
           "202072017183920657",
           "202072017183922748",
           "202072017183943575",
           "202072017183987677"
           ]
    variation50 = [1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1]
    variation25 = [4, 1, 2, 4, 2, 4, 1, 3, 2, 1, 4, 4, 1, 1, 2, 3, 4, 1, 3, 4]

    for i in range(0, len(ids)):
        v = Flagship.instance().create_visitor(ids[i])
        v.synchronize_modifications()
        v25 = v.get_modification("variation", 0)
        v50 = v.get_modification("variation50", 0)
        print ("{} v50 {} == variation50[{}] {}".format(ids[i], v50, i, variation50[i]))
        print ("{} v25 {} == variation25[{}] {}".format(ids[i], v25, i, variation25[i]))
        assert v25 == variation25[i]
        assert v50 == variation50[i]
Beispiel #3
0
def test_visitor_get_activate():
    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key", Config(event_handler=None))
    visitor = fs.create_visitor("visitor_1")

    json_response = '{"visitorId":"visitor_1","campaigns":[{"id":"xxxxd0qhl5801abv9ib0",' \
                    '"variationGroupId":"xxxxd0qhl5801abv9ic0","variation":{"id":"xxxxd0qhl5801abv9icg",' \
                    '"modifications":{"type":"FLAG","value":{"featureEnabled":true}}}}]} '

    responses.reset()
    responses.add(
        responses.POST,
        'https://decision.flagship.io/v2/my_env_id/campaigns/?exposeAllKeys=true&sendContextEvent=false',
        json=json.loads(json_response),
        status=200)
    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/my_env_id/events',
                  status=200)
    visitor.synchronize_modifications()

    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/activate',
                  status=200)
    visitor.activate_modification('featureEnabled')
    visitor.activate_modification('xxxxxxx')
Beispiel #4
0
def test_visitor_update_context():
    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key", Config(event_handler=None))

    class Eight:
        def __init__(self):
            pass

    visitor = fs.create_visitor("Pam", {'isVIP': True})
    visitor.update_context(('one', 1), False)
    visitor.update_context(('two', 2, 2))
    visitor.update_context(
        {
            0: 'zero',
            'three': 3,
            'four': {},
            'five': "five",
            'six': [],
            'seven': 7.334,
            'eight': Eight()
        }, False)
    assert 0 not in visitor._context
    assert visitor._context['isVIP'] is True
    assert visitor._context['one'] == 1
    assert 'two' not in visitor._context
    assert visitor._context['three'] == 3
    assert 'four' not in visitor._context
    assert visitor._context['five'] == 'five'
    assert 'six' not in visitor._context
    assert visitor._context['seven'] == 7.334
    assert 'eight' not in visitor._context
Beispiel #5
0
def test_visitor_get_modification():
    print(
        "_______________________________1_____________________________________"
    )
    json_response2 = '{"visitorId":"visitor_1","campaigns":[{"id":"xxxxesjojh803lh57qo0",' \
                     '"variationGroupId":"xxxxesjojh803lh57qp0","variation":{"id":"xxxxesjojh803lh57qpg",' \
                     '"modifications":{"type":"FLAG","value":{"my_flag_nb":100}}}},{"id":"xxxxsp9j5mf4g0fdhkv2g",' \
                     '"variationGroupId":"xxxxp9j5mf4g0fdhkv3g","variation":{"id":"xxxxp9j5mf4g0fdhkv4g",' \
                     '"modifications":{"type":"JSON","value":{"btn-color":"red"}}}},{"id":"xxxxrfe4jaeg0gi1bhog",' \
                     '"variationGroupId":"xxxxrfe4jaeg0gi1bhpg","variation":{"id":"xxxxrfe4jaeg0gi1bhq0",' \
                     '"modifications":{"type":"FLAG","value":{"featureEnabled":true}}}},{"id":"xxxx9b1q4vl00quhh0rg",' \
                     '"variationGroupId":"xxxx9b1q4vl00quhh0sg","variation":{"id":"xxxx9b1q4vl00quhh0tg",' \
                     '"modifications":{"type":"JSON","value":{"k1":"v1","k2":null,"k3":null,"k6":"v6","k7":null}}}}]} '

    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key",
             Config(event_handler=None, mode=Config.Mode.API))
    visitor = fs.create_visitor("visitor_1")

    responses.reset()
    responses.add(
        responses.POST,
        'https://decision.flagship.io/v2/my_env_id/campaigns/?exposeAllKeys=true&sendContextEvent=false',
        json=json.loads(json_response2),
        status=200)
    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/my_env_id/events',
                  status=200)
    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/activate',
                  status=200)

    val = visitor.synchronize_modifications()
    assert visitor.get_modification("aaaaaaaa", "bbbb", True) == 'bbbb'
    assert visitor.get_modification("btn-color", 'blue', True) == 'red'
Beispiel #6
0
def test_flagship_start_wrong_param2():
    try:
        fs = Flagship.instance()
        fs.start(Config(12, 33))
        assert False
    except Exception as e:
        assert True
Beispiel #7
0
def test_config_params():
    try:
        fs = Flagship.instance()
        fs.start("my_env_id", "my_api_key", Config(event_handler=None))
        assert True
    except Exception as e:
        assert False
Beispiel #8
0
def test_flagship_start_wrong_param():
    try:
        fs = Flagship.instance()
        fs.start("not_config_object")
        assert False
    except Exception as e:
        assert True
Beispiel #9
0
def test_create_visitor_wrong_param():
    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key", Config(event_handler=None))
    try:
        visitor = fs.create_visitor(1, None)
        assert False
    except Exception as e:
        assert True
Beispiel #10
0
def test_config_handler_wrong_event_handler():
    class Wrong:
        def __init__(self):
            pass

    try:
        fs = Flagship.instance()
        fs.start("my_env_id", "my_api_key", Config(event_handler=Wrong()))
        assert False
    except Exception as e:
        assert True
Beispiel #11
0
def test_bucketing_wrong_config():
    class Wrong:
        def __init__(self):
            pass

    try:
        fs = Flagship.instance()
        fs.start("my_env_id", "my_api", Config(mode=Wrong))
        assert False
    except Exception as e:
        assert True
Beispiel #12
0
def test_bucketing_polling():
    responses.reset()
    try:
        os.remove("bucketing.json")
    except Exception as e:
        print("No Bucketing file")
    json_response = '{"campaigns":[{"variationGroups":[{"variations":[{"allocation":100,"modifications":{"type":"FLAG","value":{"featureEnabled":true}},"id":"xxxx"}],"targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","value":true,"key":"isVIPUser"}]}]},"id":"yyyy"},{"variations":[{"allocation":100,"modifications":{"type":"FLAG","value":{"featureEnabled":false}},"id":"cccc"}],"targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","value":false,"key":"isVIPUser"}]}]},"id":"vvvv"}],"type":"toggle","id":"aaaa"},{"variationGroups":[{"variations":[{"allocation":25,"modifications":{"type":"JSON","value":{"rank_plus":null,"rank":null}},"id":"zzzz","reference":true},{"allocation":25,"modifications":{"type":"JSON","value":{"rank_plus":null,"rank":1111}},"id":"eeee"},{"allocation":25,"modifications":{"type":"JSON","value":{"rank_plus":null,"rank":3333}},"id":"rrrr"},{"allocation":25,"modifications":{"type":"JSON","value":{"rank_plus":22.22,"rank":2222}},"id":"tttt"}],"targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","value":"password","key":"access"}]}]},"id":"yyyy"}],"type":"ab","id":"iiii"}]}'
    headers = {
        "Last-Modified": "Fri,  05 Jun 2020 12:20:40 GMT"
    }
    responses.add(responses.GET,'https://cdn.flagship.io/my_env_id/bucketing.json', json=json.loads(json_response), status=200, headers=headers)

    def add_responses():
        responses.add(responses.POST,
                      'https://decision.flagship.io/v2/my_env_id/events',
                      json=json.loads('{}'), status=200)

        responses.add(responses.POST,
                      'https://decision.flagship.io/v2/my_env_id/events', status=200)

        responses.add(responses.POST, 'https://ariane.abtasty.com/', status=200)

        responses.add(responses.POST, 'https://decision.flagship.io/v2/activate', status=200)

    add_responses()

    fs = Flagship.instance()
    # # print "#=> " + str(fs._bucketing_manager.is_bucketing_thread_running())
    fs.start("my_env_id", "my_api_key", Config(mode=Config.Mode.BUCKETING, polling_interval=2))  # 1

    visitor = fs.create_visitor("visitor1")
    visitor2 = fs.create_visitor("visitor2")

    hit = Page("test_bucketing_polling_panic")
    i = 0
    while i < 10:  # 10 + 5 polling

        visitor.update_context(("isVIPUser", i % 2 == 0), True)  # 1
        visitor.activate_modification("featureEnabled")  # 1
        visitor.send_hit(hit)  # 1

        visitor2.update_context(("isVIPUser", i % 2 == 1), True)  # 1
        visitor2.activate_modification("featureEnabled")  # 1
        visitor2.send_hit(hit)  # 1

        assert visitor.get_modification("featureEnabled", False) == (i % 2 == 0)
        assert visitor2.get_modification("featureEnabled", False) == (i % 2 == 1)

        i += 1
        time.sleep(1)
    # assert len(responses.calls) == 6
    assert len(responses.calls) == 67
    fs.close()
Beispiel #13
0
def test_visitor_send_hits():
    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key",
             Config(event_handler=None, mode=Config.Mode.API))
    visitor = fs.create_visitor("visitor_1")

    json_response = '{"visitorId":"visitor_1","campaigns":[{"id":"xxxxd0qhl5801abv9ib0",' \
                    '"variationGroupId":"xxxxd0qhl5801abv9ic0","variation":{"id":"xxxxd0qhl5801abv9icg",' \
                    '"modifications":{"type":"FLAG","value":{"featureEnabled":true}}}}]} '

    responses.reset()
    responses.add(
        responses.POST,
        'https://decision.flagship.io/v2/my_env_id/campaigns/?exposeAllKeys=true&sendContextEvent=false',
        json=json.loads(json_response),
        status=200)
    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/my_env_id/events',
                  status=200)
    visitor.synchronize_modifications()

    responses.add(responses.POST, 'https://ariane.abtasty.com/', status=200)

    visitor.send_hit(
        Page("script.py").with_ip("133.3.223.1").with_locale(
            "fr-fr").with_resolution(640, 480).with_session_number(3))

    visitor.send_hit(
        Event(EventCategory.USER_ENGAGEMENT, "this is action").with_ip(
            '6.6.6.6').with_event_label('this is my label').with_locale(
                'lol_hihi').with_event_value(323))

    visitor.send_hit(
        Transaction("#309830", "purchases").with_locale("uk-uk").with_ip(
            "30.334.3.33").with_session_number(3).with_currency(
                "EUR").with_item_count(3).with_payment_method("credit_card").
        with_shipping_cost(4.99).with_shipping_method("1d").with_taxes(
            9.99).with_total_revenue(420.00).with_coupon_code("#SAVE10"))

    visitor.send_hit(
        Item("#309830", "ATX2080", "#cg_atx_20802020").with_item_category(
            "hardware").with_item_quantity(2).with_price(210.00))

    class FakeHit:
        def __init__(self):
            pass

    visitor.send_hit(FakeHit())
    assert len(responses.calls) == 6
Beispiel #14
0
def test_init():
    class CustomEventHandler(FlagshipEventHandler):
        def __init__(self):
            FlagshipEventHandler.__init__(self)

        def on_log(self, level, message):
            FlagshipEventHandler.on_log(self, level, ">>> " + message)
            # print("on log >> " + message)
            pass

        def on_exception_raised(self, exception, traceback):
            FlagshipEventHandler.on_exception_raised(self, exception,
                                                     traceback)
            # print("on exception >> " + str(exception))
            pass

    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key",
             Config(event_handler=CustomEventHandler()))
    assert Flagship.instance()._config.env_id == "my_env_id"
    assert Flagship.instance()._config.api_key == "my_api_key"
    assert Flagship.instance()._config.event_handler is not None
    assert isinstance(Flagship.instance()._config.event_handler,
                      CustomEventHandler)
Beispiel #15
0
def test_visitor_get_modification2():
    json_response2 = '{"visitorId":"visitor_1","campaigns":[{"id":"xxxxesjojh803lh57qo0",' \
                     '"variationGroupId":"xxxxesjojh803lh57qp0","variation":{"id":"xxxxesjojh803lh57qpg",' \
                     '"modifications":{"type":"FLAG","value":{"my_flag_nb":100}}}},{"id":"xxxxsp9j5mf4g0fdhkv2g",' \
                     '"variationGroupId":"xxxxp9j5mf4g0fdhkv3g","variation":{"id":"xxxxp9j5mf4g0fdhkv4g",' \
                     '"modifications":{"type":"JSON","value":{"btn-color":"red"}}}},{"id":"xxxxrfe4jaeg0gi1bhog",' \
                     '"variationGroupId":"xxxxrfe4jaeg0gi1bhpg","variation":{"id":"xxxxrfe4jaeg0gi1bhq0",' \
                     '"modifications":{"type":"FLAG","value":{"featureEnabled":true}}}},{"id":"xxxx9b1q4vl00quhh0rg",' \
                     '"variationGroupId":"xxxx9b1q4vl00quhh0sg","variation":{"id":"xxxx9b1q4vl00quhh0tg",' \
                     '"modifications":{"type":"JSON","value":{"k1":"v1","k2":null,"k3":null,"k6":"v6","k7":null}}}},' \
                     '{"id":"bsbgq4rjhsqg11tntt1g","variationGroupId":"bsbgq4rjhsqg11tntt2g","variation":{' \
                     '"id":"bsbgq4rjhsqg11tntt3g","modifications":{"type":"JSON","value":{"array":[1,2,3],"complex":{' \
                     '"carray":[{"cobject":0}]},"object":{"value":123456}}},"reference":false}}]} '

    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key",
             Config(event_handler=None, mode=Config.Mode.API))
    visitor = fs.create_visitor("visitor_1")

    responses.reset()
    responses.add(
        responses.POST,
        'https://decision.flagship.io/v2/my_env_id/campaigns/?exposeAllKeys=true&sendContextEvent=false',
        json=json.loads(json_response2),
        status=200)
    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/my_env_id/events',
                  status=200)

    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/activate',
                  status=200)

    visitor.update_context(('titi', 4), True)

    assert visitor.get_modification("aaaaaaaa", "bbbb", True) == 'bbbb'
    assert visitor.get_modification("btn-color", 'blue', True) == 'red'
    assert visitor.get_modification("k2", 'yellow', True) == 'yellow'

    assert visitor.get_modification("btn-color", "yellow") == 'red'
    assert visitor.get_modification("do_not_exists", 'None') is "None"

    assert visitor.get_modification("complex", {}) == {
        "carray": [{
            "cobject": 0
        }]
    }
    assert visitor.get_modification("array", []) == [1, 2, 3]
Beispiel #16
0
 def e_test_bucketing_200_again():
     json_response = '{"campaigns":[{"variationGroups":[{"variations":[{"allocation":100,"modifications":{"type":"FLAG","value":{"featureEnabled":true}},"id":"xxxx"}],"targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","value":true,"key":"isVIPUser"}]}]},"id":"yyyy"},{"variations":[{"allocation":100,"modifications":{"type":"FLAG","value":{"featureEnabled":false}},"id":"cccc"}],"targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","value":false,"key":"isVIPUser"}]}]},"id":"vvvv"}],"type":"toggle","id":"aaaa"},{"variationGroups":[{"variations":[{"allocation":25,"modifications":{"type":"JSON","value":{"rank_plus":null,"rank":null}},"id":"zzzz","reference":true},{"allocation":25,"modifications":{"type":"JSON","value":{"rank_plus":null,"rank":1111}},"id":"eeee"},{"allocation":25,"modifications":{"type":"JSON","value":{"rank_plus":null,"rank":3333}},"id":"rrrr"},{"allocation":25,"modifications":{"type":"JSON","value":{"rank_plus":22.22,"rank":2222}},"id":"tttt"}],"targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","value":"password","key":"access"}]}]},"id":"yyyy"}],"type":"ab","id":"iiii"}]}'
     headers = {
         "Last-Modified": "Fri,  05 Jun 2023 12:20:40 GMT"
     }
     responses.reset()
     responses.add(responses.GET,
                   'https://cdn.flagship.io/my_env_id/bucketing.json',
                   json=json.loads(json_response), status=200, adding_headers=headers)
     fs = Flagship.instance()
     fs.start("my_env_id", "my_api_key", Config(mode=Config.Mode.BUCKETING, polling_interval=-1))
     with open("bucketing.json", 'r') as f:
         content = f.read()
         assert len(content) > 2
         json_object = json.loads(content)
         last_modified = json_object['last_modified']
         assert last_modified is not None
         assert last_modified == "Fri,  05 Jun 2023 12:20:40 GMT"
Beispiel #17
0
def test_create_visitor():
    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key", Config(event_handler=None))

    try:
        visitor = fs.create_visitor("Pam", {'isVIP': True})
        assert visitor._visitor_id == 'Pam'
        assert visitor._context['isVIP'] is True
        assert visitor._env_id == "my_env_id"
        assert visitor._api_key == "my_api_key"
        assert visitor._api_manager is not None
        assert visitor._api_manager._env_id == "my_env_id"
        assert visitor._api_manager.api_key == "my_api_key"
        assert len(visitor._modifications) == 0
        assert True
    except Exception as e:
        print(traceback.print_exc())
        assert False
Beispiel #18
0
def test_bucketing_panic():
    try:
        os.remove("bucketing.json")
    except Exception as e:
        print("No Bucketing file")

    json_response = '{"panic":true}'
    responses.reset()
    headers = {
        "Last-Modified": "Fri,  05 Jun 2020 12:20:40 GMT"
    }
    responses.add(responses.GET,
                  'https://cdn.flagship.io/my_env_id/bucketing.json',
                  json=json.loads(json_response), status=200, headers=headers)
    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key", Config(mode=Config.Mode.BUCKETING, polling_interval=2))  # 1

    visitor = fs.create_visitor("visitor1")
    visitor2 = fs.create_visitor("visitor2")
    hit = Page("test_bucketing_polling_panic")

    i = 0
    while i < 10:   # 6 polling

        visitor.update_context(("isVIPUser", i % 2 == 0), True)
        visitor.activate_modification("featureEnabled")
        visitor.send_hit(hit)

        visitor2.update_context(("isVIPUser", i % 2 == 1), True)
        visitor2.activate_modification("featureEnabled")
        visitor2.send_hit(hit)

        assert visitor.get_modification("featureEnabled", False) is False
        assert visitor2.get_modification("featureEnabled", False) is False

        i += 1
        time.sleep(1)

    assert len(responses.calls) == 7
    fs.close()
Beispiel #19
0
def test_is_started():
    assert Flagship.instance().is_flagship_started()
Beispiel #20
0
def test_bucketing_alloc():
    try:
        try:
            os.remove("bucketing.json")
        except Exception as e:
            print("No Bucketing file")

        json_response = '{"campaigns":[{"id":"bs8qvmo4nlr01fl9aaaa","type":"ab","variationGroups":[{"id":"bs8qvmo4nlr01fl9bbbb","targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","key":"fs_all_users","value":""}]}]},"variations":[{"id":"bs8qvmo4nlr01fl9cccc","modifications":{"type":"JSON","value":{"variation":null}},"reference":true},{"id":"bs8qvmo4nlr01fl9dddd","modifications":{"type":"JSON","value":{"variation":1}},"allocation":25},{"id":"bs8r09g4nlr01c77eeee","modifications":{"type":"JSON","value":{"variation":2}},"allocation":25},{"id":"bs8r09g4nlr01cdkffff","modifications":{"type":"JSON","value":{"variation":3}},"allocation":25},{"id":"bs8r09hsbs4011lbgggg","modifications":{"type":"JSON","value":{"variation":4}},"allocation":25}]}]},{"id":"bs8r119sbs4016mehhhh","type":"ab","variationGroups":[{"id":"bs8r119sbs4016meiiii","targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","key":"fs_all_users","value":""}]}]},"variations":[{"id":"bs8r119sbs4016mejjjj","modifications":{"type":"JSON","value":{"variation50":null}},"reference":true},{"id":"bs8r119sbs4016mekkkk","modifications":{"type":"JSON","value":{"variation50":1}},"allocation":50},{"id":"bs8r119sbs4016mellll","modifications":{"type":"JSON","value":{"variation50":2}},"allocation":50}]}]}]}'
        responses.reset()
        headers = {
            "Last-Modified": "Fri,  05 Jun 2020 12:20:40 GMT"
        }
        responses.add(responses.GET,
                      'https://cdn.flagship.io/my_env_id/bucketing.json',
                      json=json.loads(json_response), status=200, headers=headers)
        responses.add(responses.POST,
                      'https://decision.flagship.io/v2/my_env_id/events',
                      json=json.loads('{}'), status=200)

        fs = Flagship.instance()
        fs.start("my_env_id", "my_api_key", Config(mode=Config.Mode.BUCKETING, polling_interval=-1))

        v150 = 0
        v250 = 0
        v125 = 0
        v225 = 0
        v325 = 0
        v425 = 0

        x = 50000
        for i in range(0, x):
            v = Flagship.instance().create_visitor(get_random_string(10) + "_" + str(i))
            v.synchronize_modifications()
            variation = v.get_modification("variation", 0)
            variation50 = v.get_modification("variation50", 0)

            if variation50 == 1:
                v150 += 1
            elif variation50 == 2:
                v250 += 1
            else:
                assert False
            if variation == 1:
                v125 += 1
            elif variation == 2:
                v225 += 1
            elif variation == 3:
                v325 += 1
            elif variation == 4:
                v425 += 1
            else:
                assert False

        print("Results : v150 {}, v250 {}".format(v150, v250))
        print("Results : v125 {}, v225 {}, v325 {}, v425 {}".format(v125, v225, v325, v425))

        min = (x / 2 - (x * 0.008))
        max = (x / 2 + (x * 0.008))
        assert min <= v150 <= max
        assert min <= v250 <= max
        assert v150 + v250 == x

        min1 = (x / 4 - (x * 0.008))
        max1 = (x / 4 + (x * 0.008))
        assert min1 <= v125 <= max1
        assert min1 <= v225 <= max1
        assert min1 <= v325 <= max1
        assert min1 <= v425 <= max1
        assert v125 + v225 + v325 + v425 == x

    except Exception as e:
        print(e)
        assert False
Beispiel #21
0
    def test_bucketing_304():

        @responses.activate
        def e_test_bucketing_200_again():
            json_response = '{"campaigns":[{"variationGroups":[{"variations":[{"allocation":100,"modifications":{"type":"FLAG","value":{"featureEnabled":true}},"id":"xxxx"}],"targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","value":true,"key":"isVIPUser"}]}]},"id":"yyyy"},{"variations":[{"allocation":100,"modifications":{"type":"FLAG","value":{"featureEnabled":false}},"id":"cccc"}],"targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","value":false,"key":"isVIPUser"}]}]},"id":"vvvv"}],"type":"toggle","id":"aaaa"},{"variationGroups":[{"variations":[{"allocation":25,"modifications":{"type":"JSON","value":{"rank_plus":null,"rank":null}},"id":"zzzz","reference":true},{"allocation":25,"modifications":{"type":"JSON","value":{"rank_plus":null,"rank":1111}},"id":"eeee"},{"allocation":25,"modifications":{"type":"JSON","value":{"rank_plus":null,"rank":3333}},"id":"rrrr"},{"allocation":25,"modifications":{"type":"JSON","value":{"rank_plus":22.22,"rank":2222}},"id":"tttt"}],"targeting":{"targetingGroups":[{"targetings":[{"operator":"EQUALS","value":"password","key":"access"}]}]},"id":"yyyy"}],"type":"ab","id":"iiii"}]}'
            headers = {
                "Last-Modified": "Fri,  05 Jun 2023 12:20:40 GMT"
            }
            responses.reset()
            responses.add(responses.GET,
                          'https://cdn.flagship.io/my_env_id/bucketing.json',
                          json=json.loads(json_response), status=200, adding_headers=headers)
            fs = Flagship.instance()
            fs.start("my_env_id", "my_api_key", Config(mode=Config.Mode.BUCKETING, polling_interval=-1))
            with open("bucketing.json", 'r') as f:
                content = f.read()
                assert len(content) > 2
                json_object = json.loads(content)
                last_modified = json_object['last_modified']
                assert last_modified is not None
                assert last_modified == "Fri,  05 Jun 2023 12:20:40 GMT"

        json_response = '{}'
        headers = {
            "Last-Modified": "fake"
        }
        responses.reset()
        responses.add(responses.GET,
                      'https://cdn.flagship.io/my_env_id/bucketing.json',
                      json=json.loads(json_response), status=304, adding_headers=headers)
        responses.add(responses.POST,
                      'https://decision.flagship.io/v2/my_env_id/events',
                      json=json.loads('{}'), status=200)
        responses.add(responses.POST,
                      'https://decision.flagship.io/v2/activate',
                      json=json.loads('{}'), status=200)

        fs = Flagship.instance()
        fs.start("my_env_id", "my_api_key", Config(mode=Config.Mode.BUCKETING, polling_interval=-1))
        visitor = Flagship.instance().create_visitor("ä",
                                                     {'isVIPUser': True, 'bin_a': 1,
                                                      'bin_b': 1})  # type: FlagshipVisitor
        visitor.update_context(('access', 'password'), True)
        assert visitor.get_modification('rank', "=null", True) != "=null"
        contains_activate = False
        contains_events = False
        for c in responses.calls:

            if contains_events is False and c.request.url.__contains__('events'):
                contains_events = True
            if contains_activate is False and c.request.url.__contains__('activate'):
                contains_events = True
        assert contains_events is True
        assert contains_events is True

        with open("bucketing.json", 'r') as f:
            content = f.read()
            assert len(content) > 2
            json_object = json.loads(content)
            last_modified = json_object['last_modified']
            assert last_modified is not None
            assert last_modified == "Fri,  05 Jun 2020 12:20:40 GMT"
            e_test_bucketing_200_again()
Beispiel #22
0
def test_visitor_synchronize():
    json_response = '{"visitorId":"visitor_1","campaigns":[{"id":"xxxxd0qhl5801abv9ib0",' \
                    '"variationGroupId":"xxxxd0qhl5801abv9ic0","variation":{"id":"xxxxd0qhl5801abv9icg",' \
                    '"modifications":{"type":"FLAG","value":{"featureEnabled":true}}}}]} '

    json_response2 = '{"visitorId":"visitor_1","campaigns":[{"id":"xxxxesjojh803lh57qo0",' \
                     '"variationGroupId":"xxxxesjojh803lh57qp0","variation":{"id":"xxxxesjojh803lh57qpg",' \
                     '"modifications":{"type":"FLAG","value":{"my_flag_nb":100}}}},{"id":"xxxxsp9j5mf4g0fdhkv2g",' \
                     '"variationGroupId":"xxxxp9j5mf4g0fdhkv3g","variation":{"id":"xxxxp9j5mf4g0fdhkv4g",' \
                     '"modifications":{"type":"JSON","value":{"btn-color":"red"}}}},{"id":"xxxxrfe4jaeg0gi1bhog",' \
                     '"variationGroupId":"xxxxrfe4jaeg0gi1bhpg","variation":{"id":"xxxxrfe4jaeg0gi1bhq0",' \
                     '"modifications":{"type":"FLAG","value":{"featureEnabled":true}}}},{"id":"xxxx9b1q4vl00quhh0rg",' \
                     '"variationGroupId":"xxxx9b1q4vl00quhh0sg","variation":{"id":"xxxx9b1q4vl00quhh0tg",' \
                     '"modifications":{"type":"JSON","value":{"k1":"v1","k2":null,"k3":null,"k6":"v6","k7":null}}}}]} '

    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key",
             Config(event_handler=None, mode=Config.Mode.API))
    visitor = fs.create_visitor("visitor_1")
    responses.reset()
    responses.add(
        responses.POST,
        'https://decision.flagship.io/v2/my_env_id/campaigns/?exposeAllKeys=true&sendContextEvent=false',
        json=json.loads(json_response),
        status=200)
    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/my_env_id/events',
                  status=200)

    visitor.synchronize_modifications()
    assert 'featureEnabled' in visitor._modifications
    assert visitor._modifications['featureEnabled'].value is True
    assert visitor._modifications[
        'featureEnabled'].variation_group_id == 'xxxxd0qhl5801abv9ic0'
    assert visitor._modifications[
        'featureEnabled'].variation_id == 'xxxxd0qhl5801abv9icg'

    responses.reset()
    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/my_env_id/events',
                  status=200)
    responses.add(
        responses.POST,
        'https://decision.flagship.io/v2/my_env_id/campaigns/?exposeAllKeys=true&sendContextEvent=false',
        json=json.loads(json_response2),
        status=200)
    visitor.synchronize_modifications()

    assert 'featureEnabled' in visitor._modifications
    assert visitor._modifications['featureEnabled'].value is True
    assert visitor._modifications[
        'featureEnabled'].variation_group_id == 'xxxxrfe4jaeg0gi1bhpg'
    assert visitor._modifications[
        'featureEnabled'].variation_id == 'xxxxrfe4jaeg0gi1bhq0'

    assert 'my_flag_nb' in visitor._modifications
    assert visitor._modifications['my_flag_nb'].value == 100
    assert visitor._modifications[
        'my_flag_nb'].variation_group_id == 'xxxxesjojh803lh57qp0'
    assert visitor._modifications[
        'my_flag_nb'].variation_id == 'xxxxesjojh803lh57qpg'

    assert 'k7' in visitor._modifications
    assert visitor._modifications['k7'].value is None
    assert visitor._modifications[
        'k7'].variation_group_id == 'xxxx9b1q4vl00quhh0sg'
    assert visitor._modifications['k7'].variation_id == 'xxxx9b1q4vl00quhh0tg'

    info = visitor.get_modification_info('k7')

    assert info['campaignId'] == 'xxxx9b1q4vl00quhh0rg'
    assert info['variationGroupId'] == 'xxxx9b1q4vl00quhh0sg'
    assert info['variationId'] == 'xxxx9b1q4vl00quhh0tg'
Beispiel #23
0
def test_visitor_panic():
    responses.reset()

    fs = Flagship.instance()
    fs.start("my_env_id", "my_api_key",
             Config(event_handler=None, mode=Config.Mode.API))
    visitor = fs.create_visitor("visitor_1")

    json_response = '{"visitorId":"visitor_1","campaigns":[{"id":"xxxxd0qhl5801abv9ib0",' \
                    '"variationGroupId":"xxxxd0qhl5801abv9ic0","variation":{"id":"xxxxd0qhl5801abv9icg",' \
                    '"modifications":{"type":"FLAG","value":{"featureEnabled":true}}}}]} '
    responses.add(
        responses.POST,
        'https://decision.flagship.io/v2/my_env_id/campaigns/?exposeAllKeys=true&sendContextEvent=false',
        json=json.loads(json_response),
        status=200)

    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/my_env_id/events',
                  status=200)

    responses.add(responses.POST, 'https://ariane.abtasty.com/', status=200)

    visitor.synchronize_modifications()

    visitor.send_hit(
        Page("script.py").with_ip("133.3.223.1").with_locale(
            "fr-fr").with_resolution(640, 480).with_session_number(3))

    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/activate',
                  status=200)
    visitor.activate_modification('featureEnabled')

    assert visitor.get_modification("featureEnabled", False) is True
    assert len(responses.calls) == 4

    responses.reset()
    json_response_panic = '{"visitorId":"Toto3000","campaigns":[],"panic":true}'
    responses.add(
        responses.POST,
        'https://decision.flagship.io/v2/my_env_id/campaigns/?exposeAllKeys=true&sendContextEvent=false',
        json=json.loads(json_response_panic),
        status=200)

    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/my_env_id/events',
                  status=200)

    responses.add(responses.POST, 'https://ariane.abtasty.com/', status=200)

    visitor.synchronize_modifications()

    visitor.send_hit(
        Page("script.py").with_ip("133.3.223.1").with_locale(
            "fr-fr").with_resolution(640, 480).with_session_number(3))

    responses.add(responses.POST,
                  'https://decision.flagship.io/v2/activate',
                  status=200)
    visitor.activate_modification('featureEnabled')

    visitor.synchronize_modifications()

    assert visitor._is_panic_mode() is True
    assert visitor.get_modification("featureEnabled", False) is False
    assert len(responses.calls) == 2