Skip to content

Commit f2d2639

Browse files
germanpskalanuxIanPuchetti
authored
63 cambiar el formulario de alta de empresas para que use usercompanyprofile (#550)
* Added pytest as a runner * Added pytest as a runner on Makefile * Added joboffers app * Some fixes to the JobOffer model * Added initial joboffer detail * Added initial job offer add form * Changed joboffer's urls to use their namespace * Added some basic pytest's fixtures * Updated JobOffer's factory to include all the needed fields * Added joboffer creation test * Added joboffer admin page * Moved add button to bottom of the page (joboffers) * Added search button in admin page (joboffers) * Added joboffer edit view * Changed joboffer's choices to their long version * Fixed permission issue for analytics button on company detail view * Removed company's owner field * Implemented company association when creating and the related pemissions * Fixed formatting issues Co-authored-by: Juan Manuel Schillaci <[email protected]> Co-authored-by: Ian Puchetti <[email protected]>
1 parent 58356ca commit f2d2639

File tree

6 files changed

+134
-26
lines changed

6 files changed

+134
-26
lines changed

jobs/tests/test_views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def setUp(self):
1717

1818
def test_jobs_view_list(self):
1919
job = JobFactory(owner=self.user)
20-
company = CompanyFactory(owner=self.user, rank=3)
20+
company = CompanyFactory(rank=3)
2121
sponsored_job = JobFactory(owner=self.user, company=company)
2222
sponsored_job2 = JobFactory(owner=self.user, company=company)
2323

@@ -42,11 +42,11 @@ def test_jobs_view_list_with_tags(self):
4242
self.assertEqual(len(response.context["job_list"]), 1)
4343

4444
def test_jobs_view_list_regular_and_sponsored(self):
45-
sponsored_company = CompanyFactory(name='Name', owner=self.user, rank=3)
45+
sponsored_company = CompanyFactory(name='Name', rank=3)
4646
sponsored_job = JobFactory(owner=self.user, company=sponsored_company)
4747
sponsored_job_2 = JobFactory(owner=self.user, company=sponsored_company)
4848

49-
company = CompanyFactory(name='Other name', owner=self.user, rank=0)
49+
company = CompanyFactory(name='Other name', rank=0)
5050
job = JobFactory(owner=self.user, company=company)
5151
job_2 = JobFactory(owner=self.user, company=company)
5252

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 3.2.13 on 2022-07-07 21:22
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('pycompanies', '0003_usercompanyprofile'),
10+
]
11+
12+
operations = [
13+
migrations.RemoveField(
14+
model_name='company',
15+
name='owner',
16+
),
17+
]

pycompanies/models.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010

1111
class Company(TimeStampedModel):
1212
"""A PyAr Company that use Python."""
13-
14-
owner = models.ForeignKey(settings.AUTH_USER_MODEL,
15-
related_name='companies',
16-
on_delete=models.CASCADE)
1713
name = models.CharField('Nombre', max_length=255, unique=True)
1814
description = models.TextField('Descripción')
1915
photo = models.ImageField('Logo', upload_to='pycompanies/logos')

pycompanies/tests/factories.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from factory import Sequence, SubFactory
1+
from factory import Faker, Sequence, SubFactory
22
from factory.django import DjangoModelFactory, ImageField
33

44
from events.tests.factories import UserFactory
@@ -10,8 +10,9 @@ class Meta:
1010
model = Company
1111

1212
name = Sequence(lambda n: f'company-{n}')
13-
owner = SubFactory(UserFactory)
1413
photo = ImageField(color='blue')
14+
description = Faker('text')
15+
link = Faker('url')
1516
rank = 1
1617

1718

pycompanies/tests/test_views.py

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import factory
12
import pytest
23

34
from unittest.mock import patch
45

56
from django.contrib.messages import get_messages as contrib_get_messages
67
from django.urls import reverse
78

9+
from pycompanies.models import Company, UserCompanyProfile
810
from pycompanies.views import get_user_display_name
911
from pycompanies.tests.factories import CompanyFactory, UserCompanyProfileFactory, UserFactory
1012
from joboffers.tests.fixtures import create_admin_client, create_publisher_client # noqa
@@ -16,8 +18,9 @@
1618
ERROR_USER_DOES_NOT_EXIST = 'Le usuarie que ingresaste no existe.'
1719
USER_ASSOCIATED_CORRECTLY = 'Le usuarie fue asociade correctamente.'
1820

19-
ADMIN_URL = reverse('companies:admin')
20-
COMPANY_LIST_URL = reverse('companies:company_list_all')
21+
ADMIN_URL = 'companies:admin'
22+
LIST_URL = 'companies:company_list_all'
23+
CREATE_URL = 'companies:add'
2124

2225

2326
def get_plain_messages(request):
@@ -39,8 +42,10 @@ def test_associate_nonexistent_user(logged_client):
3942
response = logged_client.post(ASSOCIATE_URL, data={'username': 'pepito'})
4043
message = get_plain_messages(response)[0]
4144

45+
admin_url = reverse(ADMIN_URL)
46+
4247
assert 302 == response.status_code
43-
assert ADMIN_URL == response.url
48+
assert admin_url == response.url
4449
assert ERROR_USER_DOES_NOT_EXIST == message
4550

4651

@@ -56,8 +61,10 @@ def test_associate_user_in_company(logged_client, user):
5661
response = logged_client.post(ASSOCIATE_URL, data={'username': user.username})
5762
message = get_plain_messages(response)[0]
5863

64+
admin_url = reverse(ADMIN_URL)
65+
5966
assert 302 == response.status_code
60-
assert ADMIN_URL == response.url
67+
assert admin_url == response.url
6168
assert USER_ASSOCIATED_CORRECTLY == message
6269

6370

@@ -78,7 +85,7 @@ def test_associate_user_already_in_company(logged_client, user):
7885
message = get_plain_messages(response)[0]
7986

8087
assert 302 == response.status_code
81-
assert ADMIN_URL == response.url
88+
assert reverse(ADMIN_URL) == response.url
8289
assert ERROR_USER_ALREADY_IN_COMPANY == message
8390

8491

@@ -99,7 +106,7 @@ def test_associate_user_in_other_company(logged_client, user):
99106
message = get_plain_messages(response)[0]
100107

101108
assert 302 == response.status_code
102-
assert ADMIN_URL == response.url
109+
assert reverse(ADMIN_URL) == response.url
103110
assert message == ERROR_USER_IN_OTHER_COMPANY
104111

105112

@@ -108,7 +115,7 @@ def test_company_admin_with_no_logged_user_should_redirect(client):
108115
"""
109116
Should redirect if the user is not logged
110117
"""
111-
response = client.get(ADMIN_URL)
118+
response = client.get(reverse(ADMIN_URL))
112119

113120
assert 302 == response.status_code
114121

@@ -118,7 +125,7 @@ def test_company_admin_with_no_company_logged_user_should_redirect(logged_client
118125
"""
119126
Should redirect if the user is logged but not associated to a company
120127
"""
121-
response = logged_client.get(ADMIN_URL)
128+
response = logged_client.get(reverse(ADMIN_URL))
122129

123130
assert 302 == response.status_code
124131

@@ -131,7 +138,7 @@ def test_company_admin_with_company_logged_user_should_not_redirect(logged_clien
131138
company = CompanyFactory.create(name='company')
132139
UserCompanyProfileFactory.create(company=company, user=user)
133140

134-
response = logged_client.get(ADMIN_URL)
141+
response = logged_client.get(reverse(ADMIN_URL))
135142

136143
assert 200 == response.status_code
137144

@@ -227,13 +234,17 @@ def test_company_disassociate_one_user_from_company(logged_client, user):
227234

228235

229236
@pytest.mark.django_db
230-
def test_company_detail_doesnt_show_analytics_button_for_normal_user(logged_client):
237+
def test_company_detail_doesnt_show_analytics_button_for_normal_user(user, logged_client):
231238
"""
232239
Test that the company page doesn't show the analytics button for authenticated users that
233240
doesn't belong to the current company
234241
"""
242+
logged_user = user
235243
client = logged_client
236244
company = CompanyFactory.create(name='company_1')
245+
UserCompanyProfileFactory.create(company=company)
246+
# Associate the logged user to another company to cover this also that case
247+
UserCompanyProfileFactory.create(user=logged_user)
237248

238249
target_url = reverse('companies:detail', kwargs={'pk': company.id})
239250

@@ -338,6 +349,75 @@ def test_render_company_analytics_ok(
338349
assert table_views == expected_table_views
339350

340351

352+
@pytest.mark.django_db
353+
def test_company_create_view_GET_doesnt_allow_creation_of_company_for_logged_user_with_company(
354+
user, logged_client
355+
):
356+
"""
357+
Test that the company create GET doesn't allow creation of multiple companies for GET
358+
"""
359+
client = logged_client
360+
UserCompanyProfileFactory.create(user=user)
361+
362+
response = client.get(reverse(CREATE_URL))
363+
364+
assert response.status_code == 403
365+
366+
367+
@pytest.mark.django_db
368+
def test_company_create_view_POST_doesnt_allow_creation_of_company_for_logged_user_with_company(
369+
user, logged_client
370+
):
371+
"""
372+
Test that the company create POST doesn't allow creation of multiple companies
373+
"""
374+
client = logged_client
375+
UserCompanyProfileFactory.create(user=user)
376+
377+
response = client.post(reverse(CREATE_URL))
378+
379+
assert response.status_code == 403
380+
381+
382+
@pytest.mark.django_db
383+
def test_company_list_view_includes_own_company_for_logged_user_with_company(
384+
user, logged_client
385+
):
386+
"""
387+
Test that the company list view does not includes own_company for user with company created
388+
(Doesn't allow creation of multiple companies)
389+
"""
390+
client = logged_client
391+
UserCompanyProfileFactory.create(user=user)
392+
393+
target_url = reverse(LIST_URL)
394+
395+
response = client.get(target_url)
396+
assert 'own_company' in response.context_data
397+
398+
399+
@pytest.mark.django_db
400+
def test_create_company_associates_the_user_to_a_company(user, logged_client):
401+
"""
402+
Test that company creation associates the logged user to that company
403+
"""
404+
client = logged_client
405+
target_url = reverse(CREATE_URL)
406+
407+
company_data = factory.build(
408+
dict,
409+
FACTORY_CLASS=CompanyFactory
410+
)
411+
412+
assert Company.objects.count() == 0
413+
414+
response = client.post(target_url, company_data, format="multipart")
415+
416+
assert response.status_code == 302
417+
assert Company.objects.count() == 1
418+
assert UserCompanyProfile.objects.for_user(user=user)
419+
420+
341421
def test_get_user_display_name_without_first_name_and_last_name():
342422
"""
343423
Test return for an user without first_name and last_name
@@ -385,7 +465,7 @@ def test_company_list_view_includes_user_and_own_company_for_publisher(publisher
385465
"""
386466
client = publisher_client
387467

388-
response = client.get(COMPANY_LIST_URL)
468+
response = client.get(reverse(LIST_URL))
389469

390470
assert response.context_data['user'].is_authenticated
391471
assert 'own_company' in response.context_data
@@ -399,7 +479,7 @@ def test_company_list_view_includes_user_and_own_company_for_user_without_compan
399479
"""
400480
client = logged_client
401481

402-
response = client.get(COMPANY_LIST_URL)
482+
response = client.get(reverse(LIST_URL))
403483

404484
assert response.context_data['user'].is_authenticated
405485
assert 'own_company' not in response.context_data
@@ -411,7 +491,7 @@ def test_company_list_view_includes_user_and_own_company_for_unlogged_user(clien
411491
Test that the company list view includes user and own_company for anonymous user
412492
"""
413493

414-
response = client.get(COMPANY_LIST_URL)
494+
response = client.get(reverse(LIST_URL))
415495

416496
assert response.context_data['user'].is_anonymous
417497
assert 'own_company' not in response.context_data

pycompanies/views.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ def get_context_data(self, **kwargs):
3030
context = super().get_context_data(**kwargs)
3131

3232
user = self.request.user
33+
company = self.object
3334

34-
if UserCompanyProfile.objects.for_user(user=user) or user.is_superuser:
35+
if UserCompanyProfile.objects.for_user(user=user, company=company) or user.is_superuser:
3536
context['can_view_analytics'] = True
3637
else:
3738
context['can_view_analytics'] = False
@@ -52,7 +53,7 @@ def get_context_data(self, **kwargs):
5253
context['user'] = self.request.user
5354

5455
user_company = UserCompanyProfile.objects.for_user(user=self.request.user)
55-
if self.request.user.is_anonymous is False and user_company:
56+
if user_company:
5657
context['own_company'] = user_company.company
5758
return context
5859

@@ -63,9 +64,22 @@ class CompanyCreateView(LoginRequiredMixin, CreateView):
6364
success_url = '/empresas/'
6465
template_name = 'companies/company_form.html'
6566

67+
def dispatch(self, request, *args, **kwargs):
68+
user = self.request.user
69+
company_profile = UserCompanyProfile.objects.for_user(user=user)
70+
71+
if company_profile:
72+
raise PermissionDenied
73+
74+
return super().dispatch(request, *args, **kwargs)
75+
6676
def form_valid(self, form):
67-
form.instance.owner = self.request.user
68-
return super().form_valid(form)
77+
response = super().form_valid(form)
78+
company = form.instance
79+
user = self.request.user
80+
UserCompanyProfile.objects.create(user=user, company=company)
81+
82+
return response
6983

7084

7185
class CompanyUpdateView(LoginRequiredMixin, OwnedObject, UpdateView):

0 commit comments

Comments
 (0)