def test_404_wrong_paycheck(self): '''404 Given if requested paycheck doesn't belong to user''' PaycheckFactory(user=self.user) user2 = create_another_user() paycheck2 = PaycheckFactory(user=user2) request_url = reverse('paychecks:paycheck', kwargs={"paycheck_id": paycheck2.id}) response = self.client.get(request_url) self.assertEqual(response.status_code, 404)
def test_single_paycheck(self): proto_paycheck = PaycheckFactory() assert (reverse("paychecks:paycheck", kwargs={"paycheck_id": proto_paycheck.id }) == f"/paychecks/{proto_paycheck.id}") assert resolve(f"/paychecks/{proto_paycheck.id}" ).view_name == "paychecks:paycheck"
def test_edit_paycheck(self): proto_paycheck = PaycheckFactory() assert (reverse("paychecks:paycheck_edit", kwargs={"pk": proto_paycheck.id }) == f"/paychecks/{proto_paycheck.id}/edit") assert resolve(f"/paychecks/{proto_paycheck.id}/edit" ).view_name == "paychecks:paycheck_edit"
def test_paycheck_redirect(self): paycheck = PaycheckFactory(user=self.user) request_url = reverse('paychecks:paychecks') request = self.factory.get(request_url, {'paycheck_id': paycheck.id}) request.user = self.user response = index(request) self.assertContains(response, "", status_code=302)
def test_404_on_not_users_paycheck(self): user2 = create_another_user() paycheck = PaycheckFactory(user=user2) request_url = reverse('paychecks:deduction_add') data = {'paycheck': paycheck.id} response = self.client.post(request_url, data) self.assertEqual(response.status_code, 404)
def test_404_on_unauthorized_paycheck(self): user2 = create_another_user() paycheck2 = PaycheckFactory(user=user2) request_url = reverse('paychecks:paystub_add', kwargs={'paycheck_id': paycheck2.id}) response = self.client.get(request_url) self.assertEqual(response.status_code, 404)
def test_add_paystub(self): proto_paycheck = PaycheckFactory() assert (reverse("paychecks:paystub_add", kwargs={ "paycheck_id": proto_paycheck.id }) == f"/paychecks/paystub/{proto_paycheck.id}/add") assert resolve(f"/paychecks/paystub/{proto_paycheck.id}/add" ).view_name == "paychecks:paystub_add"
def test_paystub_rendering(self): paycheck = PaycheckFactory(user=self.user) paystub = PaystubFactory(paycheck=paycheck, user=self.user) create_flex_account(self.user) request_url = reverse('paychecks:paycheck', kwargs={"paycheck_id": paycheck.id}) response = self.client.get(request_url) self.assertEqual(response.status_code, 200)
def setUp(self): self.factory = RequestFactory() self.user = UserFactory() self.password = '******' self.user.set_password(self.password) self.user.save() self.client.login(username=self.user.username, password=self.password) self.paycheck = PaycheckFactory(user=self.user) self.paystub = PaystubFactory(paycheck=self.paycheck, user=self.user)
def test_error_deleting_other_user_deduction(self): user2 = create_another_user() paycheck = PaycheckFactory(user=user2) deduction = DeductionFactory(paycheck=paycheck, user=user2) request_url = reverse('paychecks:deduction_delete', kwargs={"pk": deduction.id}) response = self.client.post(request_url, {}, follow=True) self.assertEqual(response.status_code, 403)
def test_error_deleting_other_user_paystub(self): '''Cannot delete another users paystub''' user2 = create_another_user() paycheck = PaycheckFactory(user=user2) paystub = PaystubFactory(paycheck=paycheck, user=user2) request_url = reverse('paychecks:paystub_delete', kwargs={"pk": paystub.id}) response = self.client.post(request_url, follow=True) self.assertEqual(response.status_code, 403)
def test_redirect_first_paycheck(self): '''Redirects to users first paycheck if general page viewed''' paycheck = PaycheckFactory(user=self.user) request_url = reverse('paychecks:paychecks') request = self.factory.get(request_url) request.user = self.user response = response_attach_client(index(request), self.user.username, self.password) self.assertRedirects( response, reverse('paychecks:paycheck', kwargs={"paycheck_id": paycheck.id})) # add another paycheck and make sure first one is still correct PaycheckFactory(user=self.user) response = response_attach_client(index(request), self.user.username, self.password) self.assertRedirects( response, reverse('paychecks:paycheck', kwargs={"paycheck_id": paycheck.id}))
def test_clean_paycheck(self): proto_paycheck = PaycheckFactory.build() proto_user = UserFactory.create() form = PaycheckForm({ "company": proto_paycheck.company, "annual_salary": proto_paycheck.annual_salary, "paychecks_per_year": proto_paycheck.paychecks_per_year, "user": proto_user.id, }) assert form.is_valid() form.save()
def test_update_paycheck(self): paycheck = PaycheckFactory(user=self.user) new_salary = Decimal( Faker("random_int", min=1000, max=200000).generate()).quantize(Decimal('.01')) data = { 'company': paycheck.company, 'annual_salary': new_salary, 'paychecks_per_year': paycheck.paychecks_per_year, 'user': self.user.id, 'active': paycheck.active, } request_url = reverse('paychecks:paycheck_edit', args=(paycheck.id, )) response = self.client.post(request_url, data, follow=True) self.assertRedirects(response, reverse('paychecks:paycheck', args=(paycheck.id, )), status_code=302) paycheck.refresh_from_db() self.assertEqual(paycheck.annual_salary, new_salary)
def test_clean_deduction(self): proto_paycheck = PaycheckFactory.create() proto_user = UserFactory.create() proto_deduction = DeductionFactory.build() form = DeductionForm({ "paycheck": proto_paycheck.id, "description": proto_deduction.description, "deduction_type": proto_deduction.deduction_type, "amount": proto_deduction.amount, "user": proto_user.id, }) assert form.is_valid() form.save()
def test_one_paycheck(self): '''Responses given for one paycheck asked for''' first_paycheck = PaycheckFactory(user=self.user) # create random number of paychecks user_paychecks = PaycheckFactory.create_batch(randint(1, 10), user=self.user) user_paychecks.append(first_paycheck) user2 = create_another_user() paycheck_user2 = PaycheckFactory(user=user2) # proper response request_url = reverse('paychecks:paycheck', kwargs={"paycheck_id": first_paycheck.id}) response = self.client.get(request_url) self.assertEqual(response.status_code, 200) # check context self.assertIsInstance(response.context['paycheck'], Paycheck) self.assertTemplateUsed(response, 'paychecks/paychecks.html') # make sure other users paychecks not in response self.assertEqual(len(response.context['paychecks']), len(user_paychecks))
def test_deductions(self): paycheck = PaycheckFactory(user=self.user) paycheck_gross = str( round(paycheck.annual_salary / paycheck.paychecks_per_year, 2)) deductions = DeductionFactory.create_batch(randint(1, 10), user=self.user, paycheck=paycheck) deduction_total = sum([o.amount for o in deductions]) take_home_pay = Decimal(paycheck_gross) - deduction_total request_url = reverse('paychecks:paycheck', kwargs={"paycheck_id": paycheck.id}) response = self.client.get(request_url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context['deductions']), len(deductions)) self.assertEqual(response.context['deduction_total'], deduction_total) self.assertEqual(response.context['take_home_pay'], take_home_pay)
def test_create_paycheck(self): request_url = reverse('paychecks:paycheck_create') paycheck = PaycheckFactory.build(user=self.user) data = { 'company': paycheck.company, 'annual_salary': paycheck.annual_salary, 'paychecks_per_year': paycheck.paychecks_per_year, 'user': self.user.id, 'active': paycheck.active, } response = self.client.post(request_url, data, follow=True) new_paycheck = Paycheck.objects.last() self.assertEqual(repr(new_paycheck), repr(paycheck)) self.assertRedirects(response, reverse('paychecks:paycheck', args=(new_paycheck.pk, )), status_code=302)
def test_clean_paystub(self): proto_paystub = PaystubFactory.build() proto_paycheck = PaycheckFactory.create() proto_user = UserFactory.create() form = PaystubForm({ "paycheck": proto_paycheck.id, "gross_pay": proto_paystub.gross_pay, "start_date": proto_paystub.start_date, "end_date": proto_paystub.end_date, }) assert form.is_valid() # user is still needed with self.assertRaises(IntegrityError): form.save() paystub = form.save(commit=False) paystub.user = proto_user paystub.save()
def test_string_representation(self): proto_paycheck = PaycheckFactory() self.assertEqual(str(proto_paycheck), proto_paycheck.company)