Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1464 from akatsoulas/waffle-basket-flag
Browse files Browse the repository at this point in the history
[fix bug 1282806] Add a waffle switch for basket tasks.
  • Loading branch information
akatsoulas committed Jun 29, 2016
2 parents 452ea4f + b01abe6 commit 564ad3c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 4 additions & 2 deletions mozillians/users/tasks.py
Expand Up @@ -7,6 +7,7 @@

import basket
import requests
import waffle
from celery.task import task
from celery.exceptions import MaxRetriesExceededError
from elasticutils.contrib.django import get_es
Expand Down Expand Up @@ -75,7 +76,8 @@ def update_basket_task(instance_id):
except UserProfile.DoesNotExist:
instance = None

if not BASKET_ENABLED or not instance or not instance.is_vouched:
if (not BASKET_ENABLED or not instance or not instance.is_vouched or
not waffle.switch_is_active('BASKET_SWITCH_ENABLED')):
return

email = instance.user.email
Expand Down Expand Up @@ -171,7 +173,7 @@ def unsubscribe_from_basket_task(email, basket_token):
# look anything up about the user locally. It has to make do
# with the email and token passed in.

if not BASKET_ENABLED:
if not BASKET_ENABLED or not waffle.switch_is_active('BASKET_SWITCH_ENABLED'):
return

try:
Expand Down
13 changes: 10 additions & 3 deletions mozillians/users/tests/test_tasks.py
Expand Up @@ -124,8 +124,9 @@ def test_email_basket_managers(self, send_mail_mock):

@override_settings(BASKET_NEWSLETTER='newsletter')
@patch('mozillians.users.tasks.BASKET_ENABLED', True)
@patch('mozillians.users.tasks.waffle.switch_is_active')
@patch('mozillians.users.tasks.basket')
def test_change_email(self, mock_basket):
def test_change_email(self, mock_basket, switch_is_active_mock):
# When a user's email is changed, their old email is unsubscribed
# from all newsletters and their new email is subscribed to them.

Expand All @@ -140,6 +141,7 @@ def test_change_email(self, mock_basket):
mock_basket.subscribe.return_value = {
'token': token,
}
switch_is_active_mock.return_value = True
user = UserFactory.create(email=email)
up = UserProfile.objects.get(pk=user.userprofile.pk)
eq_(token, up.basket_token)
Expand All @@ -165,18 +167,23 @@ def test_change_email(self, mock_basket):
eq_(new_token, up.basket_token)

@override_settings(BASKET_NEWSLETTER='newsletter')
@patch('mozillians.users.tasks.waffle.switch_is_active')
@patch('mozillians.users.tasks.basket.unsubscribe')
def test_unsubscribe_from_basket_task(self, unsubscribe_mock):
def test_unsubscribe_from_basket_task(self, unsubscribe_mock, switch_is_active_mock):
switch_is_active_mock.return_value = True
user = UserFactory.create(userprofile={'basket_token': 'foo'})
with patch('mozillians.users.tasks.BASKET_ENABLED', True):
unsubscribe_from_basket_task(user.email, user.userprofile.basket_token)
unsubscribe_mock.assert_called_with(
user.userprofile.basket_token, user.email, newsletters='newsletter')

@override_settings(BASKET_NEWSLETTER='newsletter')
@patch('mozillians.users.tasks.waffle.switch_is_active')
@patch('mozillians.users.tasks.basket')
@patch.object(UserProfile, 'lookup_basket_token')
def test_unsubscribe_from_basket_task_without_token(self, lookup_token_mock, basket_mock):
def test_unsubscribe_from_basket_task_without_token(self, lookup_token_mock, basket_mock,
switch_is_active_mock):
switch_is_active_mock.return_value = True
lookup_token_mock.return_value = 'basket_token'
basket_mock.lookup_user.return_value = {'token': 'basket_token'}
user = UserFactory.create(userprofile={'basket_token': ''})
Expand Down

0 comments on commit 564ad3c

Please sign in to comment.