o
    ˷e                     @   s   d dl Z d dlmZ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 d d	lmZmZ d
dlmZ G dd deeejZedddiidG dd deZeddddiiidG dd deZdS )    N)parse_qsurlparse)override_settings)reverse)	urlencode)SocialAccount)registry)create_oauth2_tests)MockedResponsemocked_response   )ShopifyProviderc                   @   s&   e Zd Zdd Zd
ddZdd Zd	S )ShopifyTestsc                 C   s   t | jjd }| |d d |d | j|d}ttd|ddi| | j	|d|d	 d dd
}W d    |S 1 sAw   Y  |S )N	_callbackredirect_urir   )with_refresh_token   zcontent-typezapplication/jsonteststate)coder   shop)
r   provideridassertGreaterfindget_login_response_jsonr   r
   clientget)selfqresp	resp_mockr   complete_urlresponse_json r$   d/var/www/ideatree/venv/lib/python3.10/site-packages/allauth/socialaccount/providers/shopify/tests.py_complete_shopify_login   s(   

z$ShopifyTests._complete_shopify_loginloginTc                 C   sf   t | jjd d t|dd }| j|}| |jd t|d }t	|j
}| ||||}|S )N_login?r   processr   i.  location)r   r   r   r   r   postassertEqualstatus_coder   r   queryr&   )r   r!   r+   r   urlr    pr   r$   r$   r%   r'   %   s   
zShopifyTests.loginc                 C   s
   t ddS )Nr   z
        {
            "shop": {
                "id": "1234566",
                "name": "Test Shop",
                "email": "email@example.com"
            }
        }
        )r
   )r   r$   r$   r%   get_mocked_response2   s   z ShopifyTests.get_mocked_responseNr'   T)__name__
__module____qualname__r&   r'   r3   r$   r$   r$   r%   r      s    
r   shopifyIS_EMBEDDEDT)SOCIALACCOUNT_PROVIDERSc                   @   s   e Zd ZdZdddZdS )ShopifyEmbeddedTestsz
    Shopify embedded apps (that run within an iFrame) require a JS (not server)
    redirect for starting the oauth2 process.

    See Also:
    https://help.shopify.com/api/sdks/embedded-app-sdk/getting-started#oauth
    r'   Tc                 C   s   | j t| jjd d t|dd }| |jd |j	d}| 
d|v d| | 
|jd	 | 
d
|v o=d|v d| t|dd dd }t|j}| ||||}|S )Nr(   r)   r   r*   r   utf8scriptz(Content missing script tag. [Actual: {}]z4Redirect JS must be allowed to run in Shopify iframez<!DOCTYPE html><html><head>z</head><body></body></html>z-Expected standard HTML skeleton. [Actual: {}]z
;</script>r   zlocation.href = "r   )r   r-   r   r   r   r   r.   r/   contentdecode
assertTrueformatxframe_options_exemptr   splitr   r0   r&   )r   r!   r+   r   r    actual_contentr2   r   r$   r$   r%   r'   K   s8   
zShopifyEmbeddedTests.loginNr4   )r5   r6   r7   __doc__r'   r$   r$   r$   r%   r;   A   s    r;   AUTH_PARAMSzgrant_options[]zper-userc                   @   s0   e Zd ZdZd	ddZedddddd ZdS )
ShopifyPerUserAccessTestsaQ  
    Shopify has two access modes, offline (the default) and online/per-user.
    Enabling 'online' access should cause all-auth to tie the logged in
    Shopify user to the all-auth account (rather than the shop as a whole).

    See Also:
    https://help.shopify.com/api/getting-started/authentication/
    oauth#api-access-modes
    Tc              
   C   s2   ddddddddd	d
d}|rd|d< t |S )Ntestaczwrite_orders,read_customersiQ write_ordersC5JonSmithjon@example.comT)r   
first_name	last_nameemailaccount_owner)access_tokenscope
expires_inassociated_user_scopeassociated_usertestrfrefresh_token)jsondumps)r   r   response_datar$   r$   r%   r   x   s   
z1ShopifyPerUserAccessTests.get_login_response_json)SOCIALACCOUNT_AUTO_SIGNUPSOCIALACCOUNT_EMAIL_REQUIREDACCOUNT_EMAIL_REQUIREDc                 C   s   |   }| |}| j|ddd tjj| jjdd }| 	| | 
d|jv  | |jjd | |jjd | |jjd	 d S )
Nz/accounts/profile/F)fetch_redirect_responserJ   )r   uidrV   rM   rK   rL   )r3   r'   assertRedirectsr   objectsfilterr   r   firstassertIsNotNoner@   
extra_datar.   userrP   rN   rO   )r   
resp_mocksr    social_accountr$   r$   r%   test_associated_user   s   

z.ShopifyPerUserAccessTests.test_associated_userN)T)r5   r6   r7   rE   r   r   rj   r$   r$   r$   r%   rG   h   s    

rG   )rY   urllib.parser   r   django.test.utilsr   django.urlsr   django.utils.httpr   allauth.socialaccount.modelsr   allauth.socialaccount.providersr   allauth.socialaccount.testsr	   allauth.testsr
   r   r   r   by_idr   r   r;   rG   r$   r$   r$   r%   <module>   s$    1&