class TestInvoice(TestCase): def setUp(self): super(TestInvoice, self).setUp() self.base = Invoice(auth_key=config.demo_auth_key) def test_create(self): client = self.base.add(client_id=1, due_date="12/30/2016", fee_bearer="client", items=[{ "item": "Website Design", "description": "5 Pages Website plus 1 Year Web Hosting", "unit_cost": "50000.00", "quantity": "1" }])
def buy_tokens(request, ballot_url): ballot = get_object_or_404(BallotPaper, ballot_url=ballot_url) user = ballot.created_by if request.method != 'POST': form = InvoiceForm() else: form = InvoiceForm(request.POST) if form.is_valid(): cl_form = form.cleaned_data if cl_form['phone']: profile = Profile.objects.filter(user=request.user).update(phone=cl_form['phone']) try: PurchaseInvoice.objects.get( Q(ballot_paper=ballot), Q(ballot_paper__has_free_tokens=True) | Q(ballot_paper__has_paid_tokens=True) ) except (PurchaseInvoice.DoesNotExist): p_invoice = PurchaseInvoice.objects.create( user=request.user, ballot_paper=ballot, date_created=datetime.datetime.now(), due_date=datetime.datetime.now().date() + datetime.timedelta(days=2) ) p_invoice.save() token_item = Item.objects.create( invoice=p_invoice, item='Voter Token', description='Voter Tokens for %s.' % (ballot.ballot_name), unit_cost=25.00, quantity=cl_form['quantity'] ) token_item.save() token_item_dict = make_dict(token_item) item_list=[token_item_dict] try: auth_payant(key, 'demo') except (ConnectionError): p_invoice.delete() context3 = { 'form': form, 'ballot': ballot, 'conn_error': 'Network connection error, try again.' } return render(request, 'transactions/buy_tokens.html', context3) else: pay_invoice = Invoice(key) if request.user.profile.payant_id: new_invoice = pay_invoice.add( client_id=(request.user.profile.payant_id), due_date=p_invoice.due_date.strftime('%m/%d/%Y'), fee_bearer='account', items=item_list ) else: new_client = { "first_name": request.user.first_name, "last_name": request.user.last_name, "email": request.user.email, "phone": request.user.profile.phone } new_invoice = pay_invoice.add( new=True, client=new_client, due_date=p_invoice.due_date.strftime('%m/%d/%Y'), fee_bearer='account', items=item_list ) p_invoice.reference_code = new_invoice[2]['reference_code'] p_invoice.status = new_invoice[2]['status'] p_invoice.save() if not request.user.profile.payant_id: request.user.profile.payant_id = new_invoice[2]['client_id'] request.user.profile.save() return HttpResponseRedirect(reverse( 'trxns:get_invoice', args=[p_invoice.reference_code]) ) else: context = {'form': form, 'ballot': ballot, 'cant_buy': 'Sorry, you cannot buy more tokens on this ballot' } return render(request, 'transactions/buy_tokens.html', context) context = {'form': form, 'ballot': ballot} return render(request, 'transactions/buy_tokens.html', context)
def setUp(self): super().setUp() self.base = Invoice(auth_key=config.demo_auth_key)
class TestInvoiceMock(BaseCallTestCase): def setUp(self): super().setUp() self.base = Invoice(auth_key=config.demo_auth_key) def test_invoice_was_successfully_created(self): data = { 'due_date': '1483056000', 'company_id': '16', 'payment_id': None, 'client': config.test_user, 'updated_at': '2017-01-07 09:54:46', 'id': '37', 'fee_bearer': 'client', 'deleted_at': None, 'reference_code': 'EPC8ImHqnFYNdjz4AOsp', 'items': [] } self.mock_post.return_value = self.mock_response( { 'data': data, 'message': 'Invoice created successfully.', 'status': 'success' }, overwrite=True, status_code=200) client = self.base.add(client_id=1, due_date="12/30/2016", fee_bearer="client", items=[{ "item": "Website Design", "description": "5 Pages Website plus 1 Year Web Hosting", "unit_cost": "50000.00", "quantity": "1" }]) self.assertEqual(client[0], 200) self.assertEqual(client[1], 'success') self.assertEqual(client[2], data) def test_invoice_creation_with_error_message(self): self.mock_post.return_value = self.mock_response( { 'message': "Client not found", "status": "error", }, overwrite=True, status_code=404) client = self.base.add(client_id=1, due_date="12/30/2016", fee_bearer="client", items=[{ "item": "Website Design", "description": "5 Pages Website plus 1 Year Web Hosting", "unit_cost": "50000.00", "quantity": "1" }]) self.assertEqual(client[0], 404) self.assertEqual(client[1], 'error') self.assertEqual(client[2], 'Client not found')