Skip to content

Commit

Permalink
Merge pull request #231 from monzo/exclude-bots
Browse files Browse the repository at this point in the history
Allow for bot users to be excluded from the cache
  • Loading branch information
Chris authored Oct 7, 2020
2 parents 2ffdd2d + add90dd commit 3d66665
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 42 deletions.
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pytest-pythonpath==0.7.3
pytest-sugar==0.9.2
pytest-watch==4.2.0
pytest-randomly==3.1.0
factory-boy>=2.12.0
factory-boy>=3.0.0
faker>=2.0.0
psycopg2-binary==2.8.2

Expand Down
4 changes: 3 additions & 1 deletion response/slack/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
logger = logging.getLogger(__name__)


def update_user_cache():
def update_user_cache(exclude_bots=False):
cursor = None
while cursor != "":
response = settings.SLACK_CLIENT.get_paginated_users(limit=200, cursor=cursor)
Expand All @@ -19,6 +19,8 @@ def update_user_cache():
logger.info(f"Updating {len(users)} users in the cache")
with transaction.atomic():
for user in users:
if exclude_bots and user["is_bot"]:
continue
ExternalUser.objects.update_or_create_slack(
external_id=user["id"],
defaults={
Expand Down
4 changes: 2 additions & 2 deletions tests/factories/action.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random

import factory
import factory.django
from django.db.models.signals import post_save
from faker import Factory

Expand All @@ -10,7 +10,7 @@


@factory.django.mute_signals(post_save)
class ActionFactory(factory.DjangoModelFactory):
class ActionFactory(factory.django.DjangoModelFactory):
class Meta:
model = Action

Expand Down
4 changes: 2 additions & 2 deletions tests/factories/event.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import random

import factory
import factory.django
from faker import Factory

from response.core.models import Event

faker = Factory.create()


class EventFactory(factory.DjangoModelFactory):
class EventFactory(factory.django.DjangoModelFactory):
class Meta:
model = Event

Expand Down
6 changes: 3 additions & 3 deletions tests/factories/incident.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random

import factory
import factory.django
from django.db.models.signals import post_save
from faker import Factory

Expand All @@ -12,13 +12,13 @@
faker = Factory.create()


class CommsChannelFactory(factory.DjangoModelFactory):
class CommsChannelFactory(factory.django.DjangoModelFactory):
class Meta:
model = CommsChannel


@factory.django.mute_signals(post_save)
class IncidentFactory(factory.DjangoModelFactory):
class IncidentFactory(factory.django.DjangoModelFactory):
class Meta:
model = Incident

Expand Down
4 changes: 2 additions & 2 deletions tests/factories/timeline.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import random

import factory
import factory.django
from faker import Factory

from response.core.models import TimelineEvent

faker = Factory.create()


class TimelineEventFactory(factory.DjangoModelFactory):
class TimelineEventFactory(factory.django.DjangoModelFactory):
class Meta:
model = TimelineEvent

Expand Down
6 changes: 3 additions & 3 deletions tests/factories/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import factory
import factory.django
from django.contrib.auth.models import User
from faker import Factory

Expand All @@ -7,15 +7,15 @@
faker = Factory.create()


class UserFactory(factory.DjangoModelFactory):
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = User

username = factory.Sequence(lambda i: faker.user_name() + str(i))
password = factory.LazyFunction(faker.password)


class ExternalUserFactory(factory.DjangoModelFactory):
class ExternalUserFactory(factory.django.DjangoModelFactory):
class Meta:
model = ExternalUser

Expand Down
56 changes: 28 additions & 28 deletions tests/slack/slack_payloads.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
users_list_response = {
"ok": "True",
"ok": True,
"members": [
{
"id": "W012A3CDE",
"team_id": "T012AB3C4",
"name": "spengler",
"deleted": "False",
"deleted": False,
"color": "9f69e7",
"real_name": "spengler",
"tz": "America/Los_Angeles",
Expand All @@ -28,21 +28,21 @@
"image_512": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg",
"team": "T012AB3C4",
},
"is_admin": "True",
"is_owner": "False",
"is_primary_owner": "False",
"is_restricted": "False",
"is_ultra_restricted": "False",
"is_bot": "False",
"is_admin": True,
"is_owner": False,
"is_primary_owner": False,
"is_restricted": False,
"is_ultra_restricted": False,
"is_bot": False,
"updated": 1502138686,
"is_app_user": "False",
"has_2fa": "False",
"is_app_user": False,
"has_2fa": False,
},
{
"id": "U12345678",
"team_id": "T0G9PQBBK",
"name": "glinda",
"deleted": "False",
"deleted": False,
"color": "9f69e7",
"real_name": "Glinda Southgood",
"tz": "America/Los_Angeles",
Expand All @@ -69,14 +69,14 @@
"display_name_normalized": "Glinda the Fairly Good",
"email": "[email protected]",
},
"is_admin": "True",
"is_owner": "False",
"is_primary_owner": "False",
"is_restricted": "False",
"is_ultra_restricted": "False",
"is_bot": "False",
"is_admin": True,
"is_owner": False,
"is_primary_owner": False,
"is_restricted": False,
"is_ultra_restricted": False,
"is_bot": True,
"updated": 1480527098,
"has_2fa": "False",
"has_2fa": False,
},
],
"cache_ts": 1498777272,
Expand All @@ -85,13 +85,13 @@


users_list_new = {
"ok": "True",
"ok": True,
"members": [
{
"id": "U10293847",
"team_id": "T012AB3C4",
"name": "venkman",
"deleted": "False",
"deleted": False,
"color": "9f69e7",
"real_name": "venkman",
"tz": "America/Los_Angeles",
Expand All @@ -114,15 +114,15 @@
"image_512": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg",
"team": "T012AB3C4",
},
"is_admin": "True",
"is_owner": "False",
"is_primary_owner": "False",
"is_restricted": "False",
"is_ultra_restricted": "False",
"is_bot": "False",
"is_admin": True,
"is_owner": False,
"is_primary_owner": False,
"is_restricted": False,
"is_ultra_restricted": False,
"is_bot": False,
"updated": 1502138686,
"is_app_user": "False",
"has_2fa": "False",
"is_app_user": False,
"has_2fa": False,
}
],
"cache_ts": 1498777272,
Expand Down
9 changes: 9 additions & 0 deletions tests/slack/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def test_update_cache_from_empty(mock_slack):
assert len(ExternalUser.objects.all()) == 2


@pytest.mark.django_db
def test_update_cache_excludes_bots(mock_slack):
mock_slack.get_paginated_users.return_value = users_list_response

update_user_cache(True)

assert len(ExternalUser.objects.all()) == 1


@pytest.mark.django_db
def test_update_cache_from_populated(mock_slack):
mock_slack.get_paginated_users.return_value = users_list_response
Expand Down

0 comments on commit 3d66665

Please sign in to comment.