|
| 1 | +import business.models |
| 2 | +import business.tests.auth.base |
| 3 | +import parameterized |
| 4 | +import rest_framework.status |
| 5 | +import rest_framework.test |
| 6 | + |
| 7 | + |
| 8 | +class InvalidCompanyRegistrationTestCase( |
| 9 | + business.tests.auth.base.BaseBusinessAuthTestCase, |
| 10 | +): |
| 11 | + def test_duplicate_email_registration(self): |
| 12 | + business.models.Company.objects.create_company( |
| 13 | + name=self.valid_data['name'], |
| 14 | + email=self.valid_data['email'], |
| 15 | + password=self.valid_data['password'], |
| 16 | + ) |
| 17 | + self.assertTrue( |
| 18 | + business.models.Company.objects.filter( |
| 19 | + email=self.valid_data['email'], |
| 20 | + ).exists(), |
| 21 | + ) |
| 22 | + |
| 23 | + response = self.client.post( |
| 24 | + self.signup_url, |
| 25 | + self.valid_data, |
| 26 | + format='json', |
| 27 | + ) |
| 28 | + self.assertEqual( |
| 29 | + response.status_code, |
| 30 | + rest_framework.status.HTTP_409_CONFLICT, |
| 31 | + ) |
| 32 | + |
| 33 | + @parameterized.parameterized.expand( |
| 34 | + [ |
| 35 | + ('short_password_1', 'easypwd'), |
| 36 | + ('short_password_2', 'Ar1@!$'), |
| 37 | + ('no_digits', 'PasswordWithoutDigits'), |
| 38 | + ('no_special_chars', 'PasswordWithoutSpecial1'), |
| 39 | + ('common_phrase', 'whereismymoney777'), |
| 40 | + ('missing_uppercase', 'lowercase123$'), |
| 41 | + ('missing_lowercase', 'UPPERCASE123$'), |
| 42 | + ('non_ascii', 'Päss123$!AAd'), |
| 43 | + ('emoji', '😎werY!!*Dj3sd'), |
| 44 | + ], |
| 45 | + ) |
| 46 | + def test_invalid_password_cases(self, _, invalid_password): |
| 47 | + test_data = {**self.valid_data, 'password': invalid_password} |
| 48 | + response = self.client.post(self.signup_url, test_data, format='json') |
| 49 | + self.assertEqual( |
| 50 | + response.status_code, |
| 51 | + rest_framework.status.HTTP_400_BAD_REQUEST, |
| 52 | + f'Failed for password: {invalid_password}', |
| 53 | + ) |
| 54 | + |
| 55 | + @parameterized.parameterized.expand( |
| 56 | + [ |
| 57 | + ('domain_missing_dot', 'a@b'), |
| 58 | + ('domain_missing_dot', 'test@dom'), |
| 59 | + ('missing_local_part', '@domain.com'), |
| 60 | + ('missing_at_symbol', 'missing.at.sign'), |
| 61 | + ('multiple_at_symbols', 'double@@at.com'), |
| 62 | + ], |
| 63 | + ) |
| 64 | + def test_invalid_email_cases(self, _, invalid_email): |
| 65 | + test_data = {**self.valid_data, 'email': invalid_email} |
| 66 | + response = self.client.post(self.signup_url, test_data, format='json') |
| 67 | + self.assertEqual( |
| 68 | + response.status_code, |
| 69 | + rest_framework.status.HTTP_400_BAD_REQUEST, |
| 70 | + f'Failed for email: {invalid_email}', |
| 71 | + ) |
| 72 | + |
| 73 | + def test_short_company_name(self): |
| 74 | + test_data = {**self.valid_data, 'name': 'A'} |
| 75 | + response = self.client.post(self.signup_url, test_data, format='json') |
| 76 | + self.assertEqual( |
| 77 | + response.status_code, |
| 78 | + rest_framework.status.HTTP_400_BAD_REQUEST, |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +class InvalidCompanyAuthenticationTestCase( |
| 83 | + business.tests.auth.base.BaseBusinessAuthTestCase, |
| 84 | +): |
| 85 | + @parameterized.parameterized.expand( |
| 86 | + [ |
| 87 | + ('missing_password', {'email': 'valid@example.com'}, 'password'), |
| 88 | + ('missing_email', {'password': 'any'}, 'email'), |
| 89 | + ('empty_data', {}, ['email', 'password']), |
| 90 | + ], |
| 91 | + ) |
| 92 | + def test_missing_required_fields(self, case_name, data, expected_fields): |
| 93 | + response = self.client.post(self.signin_url, data, format='json') |
| 94 | + self.assertEqual( |
| 95 | + response.status_code, |
| 96 | + rest_framework.status.HTTP_400_BAD_REQUEST, |
| 97 | + ) |
| 98 | + |
| 99 | + def test_signin_invalid_password(self): |
| 100 | + business.models.Company.objects.create_company( |
| 101 | + email=self.valid_data['email'], |
| 102 | + name=self.valid_data['name'], |
| 103 | + password=self.valid_data['password'], |
| 104 | + ) |
| 105 | + |
| 106 | + data = { |
| 107 | + 'email': self.valid_data['email'], |
| 108 | + 'password': 'SuperInvalidPassword2000!', |
| 109 | + } |
| 110 | + response = self.client.post( |
| 111 | + self.signin_url, |
| 112 | + data, |
| 113 | + format='json', |
| 114 | + ) |
| 115 | + self.assertEqual( |
| 116 | + response.status_code, |
| 117 | + rest_framework.status.HTTP_401_UNAUTHORIZED, |
| 118 | + ) |
0 commit comments