def test_auth_and_reauth_token(client_library_server_2_0_0): # TODO: need to check what the purpose of this test is, and how it # works with the automatic auth check on CL init # if there's environ vars for username and password set # then delete them b/c we rely on specific usernames # and passwords for this test! # docs: https://github.com/getsentry/responses try: del os.environ["VIRL2_PASS"] del os.environ["VIRL2_USER"] except KeyError: pass # mock failed authentication: responses.add(responses.POST, "https://0.0.0.0/fake_url/api/v0/authenticate", status=403) responses.add(responses.GET, "https://0.0.0.0/fake_url/api/v0/authok", status=401) # mock successful authentication: responses.add( responses.POST, "https://0.0.0.0/fake_url/api/v0/authenticate", json="7bbcan78a98bch7nh3cm7hao3nc7", ) responses.add(responses.GET, "https://0.0.0.0/fake_url/api/v0/authok") # mock get labs responses.add(responses.GET, "https://0.0.0.0/fake_url/api/v0/labs", json=[]) with pytest.raises(InitializationError): # Test returns custom exception when instructed to raise on failure ClientLibrary( url="http://0.0.0.0/fake_url/", username="******", password="******", raise_for_auth_failure=True, ) cl = ClientLibrary(url="http://0.0.0.0/fake_url/", username="******", password="******") cl.all_labs() # for idx, item in enumerate(responses.calls): # print(idx, item.request.url) # # this is what we expect: # 0 https://0.0.0.0/fake_url/api/v0/authenticate # 1 https://0.0.0.0/fake_url/api/v0/authenticate # 2 https://0.0.0.0/fake_url/api/v0/authok # 3 https://0.0.0.0/fake_url/api/v0/authenticate # 4 https://0.0.0.0/fake_url/api/v0/authok # 5 https://0.0.0.0/fake_url/api/v0/labs assert (responses.calls[0].request.url == "https://0.0.0.0/fake_url/api/v0/authenticate") assert json.loads(responses.calls[0].request.body) == { "username": "******", "password": "******", } assert (responses.calls[1].request.url == "https://0.0.0.0/fake_url/api/v0/authenticate") assert responses.calls[ 2].request.url == "https://0.0.0.0/fake_url/api/v0/authok" assert (responses.calls[3].request.url == "https://0.0.0.0/fake_url/api/v0/authenticate") assert responses.calls[ 4].request.url == "https://0.0.0.0/fake_url/api/v0/authok" assert responses.calls[ 5].request.url == "https://0.0.0.0/fake_url/api/v0/labs" assert len(responses.calls) == 6
def test_auth_and_reauth_token(client_library_server_current): # mock failed authentication: responses.add(responses.POST, "https://0.0.0.0/fake_url/api/v0/authenticate", status=403) responses.add(responses.GET, "https://0.0.0.0/fake_url/api/v0/authok", status=401) # mock successful authentication: responses.add( responses.POST, "https://0.0.0.0/fake_url/api/v0/authenticate", json="7bbcan78a98bch7nh3cm7hao3nc7", ) responses.add(responses.GET, "https://0.0.0.0/fake_url/api/v0/authok") # mock get labs responses.add(responses.GET, "https://0.0.0.0/fake_url/api/v0/labs", json=[]) with pytest.raises(InitializationError): # Test returns custom exception when instructed to raise on failure ClientLibrary( url="https://0.0.0.0/fake_url/", username="******", password="******", raise_for_auth_failure=True, ) cl = ClientLibrary(url="https://0.0.0.0/fake_url/", username="******", password="******") cl.all_labs() # for idx, item in enumerate(responses.calls): # print(idx, item.request.url) # # this is what we expect: # 0 https://0.0.0.0/fake_url/api/v0/authenticate # 1 https://0.0.0.0/fake_url/api/v0/authenticate # 2 https://0.0.0.0/fake_url/api/v0/authok # 3 https://0.0.0.0/fake_url/api/v0/authenticate # 4 https://0.0.0.0/fake_url/api/v0/authok # 5 https://0.0.0.0/fake_url/api/v0/labs assert (responses.calls[0].request.url == "https://0.0.0.0/fake_url/api/v0/authenticate") assert json.loads(responses.calls[0].request.body) == { "username": "******", "password": "******", } assert (responses.calls[1].request.url == "https://0.0.0.0/fake_url/api/v0/authenticate") assert responses.calls[ 2].request.url == "https://0.0.0.0/fake_url/api/v0/authok" assert (responses.calls[3].request.url == "https://0.0.0.0/fake_url/api/v0/authenticate") assert responses.calls[ 4].request.url == "https://0.0.0.0/fake_url/api/v0/authok" assert responses.calls[ 5].request.url == "https://0.0.0.0/fake_url/api/v0/labs" assert len(responses.calls) == 6