def test_calculate_balance(self): available_calendar = CalendarFactory( activity__organizer=self.organizer) available_orders = OrderFactory.create_batch( size=3, calendar=available_calendar, status='approved', fee=0, amount=factory.Iterator([20000, 30000])) unavailable_calendar = CalendarFactory( activity__organizer=self.organizer) unavailable_orders = OrderFactory.create_batch( size=4, calendar=unavailable_calendar, status='approved', fee=0, amount=factory.Iterator([20000, 30000])) status = [*['available'] * 3, *['unavailable'] * 4] BalanceLogFactory.create_batch( size=7, order=factory.Iterator([*available_orders, *unavailable_orders]), organizer=self.organizer, status=factory.Iterator(status)) task = CalculateOrganizerBalanceTask() task.delay(organizer_ids=[self.organizer.id]) balance = Balance.objects.get(id=self.organizer.balance.id) self.assertEqual(balance.available, 70000) self.assertEqual(balance.unavailable, 100000)
class OrderModelTest(APITestCase): """ Class for test the model Order """ def setUp(self): # Arrangement self.student = StudentFactory() self.order = OrderFactory(amount=500, student=self.student) def test_get_total(self): """ Test method get_total without coupon """ self.assertEqual(self.order.get_total(student=self.student), self.order.amount) def test_get_total_with_coupon_not_used(self): """ Test method get_total with a coupon not used """ # Arrangement coupon_type = mommy.make(CouponType, name='referred', amount=100) redeem = mommy.make(Redeem, student=self.student, coupon__coupon_type=coupon_type) self.order.coupon = redeem.coupon self.order.save() self.assertEqual(self.order.get_total(student=self.student), self.order.amount) def test_get_total_with_coupon_used(self): """ Test method get_total with a coupon used """ # Arrangement coupon_type = mommy.make(CouponType, name='referred', amount=100) redeem = mommy.make(Redeem, student=self.student, coupon__coupon_type=coupon_type, used=True) self.order.coupon = redeem.coupon self.order.save() total = self.order.amount - redeem.coupon.coupon_type.amount self.assertEqual(self.order.get_total(student=self.student), total) def test_get_total_error(self): """ Test case when total is called without student """ with self.assertRaisesRegex(TypeError, 'get_total().*student'): self.order.get_total()
def test_having_an_free_activity(self, delay): """ Test case should create the coupon because the student has a free activity """ # Arrangement OrderFactory(student=self.another_student, calendar__is_free=True) # Counter coupon_counter = Coupon.objects.count() redeem_counter = Redeem.objects.count() # Call the task task = ReferrerCouponTask() task.delay(student_id=self.another_student.id, order_id=self.order.id) self.assertEqual(Coupon.objects.count(), coupon_counter + 1) self.assertTrue( Coupon.objects.filter(coupon_type=self.coupon_type).exists()) self.assertEqual(Redeem.objects.count(), redeem_counter + 1) self.assertTrue( Redeem.objects.filter( student=self.student, coupon__coupon_type=self.coupon_type).exists())
def test_send_pse_order_approved(self, send_mail): """ Test task send the email successfully when: - the order is approved - the payment method is pse """ payment = PaymentFactory(payment_type='PSE') order = OrderFactory(status=Order.ORDER_APPROVED_STATUS, payment=payment) email = order.calendar.activity.organizer.user.email assistants = AssistantFactory.create_batch(1, order=order) send_mail.return_value = [{ 'email': email, 'status': 'sent', 'reject_reason': None }] task = SendNewEnrollmentEmailTask() task_id = task.delay(order.id) context = { **self.get_context_data(order, assistants), 'status': 'Aprobada', 'payment_type': 'PSE', 'card_type': dict(Payment.CARD_TYPE)[payment.card_type], 'coupon_amount': None, } self.assertTrue(EmailTaskRecord.objects.filter( task_id=task_id, to=email, status='sent', data=context, template_name='payments/email/new_enrollment.html').exists())
def create_orders(self): self.stdout.write('Creando orders') orders = list() fee = self.create_fee() for calendar in self.calendars: size = self.get_quantity() quantity = size if calendar.available_capacity < 1: continue orders.append( OrderFactory.create_batch(size, calendar=calendar, student=factory.Iterator( Student.objects.all(), cycle=True), fee=fee, fee_detail={}, quantity=quantity)) for o in self.flat_list(orders): fee_detail = Order.get_fee_detail(o, False, o.payment, None) fee = fee_detail.get('total_fee') o.fee, o.fee_detail = o.fee, fee_detail o.save() return self.flat_list(orders)
def setUp(self): super(ListAndCreateOrganizerMessageViewTest, self).setUp() self.url = reverse('messages:list_and_create') self.calendar = CalendarFactory(activity__organizer=self.organizer) self.orders = OrderFactory.create_batch(3, calendar=self.calendar, status=Order.ORDER_APPROVED_STATUS) self.organizer_messages = OrganizerMessageFactory.create_batch(3, organizer=self.organizer, calendar=self.calendar) self.organizer_message_relation = OrganizerMessageStudentRelationFactory( organizer_message=self.organizer_messages[0], student=self.student, )
def test_balance_not_yet_available(self): calendar = CalendarFactory(activity__organizer=self.organizer, initial_date=now().date()) order = OrderFactory(calendar=calendar) balance_log = BalanceLogFactory(organizer=self.organizer, order=order) task = BalanceLogToAvailableTask() result = task.delay() self.assertEqual( BalanceLog.objects.get(id=balance_log.id).status, 'unavailable') self.assertEqual(result.result, [])
def test_get_list_orders_of_current_user_ok(self): order_objs = [ OrderFactory( user=self.authenticated_user, status=factories.FuzzyChoice( OrderStatusesEnum.values()).fuzz(), ) for _ in range(30) ] resp = self.get_json() self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(len(order_objs), resp.data['count'])
def test_balance_log_available(self): last_week = (now() - timedelta(days=5)).date() calendar = CalendarFactory(activity__organizer=self.organizer, initial_date=last_week) order = OrderFactory(calendar=calendar) balance_log = BalanceLogFactory(organizer=self.organizer, order=order) task = BalanceLogToAvailableTask() result = task.delay() self.assertEqual( BalanceLog.objects.get(id=balance_log.id).status, 'available') self.assertEqual(result.result, [self.organizer.id])
def setUp(self): self.activity = ActivityFactory() self.author = mommy.make(Student) self.order = OrderFactory(status=Order.ORDER_APPROVED_STATUS, calendar__activity=self.activity, calendar__initial_date=now() - timedelta(days=3), student=self.author) self.data = { 'rating': 5, 'activity': self.activity.id, } self.context = self.get_context()
def setUp(self): self.organizer = OrganizerFactory() self.calendar = CalendarFactory(activity__organizer=self.organizer) self.organizer_message = OrganizerMessageFactory( calendar=self.calendar) self.orders = OrderFactory.create_batch( size=3, calendar=self.calendar, status=Order.ORDER_APPROVED_STATUS) self.assistants = [] for order in self.orders: self.assistants.append( AssistantFactory(order=order, email=order.student.user.email, enrolled=True))
def test_put_cancel_an_order_ok(self): """ Test for API PUT /api/v1/orders/me/{order_id}/cancel/ """ # Create an order for test waiting_order_obj = OrderFactory( user=self.authenticated_user, status=OrderStatusesEnum.WAITING.value, ) # Cancel an order waiting_order_id = waiting_order_obj.id resp = self.put_json(data={}, fragment=f'{waiting_order_id}/cancel/') self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(resp.data.get('order_id'), waiting_order_id)
def test_send_mail_failed(self, send_mail): """ Test the task when the send_mail fails """ payment = PaymentFactory(payment_type='CC', card_type='visa') order = OrderFactory(status=Order.ORDER_APPROVED_STATUS, payment=payment) send_mail.side_effect = Exception('Connection failed') task = SendPaymentEmailTask() task_id = task.delay(order.id) self.assertTrue(EmailTaskRecord.objects.filter( task_id=task_id, to=order.student.user.email, status='error', reject_reason='Connection failed').exists())
def test_put_pay_an_order_ok(self): """ API PUT /api/v1/orders/me/{order_id}/payment/ """ can_pay_statuses = [ OrderStatusesEnum.IN_PROGRESS.value, OrderStatusesEnum.SHIPPING.value, ] for order_status in can_pay_statuses: order_obj = OrderFactory( user=self.authenticated_user, status=order_status, ) # Cancel an order order_id = order_obj.id resp = self.put_json(data={}, fragment=f'{order_id}/payment/') self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(resp.data.get('order_id'), order_id)
def test_send_pse_order_with_coupon_declined(self, send_mail): """ Test task send the email successfully when: - the order is declined - the payment method is pse - the order has a coupon """ student = StudentFactory() redeem = RedeemFactory(student=student, used=True) payment = PaymentFactory(payment_type='PSE') order = OrderFactory(status=Order.ORDER_DECLINED_STATUS, payment=payment, student=student, coupon=redeem.coupon) email = order.student.user.email assistants = AssistantFactory.create_batch(1, order=order) transaction_error = 'ERROR' send_mail.return_value = [{ 'email': email, 'status': 'sent', 'reject_reason': None }] task = SendPaymentEmailTask() task_id = task.delay(order.id, transaction_error=transaction_error) context = { **self.get_context_data(order, assistants), 'status': 'Declinada', 'payment_type': 'PSE', 'card_type': dict(Payment.CARD_TYPE)[payment.card_type], 'coupon_amount': redeem.coupon.coupon_type.amount, 'error': transaction_error, } self.assertTrue(EmailTaskRecord.objects.filter( task_id=task_id, to=email, status='sent', data=context, template_name='payments/email/payment_declined.html').exists())
def _seed_orders(cls) -> None: """ Seeding orders """ order_status = [ OrderStatusesEnum.SHIPPING.value, OrderStatusesEnum.WAITING.value, OrderStatusesEnum.CANCELED.value, OrderStatusesEnum.PAID.value, OrderStatusesEnum.IN_PROGRESS.value, ] for _ in range(50): order_obj = OrderFactory( user=FuzzyChoice(cls.users).fuzz(), status=FuzzyChoice(order_status).fuzz(), ) cls.orders.append(order_obj) for _ in range(FuzzyInteger(5, 10).fuzz()): OrderItemFactory(order=order_obj, product=FuzzyChoice(cls.products).fuzz())
def test_send_creditcard_with_coupon_order_approved(self, send_mail): """ Test task send the email successfully when: - the order is approved - the payment method is credit card - the order has a coupon """ student = StudentFactory() redeem = RedeemFactory(student=student, used=True) payment = PaymentFactory(payment_type='CC', card_type='visa') order = OrderFactory(status=Order.ORDER_APPROVED_STATUS, payment=payment, student=student, coupon=redeem.coupon) email = order.student.user.email assistants = AssistantFactory.create_batch(1, order=order) send_mail.return_value = [{ 'email': email, 'status': 'sent', 'reject_reason': None }] task = SendPaymentEmailTask() task_id = task.delay(order.id) context = { **self.get_context_data(order, assistants), 'status': 'Aprobada', 'payment_type': 'Crédito', 'card_type': 'VISA', 'coupon_amount': redeem.coupon.coupon_type.amount, } self.assertTrue(EmailTaskRecord.objects.filter( task_id=task_id, to=email, status='sent', data=context, template_name='payments/email/payment_approved.html').exists())
def test_put_cancel_an_order_bad_request(self): """ If an user is not `Waiting` status. We cannot cancel. API PUT /api/v1/orders/me/{order_id}/cancel/ """ statuses = OrderStatusesEnum.values() statuses.remove(OrderStatusesEnum.WAITING.value) # Test for all order status except WAITING for order_status in statuses: not_waiting_order_obj = OrderFactory( user=self.authenticated_user, status=order_status, ) # Cancel an order not_waiting_order_id = not_waiting_order_obj.id resp = self.put_json(data={}, fragment=f'{not_waiting_order_id}/cancel/') self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) self.has_valid_custom_error_response(resp)
def test_send_creditcard_order_without_coupon_declined(self, send_mail): """ Test task send the email successfully when: - the order is declined - the payment method is credit card - the order doesn't have a coupon """ payment = PaymentFactory(payment_type='CC', card_type='visa') order = OrderFactory(status=Order.ORDER_DECLINED_STATUS, payment=payment) email = order.student.user.email assistants = AssistantFactory.create_batch(1, order=order) transaction_error = 'ERROR' send_mail.return_value = [{ 'email': email, 'status': 'sent', 'reject_reason': None }] task = SendPaymentEmailTask() task_id = task.delay(order.id, transaction_error=transaction_error) context = { **self.get_context_data(order, assistants), 'status': 'Declinada', 'payment_type': 'Crédito', 'card_type': 'VISA', 'coupon_amount': None, 'error': transaction_error, } self.assertTrue(EmailTaskRecord.objects.filter( task_id=task_id, to=email, status='sent', data=context, template_name='payments/email/payment_declined.html').exists())
def test_put_pay_an_order_bad_request(self): """ API PUT /api/v1/orders/me/{order_id}/payment/ """ can_not_pay_statuses = [ OrderStatusesEnum.SHOPPING.value, OrderStatusesEnum.WAITING.value, OrderStatusesEnum.CANCELED.value, OrderStatusesEnum.PAID.value, ] for order_status in can_not_pay_statuses: # Create order order_obj = OrderFactory( user=self.authenticated_user, status=order_status, ) # Pay an order order_id = order_obj.id resp = self.put_json(data={}, fragment=f'{order_id}/payment/') self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) self.has_valid_custom_error_response(resp)
def test_with_an_payed_activity(self): """ Test case when shouldn't create a referrer coupon because the student already has a payed activity """ # Arrangement OrderFactory(student=self.another_student) # Counter coupon_counter = Coupon.objects.count() redeem_counter = Redeem.objects.count() # Call the task task = ReferrerCouponTask() task.delay(student_id=self.another_student.id, order_id=self.order.id) self.assertEqual(Coupon.objects.count(), coupon_counter) self.assertEqual(Redeem.objects.count(), redeem_counter) self.assertFalse( Redeem.objects.filter( student=self.student, coupon__coupon_type=self.coupon_type).exists())
def setUp(self): self.calendar = CalendarFactory() self.order = OrderFactory(calendar=self.calendar, status='approved')
def setUp(self): self.calendar = CalendarFactory() self.order = OrderFactory(calendar=self.calendar, status=Order.ORDER_APPROVED_STATUS) self.assistants = AssistantFactory.create_batch(2, order=self.order)
def setUp(self): self.payu_callback_url = reverse('payments:notification') self.payment = PaymentFactory() self.order = OrderFactory(payment=self.payment, status=Order.ORDER_PENDING_STATUS)
def setUp(self): self.factory = RequestFactory() self.order = OrderFactory()
def setUp(self): # Arrangement self.student = StudentFactory() self.order = OrderFactory(amount=500, student=self.student)
def setUp(self): # Calling the super (initialization) super(ReviewAPITest, self).setUp() # Objects needed self.activity = ActivityFactory(organizer=self.organizer, published=True) self.calendar = CalendarFactory( activity=self.activity, initial_date=(now() - timedelta(days=2)).date()) self.order = OrderFactory(student=self.student, calendar=self.calendar, status=Order.ORDER_APPROVED_STATUS) self.post = {'rating': 4, 'comment': 'First comment!'} self.read_review_post = {'rating': 4, 'comment': 'Im a read review!'} self.unread_review_post = { 'rating': 4, 'comment': 'Im an unread review!' } self.put = {'rating': 2, 'reply': 'Thank you!'} self.review = mommy.make(Review, author=self.student, activity=self.activity, **self.post) self.read_review = mommy.make(Review, author=self.student, activity=self.activity, read=True, **self.read_review_post) self.unread_review = mommy.make(Review, author=self.student, activity=self.activity, read=False, **self.unread_review_post) # URLs self.list_by_organizer_url = reverse( 'reviews:list_by_organizer', kwargs={'organizer_pk': self.organizer.id}) self.list_by_student_url = reverse( 'reviews:list_by_student', kwargs={'student_pk': self.student.id}) self.create_url = reverse('reviews:create', kwargs={'activity_pk': self.activity.id}) self.retrieve_update_delete_url = reverse( 'reviews:reply', kwargs={'pk': self.review.id}) self.report_url = reverse('reviews:report', kwargs={'pk': self.review.id}) self.read_url = reverse('reviews:read', kwargs={'pk': self.review.id}) # Counters self.review_count = Review.objects.count() self.activity_reviews = self.activity.reviews.count() # Set permissions add_review = Permission.objects.get_by_natural_key( 'add_review', 'reviews', 'review') add_review.user_set.add(self.student.user, self.another_student.user) change_review = Permission.objects.get_by_natural_key( 'change_review', 'reviews', 'review') change_review.user_set.add(self.organizer.user, self.another_organizer.user) assign_perm('reviews.reply_review', user_or_group=self.organizer.user, obj=self.review) assign_perm('reviews.report_review', user_or_group=self.organizer.user, obj=self.review) assign_perm('reviews.read_review', user_or_group=self.organizer.user, obj=self.review)
def setUp(self): self.order = OrderFactory(amount=500, status=Order.ORDER_APPROVED_STATUS) self.assistants = mommy.make(Assistant, order=self.order, _quantity=2)
def setUp(self): super(OrdersAPITest, self).setUp() # Create Activities objects self.activity = ActivityFactory(organizer=self.organizer, published=True) self.other_activity = ActivityFactory(organizer=self.organizer, published=True) self.active_activity = ActivityFactory(published=True) self.inactive_activity = ActivityFactory() # Create Calendards objects self.calendar = mommy.make(Calendar, activity=self.activity) self.other_calendar = mommy.make(Calendar, activity=self.other_activity) self.free_calendar = mommy.make(Calendar, is_free=True, activity=self.active_activity, available_capacity=10) self.inactive_calendar = mommy.make(Calendar, activity=self.inactive_activity) self.full_calendar = mommy.make(Calendar, activity=self.active_activity, available_capacity=0) self.closed_enroll_calendar = mommy.make(Calendar, activity=self.active_activity, enroll_open=False) # Create Orders objects OrderFactory.create_batch(student=self.student, size=2) OrderFactory.create_batch(calendar=self.calendar, size=2) OrderFactory.create_batch(calendar=self.other_calendar, size=2) self.order = OrderFactory(student=self.student) self.another_other = OrderFactory() # URLs self.orders_by_activity_url = reverse( 'orders:create_or_list_by_activity', kwargs={'activity_pk': self.activity.id}) self.orders_by_student_url = reverse( 'orders:list_by_student', kwargs={'student_pk': self.student.id}) self.orders_by_organizer_url = reverse( 'orders:list_by_organizer', kwargs={'organizer_pk': self.organizer.id}) self.order_retrieve_url = reverse('orders:retrieve', kwargs={'order_pk': self.order.id}) self.another_order_retrieve_url = reverse( 'orders:retrieve', kwargs={'order_pk': self.another_other.id}) self.create_order_url = reverse( 'orders:create_or_list_by_activity', kwargs={'activity_pk': self.active_activity.id}) self.create_inactive_activity_order_url = reverse( 'orders:create_or_list_by_activity', kwargs={'activity_pk': self.inactive_activity.id}) # Set permissions permission = Permission.objects.get_by_natural_key( 'add_order', 'orders', 'order') permission.user_set.add(self.student.user) # counts self.orders_count = Order.objects.all().count() self.student_orders_count = Order.objects.filter( student=self.student).count() self.activity_orders_count = Order.objects.filter( calendar=self.calendar).count() self.organizer_orders_count = Order.objects. \ filter(calendar__activity__organizer=self.organizer).count()