
    $j                         d dl Zd dlmc mZ d dlmZmZ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mZ d dlmZ d d	lmZmZmZmZmZmZmZmZm Z m!Z! d d
l"m#Z#m$Z$ d Z%ddddZ&d Z'd Z(d Z)d Z*d Z+d Z,dS )    N)datetimetimezone	timedelta)Path)
TestClient)Sessionselect)get_settings)init_dbreset_db_for_tests)app)

AppSettingActivityEventExternalPostPlatformAccount	PostDraftProvider
VideoAssetVideoStatusWebsiteCompanionYouTubeUploadAudit)decrypt_tokenencrypt_tokenc                     t                      }d|_        | dz  |_        d |_        d |_        d|_        d|_        t          |          }t          |           |t          t                    fS )Nz	sqlite://storagez7http://testserver/api/accounts/youtube/connect/callbackF)r
   database_urlstorage_rootgoogle_client_idgoogle_client_secretgoogle_redirect_uriyoutube_upload_dry_runr   r   r   r   )tmp_pathsettingsengines      ^/home/agent/projects/AutoShorts_Dashboard/backend/tests/test_sprint8_youtube_private_upload.pysetup_test_dbr&      sl    ~~H'H$y0H $H$(H!#\H &+H#XFv:c??""    private privacysuffixc                   |dz  }|j                             dd           |                    d           t          |           5 }t	          d| d| dt          |          |                                j        d| d	t          j	        d
	  	        }t          |j        t          j        ddddg|ddddii          }t          t          j        d| d| dgt          d          t          d          t!          j        t$          j                  t)          d          z   dd	  	        }|                    |           |                    |           |                    |           |                                 |                    |           |                    |           |                    |           t1          |j        |j        ddd          }	|                    |	           |                                 |                    |	           |j        |j        |j        |	j        fcd d d            S # 1 swxY w Y   d S ) Nz	video.mp4T)parentsexist_oks   fake video byteszsprint8-video
package_s8prepared_packagesprint8checksumzSprint 8 Upload)	candidate_id
package_idsource	file_path	file_sizechecksumworking_titlestatusis_aigczSprint 8 Upload TitlezReviewed descriptioncybersecurityonlinesafety22
disclosuresynthetic_media)video_asset_idprovidertitledescription	tags_jsonprivacy_statuscategory_idmetadata_jsonzYouTube Testzyt-testz.https://www.googleapis.com/auth/youtube.uploadaccess-tokenrefresh-token   )hours	connected)	rB   display_nameexternal_account_idscopesencrypted_access_tokenencrypted_refresh_tokentoken_expires_atr:   
is_defaultzSprint 8 Guidezsprint-8-guide	generated)rA   post_draft_idrC   slugr:   )parentmkdirwrite_bytesr   r   strstatst_sizer   ready_for_reviewr   idr   youtuber   r   r   nowr   utcr   addcommitrefreshr   )
r$   r"   r+   r,   
video_filesessionvideodraftaccountcomps
             r%   make_video_draft_accountrl   &   s   K'JD4888.///	 7G(@(@(@MbZ`MbMbk}  JM  NX  JY  JY  eo  et  et  ev  ev  e~  Ic  [a  Ic  Ic  sD  MX  Mi  sw  x  x  xH<LTk  zP  ]l  n|  \}  NU  cg  xD  GX  Z^  F_  w`  a  a  a!8+;JaY_JaJa  xJ  BH  xJ  xJ  TD  SE  ^k  lz  ^{  ^{  Ub  cr  Us  Us  FN  FR  S[  S_  F`  F`  cl  st  cu  cu  cu  Fu  ~I  VZ  [  [  [EGKK...G0D0D0DgnnFVFVFVX_XgXghmXnXnXnpwpp  AF  qG  qG  qG  IP  IX  IX  Y`  Ia  Ia  IauxuxWgn~  HS  T  T  TD7>>+++W__T-B-B-Bx7:tw67 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7s   G3II
I
c                    t          |           \  }}|                    d          }|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }dd|iz  }t          t          j
        |                    d x}x}}|                                d	         d
         }	d}|	|k    }
|
slt          j        d|
fd|	|f          t          j        |	          t          j        |          dz  }dd|iz  }t          t          j
        |                    d x}	x}
}d S )N#/api/accounts/youtube/connect/start  ==z3%(py2)s
{%(py2)s = %(py0)s.status_code
} == %(py5)sresponsepy0py2py5assert %(py7)spy7detailmessagezGoogle OAuth config missingz%(py1)s == %(py4)spy1py4assert %(py6)spy6)r&   getstatus_code
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanationjson)r"   _clientrs   @py_assert1@py_assert4@py_assert3@py_format6@py_format8@py_assert0@py_assert2@py_format5@py_format7s                r%    test_oauth_start_requires_configr   4   s   h''IAvzz?@@H&3&3&&&&&&&&&&3&&&&&&&&&&&&8&&&&&8&&&&&&&&&&3&&&&&&&&&&&&&&&&&&&&&&&&&&&==??8$Y/P3PP/3PPPPPPPPPPP/3PPPPPP/PPPP3PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPr'   c                     t          |           \  }}t                      }d|_        d|_        |                    d          }|j        }d}||k    }|st          j        d|fd||f          dt          j	                    v st          j
        |          rt          j        |          ndt          j        |          t          j        |          dz  }d	d
|iz  }	t          t          j        |	                    d x}x}}|                                }
d}||
v }|st          j        d|fd||
f          t          j        |          dt          j	                    v st          j
        |
          rt          j        |
          nddz  }dd|iz  }t          t          j        |                    d x}}t          |          5 }|                    t           d          }|j        d         }|
d         }||k    }|slt          j        d|fd||f          t          j        |          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}x}}d d d            d S # 1 swxY w Y   d S )Nz	client-idzclient-secretrn      rp   rr   rs   rt   rx   ry   authorization_urlinz%(py1)s in %(py3)sbodyr~   py3assert %(py5)srw   youtube_oauth_statestater|   r}   r   r   )r&   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   
value_json)r"   r$   r   r#   rs   r   r   r   r   r   r   r   r   @py_format4rg   rowr   r   s                     r%   -test_oauth_start_stores_state_when_configuredr   ;   s!   "8,,NFF~~H +H$3H!zz?@@H&3&3&&&&&&&&&&3&&&&&&&&&&&&8&&&&&8&&&&&&&&&&3&&&&&&&&&&&&&&&&&&&&&&&&&&&==??D&$&&&&&&&&&&$&&&&&&&&&&&&&&&&$&&&&&$&&&&&&&&&&&&&&&&&&&&&&&&&&&	 8Gkk*&;<<~g&7$w-7&-7777777777&-77777&7777-7777777777777777777777777778 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8s   B+JJ
Jc                     t          d          } d}| |k    }|st          j        d|fd| |f          dt          j                    v st          j        |           rt          j        |           ndt          j        |          dz  }dd|iz  }t          t          j        |                    d x}}t          |           }d}||k    }|st          j        d|fd	||f          d
t          j                    v st          j        t                    rt          j        t                    nd
dt          j                    v st          j        |           rt          j        |           ndt          j        |          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}x}}d S )Nzrefresh-token-secret)!=)z%(py0)s != %(py3)s	encryptedru   r   r   rw   rp   )z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr   )ru   r~   r   r   zassert %(py8)spy8)
r   r   r   r   r   r   r   r   r   r   )	r   r   r   r   r   @py_assert5r   r   @py_format9s	            r%   5test_encrypted_tokens_roundtrip_and_are_not_plaintextr   I   s   455I..9...........9.............9.....9................................##='==#'===========#'============================================#===='==============================r'   c           	      x   t          |           \  }}t          ||           \  }}}}|                    d| d|i          }|j        }d}||k    }	|	st	          j        d|	fd||f          dt          j                    v st	          j        |          rt	          j	        |          ndt	          j	        |          t	          j	        |          dz  }
d	d
|
iz  }t          t	          j        |                    d x}x}	}d}|j        }|j        } |            }||v }|st	          j        d|fd||f          t	          j	        |          dt          j                    v st	          j        |          rt	          j	        |          ndt	          j	        |          t	          j	        |          t	          j	        |          dz  }dd|iz  }t          t	          j        |                    d x}x}x}x}}t          || dz  dd          \  }}}}|                    d| |ddddd          }|j        }d}||k    }	|	st	          j        d|	fd||f          dt          j                    v st	          j        |          rt	          j	        |          ndt	          j	        |          t	          j	        |          dz  }
d	d
|
iz  }t          t	          j        |                    d x}x}	}d}|j        }||v }|st	          j        d|fd||f          t	          j	        |          dt          j                    v st	          j        |          rt	          j	        |          ndt	          j	        |          dz  }
d	d
|
iz  }t          t	          j        |                    d x}x}}d S )N$/api/publish/youtube/private-upload/
account_idr   ro   rp   rr   rs   rt   rx   ry   confirmationr   )z^%(py1)s in %(py9)s
{%(py9)s = %(py7)s
{%(py7)s = %(py5)s
{%(py5)s = %(py3)s.text
}.lower
}()
})r~   r   rw   ry   py9zassert %(py11)spy11twounlisted2r*   Tr   confirm_private_uploadconfirm_metadata_reviewedconfirm_disclosure_revieweddry_runzprivate uploads only)z,%(py1)s in %(py5)s
{%(py5)s = %(py3)s.text
})r~   r   rw   )r&   rl   postr   r   r   r   r   r   r   r   r   textlower)r"   r$   r   r   draft_idr   rs   r   r   r   r   r   r   @py_assert6@py_assert8r   @py_format10@py_format12	draft2_idaccount2_ids                       r%   9test_upload_rejects_missing_confirmations_and_non_privater   O   s   "8,,NFF!9&(!K!KAxQ{{L(LLT`blSm{nnH&3&3&&&&&&&&&&3&&&&&&&&&&&&8&&&&&8&&&&&&&&&&3&&&&&&&&&&&&&&&&&&&&&&&&&&&2X]2]0200222>22222222222>222222>22222222222X22222X222222]22220222222222222222222222222222222222222#;FHuDT^hqt#u#u#u Ay+q{{M)MMcn  KO  nr  SW  dh  Ui  Ui{  j  jH&3&3&&&&&&&&&&3&&&&&&&&&&&&8&&&&&8&&&&&&&&&&3&&&&&&&&&&&&&&&&&&&&&&&&&&&!2X]2!]2222222222!]22222!22222222222X22222X222222]22222222222222222222222222222r'   c           	      r   t          |           \  }}t          ||           \  }}}}|                    d| |ddddd          }|j        }d}	||	k    }
|
st	          j        d|
fd||	f          dt          j                    v st	          j        |          rt	          j	        |          ndt	          j	        |          t	          j	        |	          d	z  }d
d|iz  }t          t	          j        |                    d x}x}
}	|                                }|d         }d}
||
u }|slt	          j        d|fd||
f          t	          j	        |          t	          j	        |
          dz  }dd|iz  }t          t	          j        |                    d x}x}}
|d         }|j        }d}	 ||	          }|ssdt	          j	        |          t	          j	        |          t	          j	        |	          t	          j	        |          dz  }t          t	          j        |                    d x}x}x}	}|d         }d}
||
k    }|slt	          j        d|fd||
f          t	          j	        |          t	          j	        |
          dz  }dd|iz  }t          t	          j        |                    d x}x}}
t          |          5 }|                    t           |          }|                    t"          |          }|                    t$          |          }|                    t)          t*                                        t*          j        |d         k                                                        }|                    t)          t2                                        t2          j        |d         k                                                        }|                    t)          t6                                        t6          j        dk                                                        }|j        }|d         }	||	k    }
|
st	          j        d|
fd||	f          dt          j                    v st	          j        |          rt	          j	        |          ndt	          j	        |          t	          j	        |	          d	z  }d
d|iz  }t          t	          j        |                    d x}x}
}	|j        }|d         }	||	k    }
|
st	          j        d|
fd||	f          dt          j                    v st	          j        |          rt	          j	        |          ndt	          j	        |          t	          j	        |	          d	z  }d
d|iz  }t          t	          j        |                    d x}x}
}	d }||u}|st	          j        d|fd||f          d t          j                    v st	          j        |          rt	          j	        |          nd t	          j	        |          d!z  }d"d#|iz  }t          t	          j        |                    d x}}d }||u}|st	          j        d|fd||f          d$t          j                    v st	          j        |          rt	          j	        |          nd$t	          j	        |          d!z  }d"d#|iz  }t          t	          j        |                    d x}}|j        }d%}	||	k    }
|
st	          j        d|
fd&||	f          d$t          j                    v st	          j        |          rt	          j	        |          nd$t	          j	        |          t	          j	        |	          d	z  }d
d|iz  }t          t	          j        |                    d x}x}
}	|j         }d'}	||	u }
|
st	          j        d|
fd(||	f          d$t          j                    v st	          j        |          rt	          j	        |          nd$t	          j	        |          t	          j	        |	          d	z  }d
d|iz  }t          t	          j        |                    d x}x}
}	d)}|j!        }tE          |          }||v}|st	          j        d*|fd+||f          t	          j	        |          d,t          j                    v st	          j        tD                    rt	          j	        tD                    nd,d$t          j                    v st	          j        |          rt	          j	        |          nd$t	          j	        |          t	          j	        |          d-z  }d.d/|iz  }t          t	          j        |                    d x}x}x}}|j#        }d}	||	k    }
|
st	          j        d|
fd0||	f          d1t          j                    v st	          j        |          rt	          j	        |          nd1t	          j	        |          t	          j	        |	          d	z  }d
d|iz  }t          t	          j        |                    d x}x}
}	|j$        }d'}	||	u }
|
st	          j        d|
fd2||	f          d1t          j                    v st	          j        |          rt	          j	        |          nd1t	          j	        |          t	          j	        |	          d	z  }d
d|iz  }t          t	          j        |                    d x}x}
}	|sed3d4d5t          j                    v st	          j        |          rt	          j	        |          nd5iz  }t          t	          j        |                    	 d d d            d S # 1 swxY w Y   d S )6Nr   Tr   r   r   rp   rr   rs   rt   rx   ry   r   )is)z%(py1)s is %(py4)sr}   r   r   youtube_video_iddryrun_zLassert %(py7)s
{%(py7)s = %(py3)s
{%(py3)s = %(py1)s.startswith
}(%(py5)s)
})r~   r   rw   ry   website_link_statuswaiting_for_public_videor|    youtube_upload_private_succeeded)z8%(py2)s
{%(py2)s = %(py0)s.external_post_id
} == %(py5)sri   )z;%(py2)s
{%(py2)s = %(py0)s.external_youtube_id
} == %(py5)srh   )is not)z%(py0)s is not %(py3)sexternalr   r   rw   auditr(   )z6%(py2)s
{%(py2)s = %(py0)s.privacy_status
} == %(py5)sF)z:%(py2)s
{%(py2)s = %(py0)s.notify_subscribers
} is %(py5)srI   not in)z_%(py1)s not in %(py8)s
{%(py8)s = %(py3)s(%(py6)s
{%(py6)s = %(py4)s.request_redacted_json
})
}r[   )r~   r   r   r   r   zassert %(py10)spy10)z.%(py2)s
{%(py2)s = %(py0)s.status
} == %(py5)srk   )z<%(py2)s
{%(py2)s = %(py0)s.website_link_allowed
} is %(py5)sassert %(py0)sru   events)%r&   rl   r   r   r   r   r   r   r   r   r   r   r   
startswithr   r   r   r   r   execr	   r   whereexternal_post_idfirstr   r   r   
event_typeallexternal_youtube_idrF   notify_subscribersrequest_redacted_jsonr[   r:   website_link_allowed) r"   r$   r   video_idr   r   comp_idrs   r   r   r   r   r   r   r   r   r   r   r   rg   ri   rh   rk   r   r   r   r   r   @py_assert7r   @py_format11@py_format1s                                    r%   ;test_dry_run_private_upload_creates_audit_and_links_contextr   \   sI   "8,,NFF.Fvx.X.X+Hh
G{{L(LLbl  IM  lp  QU  bf  Tg  Tg{  h  hH&3&3&&&&&&&&&&3&&&&&&&&&&&&8&&&&&8&&&&&&&&&&3&&&&&&&&&&&&&&&&&&&&&&&&&&&==??D	?"d"?d""""""""""?d"""""?""""d""""""""""""""""""""""""""""#9#.9y9..y99999999#9999.9999y9999999999999999999999999999%&D*DD&*DDDDDDDDDDD&*DDDDDD&DDDD*DDDDDDDDDDDDDDDDDDDDDDDDDDDD	 GIx00J11{{+W55<<| 4 4 : :<;X\`as\t;t u uvv||~~V$677==>P>aeij|e}>}~~  F  F  H  Hf]3399-:RVx:xyyzz~~  A  A%A.@)AA%)AAAAAAAAAAA%)AAAAAAAAAAAAAuAAAAAuAAAAAA%AAAA)AAAAAAAAAAAAAAAAAAAAAAAAAAAA(DD1C,DD(,DDDDDDDDDDD(,DDDDDDDDDDDDDuDDDDDuDDDDDD(DDDD,DDDDDDDDDDDDDDDDDDDDDDDDDDDD##xt##########xt############x#####x######t#########################  uD          uD            u     u      D                         #0y0#y0000000000#y000000000000u00000u000000#0000y000000000000000000000000000'050'50000000000'5000000000000u00000u000000'00005000000000000000000000000000E)DES)D%E%EE~%EEEEEEEEEEE~%EEEEEE~EEEEEEEEEEESEEEEESEEEEEEEEEEEEEEEEEEEEEEEE)DEEEE%EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE{888{88888888888{8888888888888t88888t888888{88888888888888888888888888888888(1E1(E1111111111(E111111111111t11111t111111(1111E111111111111111111111111111vv!                 s   cn,,n03n0c           	      ^   t          |           \  }}t          ||           \  }}}}|                    d| |ddddd          }|j        }d}||k    }	|	st	          j        d|	fd||f          dt          j                    v st	          j        |          rt	          j	        |          ndt	          j	        |          t	          j	        |          d	z  }
d
d|
iz  }t          t	          j        |                    d x}x}	}|                    d                                          d         }|sedddt          j                    v st	          j        |          rt	          j	        |          ndiz  }t          t	          j        |                    t          |          }d}||v}|st	          j        d|fd||f          t	          j	        |          dt          j                    v st	          j        |          rt	          j	        |          nddz  }dd|iz  }
t          t	          j        |
                    d x}}d}||v}|st	          j        d|fd||f          t	          j	        |          dt          j                    v st	          j        |          rt	          j	        |          nddz  }dd|iz  }
t          t	          j        |
                    d x}}|                                d         }||v }|st	          j        d|fd||f          t	          j	        |          dt          j                    v st	          j        |          rt	          j	        |          nddz  }dd|iz  }
t          t	          j        |
                    d x}}d S )Nr   Tr   r   r   rp   rr   resultrt   rx   ry   z/api/logs?kind=youtube_uploadsitemsr   ru   logsrJ   r   )z%(py1)s not in %(py3)s
serializedr   r   rw   rI   r   r   r   )r&   rl   r   r   r   r   r   r   r   r   r   r   r   r   r[   )r"   r$   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                     r%   5test_logs_include_youtube_upload_audit_without_tokensr   x   s/   "8,,NFF!9&(!K!KAxQ[[JJJ`j  GK  jn  OS  `d  Re  Re[  f  fF$$$$$$$$$$$$$$$$$$$$$$$$6$$$$$6$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$::677<<>>wGDKKKKKKKKKKKK4KKKKK4KKKKKKKKKKKKKKKKTJ,?*,,,,,,,,,,?*,,,,,?,,,,,,,,,,,*,,,,,*,,,,,,,,,,,,,,,,,,,,,,,,,,,+>++++++++++>+++++>+++++++++++++++++++++++++++++++++++++++++++;;==+,:,
::::::::::,
:::::,:::::::::::
:::::
:::::::::::::::::::::::::::::r'   )-builtinsr   _pytest.assertion.rewrite	assertionrewriter   r   r   r   pathlibr   fastapi.testclientr   sqlmodelr   r	   app.core.configr
   app.db.sessionr   r   app.mainr   app.models.corer   r   r   r   r   r   r   r   r   r   app.security.tokensr   r   r&   rl   r   r   r   r   r   r    r'   r%   <module>r      s                 2 2 2 2 2 2 2 2 2 2       ) ) ) ) ) ) $ $ $ $ $ $ $ $ ( ( ( ( ( ( 6 6 6 6 6 6 6 6                              = < < < < < < <
# 
# 
# ;DB 7 7 7 7 7Q Q Q8 8 8> > >
3 
3 
3  8
; 
; 
; 
; 
;r'   