Skip to content

Commit

Permalink
Send email alert on low priced bounties
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Lipert committed Dec 11, 2018
1 parent 3628c64 commit 0ee3452
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
68 changes: 68 additions & 0 deletions app/bounty_requests/tests/test_bounty_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-
"""Handle marketing mail related tests.
Copyright (C) 2018 Gitcoin Core
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from unittest.mock import patch

from django.utils import timezone

from bounty_requests.views import LOW_BOUNTY_THRESHOLD
from dashboard.models import Profile
from test_plus.test import APITestCase


class BountyRequestTest(APITestCase):

def setUp(self):
self.email = '[email protected]'
self.user = self.make_user()
self.user.email = self.email
self.user.profile = Profile.objects.create(
user=self.user,
handle='testuser',
last_sync_date=timezone.now(),
data={},
)
self.user.save()

@patch('marketing.mails.send_mail')
def test_lowball_bounty(self, mock_send_mail):
"""Test that an email alert is sent for low priced bounties."""
with self.login(username=self.user.username, password='password'):
response = self.post('/requests/',
data={'amount': LOW_BOUNTY_THRESHOLD - 1,
'github_url': 'https://github.com/gitcoinco/test',
'eth_address': '0x0',
'comment': 'test'},
extra={'format': 'json'})
self.response_200(response)
# In addition to the standard new bounty email, an alert email is sent
assert mock_send_mail.call_count == 2

@patch('marketing.mails.send_mail')
def test_normal_bounty(self, mock_send_mail):
"""Test that no email alert is sent for resonably priced bounties."""
with self.login(username=self.user.username, password='password'):
response = self.post('/requests/',
data={'amount': LOW_BOUNTY_THRESHOLD,
'github_url': 'https://github.com/gitcoinco/test',
'eth_address': '0x0',
'comment': 'test'},
extra={'format': 'json'})
self.response_200(response)
assert mock_send_mail.call_count == 1
6 changes: 5 additions & 1 deletion app/bounty_requests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
from django.views.decorators.csrf import csrf_exempt

from linkshortener.models import Link
from marketing.mails import new_bounty_request
from marketing.mails import low_bounty_request, new_bounty_request
from ratelimit.decorators import ratelimit

from .forms import BountyRequestForm
from .models import BountyRequest

LOW_BOUNTY_THRESHOLD = 5.0


@ratelimit(key='ip', rate='5/m', method=ratelimit.UNSAFE, block=True)
@csrf_exempt
Expand Down Expand Up @@ -63,6 +65,8 @@ def bounty_request(request):
model.requested_by = profile
model.save()
new_bounty_request(model)
if (model.amount < LOW_BOUNTY_THRESHOLD):
low_bounty_request(model)
return JsonResponse({'msg': _('Bounty Request received.')}, status=200)

form = BountyRequestForm()
Expand Down
23 changes: 23 additions & 0 deletions app/marketing/mails.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,29 @@ def new_bounty_request(model):
translation.activate(cur_language)


def low_bounty_request(bounty):
to_email = '[email protected]'
from_email = bounty.requested_by.email or settings.SERVER_EMAIL
cur_language = translation.get_language()
try:
setup_lang(to_email)
subject = _("Low Bounty Alert")
body_str = _("A low bounty was set from")
body = f"{body_str} {bounty.requested_by} ({bounty.requested_by.email}):"\
f"{settings.BASE_URL}_administrationbounty_requests/bountyrequest/{bounty.pk}/change\n\n"\
f"Github: {bounty.github_url}\n"
send_mail(
from_email,
to_email,
subject,
body,
from_name=_("No Reply from Gitcoin.co"),
categories=['admin'],
)
finally:
translation.activate(cur_language)


def new_funding_limit_increase_request(profile, cleaned_data):
to_email = '[email protected]'
from_email = profile.email or settings.SERVER_EMAIL
Expand Down
1 change: 1 addition & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ isort==4.3.4
responses==0.10.4
requests_mock==1.5.2
django-test-plus==1.1.1
idna==2.7
git+https://github.com/google/yapf.git#egg=yapf

0 comments on commit 0ee3452

Please sign in to comment.