
    Q8ii                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ dd	lmZ dd
lmZ d Z G d de      Zy)z$API tests for transaction endpoints.    )annotations)	timedelta)Decimal)reverse)timezone)status)	APIClientAPITestCase)Transaction)Userc                8    t        | t              r	d| v r| d   S | S )z1Handle paginated and non-paginated DRF responses.results)
isinstancedict)payloads    S/home/cursorai/projects/telegram-earn/backend/apps/transactions/tests/test_views.pyget_resultsr      s#    '4 Y'%9y!!N    c                  >    e Zd Zd
dZd Zd Zd Zd Zd Zd Z	d Z
y	)TransactionApiTestsc                    t         j                  j                  ddd      | _        t	               | _        | j
                  j                  | j                         y )Ni)#  TxUsertxusertelegram_user_id
first_nameusername)user)r   objectscreate_userr   r	   clientforce_authenticate)selfs    r   setUpzTransactionApiTests.setUp   sJ    LL,,! - 
	
  k&&DII&6r   c                    t               }|j                  t        d            }| j                  |j                  t
        j                         y)z&Unauthenticated requests are rejected.transaction-listN)r	   getr   assertEqualstatus_coder   HTTP_401_UNAUTHORIZED)r#   r!   responses      r   test_requires_authenticationz0TransactionApiTests.test_requires_authentication"   s9    ::g&89:--v/K/KLr   c           
        t         j                  j                  ddd      }t        j                  j	                  | j
                  t        j                  t        d      t        j                  t        d      t        d            }t        j                  j	                  |t        j                  t        d      t        j                  t        d	      t        d
             | j                  j                  t        d            }| j                  |j                  t        j                          t#        |j$                        }| j                  t'        |      d       | j                  |d   d   |j(                         y)z4Authenticated user only sees their own transactions.i*#  Otherotherr   
5.000000000r   transaction_typeamountr   balance_beforebalance_after
2.00000000z10.00000000z
8.00000000r&      r   idN)r   r   r    r   creater   TYPE_REWARDr   STATUS_COMPLETEDTYPE_WITHDRAWALr!   r'   r   r(   r)   r   HTTP_200_OKr   datalenr9   )r#   
other_usertx_userr+   r   s        r   )test_list_is_scoped_to_authenticated_userz=TransactionApiTests.test_list_is_scoped_to_authenticated_user(   s,   \\--!g . 

 %%,,(44<(//"3<!,/ - 
 	""(88<(//"=1!,/ 	# 	
 ;;??7+=#>?--v/A/ABhmm,Wq)D)7::6r   c           
        t         j                  j                  | j                  t         j                  t        d      t         j                  t        d      t        d             t         j                  j                  | j                  t         j                  t        d      t         j                  t        d      t        d            }t        d      }| j                  j                  |dt         j                  i      }| j                  |j                  t        j                         t        |j                         }| j                  t#        |      d       | j                  |d	   d
   |j$                         y)z9GET /transactions supports filtering by transaction type.
3.00000000r1   r2   
1.00000000z
4.00000000r&   typer8   r   r9   N)r   r   r:   r   TYPE_COMMISSIONr   r<   r;   r   r!   r'   r(   r)   r   r>   r   r?   r@   r9   )r#   	reward_txurlr+   r   s        r   test_filter_by_transaction_typez3TransactionApiTests.test_filter_by_transaction_typeE   s    ""(88<(//"3<!,/ 	# 	
  ''..(44<(//"<0!,/ / 
	 ();;??31H1H(IJ--v/A/ABhmm,Wq)D)9<<8r   c           
        t         j                  j                  | j                  t         j                  t        d      t         j                  t        d      t        d             t        d      }| j                  j                  |ddi      }| j                  |j                  t        j                         t        |j                        }| j                  t!        |      d       y)	z;Unknown type filter should not error and returns empty set.rF   r1   r2   r&   rG   nonexistentr   N)r   r   r:   r   r;   r   r<   r   r!   r'   r(   r)   r   r>   r   r?   r@   )r#   rJ   r+   r   s       r   +test_filter_unknown_type_returns_empty_listz?TransactionApiTests.test_filter_unknown_type_returns_empty_list`   s    ""(44<(//"3<!,/ 	# 	
 ();;??3(?@--v/A/ABhmm,Wq)r   c           
        t         j                  j                  | j                  t         j                  t        d      t         j                  t        d      t        d            }t         j                  j                  |j                        j                  t        j                         t        d      z
         t         j                  j                  | j                  t         j                  t        d      t         j                  t        d      t        d	            }| j                  j                  t!        d
            }| j#                  |j$                  t&        j(                         t+        |j,                        }| j#                  |D cg c]  }|d   	 c}|j                  |j                  g       yc c}w )z-Most recent transactions should appear first.rF   r1   r2   )r9   r8   )days)
created_atr7   z-1.00000000r&   r9   N)r   r   r:   r   r;   r   r<   filterr9   updater   nowr   r=   r!   r'   r   r(   r)   r   r>   r   r?   )r#   oldernewerr+   r   txs         r   )test_list_orders_descending_by_created_atz=TransactionApiTests.test_list_orders_descending_by_created_atr   sR   ##**(44<(//"3<!,/ + 
 	""ehh"/66||~	q(99 	7 	
 ##**(88<(//"<0!-0 + 
 ;;??7+=#>?--v/A/ABhmm,W5Wr"T(W5%((7KL5s   Gc           
        t         j                  j                  ddd      }t        j                  j	                  |t        j
                  t        d      t        j                  t        d      t        d            }t        d|j                  g	      }| j                  j                  |      }| j                  |j                  t        j                         y
)z5User cannot access another user's transaction detail.i'  r.   	otheruserr   r0   r1   r2   ztransaction-detail)argsN)r   r   r    r   r:   TYPE_DEPOSITr   r<   r   r9   r!   r'   r(   r)   r   HTTP_404_NOT_FOUND)r#   rA   other_txrJ   r+   s        r   1test_retrieve_other_users_transaction_returns_404zETransactionApiTests.test_retrieve_other_users_transaction_returns_404   s    \\--!g . 

 &&--(55<(//"3<!,/ . 
 *(++?;;??3'--v/H/HIr   c                j   t         j                  j                  | j                  t         j                  t        d      t         j                  t        d      t        d      ddddd	      }| j                  j                  t        d	            }| j                  |j                  t        j                         t        |j                        d
   }| j                  |d   |j                          | j                  |d   d       | j                  |d   d       | j                  |d   d   d       y)z6Serializer should include blockchain/hash/data fields.rE   r1   tron0xabczhttps://explorer/0xabctest)explorer_linknote)	r   r3   r4   r   r5   r6   
blockchaintransaction_hashr?   r&   r   r9   rf   rg   r?   re   N)r   r   r:   r   r\   r   STATUS_PENDINGr!   r'   r   r(   r)   r   r>   r   r?   r9   )r#   rW   r+   results       r   test_returns_metadata_fieldsz0TransactionApiTests.test_returns_metadata_fields   s      ''(55<(--"3<!,/$#;VL ( 

 ;;??7+=#>?--v/A/ABX]]+A.ruu--v6 23W=/8r   N)returnNone)__name__
__module____qualname__r$   r,   rC   rK   rN   rX   r_   rj    r   r   r   r      s-    7M7:96*$M8J&9r   r   N)__doc__
__future__r   datetimer   decimalr   django.urlsr   django.utilsr   rest_frameworkr   rest_framework.testr	   r
   apps.transactions.modelsr   apps.users.modelsr   r   r   rp   r   r   <module>r{      s6    * "    ! ! 6 0 "^9+ ^9r   