Esempio n. 1
0
def test_remove_preferences_multiple_remove_on_opt_in(session, subscription):
    preference_1 = subscription.datetime[0]
    preference_2 = subscription.datetime[1]
    user_pref_1 = UserSubscriptionPreferences(
        preference=preference_1,
        subscription=subscription,
    )
    user_pref_2 = UserSubscriptionPreferences(
        preference=preference_2,
        subscription=subscription,
    )
    session.add(user_pref_1)
    session.add(user_pref_2)
    user = User(email='*****@*****.**',
                meta_data={'department': 'dept'},
                subscription_preferences=[user_pref_1, user_pref_2])
    session.add(user)
    session.commit()

    assert user.subscription_preferences == [user_pref_1, user_pref_2]
    updated_preferences = {preference_1.id: False, preference_2.id: False}
    removed = remove_preferences(user, updated_preferences, subscription.id)
    assert removed == {user_pref_1.preference_id, user_pref_2.preference_id}
    user = user = User.query.filter(User.id == user.id).one()
    assert user.subscription_preferences == []
    assert UserSubscriptionPreferences.query.all() == []
Esempio n. 2
0
def test_remove_preferences_removes_on_opt_out(minimal_database, subscription):
    preference = subscription.datetime[0]
    user_pref = UserSubscriptionPreferences(
        preference=preference,
        subscription=subscription.key,
    ).put()
    user = User(email='*****@*****.**',
                metadata={'department': 'dept'},
                subscription_preferences=[user_pref])
    user.put()

    assert user.subscription_preferences == [user_pref]
    updated_preferences = {preference: False}
    removed = remove_preferences(user, updated_preferences, subscription.key)
    assert removed == {user_pref}
    user = user.key.get()
    assert user.subscription_preferences == []
    assert UserSubscriptionPreferences.query().fetch() == []
Esempio n. 3
0
def test_remove_preferences_does_not_remove_on_opt_in(session, subscription):
    preference = subscription.datetime[0]
    user_pref = UserSubscriptionPreferences(
        preference=preference,
        subscription=subscription,
    )
    session.add(user_pref)
    user = User(email='*****@*****.**',
                meta_data={'department': 'dept'},
                subscription_preferences=[user_pref])
    session.add(user)
    session.commit()

    assert user.subscription_preferences == [user_pref]
    updated_preferences = {preference.id: True}
    removed = remove_preferences(user, updated_preferences, subscription)
    assert removed == set()
    user = User.query.filter(User.id == user.id).one()
    assert user.subscription_preferences == [user_pref]
    assert len(UserSubscriptionPreferences.query.all()) == 1
Esempio n. 4
0
def test_remove_preferences_removes_on_opt_out(session, subscription):
    preference = subscription.datetime[0]
    user_pref = UserSubscriptionPreferences(
        preference=preference,
        subscription=subscription,
    )
    session.add(user_pref)
    user = User(email='*****@*****.**',
                meta_data={'department': 'dept'},
                subscription_preferences=[user_pref])
    session.add(user)
    session.commit()

    assert user.subscription_preferences == [user_pref]
    updated_preferences = {preference.id: False}
    removed = remove_preferences(user, updated_preferences, subscription.id)
    assert removed == {user_pref.preference_id}
    user = User.query.filter(User.id == user.id).one()
    assert user.subscription_preferences == []
    assert UserSubscriptionPreferences.query.all() == []
Esempio n. 5
0
def preferences_api_post(subscription):
    data = request.json
    user = get_user()
    if not user:
        return '400'

    subscription_key = ndb.Key(urlsafe=subscription)
    form_selection = {}
    for key, value in data.items():
        form_selection[ndb.Key(urlsafe=key)] = value

    removed = remove_preferences(user, form_selection, subscription_key)
    logging.info('Removed')
    logging.info(removed)

    added = add_preferences(user, form_selection, subscription_key)
    logging.info('Added')
    logging.info(added)

    return 'OK'
Esempio n. 6
0
def preferences_api_post(subscription_id: int) -> str:
    data = request.json
    user = get_user(data.get('email'))
    del data['email']
    if not user:
        return '400'

    form_selection = {}
    for key, value in data.items():
        # Convert key (a table id) to an int
        form_selection[int(key)] = value

    removed = remove_preferences(user, form_selection, subscription_id)
    logging.info('Removed')
    logging.info(removed)

    added = add_preferences(user, form_selection, subscription_id)
    logging.info('Added')
    logging.info(added)

    return 'OK'
Esempio n. 7
0
def test_remove_preferences_multiple_remove_on_opt_in(minimal_database,
                                                      subscription):
    preference_1 = subscription.datetime[0]
    preference_2 = subscription.datetime[1]
    user_pref_1 = UserSubscriptionPreferences(
        preference=preference_1,
        subscription=subscription.key,
    ).put()
    user_pref_2 = UserSubscriptionPreferences(
        preference=preference_2,
        subscription=subscription.key,
    ).put()
    user = User(email='*****@*****.**',
                metadata={'department': 'dept'},
                subscription_preferences=[user_pref_1, user_pref_2])
    user.put()

    assert user.subscription_preferences == [user_pref_1, user_pref_2]
    updated_preferences = {preference_1: False, preference_2: False}
    removed = remove_preferences(user, updated_preferences, subscription.key)
    assert removed == {user_pref_1, user_pref_2}
    user = user.key.get()
    assert user.subscription_preferences == []
    assert UserSubscriptionPreferences.query().fetch() == []