diff --git a/tests/test_signup.py b/tests/test_signup.py index afe167f..74ea2fe 100644 --- a/tests/test_signup.py +++ b/tests/test_signup.py @@ -3,27 +3,42 @@ import pytest import responses -from tgtg import BASE_URL, SIGNUP_BY_EMAIL_ENDPOINT, TgtgClient +from tgtg import AUTH_POLLING_ENDPOINT, BASE_URL, SIGNUP_BY_EMAIL_ENDPOINT, TgtgClient from tgtg.exceptions import TgtgAPIError def test_signup_ok(): + responses.add( + responses.POST, + urljoin(BASE_URL, AUTH_POLLING_ENDPOINT), + json={ + "access_token": "an_access_token", + "refresh_token": "a_refresh_token", + "startup_data": {"user": {"user_id": 1234}}, + }, + status=200, + adding_headers={"set-cookie": "sweet sweet cookie"}, + ) + responses.add( + responses.POST, + urljoin(BASE_URL, AUTH_POLLING_ENDPOINT), + status=202, + ) responses.add( responses.POST, urljoin(BASE_URL, SIGNUP_BY_EMAIL_ENDPOINT), json={ - "login_response": { - "access_token": "an_access_token", - "refresh_token": "a_refresh_token", - "startup_data": {"user": {"user_id": 1234}}, - } + "state": "WAIT", + "polling_id": "a_polling_id", }, status=200, ) - client = TgtgClient().signup_by_email(email="test@test.com") + client = TgtgClient() + client.signup_by_email(email="test@test.com") assert client.access_token == "an_access_token" assert client.refresh_token == "a_refresh_token" assert client.user_id == 1234 + assert client.cookie == "sweet sweet cookie" def test_signup_fail(): diff --git a/tgtg/__init__.py b/tgtg/__init__.py index e63839c..f7ceb83 100644 --- a/tgtg/__init__.py +++ b/tgtg/__init__.py @@ -402,13 +402,11 @@ def signup_by_email( timeout=self.timeout, ) if response.status_code == HTTPStatus.OK: - self.access_token = response.json()["login_response"]["access_token"] - self.refresh_token = response.json()["login_response"]["refresh_token"] - self.last_time_token_refreshed = datetime.datetime.now() - self.user_id = response.json()["login_response"]["startup_data"]["user"][ - "user_id" - ] - return self + first_signup_response = response.json() + if first_signup_response["state"] == "WAIT": + self.start_polling(first_signup_response["polling_id"]) + else: + raise TgtgAPIError(response.status_code, response.content) else: raise TgtgAPIError(response.status_code, response.content)