lingo: search user by SAML name_id on remote invoices notifying (#22755)
This commit is contained in:
parent
265752b496
commit
d560d5ff81
|
@ -47,6 +47,12 @@ from combo.data.library import register_cell_class
|
|||
from combo.utils import NothingInCacheException, aes_hex_encrypt, requests
|
||||
from combo.apps.notifications.models import Notification
|
||||
|
||||
try:
|
||||
from mellon.models import UserSAMLIdentifier
|
||||
except ImportError:
|
||||
UserSAMLIdentifier = None
|
||||
|
||||
|
||||
EXPIRED = 9999
|
||||
|
||||
|
||||
|
@ -210,7 +216,7 @@ class Regie(models.Model):
|
|||
user_cancellable=False).save()
|
||||
|
||||
def get_remote_pending_invoices(self):
|
||||
if not self.is_remote():
|
||||
if not self.is_remote() or UserSAMLIdentifier is None:
|
||||
return {}
|
||||
url = self.webservice_url + '/users/with-pending-invoices/'
|
||||
response = requests.get(url, remote_service='auto', cache_duration=0,
|
||||
|
@ -260,12 +266,15 @@ class Regie(models.Model):
|
|||
return notification_id
|
||||
|
||||
def notify_new_remote_invoices(self):
|
||||
if UserSAMLIdentifier is None:
|
||||
# remote invoices retrieval requires SAML
|
||||
return
|
||||
pending_invoices = self.get_remote_pending_invoices()
|
||||
notification_ids = []
|
||||
for uuid, items in pending_invoices.iteritems():
|
||||
try:
|
||||
user = User.objects.get(username=uuid)
|
||||
except User.DoesNotExist:
|
||||
user = UserSAMLIdentifier.objects.get(name_id=uuid).user
|
||||
except UserSAMLIdentifier.DoesNotExist:
|
||||
continue
|
||||
for invoice in items['invoices']:
|
||||
if Decimal(invoice['total_amount']) >= self.payment_min_amount:
|
||||
|
|
|
@ -384,8 +384,12 @@ def test_remote_invoice_successfull_payment_redirect(mock_get, mock_pay_invoice,
|
|||
assert urlparse.urlparse(resp.location).path == '/active-remote-invoices-page/'
|
||||
|
||||
|
||||
@mock.patch('combo.apps.lingo.models.UserSAMLIdentifier')
|
||||
@mock.patch('combo.apps.lingo.models.requests.get')
|
||||
def test_send_new_remote_invoices_by_email(mock_get, admin, app, remote_regie, mailoutbox):
|
||||
def test_send_new_remote_invoices_by_email(mock_get, user_saml, admin, app, remote_regie, mailoutbox):
|
||||
mocked_objects = mock.Mock()
|
||||
mocked_objects.get.return_value = mock.Mock(user=admin)
|
||||
user_saml.objects = mocked_objects
|
||||
datetime_format = '%Y-%m-%dT%H:%M:%S'
|
||||
invoice_now = now()
|
||||
creation_date = (invoice_now - timedelta(days=1)).strftime(datetime_format)
|
||||
|
|
|
@ -345,35 +345,54 @@ def test_notify_remote_items(mock_get, app, user, user2, regie):
|
|||
regie.webservice_url = 'http://example.org/regie' # is_remote
|
||||
regie.save()
|
||||
regie.notify_new_remote_invoices()
|
||||
assert 'NameID=' not in mock_get.call_args[0][1]
|
||||
assert 'email=' not in mock_get.call_args[0][1]
|
||||
mock_get.assert_not_called()
|
||||
|
||||
assert Notification.objects.filter(external_id__startswith='invoice-%s:' % regie.slug).visible().new().count() == 2
|
||||
assert Notification.objects.filter(external_id__startswith='invoice-%s:reminder-' % regie.slug).count() == 0
|
||||
assert Notification.objects.count() == 2
|
||||
for notif in Notification.objects.all():
|
||||
assert notif.url == '', notif.id
|
||||
with mock.patch('combo.apps.lingo.models.UserSAMLIdentifier') as user_saml:
|
||||
# simulate class exception
|
||||
class DoesNotExist(Exception):
|
||||
pass
|
||||
user_saml.DoesNotExist = DoesNotExist
|
||||
def side_effect(*args, **kwargs):
|
||||
name_id = kwargs['name_id']
|
||||
if name_id == 'foo':
|
||||
raise user_saml.DoesNotExist
|
||||
return mock.Mock(user=User.objects.get(username=name_id))
|
||||
|
||||
page = Page(title='Active Items', slug='active_items', template_name='standard')
|
||||
page.save()
|
||||
cell = ActiveItems(page=page, placeholder='content', order=0)
|
||||
cell.save()
|
||||
mocked_objects = mock.Mock()
|
||||
mocked_objects.get = mock.Mock(side_effect=side_effect)
|
||||
user_saml.objects = mocked_objects
|
||||
|
||||
for user in FAKE_PENDING_INVOICES['data']:
|
||||
for invoice in FAKE_PENDING_INVOICES['data'][user]['invoices']:
|
||||
invoice['pay_limit_date'] = new_pay_limit_date
|
||||
assert Notification.objects.all().count() == 0
|
||||
|
||||
# create remind notifications
|
||||
regie.notify_new_remote_invoices()
|
||||
assert Notification.objects.exclude(external_id__startswith='invoice-%s:reminder-' % regie.slug) \
|
||||
.visible().count() == 0
|
||||
assert Notification.objects.filter(external_id__startswith='invoice-%s:reminder-' % regie.slug) \
|
||||
.visible().new().count() == 2
|
||||
assert Notification.objects.count() == 4
|
||||
regie.notify_new_remote_invoices()
|
||||
assert 'NameID=' not in mock_get.call_args[0][1]
|
||||
assert 'email=' not in mock_get.call_args[0][1]
|
||||
assert Notification.objects.filter(external_id__startswith='invoice-%s:' % regie.slug).visible().new().count() == 2
|
||||
assert Notification.objects.filter(external_id__startswith='invoice-%s:reminder-' % regie.slug).count() == 0
|
||||
assert Notification.objects.count() == 2
|
||||
for notif in Notification.objects.all():
|
||||
assert notif.url == '', notif.id
|
||||
|
||||
# url appeared on new new reminder notifications
|
||||
assert len([notif for notif in Notification.objects.all() if notif.url == page.get_online_url()]) == 2
|
||||
page = Page(title='Active Items', slug='active_items', template_name='standard')
|
||||
page.save()
|
||||
cell = ActiveItems(page=page, placeholder='content', order=0)
|
||||
cell.save()
|
||||
|
||||
# be sure the are no more reminders created
|
||||
regie.notify_new_remote_invoices()
|
||||
assert Notification.objects.count() == 4
|
||||
for username in FAKE_PENDING_INVOICES['data']:
|
||||
for invoice in FAKE_PENDING_INVOICES['data'][username]['invoices']:
|
||||
invoice['pay_limit_date'] = new_pay_limit_date
|
||||
|
||||
# create remind notifications
|
||||
regie.notify_new_remote_invoices()
|
||||
assert Notification.objects.exclude(external_id__startswith='invoice-%s:reminder-' % regie.slug) \
|
||||
.visible().count() == 0
|
||||
assert Notification.objects.filter(external_id__startswith='invoice-%s:reminder-' % regie.slug) \
|
||||
.visible().new().count() == 2
|
||||
assert Notification.objects.count() == 4
|
||||
|
||||
# url appeared on new reminder notifications
|
||||
assert len([notif for notif in Notification.objects.all() if notif.url == page.get_online_url()]) == 2
|
||||
|
||||
# be sure the are no more reminders created
|
||||
regie.notify_new_remote_invoices()
|
||||
assert Notification.objects.count() == 4
|
||||
|
|
Loading…
Reference in New Issue