Skip to content

Commit

Permalink
add support for rule logic with users that have a list of roles (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
kentwills authored May 6, 2020
1 parent 77b6d5f commit 3784cba
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
32 changes: 32 additions & 0 deletions api/tests/logic/subscription_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,38 @@ def test_filter_subscriptions_by_user_data_any(database):
assert subscriptions == []


def test_filter_subscriptions_by_user_data_list(database):
preference = UserSubscriptionPreferences(
subscription=database.sub.key,
preference=database.prefs[0].key
)
preference.put()
user = User(
email='a@a.com',
subscription_preferences=[preference.key]
)
user.metadata = {"role": ["pushmaster", "technical_lead"]}
user.put()

rule = Rule(name="role", value="pushmaster").put()
database.sub.user_rules = [rule]
database.sub.rule_logic = 'any'
database.sub.put()

merged_preferences = merge_subscriptions_with_preferences(user)
subscriptions = filter_subscriptions_by_user_data(merged_preferences, user)

assert len(subscriptions) == 1
assert subscriptions[0]['id'] == database.sub.key.urlsafe()

user.metadata = {"role": "infra"}
user.put()
merged_preferences = merge_subscriptions_with_preferences(user)
subscriptions = filter_subscriptions_by_user_data(merged_preferences, user)

assert len(subscriptions) == 0


def test_filter_subscriptions_by_user_data_all(database):
database.sub.rule_logic = 'all'
rule1 = Rule(name="department", value="a").put()
Expand Down
4 changes: 2 additions & 2 deletions api/tests/matching/group_match_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_generate_groups():
assert [x for x in result] == [[1, 2], [3, 4]]


def test_generate_group_meetings_invalid_number_of_users():
def test_generate_group_meetings_invalid_number_of_users(minimal_database):
pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1)).put()
subscription = MeetingSubscription(title='all engineering weekly', datetime=[pref_1]).put()
user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription).put()
Expand All @@ -60,7 +60,7 @@ def test_generate_group_meetings_invalid_number_of_users():
assert set(user.key for user in unmatched) == {user1, user2}


def test_generate_group_meetings():
def test_generate_group_meetings(minimal_database):
pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1)).put()
subscription = MeetingSubscription(title='all engineering weekly', datetime=[pref_1]).put()
user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription).put()
Expand Down
12 changes: 8 additions & 4 deletions api/yelp_beans/logic/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ def apply_rules(user, subscription, subscription_rules, rule_logic):
subscription_rules: models.Rule()
rule_logic: all(), any()
"""
rules = {
user.metadata.get(rule.get().name) == rule.get().value
for rule in subscription_rules
}
rules = set()
for rule in subscription_rules:
user_rule = user.metadata.get(rule.get().name)
subscription_rule = rule.get().value
if type(user_rule) is list:
rules.add(subscription_rule in user_rule)
else:
rules.add(user_rule == subscription_rule)
if rule_logic(rules):
return subscription

Expand Down

0 comments on commit 3784cba

Please sign in to comment.