diff --git a/tconnectsync/api/webui.py b/tconnectsync/api/webui.py
index bd67d34..7dfa6c0 100644
--- a/tconnectsync/api/webui.py
+++ b/tconnectsync/api/webui.py
@@ -30,10 +30,14 @@ class WebUIScraper:
return self.controliq.needs_relogin()
def _get(self, endpoint):
- r = self.controliq.loginSession.get(self.BASE_URL + endpoint, headers=base_headers())
+ r = self.controliq.loginSession.get(self.BASE_URL + endpoint, headers=base_headers(), allow_redirects=True)
if r.status_code != 200:
raise ApiException(r.status_code, "WebUIScraper HTTP %s response: %s" % (str(r.status_code), r.text))
+
+ if 'login.aspx' in r.url:
+ raise ApiException(401, "WebUIScraper HTTP %s response for login page, returning 401: %s" % (str(r.status_code), r.url))
+
return r
diff --git a/tests/api/test_webui.py b/tests/api/test_webui.py
index 345d919..8b54f06 100644
--- a/tests/api/test_webui.py
+++ b/tests/api/test_webui.py
@@ -18,7 +18,7 @@ from .fake import ControlIQApi
from tconnectsync.api.controliq import ControlIQApi as RealControlIQApi
from tconnectsync.api.common import ApiException, ApiLoginException, base_headers
-class TestWebUIScraper(unittest.TestCase):
+class TestWebUIScraperMyDevices(unittest.TestCase):
maxDiff = None
DEVICES_HTML = """
@@ -319,6 +319,64 @@ class TestWebUIScraper(unittest.TestCase):
'guid': None
})})
+ REDIRECT_HTML = """
Object moved
+Object moved to here.
+"""
+
+ LOGIN_HTML = """"""
+
+ def test_relogin_when_redirected_to_login_page(self):
+ ciq = ControlIQApi()
+ ciq.needs_relogin = lambda: False
+ ciq.loginSession = requests.Session()
+ ciq.LOGIN_URL = RealControlIQApi.LOGIN_URL
+ ciq._email = 'EMAIL'
+ ciq._password = 'PASSWORD'
+
+ login_times = []
+ def fake_login(email, password):
+ self.assertEqual(email, ciq._email)
+ self.assertEqual(password, ciq._password)
+
+ login_times.append(1)
+ return True
+
+ ciq.login = fake_login
+ webui = WebUIScraper(ciq)
+
+ ciq.login(ciq._email, ciq._password)
+ self.assertEqual(len(login_times), 1)
+
+ with requests_mock.Mocker() as m:
+ m.get('https://tconnect.tandemdiabetes.com/myaccount/my_devices.aspx',
+ request_headers=base_headers(),
+
+ status_code=302,
+ headers={'Location': 'https://tconnect.tandemdiabetes.com/login.aspx?ReturnUrl=%2fmyaccount%2fmy_devices.aspx'},
+ text=self.REDIRECT_HTML)
+
+ m.get('https://tconnect.tandemdiabetes.com/login.aspx?ReturnUrl=%2fmyaccount%2fmy_devices.aspx',
+ request_headers=base_headers(),
+
+ status_code=200,
+ text=self.LOGIN_HTML)
+
+ self.assertRaises(ApiException, webui.my_devices)
+ self.assertEqual(len(login_times), 2)
+
+ with requests_mock.Mocker() as m:
+ m.get('https://tconnect.tandemdiabetes.com/myaccount/my_devices.aspx',
+ request_headers=base_headers(),
+
+ text=self.DEVICES_HTML)
+
+ devices = webui.my_devices()
+ self.assertEqual(len(login_times), 2)
+
+
+class TestWebUIScraperPumpSettings(unittest.TestCase):
+ maxDiff = None
+
PUMP_SETTINGS_HTML = """