1+ import factory
12import pytest
23
34from unittest .mock import patch
45
56from django .contrib .messages import get_messages as contrib_get_messages
67from django .urls import reverse
78
9+ from pycompanies .models import Company , UserCompanyProfile
810from pycompanies .views import get_user_display_name
911from pycompanies .tests .factories import CompanyFactory , UserCompanyProfileFactory , UserFactory
1012from joboffers .tests .fixtures import create_admin_client , create_publisher_client # noqa
1618ERROR_USER_DOES_NOT_EXIST = 'Le usuarie que ingresaste no existe.'
1719USER_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
2326def 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+
341421def 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
0 commit comments