Skip to content

Commit

Permalink
Added checks before creating a company (#356)
Browse files Browse the repository at this point in the history
* Added checks before creating a company

* fixed issues

* added tests

* made code more specific

* added test for redirection
  • Loading branch information
facosta-eb authored Jun 10, 2020
1 parent c9f55da commit f9ab5d2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions supplier_app/constants/custom_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
TAXPAYER_WORKDAY_UNIQUE_ERROR = _("There is other taxpayer with that workday id")

THANKS = _("Thank you. We'll get back to you shortly.")

COMPANY_ALREADY_EXIST = _('That company name and/or email is already in use!')
18 changes: 18 additions & 0 deletions supplier_app/tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,24 @@ def test_valid_company_creation(self):
self.company_constants['name']
)

def test_duplicated_company_creation(self):
self.client.force_login(self.ap_user)
self._make_post()
self._make_post()
self.assertEqual(
len(Company.objects.filter(name='Eventbrite')),
1
)

def test_duplicated_company_creation_redirect_to_self(self):
self.client.force_login(self.ap_user)
self._make_post()
response = self._make_post()
self.assertEqual(
response.redirect_chain[0],
(reverse('company-create'), HTTPStatus.FOUND)
)

def test_valid_company_creation_as_buyer(self):
self.client.force_login(self.user_buyer_with_google_social)
self._make_post()
Expand Down
10 changes: 10 additions & 0 deletions supplier_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
TAXPAYER_WITHOUT_WORKDAY_ID_MESSAGE,
TAXPAYER_WORKDAY_UNIQUE_ERROR,
THANKS,
COMPANY_ALREADY_EXIST,
)
from supplier_app.constants.taxpayer_status import (
TAXPAYER_STATUS_APPROVED,
Expand Down Expand Up @@ -109,6 +110,8 @@ def get_context_data(self, **kwargs):
return context

def form_valid(self, form):
if Company.objects.filter(name=form.data['name']).exists():
return HttpResponseRedirect(self.get_failure_url())
company = self.save_company(form)
InvitingBuyer.objects.create(company=company, inviting_buyer=self.request.user)
EBEntityCompany.objects.create(company=company, eb_entity=EBEntity.objects.get(pk=form.data['eb_entity']))
Expand All @@ -121,6 +124,13 @@ def save_company(self, forms):

def get_success_url(self):
return reverse('company-list')

def get_failure_url(self):
messages.error(
self.request,
COMPANY_ALREADY_EXIST,
)
return reverse('company-create')

class CompanyListView(LoginRequiredMixin, ListView):
model = Company
Expand Down
1 change: 1 addition & 0 deletions templates/supplier_app/AP/company_creation.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

{% block content %}
{% include 'navbar.html' %}
{% include 'messages_notification.html' %}
<h1 class='text-center mb-5'>
{% trans "Create a new company" %}
</h1>
Expand Down

0 comments on commit f9ab5d2

Please sign in to comment.