def test_로그인한_사용자가_본인_장바구니에_있는_상품옵션을_주문하려고_할_때_중복_요청으로_이미_프로세스가_진행중인_경우_423_에러(self): order_count_query = Order.objects.filter(user=self.me) order_product_count_query = OrderProduct.objects.filter(user=self.me) payment_count_query = Payment.objects.filter(order__user=self.me) order_count_before_order_create = order_count_query.count() order_product_count_before_order_create = order_product_count_query.count() payment_count_before_order_create = payment_count_query.count() cart_ids_to_order = [ self.me_cart_option1.id, self.me_cart_option2.id, self.me_cart_option3.id, self.me_cart_option4.id ] lock_key = ORDER_CREATE_LOCK_USER_(self.me.id) has_obtained_redis_lock(lock_key, 5) self.client.force_authenticate(user=self.me) response = self.client.post( path=self.api_url, content_type='application/json', data=json.dumps({ 'cart_ids': cart_ids_to_order, 'shipping_address': '서울시 동작구 아무곳이나', 'shipping_request_note': '경비실에 맡겨주세요', 'pay_method': 'not_allowed_pay_method', }) ) release_redis_lock(lock_key) self.assertEqual(response.status_code, 423) self.assertEqual(response.json(), '처리중입니다.') self.assertEqual(order_count_query.count(), order_count_before_order_create + 0) self.assertEqual(order_product_count_query.count(), order_product_count_before_order_create + 0) self.assertEqual(payment_count_query.count(), payment_count_before_order_create + 0)
def test_로그인한_사용자가_장바구니에_상품을_담을_때_다른_사용자의_레디스_락과는_상관없이_201_성공(self): count_query = Cart.objects.filter(user=self.normal_user) cart_count_before_cart_create = count_query.count() lock_key = CART_CREATE_LOCK_USER_('anybody_id') has_obtained_redis_lock(lock_key, 5) self.client.force_authenticate(user=self.normal_user) response = self.client.post( path=self.api_url, content_type='application/json', data=json.dumps({ 'product_option_id': self.product1_option.id, 'quantity': 1 }) ) release_redis_lock(lock_key) self.assertEqual(response.status_code, 201) self.assertEqual(count_query.count(), cart_count_before_cart_create + 1)
def test_로그인한_사용자가_장바구니에_상품을_담을_때_중복_요청으로_이미_프로세스가_진행중인_경우_423_에러(self): count_query = Cart.objects.filter(user=self.normal_user) cart_count_before_cart_create = count_query.count() lock_key = CART_CREATE_LOCK_USER_(self.normal_user.id) has_obtained_redis_lock(lock_key, 5) self.client.force_authenticate(user=self.normal_user) response = self.client.post( path=self.api_url, content_type='application/json', data=json.dumps({ 'product_option_id': self.product1_option.id, 'quantity': 1 }) ) release_redis_lock(lock_key) self.assertEqual(response.status_code, 423) self.assertEqual(count_query.count(), cart_count_before_cart_create + 0) self.assertEqual(response.json(), '처리중입니다.')
def create(self, request, *args, **kwargs): lock_key = ORDER_CREATE_LOCK_USER_(request.user.id) if has_obtained_redis_lock(lock_key, 3): try: response = super().create(request, *args, **kwargs) finally: release_redis_lock(lock_key) else: response = Response('처리중입니다.', status=status.HTTP_423_LOCKED) return response
def test_로그인한_사용자가_본인_장바구니에_있는_상품옵션을_주문할_때_다른_사용자_주문의_레디스_락과_상관없이_201_성공(self): order_count_query = Order.objects.filter(user=self.me) order_product_count_query = OrderProduct.objects.filter(user=self.me) payment_count_query = Payment.objects.filter(order__user=self.me) order_count_before_order_create = order_count_query.count() order_product_count_before_order_create = order_product_count_query.count() payment_count_before_order_create = payment_count_query.count() cart_ids_to_order = [ self.me_cart_option1.id, self.me_cart_option2.id, self.me_cart_option3.id, self.me_cart_option4.id ] lock_key = ORDER_CREATE_LOCK_USER_('any_user_id') has_obtained_redis_lock(lock_key, 5) self.client.force_authenticate(user=self.me) response = self.client.post( path=self.api_url, content_type='application/json', data=json.dumps({ 'cart_ids': cart_ids_to_order, 'shipping_address': '서울시 동작구 아무곳이나', 'shipping_request_note': '경비실에 맡겨주세요', 'pay_method': 'CARD', }) ) release_redis_lock(lock_key) self.assertEqual(response.status_code, 201) self.assertEqual(order_count_query.count(), order_count_before_order_create + 1) self.assertEqual( order_product_count_query.count(), order_product_count_before_order_create + len(cart_ids_to_order) ) self.assertEqual(payment_count_query.count(), payment_count_before_order_create + 1) self.assertEqual( set(response.json()), { 'id', 'order_uid', 'shipping_price', 'shipping_address', 'shipping_request_note', 'created_at', 'updated_at', 'is_paid', 'user', 'order_products', 'payment', } ) final_shipping_price = ( min( self.me_cart_option1.product_option.product.shipping_price, self.me_cart_option2.product_option.product.shipping_price ) + self.me_cart_option3.product_option.product.shipping_price + self.me_cart_option4.product_option.product.shipping_price ) self.assertEqual( response.json()['shipping_price'], final_shipping_price, ) self.assertEqual( response.json()['payment']['pay_price'], (self.me_cart_option1.quantity * self.me_cart_option1.product_option.product.price) + (self.me_cart_option2.quantity * self.me_cart_option2.product_option.product.price) + (self.me_cart_option3.quantity * self.me_cart_option3.product_option.product.price) + (self.me_cart_option4.quantity * self.me_cart_option4.product_option.product.price) + final_shipping_price )