def test_company_user_auth_succeeds_for_valid_account(self): """Verify that a company rep user can log in if their company's subscription is not expired. """ # Create a company rep for the user, with a company that is active # (is_expired returns False) company = Company(name='Test Company', expiration_date=datetime.date.today()) company.save() with patch.object(Company, 'is_expired', return_value=False) as mock_is_expired: rep = CompanyRep(user=self.user, company=company) rep.save() # Ensure that the user can be authenticated form = AuthenticationForm(None, self.form_data) self.assertTrue(form.is_valid()) self.assertEquals(mock_is_expired.call_count, 1)
def save(self, *args, **kwargs): """Save a CompanyRep object along with the new user account.""" # Set password1 in the cleanded_data to None so that the user is given # an unusable password in the superclass save() method self.cleaned_data['password1'] = None rep_user = super(CompanyRepCreationForm, self).save(*args, **kwargs) rep_company = self.cleaned_data['company'] CompanyRep(user=rep_user, company=rep_company).save() return rep_user
def test_companyrep_delete_view(self): """Ensure that representatives can be deleted and that deleted reps have their accounts disabled. """ company = Company(expiration_date='3000-01-01') company.save() companyrep_user = self.user_model.objects.create_user( username='******', password='******') companyrep_user_pk = companyrep_user.pk companyrep = CompanyRep(company=company, user=companyrep_user) companyrep.save() self.assertTrue( self.client.login(username=self.user.username, password='******')) self.assertTrue(companyrep_user.is_active) self.assertTrue( self.client.login(username='******', password='******')) # Use the view self.assertTrue( self.client.login(username=self.user.username, password='******')) response = self.client.get(reverse('companies:rep-delete', args=(companyrep.pk, )), follow=True) self.assertContains(response, 'Are you sure') self.client.post(reverse('companies:rep-delete', args=(companyrep.pk, )), follow=True) # Check that everything has been deleted self.assertFalse(CompanyRep.objects.exists()) # Check that the rep can't log in companyrep_user = User.objects.get(pk=companyrep_user_pk) self.assertFalse(companyrep_user.is_active) self.assertFalse( self.client.login(username='******', password='******'))
def test_company_user_auth_fails_for_expired_account(self): """Verify that a company rep user cannot log in if their company's subscription is expired. """ # Create a company rep for the user, with a company that has an expired # subscription (is_expired returns True) company = Company(name='Test Company', expiration_date=datetime.date.today()) company.save() with patch.object(Company, 'is_expired', return_value=True) as mock_is_expired: rep = CompanyRep(user=self.user, company=company) rep.save() # Ensure that the user cannot be authenticated form = AuthenticationForm(None, self.form_data) self.assertFalse(form.is_valid()) expected_error_msg = ( '{}\'s subscription to this website has expired'.format( company.name)) self.assertIn(expected_error_msg, form.non_field_errors()[0]) self.assertEquals(mock_is_expired.call_count, 1)