def test_auth_webbrowser_get(): """Authentication by WebBrowser positive test case.""" ref_token = "MOCK_TOKEN" rest = _init_rest(REF_SSO_URL, REF_PROOF_KEY) # mock webbrowser mock_webbrowser = MagicMock() mock_webbrowser.open_new.return_value = True # mock socket mock_socket_instance = MagicMock() mock_socket_instance.getsockname.return_value = [None, 12345] mock_socket_client = MagicMock() mock_socket_client.recv.return_value = ("\r\n".join([ f"GET /?token={ref_token}&confirm=true HTTP/1.1", "User-Agent: snowflake-agent", ])).encode("utf-8") mock_socket_instance.accept.return_value = (mock_socket_client, None) mock_socket = Mock(return_value=mock_socket_instance) auth = AuthByWebBrowser(rest, APPLICATION, webbrowser_pkg=mock_webbrowser, socket_pkg=mock_socket) auth.authenticate(AUTHENTICATOR, SERVICE_NAME, ACCOUNT, USER, PASSWORD) assert not rest._connection.errorhandler.called # no error assert auth.assertion_content == ref_token body = {"data": {}} auth.update_body(body) assert body["data"]["TOKEN"] == ref_token assert body["data"]["PROOF_KEY"] == REF_PROOF_KEY assert body["data"]["AUTHENTICATOR"] == EXTERNAL_BROWSER_AUTHENTICATOR
def test_auth_webbrowser(): """ Authentication by WebBrowser positive test case """ ref_token = "MOCK_TOKEN" rest = _init_rest(REF_SSO_URL, REF_PROOF_KEY) # mock webbrowser mock_webbrowser = MagicMock() mock_webbrowser.open_new.return_value = True # mock socket mock_socket_instance = MagicMock() mock_socket_instance.getsockname.return_value = [None, 12345] mock_socket_client = MagicMock() mock_socket_client.recv.return_value = ('\r\n'.join( [u"GET /?token=MOCK_TOKEN HTTP/1.1", u"User-Agent: snowflake-agent"])).encode('utf-8') mock_socket_instance.accept.return_value = (mock_socket_client, None) mock_socket = Mock(return_value=mock_socket_instance) auth = AuthByWebBrowser(rest, APPLICATION, webbrowser_pkg=mock_webbrowser, socket_pkg=mock_socket) auth.authenticate(AUTHENTICATOR, ACCOUNT, USER, PASSWORD) assert not rest._connection.errorhandler.called # no error assert auth.assertion_content == ref_token body = {u'data': {}} auth.update_body(body) assert body[u'data'][u'TOKEN'] == ref_token assert body[u'data'][u'PROOF_KEY'] == REF_PROOF_KEY assert body[u'data'][u'AUTHENTICATOR'] == EXTERNAL_BROWSER_AUTHENTICATOR
def test_auth_webbrowser_fail_webbrowser(monkeypatch, capsys, input_text, expected_error): """Authentication by WebBrowser with failed to start web browser case.""" rest = _init_rest(REF_SSO_URL, REF_PROOF_KEY) ref_token = "MOCK_TOKEN" # mock webbrowser mock_webbrowser = MagicMock() mock_webbrowser.open_new.return_value = False # mock socket mock_socket_instance = MagicMock() mock_socket_instance.getsockname.return_value = [None, 12345] mock_socket_client = MagicMock() mock_socket_client.recv.return_value = ("\r\n".join( ["GET /?token=MOCK_TOKEN HTTP/1.1", "User-Agent: snowflake-agent"])).encode("utf-8") mock_socket_instance.accept.return_value = (mock_socket_client, None) mock_socket = Mock(return_value=mock_socket_instance) auth = AuthByWebBrowser(rest, APPLICATION, webbrowser_pkg=mock_webbrowser, socket_pkg=mock_socket) with patch("builtins.input", return_value=input_text): auth.authenticate(AUTHENTICATOR, SERVICE_NAME, ACCOUNT, USER, PASSWORD) captured = capsys.readouterr() assert captured.out == ( "Initiating login request with your identity provider. A browser window " "should have opened for you to complete the login. If you can't see it, " "check existing browser windows, or your OS settings. Press CTRL+C to " "abort and try again...\nWe were unable to open a browser window for " "you, please open the following url manually then paste the URL you " f"are redirected to into the terminal.\nURL: {REF_SSO_URL}\n") if expected_error: assert rest._connection.errorhandler.called # an error assert auth.assertion_content is None else: assert not rest._connection.errorhandler.called # no error body = {"data": {}} auth.update_body(body) assert body["data"]["TOKEN"] == ref_token assert body["data"]["PROOF_KEY"] == REF_PROOF_KEY assert body["data"]["AUTHENTICATOR"] == EXTERNAL_BROWSER_AUTHENTICATOR