Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions github_nonpublic_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,14 @@ def create_login_session(
def _login_callback(data):
data.update(dict(login=username, password=password))

_get_and_submit_form(
res = _get_and_submit_form(
session=session, url="https://github.com/login", data_callback=_login_callback
)
if "two-factor" not in res.url and "github.com" in res.url:
logging.error("Login challenged by GitHub (Captcha/Device Verification). Stuck at: %s", res.url)
raise RuntimeError(
f"Login challenged by GitHub (Captcha/Device Verification). Stuck at: {res.url}"
)

def _tfa_callback(data):
data.update(dict(otp=tfa_callback()))
Expand Down Expand Up @@ -322,8 +327,7 @@ def _data_callback(data):
)

if __name__ == "__main__":
config = ConfigObj(os.path.expanduser("~/github.ini"), _inspec=True)

config = ConfigObj(os.path.expanduser("~/github.ini"), _inspec=True) # type: ignore[not-callable]
api = Api(
username=config["username"],
password=config["password"],
Expand Down
23 changes: 23 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ def test_get_and_submit_form_by_id_error(self):
session=self.session, url='http://github.com',
form_matcher=lambda form: False)

def test_create_login_session_captcha_challenge(self):
with mock.patch.object(api, '_get_and_submit_form') as mock_submit:
mock_res = mock.MagicMock()
mock_res.url = 'https://github.com/sessions/verified-device'
mock_submit.return_value = mock_res

with self.assertRaisesRegex(RuntimeError, 'Login challenged by GitHub'):
api.create_login_session(
username='user', password='password', tfa_callback=lambda: '123'
)

def test_create_login_session_success(self):
with mock.patch.object(api, '_get_and_submit_form') as mock_submit:
mock_res = mock.MagicMock()
mock_res.url = 'https://github.com/sessions/two-factor'
mock_submit.return_value = mock_res

session = api.create_login_session(
username='user', password='password', tfa_callback=lambda: '123'
)
self.assertIsNotNone(session)
self.assertEqual(mock_submit.call_count, 2)

def test_create_business_org(self):
self._seed_session_with_file(NEW_ORG_FORM_HTML)
gh = api.Api(username='user', password='pass', session=self.session)
Expand Down
Loading