Skip to content

Commit

Permalink
Merge pull request #1045 from jefmoura/998-notify-org-admin
Browse files Browse the repository at this point in the history
Add Org admin email to notify them about the seats
  • Loading branch information
Rafael Muñoz Cárdenas authored Mar 5, 2018
2 parents 38a09ce + 7da7a2c commit 22deef6
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
1 change: 1 addition & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ services:
- CHARGEBEE_SITE_API_KEY=test_31lcdE7L3grqdkGcvy24ik3lmlJrnA0Ez
- CHARGEBEE_SITE=toladata-test
- TOLA_TRACK_SYNC_ENABLED=False
- [email protected]
6 changes: 6 additions & 0 deletions tola/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,9 @@
chargebee.configure(os.getenv('CHARGEBEE_SITE_API_KEY'), os.getenv('CHARGEBEE_SITE'))

########## END CHARGEBEE CONFIGURATION

########## EMAIL CONFIGURATION

DEFAULT_REPLY_TO = os.getenv('DEFAULT_REPLY_TO', '')

########## END EMAIL CONFIGURATION
29 changes: 25 additions & 4 deletions workflow/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
except ImportError:
pass
from django.conf import settings
from django.core.mail import EmailMessage
from django.contrib.auth.models import Group
from django.db.models import signals
from django.dispatch import receiver
Expand Down Expand Up @@ -107,8 +108,18 @@ def check_seats_save_team(sender, instance, **kwargs):
if user_addon:
available_seats = user_addon.quantity
if available_seats < org.chargebee_used_seats:
# TODO: Notify the Org admin
pass
user_email = instance.workflow_user.user.email
email = EmailMessage(
subject='Exceeded the number of editors',
body='The number of editors has exceeded the amount of '
'users set in your Subscription. Please check it out!'
'\nCurrent amount of editors: {}.\nSelected amount '
'of editors: {}.'.format(
org.chargebee_used_seats, available_seats),
to=[user_email],
reply_to=[settings.DEFAULT_REPLY_TO],
)
email.send()


@receiver(signals.pre_delete, sender=WorkflowTeam)
Expand Down Expand Up @@ -189,8 +200,18 @@ def check_seats_save_user_groups(sender, instance, **kwargs):
if user_addon:
available_seats = user_addon.quantity
if available_seats < org.chargebee_used_seats:
# TODO: Notify the Org admin
pass
user_email = instance.email
email = EmailMessage(
subject='Exceeded the number of editors',
body='The number of editors has exceeded the amount of '
'users set in your Subscription. Please check it '
'out!\nCurrent amount of editors: {}.\nSelected '
'amount of editors: {}.'.format(
org.chargebee_used_seats, available_seats),
to=[user_email],
reply_to=[settings.DEFAULT_REPLY_TO],
)
email.send()


# ORGANIZATION SIGNALS
Expand Down
63 changes: 59 additions & 4 deletions workflow/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from chargebee import Addon, Subscription
except ImportError:
pass
from django.core import mail
from django.test import TestCase, override_settings, tag
from mock import Mock, patch

Expand Down Expand Up @@ -109,7 +110,7 @@ def __init__(self, values):

addon = Addon(values)
addon.id = 'user'
addon.quantity = 0
addon.quantity = 1
self.subscription.addons = [addon]

def setUp(self):
Expand Down Expand Up @@ -216,6 +217,34 @@ def test_check_seats_save_team_org_admin(self):
organization = Organization.objects.get(pk=self.org.id)
self.assertEqual(organization.chargebee_used_seats, 1)

@override_settings(DEFAULT_REPLY_TO='[email protected]')
def test_check_seats_save_team_exceed_notify(self):
self.tola_user.user.groups.add(self.group_org_admin)
self.tola_user.user.save()
self.org = Organization.objects.get(pk=self.org.id)
user = factories.User(first_name='John', last_name='Lennon')
tolauser = factories.TolaUser(user=user, organization=self.org)

external_response = self.ExternalResponse(None)
Subscription.retrieve = Mock(return_value=external_response)
wflvl1 = factories.WorkflowLevel1(name='WorkflowLevel1')
factories.WorkflowTeam(workflow_user=tolauser,
workflowlevel1=wflvl1,
role=self.group_program_admin)

# It should notify the OrgAdmin
organization = Organization.objects.get(pk=self.org.id)
self.assertEqual(organization.chargebee_used_seats, 2)
self.assertEqual(len(mail.outbox), 1)
self.assertIn('Exceeded the number of editors', mail.outbox[0].subject)
self.assertEqual(mail.outbox[0].to, [user.email])
self.assertEqual(mail.outbox[0].reply_to, ['[email protected]'])
self.assertEqual(mail.outbox[0].body,
'The number of editors has exceeded the amount of '
'users set in your Subscription. Please check it '
'out!\nCurrent amount of editors: 2.\nSelected '
'amount of editors: 1.')


class CheckSeatsDeleteWFTeamsTest(TestCase):
class ExternalResponse:
Expand All @@ -225,7 +254,7 @@ def __init__(self, values):

addon = Addon(values)
addon.id = 'user'
addon.quantity = 0
addon.quantity = 1
self.subscription.addons = [addon]

def setUp(self):
Expand All @@ -251,7 +280,7 @@ def test_check_seats_delete_team_decrease(self):
organization = Organization.objects.get(pk=self.org.id)
self.assertEqual(organization.chargebee_used_seats, 0)

def test_check_seats_save_team_not_decrease(self):
def test_check_seats_delete_team_not_decrease(self):
external_response = self.ExternalResponse(None)
Subscription.retrieve = Mock(return_value=external_response)
wflvl1_1 = factories.WorkflowLevel1(name='WorkflowLevel1_1')
Expand Down Expand Up @@ -310,7 +339,7 @@ def __init__(self, values):

addon = Addon(values)
addon.id = 'user'
addon.quantity = 0
addon.quantity = 1
self.subscription.addons = [addon]

def setUp(self):
Expand Down Expand Up @@ -378,6 +407,32 @@ def test_check_seats_save_user_groups_demo(self, mock_tsync):
organization = Organization.objects.get(pk=self.org.id)
self.assertEqual(organization.chargebee_used_seats, 0)

@override_settings(DEFAULT_REPLY_TO='[email protected]')
def test_check_seats_save_user_groups_exceed_notify(self):
external_response = self.ExternalResponse(None)
Subscription.retrieve = Mock(return_value=external_response)
self.tola_user.user.groups.add(self.group_org_admin)
self.tola_user.user.save()

self.org = Organization.objects.get(pk=self.org.id)
user = factories.User(first_name='John', last_name='Lennon')
tolauser = factories.TolaUser(user=user, organization=self.org)
tolauser.user.groups.add(self.group_org_admin)
tolauser.user.save()

# It should notify the OrgAdmin
organization = Organization.objects.get(pk=self.org.id)
self.assertEqual(organization.chargebee_used_seats, 2)
self.assertEqual(len(mail.outbox), 1)
self.assertIn('Exceeded the number of editors', mail.outbox[0].subject)
self.assertEqual(mail.outbox[0].to, [user.email])
self.assertEqual(mail.outbox[0].reply_to, ['[email protected]'])
self.assertEqual(mail.outbox[0].body,
'The number of editors has exceeded the amount of '
'users set in your Subscription. Please check it '
'out!\nCurrent amount of editors: 2.\nSelected '
'amount of editors: 1.')


class SignalSyncTrackTest(TestCase):
def setUp(self):
Expand Down

0 comments on commit 22deef6

Please sign in to comment.