def test_no_votes_returns_correct_numbers(self): ''' Test the no_votes property returns the correct number of votes. ''' test_ticket_bug = Ticket(user=self.test_user, title='Bug Votes', content='Test content') test_ticket_bug.save() test_ticket_feature = Ticket(user=self.test_user, title='Feature Votes', content='Test content') test_ticket_feature.save() for number in range(3): voter = User.objects.create_user( username='******'.format(number), email='test{}@test.com'.format(number), password='******') bug_vote = Vote(user=voter, ticket=test_ticket_bug) bug_vote.save() feature_vote = Vote(user=voter, ticket=test_ticket_feature, count=3) feature_vote.save() ticket_zero_votes = Ticket.objects.get(id=self.bug_ticket.id) self.assertEqual(0, ticket_zero_votes.no_votes) ticket_three_votes = Ticket.objects.get(id=test_ticket_bug.id) self.assertEqual(3, ticket_three_votes.no_votes) ticket_nine_votes = Ticket.objects.get(id=test_ticket_feature.id) self.assertEqual(9, ticket_nine_votes.no_votes)
def test_no_comments_returns_total_comments(self): ''' Test the no_comments property returns a count of all comments on an Ticket. ''' test_ticket_comments = Ticket(user=self.test_user, title='Test Comments', content='Test content') test_ticket_comments.save() test_ticket_wo_comments = Ticket(user=self.test_user, title='Test Comments', content='Test content') test_ticket_wo_comments.save() comment1 = Comment(user=self.test_user, ticket=test_ticket_comments, content='Test comment 1') comment1.save() comment2 = Comment(user=self.test_user, ticket=test_ticket_comments, content='Test comment 2') comment2.save() reply1 = Comment(user=self.test_user, ticket=test_ticket_comments, reply_to=comment2, content='Test comment 3') reply1.save() self.assertEqual(0, test_ticket_wo_comments.no_comments) self.assertEqual(3, test_ticket_comments.no_comments)
def test_vote_for_feature_no_credits_returns_failiure_and_message(self): ''' Test that voting for a feature with an empty of non-existent wallet returns success as False, and an appropriate message. ''' test_ticket = Ticket(user=self.test_user, title='Voting', ticket_type='Feature', content='Test content') test_ticket.save() voter = User.objects.create_user(username='******', email='*****@*****.**', password='******') result = test_ticket.vote(voter, 5) self.assertFalse(result['success']) self.assertIn('insufficient credits', result['message'].lower()) wallet = Wallet(user=voter) wallet.save() result = test_ticket.vote(voter, 5) self.assertFalse(result['success']) self.assertIn('insufficient credits', result['message'].lower())
def test_status_returns_most_recent_status(self): ''' Test the status property returns the most advanced status that has been logged. ''' test_ticket = Ticket(user=self.test_user, title='Status', content='Test content') test_ticket.save() test_ticket = Ticket.objects.get(id=test_ticket.id) self.assertEqual('awaiting approval', test_ticket.status) test_ticket.approved = timezone.now() test_ticket.save() test_ticket = Ticket.objects.get(id=test_ticket.id) self.assertEqual('approved', test_ticket.status) test_ticket.doing = timezone.now() test_ticket.save() test_ticket = Ticket.objects.get(id=test_ticket.id) self.assertEqual('doing', test_ticket.status) test_ticket.done = timezone.now() test_ticket.save() test_ticket = Ticket.objects.get(id=test_ticket.id) self.assertEqual('done', test_ticket.status)
def setUpTestData(cls): cls.test_user = User.objects.create_user(username='******', email='*****@*****.**', password='******') cls.test_ticket = Ticket(user=cls.test_user, title='Test title', content='Test content') cls.test_ticket.save() cls.comment1 = Comment(user=cls.test_user, ticket=cls.test_ticket, content='Test comment 1') cls.comment1.save() cls.comment2 = Comment(user=cls.test_user, ticket=cls.test_ticket, content='Test comment 2') cls.comment2.save() cls.reply1 = Comment(user=cls.test_user, ticket=cls.test_ticket, reply_to=cls.comment2, content='Test comment 3') cls.reply1.save()
def test_order_txn(self): order = Order(full_name='Test', phone_number='012345', country='gb', postcode='ab123') order.save() user = User(email='*****@*****.**', first_name='Test', last_name='Case') user.save() ticket = Ticket(requester=user, title='Title') ticket.save() transaction = OrderTransaction(order=order, ticket=ticket, cost=2.2) transaction.save() print('TestCheckoutModels: OrderTransaction - test_order_txn \n \ Expected: {0}, Actual: {1} \n \ Expected: {2}, Actual: {3} \n \ Expected: {4}, Actual: {5} \n \ Expected: {6}, Actual: {7} \n \ Expected: {8}, Actual: {9}'.format( 'Test', transaction.order.full_name, '012345', transaction.order.phone_number, user, transaction.ticket.requester, 'Title', transaction.ticket.title, '2.2', transaction.cost)) self.assertEqual(transaction.order.full_name, 'Test') self.assertEqual(transaction.order.phone_number, '012345') self.assertEqual(transaction.ticket.requester, user) self.assertEqual(transaction.ticket.title, 'Title') self.assertEqual(transaction.cost, 2.2)
def test_can_create_read_update_ticket(self): # create a ticket record ticket = Ticket(**self.ticket) ticket.save() self.assertIsNotNone(ticket.id, None) # test that a ticket record has been added ticket = Ticket.objects.get(id=ticket.id) self.assertIsNotNone(ticket.id) # update a ticket record new_ticket_id = "898273499823hiuh32898w" ticket = Ticket.objects.get(id=ticket.id) ticket.ticket_id = new_ticket_id ticket.save() # update deal title for next test self.ticket['ticket_id'] = new_ticket_id self.assertEquals(ticket.ticket_id, new_ticket_id) # delete a ticket record ticket = Ticket.objects.get(id=ticket.id) Ticket.delete(ticket) with self.assertRaises(Ticket.DoesNotExist) as context: Ticket.objects.get(**self.ticket) self.assertTrue("does not exist" in context.exception.message)
def test_bar_charts(self): """ Tests popularity charts data """ ticket0 = Ticket(variety='B', issue='ticket0', verified = True, date_verified=datetime.date.today(), date_start_dev=datetime.date.today()) ticket0.save() ticket1 = Ticket.objects.create( variety = "F", issue = "ticket1", verified = True, upvotes=10 ) ticket2 = Ticket.objects.create( variety='B', issue='ticket2', upvotes=5 ) ticket3 = Ticket.objects.create( variety='F', issue='ticket3', upvotes=80 ) f_chart = FPopularityBarChart() b_chart = BPopularityBarChart() assert(f_chart.get_data()=={'ticket1': 10, 'ticket3': 80}) assert(b_chart.get_data()=={'ticket2': 5, 'ticket0': 0})
def test_ticket_time(self): #Test if a new ticket_time object can be created user = User.objects.create_user(username='******', first_name='test', last_name='tester', password='******', email='*****@*****.**') user.save() ticket = Ticket(title='test', author=user, username=user.username, description='test', ticket_type=2, status=1, comment_num=0, upvotes=0) ticket.save() time = Ticket_Time(ticket=ticket, match_ticket_id=ticket.id) time.save() self.assertEqual(time.ticket, ticket) self.assertEqual(time.match_ticket_id, ticket.id)
def commit_data(request, order_form, cart, cart_upvotes): ''' Save the order, features and feature upvotes in the backend database ''' order = order_form.save(commit=False) order.save() for item in cart: ticket = Ticket(title=item['title'], type='Feature', description=item['description'], requester=request.user) ticket.save() order_transaction = OrderTransaction(order=order, ticket=ticket, cost=item['feature_cost']) order_transaction.save() for item in cart_upvotes: ticket = Ticket.objects.get(pk=item['id']) if ticket.upvotes is None: ticket.upvotes = 0 ticket.upvotes += 1 ticket.save() upvoter = TicketUpvoter(upvoter_ticket=ticket, upvoter_user=request.user) upvoter.save()
def test_ActivityChartdata(self): """ Tests updates chart data """ ticket0 = Ticket(variety='B', issue='ticket0', verified = True, date_verified=datetime.date.today(), date_start_dev=datetime.date.today()) ticket0.save() ticket1 = Ticket.objects.create( variety = "F", issue = "ticket1", verified = True, date_verified=datetime.date.today(), date_start_dev=datetime.date.today(), date_done=datetime.date.today() ) ticket2 = Ticket.objects.create( variety='B', issue='ticket2', verified = True, date_verified=datetime.date.today() ) chart = ActivityLineChart() assert(chart.get_data() == {'ticket0': [datetime.date.today(), datetime.date.today(), None], 'ticket2': [datetime.date.today(), None, None], 'ticket1': [datetime.date.today(), datetime.date.today(), datetime.date.today()] })
def test_updating_cart(self): ticket = Ticket(variety='F', issue='some extra feature') ticket.save() cart = Cart(self.request) cart.add(ticket) cart.add(ticket, donation=15, update=True) self.assertEqual(len(cart), 1) self.assertEqual(cart.get_total(), 15)
def entries(self): user=User.objects.get(id=8) flight=Flight.objects.get(flight_id=3) for i in range(1,10): unique_id = get_random_string(length=10) p = Ticket(buyer=user, passenger = unique_id, flightID=flight, travelClass='EC', buyPrLine=True, buyVIP = False, buyFood = 'EN', buyWaitRoom = False) p.save()
def test_user_detail_page_does_not_contain_ticket_when_user_is_not_submitter_or_developer( self): ticket3 = Ticket(title='title3', description='d1') # now we save and check that ticket1 is part of response: ticket3.save() self.response = self.client.get(self.url) self.assertNotContains(self.response, 'title3')
def test_create_donation(self): user = User(email='*****@*****.**') user.save() ticket = Ticket(title='Test', description='My description') ticket.save() donation = Donation(ticket=ticket, user=user, amount=100) donation.save() self.assertEqual(donation.amount, 100)
def test_removing_from_cart(self): ticket = Ticket(variety='F', issue='some extra feature') ticket.save() cart = Cart(self.request) cart.add(ticket) cart.remove(ticket) self.assertEqual(len(cart), 0) self.assertEqual(cart.get_total(), 0)
def test_view_add(self): ticket = Ticket(variety='F', issue='some feature') ticket.save() response = self.client.get( reverse('cart:add_to_cart', args=(ticket.id, ))) # redirecting self.assertEqual(response.status_code, 302) self.assertEqual( dict(response.items())['Location'], '/cart/cart_detail')
def create_user(email: str, password: str, avatar_url: str) -> User: user = User(email=email, password=password, avatar_url=avatar_url) user.full_clean() user.resize_avatar() user.save() ticket = Ticket(user=user) send_ticket_to_guest_checkin(ticket) return user
def setUpTestData(cls): cls.test_user = User.objects.create_user(username='******', email='*****@*****.**', password='******') cls.bug_ticket = Ticket(user=cls.test_user, title='Test Bug', ticket_type='Bug', content='Test content') cls.bug_ticket.save() cls.bug_ticket.set_status('approved') cls.feature_ticket = Ticket(user=cls.test_user, title='Test Feature', ticket_type='Feature', content='Test content') cls.feature_ticket.save() cls.feature_ticket.set_status('approved')
def ticket_purchase(request, id_article): article = Article.objects.select_related('event').get(pk=id_article) assert article.is_active(), "Este tipo de entrada no está ya disponible." event = article.event if request.method == 'POST': email = request.POST['stripeEmail'] name = request.POST['name'] surname = request.POST['surname'] phone = request.POST.get('phone', None) token = request.POST['stripeToken'] stripe.api_key = settings.STRIPE_SECRET_KEY try: customer = stripe.Customer.create( email=email, source=token, description='{}, {}'.format(surname, name), ) charge = stripe.Charge.create(customer=customer.id, amount=article.price_in_cents, currency='EUR', description='{}/{}, {}'.format( event.hashtag, surname, name, )) if charge.paid: ticket = Ticket( article=article, customer_name=name, customer_surname=surname, customer_email=email, customer_phone=phone, payment_id=charge.id, ) ticket.save() send_ticket.delay(ticket) return redirect(links.article_bought(article.pk)) else: return stripe_payment_declined(request, charge) except stripe.error.StripeError as err: logger.error('Error de stripe') logger.error(str(err)) return stripe_payment_error(request, err) except Exception as err: logger.error('Error en el pago por stripe') logger.error(str(err)) messages.add_message(request, messages.ERROR, 'Hello world.') from django.http import HttpResponse return HttpResponse("Something goes wrong\n{}".format(err)) else: return render( request, 'events/buy-article.html', { 'event': event, 'article': article, 'stripe_public_key': settings.STRIPE_PUBLIC_KEY, })
def _create_ticket_item(self, ticket_id, ticket_request, price): ticket = Ticket( id=ticket_id, email=ticket_request.email, movieDate_id=ticket_request.movieDateId, nrOfSeats=ticket_request.nrOfSeats, status=TICKET_STATUS_CREATED, ticketRequestData=JsonSerializer().serialize(ticket_request), price=price) ticket.save()
def buy_tickets(self, quantity, session_obj): # update profile.total_cost self.total_cost += (quantity * session_obj.price) self.save() # update session.available_seats session_obj.available_seats -= quantity session_obj.update() # create ticket objects for q in range(quantity): ticket = Ticket(buyer=self.user, session=session_obj) ticket.save()
def test_set_status_returns_false_when_not_set(self): ''' Test that the set_status method returns false when it hasn't updated the status. ''' test_ticket = Ticket(user=self.test_user, title='Fail To Set Status', content='Test content') test_ticket.save() test_ticket.set_status('doing') self.assertFalse(test_ticket.set_status('approved'))
def test_cart_detail_view_content(self): """ Tests if order total is rendered as expected, and if Checkout button is visible """ ticket = Ticket(variety='F', issue='some feature') ticket.save() response = self.client.get( reverse('cart:add_to_cart', args=(ticket.id, ))) response = self.client.get(reverse('cart:cart_detail')) self.assertIn('Your Order Total is', str(response.content)) self.assertIn('Checkout', str(response.content))
def test_search_(self): user = User.objects.create_user(username='******', password='******') self.client.login(username='******', password='******') ticket = Ticket(ticketName="Test Ticket", description='test-content', author=user) ticket.save() self.client.post page = self.client.get("/search/?query=unicorn", follow=True) self.assertEqual(page.status_code, 200) self.assertTemplateUsed(page, "search_results.html")
def test_get_request_and_pay_anonymously(self): """ Automated test for what happens if user is authenticated """ stripe.api_key = settings.STRIPE_SECRET data = { 'full_name': "test_from_profile", 'phone_number': "test_from_profile", 'country': "test_from_profile", 'postcode': "test_from_profile", 'town_or_city': "test_from_profile", 'street_address1': "test_from_profile", 'county': "test_from_profile" } token = stripe.Token.create( card={ 'number': '4242424242424242', 'exp_month': '6', 'exp_year': str(datetime.today().year + 1), 'cvc': '123', }) ticket = Ticket(variety='F', issue='some feature') ticket.save() self.client.get(reverse('cart:add_to_cart', args=(ticket.id, ))) session = self.client.session response = self.client.post(reverse('checkout:order_create'), data=data) order = get_object_or_404(Order, pk=1) self.assertFalse(order.paid) token = {'stripeToken': token.id} ticket_not_upvoted = get_object_or_404(Ticket, pk=1) # still 0 upvotes self.assertEqual(str(ticket_not_upvoted), 'Ticket #1, type: F, to do, 0 upvotes') # success, transaction visible on my stripe account response = self.client.post(reverse('checkout:order_pay', args=(order.id, )), data=token) ticket_upvoted = get_object_or_404(Ticket, pk=1) order = get_object_or_404(Order, pk=1) self.assertTrue(order.paid) order_item = get_object_or_404(OrderItem, pk=1) self.assertNotEqual(str(ticket_upvoted), 'Ticket #1, type: F, to do, 0 upvotes') self.assertEqual(str(ticket_upvoted), 'Ticket #1, type: F, to do, 1 upvotes') self.assertEqual(str(order), 'Order #1, for 1 item(s), total cost of 5.00.') self.assertEqual(str(order_item), "order id is 1, ticket's id is 1 - some feature")
def test_simple_rendering(self): """ This automated test is checking page rendering """ ticket = Ticket(variety='F', issue='some feature') ticket.save() self.client.get(reverse('cart:add_to_cart', args=(ticket.id, ))) session = self.client.session response = self.client.get(reverse('checkout:order_create')) self.assertEqual(response.status_code, 200) self.assertTemplateUsed('create_oder.html') self.assertIn('Payment Details', str(response.content)) self.assertIn('Back to cart', str(response.content))
def test_user_detail_page_contains_ticket_when_user_is_submitter(self): ticket2 = Ticket( title='title2', description='d1', developer=self.user, ) # ticket is not saved, so currently user is not developerr of ticket self.assertNotContains(self.response, 'title2') # now we save and check that ticket1 is part of response: ticket2.save() self.response = self.client.get(self.url) self.assertContains(self.response, 'title2')
def createTicket(request): if request.method == "POST": title = request.POST.get('title') content = request.POST.get('content') author = request.POST.get('author') ticket_save = Ticket(title=title, content=content, author=author) ticket_save.save() print("ticket created") last_saved_ticket = Ticket.objects.last() messages.success( request, f'Ticket: {last_saved_ticket.tno} has been created successfully!') return redirect(f'/tickets/{last_saved_ticket.tno}')
def get(self, request, *args, **kwargs): """ Handles get request to the 'download_ticket' named route Returns: A PDF response containing the ticket """ user = request.user deal_id = kwargs['deal_id'] deal = Deal.objects.get(pk=deal_id) quantity = int(request.GET.get('qty')) price = quantity * deal.price qr_img, unique_id = self.generate_unique_code(deal, user) logo_url = deal.advertiser.logo_image_url() # add ticket to database ticket = Ticket( user=user, item=deal, quantity=quantity, advertiser=deal.advertiser, ticket_id=unique_id ) ticket.save() context = { 'deal': deal, 'logo_url': logo_url, 'qr_img': qr_img, 'issue_date': ticket.date_created, 'quantity': quantity, 'price': price, 'user': user } html_template = get_template('tickets/ticket.html') rendered_html = html_template.render( RequestContext(request, context)).encode(encoding="UTF-8") styles = [ CSS(string='.well-print { background-color: grey !important }'), CSS( settings.STATIC_ROOT + '/bootstrap/dist/css/bootstrap.min.css' ), CSS(settings.STATIC_ROOT + '/css/base_styles.css'), ] pdf_file = HTML( string=rendered_html, base_url=request.build_absolute_uri() ).write_pdf(stylesheets=styles) http_response = HttpResponse(pdf_file, content_type='application/pdf') http_response['Content-Disposition'] = ('attachment; ' 'filename=%s') % deal.slug return http_response