
    8i                        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mZ dd
lmZ ddlmZ d Z G d de      Zy)z,API tests for referral commission endpoints.    )annotations)	timedelta)Decimal)reverse)timezone)status)	APIClientAPITestCase)
InvestmentReward)
Commission)Userc                8    t        | t              r	d| v r| d   S | S )z1Handle paginated and non-paginated DRF responses.results)
isinstancedict)payloads    P/home/cursorai/projects/telegram-earn/backend/apps/referrals/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	)CommissionApiTestsc                6   t         j                  j                  ddd      | _        t         j                  j                  ddd| j                        | _        t         j                  j                  d	d
d      | _        t        j                  t        d            }t        j                         }t        j                  j                  | j                  t        d      |d   |d   |d   t        j                  ||t        |d         z         }t        j                  j                  |t        d      |j                               }t         j                  j                  | j                  | j                  |dt        d      t        d      t         j"                        | _        t         j                  j                  | j                  | j                  |dt        d      t        d      t         j&                        | _        t         j                  j                  | j
                  | j                  |dt        d      t        d      t         j"                         t+               | _        | j,                  j/                  | j                         y )NiA  Uplineupline)telegram_user_id
first_nameusernameiB  Downlinedownline)r   r   r   referred_byiC  Otherother100tierratedurationdays)useramountr%   daily_reward_rateduration_daysr   
start_dateend_datez
2.00000000)
investmentr+   reward_date   z50.00z
1.00000000upline_userdownline_userrewardlevelcommission_rater+   r      z30.00z
0.60000000   20.00z
0.40000000r*   )r   objectscreate_userr   r    other_upliner   get_tier_infor   r   nowcreateSTATUS_ACTIVEr   r   dater   STATUS_PENDINGpending_commissionSTATUS_DISTRIBUTEDdistributed_commissionr	   clientforce_authenticate)self	tier_inforA   r0   r6   s        r   setUpzCommissionApiTests.setUp   s2   ll..! / 

 00!!	 1 
 !LL44! 5 
 ,,WU^<	lln''..5>6"'/#J/++9)J*?@@ / 	

 &&!<(
 ' 
 #-"4"4";";--#G,<(,, #< #
 '1&8&8&?&?--#G,<(00 '@ '
# 	!!))--#G,<(,, 	" 	
  k&&DKK&8r   c                    t               }|j                  t        d            }| j                  |j                  t
        j                         y)z&Unauthenticated requests are rejected.commission-listN)r	   getr   assertEqualstatus_coder   HTTP_401_UNAUTHORIZED)rK   rI   responses      r   test_requires_authenticationz/CommissionApiTests.test_requires_authentication]   s9    ::g&789--v/K/KLr   c                   | j                   j                  t        d            }| j                  |j                  t
        j                         t        |j                        }| j                  t        |      d       |D ch c]  }|d   	 }}| j                  || j                  j                  | j                  j                  h       yc c}w )z5Authenticated user only sees commissions they earned.rO   r9   idN)rI   rP   r   rQ   rR   r   HTTP_200_OKr   datalenassertSetEqualrF   rW   rH   rK   rT   r   itemreturned_idss        r   *test_commissions_are_scoped_to_upline_userz=CommissionApiTests.test_commissions_are_scoped_to_upline_userc   s    ;;??7+<#=>--v/A/ABhmm,Wq)/67wtT
w7$$'')D)D)G)GH	
 8s   Cc                ~   t        d      }| j                  j                  |dt        j                  i      }| j                  |j                  t        j                         t        |j                        }| j                  t        |      d       | j                  |d   d   | j                  j                         y)z.GET /commissions supports filtering by status.rO   r   r2   r   rW   N)r   rI   rP   r   rG   rQ   rR   r   rX   r   rY   rZ   rH   rW   )rK   urlrT   r   s       r   test_filter_by_statusz(CommissionApiTests.test_filter_by_statusp   s    '(;;??3:3P3P(QR--v/A/ABhmm,Wq)D)4+F+F+I+IJr   c                   | j                   j                  t        d      ddi      }| j                  |j                  t
        j                         | j                  t        t        |j                              d       y)z<Unknown status filter yields an empty list instead of error.rO   r   zdoes-not-existr   N)
rI   rP   r   rQ   rR   r   rX   rZ   r   rY   rK   rT   s     r   -test_filter_unknown_status_returns_empty_listz@CommissionApiTests.test_filter_unknown_status_returns_empty_listz   sa    ;;??%&3C(D
 	--v/A/AB[78!<r   c           
        t         j                  j                  | j                  | j                  | j
                  j                  dt        d      t        d      t         j                        }t         j                  j                  |j                        j                  t        j                         t        d      z
         | j                  j!                  t#        d	            }| j%                  |j&                  t(        j*                         t-        |j.                        }|D cg c]  }|d
   	 }}| j%                  |dd | j0                  j                  | j
                  j                  g       yc c}w )z/Default ordering is newest first by created_at.r:   r;   z
0.20000000r3   rW   r2   r(   
created_atrO   rW   Nr9   )r   r=   rB   r   r    rF   r6   r   rE   filterrW   updater   rA   r   rI   rP   r   rQ   rR   r   rX   r   rY   rH   )rK   older_commissionrT   r   r]   r^   s         r   0test_orders_commissions_descending_by_created_atzCCommissionApiTests.test_orders_commissions_descending_by_created_at   s4   %--44--**11#G,<(,, 5 
 	!!%5%8%8!9@@||~	q(99 	A 	
 ;;??7+<#=>--v/A/ABhmm,/67wtT
w7!((++T-D-D-G-GH	
 8s   /E=c                B   t         j                  j                  | j                  j                        j                  t        j                         t        d      z
         | j                  j                  t        d      ddi      }| j                  |j                  t        j                         t!        |j"                        }|D cg c]  }|d   	 }}| j                  |d	d
 | j                  j                  | j$                  j                  g       y	c c}w )z=Ordering filter supports ascending created_at when requested.rg   r2   r(   rh   rO   orderingri   rW   Nr9   )r   r=   rj   rF   rW   rk   r   rA   r   rI   rP   r   rQ   rR   r   rX   r   rY   rH   r\   s        r   0test_orders_commissions_ascending_when_requestedzCCommissionApiTests.test_orders_commissions_ascending_when_requested   s    !!T%<%<%?%?!@GG||~	q(99 	H 	
 ;;??%&\(B
 	--v/A/ABhmm,/67wtT
w7!$$'')D)D)G)GH	
 8s   Dc                   | j                   j                  | j                         | j                   j                  t	        d| j
                  j                  g            }| j                  |j                  t        j                         y)z.Detail view is scoped to authenticated upline.r<   zcommission-detail)argsN)rI   rJ   r?   rP   r   rF   rW   rQ   rR   r   HTTP_404_NOT_FOUNDrd   s     r   2test_retrieve_other_uplines_commission_returns_404zECommissionApiTests.test_retrieve_other_uplines_commission_returns_404   sg    &&D,=,=&>;;??'t/F/F/I/I.JK
 	--v/H/HIr   N)returnNone)__name__
__module____qualname__rM   rU   r_   rb   re   rm   rp   rt    r   r   r   r      s/    A9FM
K=
2
$Jr   r   N)__doc__
__future__r   datetimer   decimalr   django.urlsr   django.utilsr   rest_frameworkr   rest_framework.testr	   r
   apps.investments.modelsr   r   apps.referrals.modelsr   apps.users.modelsr   r   r   rz   r   r   <module>r      s;    2 "    ! ! 6 6 , "\J \Jr   