def test_get_valid_access_token_new_refresh_token(self, monkeypatch): """Test that the get_valid_access_token manages to get a new refresh token, if an expired refresh token is being used.""" dev = HQSDevice(3, machine=DUMMY_MACHINE, user_email=DUMMY_EMAIL) dev._access_token = None dev._refresh_token = "not None" mock_response = MockResponse() monkeypatch.setattr(requests, "post", lambda *args, **kwargs: mock_response) monkeypatch.setattr(dev, "save_tokens", lambda *args, **kwargs: None) def f(*args, **kwargs): raise ExpiredRefreshTokenError monkeypatch.setattr(dev, "_refresh_access_token", f) some_token = 1234567 some_refresh_token = 111111 monkeypatch.setattr( dev, "_login", lambda *args, **kwargs: (some_token, some_refresh_token)) # The access and refresh token are set according to the output of _login assert dev.get_valid_access_token() == some_token assert dev._refresh_token == some_refresh_token
def test_get_valid_access_token_new_tokens(self, access_token_expiry, refresh_token_expiry, monkeypatch): """Test that the get_valid_access_token returns a new access and refresh tokens by logging in.""" dev = HQSDevice(3, machine=DUMMY_MACHINE, user_email=DUMMY_EMAIL) if access_token_expiry: # Set the token to an outdated token dev._access_token = jwt.encode({"exp": access_token_expiry}, "secret") if refresh_token_expiry: # Set the token to an outdated token dev._refresh_token = jwt.encode({"exp": refresh_token_expiry}, "secret") some_token = 1234567 some_refresh_token = 111111 monkeypatch.setattr( dev, "_login", lambda *args, **kwargs: (some_token, some_refresh_token)) monkeypatch.setattr(dev, "save_tokens", lambda *args, **kwargs: None) assert dev.get_valid_access_token() == some_token assert dev._refresh_token == some_refresh_token
def test_get_valid_access_token_use_stored(self): """Test that the get_valid_access_token uses a stored token if it exists and it's not expired.""" dev = HQSDevice(3, machine=DUMMY_MACHINE, user_email=DUMMY_EMAIL) valid_time = now.replace(now.year + 1) token = jwt.encode({"exp": valid_time}, "secret") dev._access_token = token assert dev.get_valid_access_token() == token
def test_get_valid_access_token_using_refresh_token( self, access_token_expiry, monkeypatch): """Test that the get_valid_access_token returns a new access token by refreshing using the refresh token.""" dev = HQSDevice(3, machine=DUMMY_MACHINE, user_email=DUMMY_EMAIL) if access_token_expiry: # Set the token to an outdated token dev._access_token = jwt.encode({"exp": access_token_expiry}, "secret") # Set a refresh token with an expiry date in the future dev._refresh_token = jwt.encode({"exp": now.replace(now.year + 1)}, "secret") mock_response = MockResponseWithTokens() monkeypatch.setattr(requests, "post", lambda *args, **kwargs: mock_response) monkeypatch.setattr(dev, "save_tokens", lambda *args, **kwargs: None) assert dev.get_valid_access_token() == MOCK_ACCESS_TOKEN
def test_get_valid_access_token_using_refresh_token_raises( self, access_token_expiry, monkeypatch): """Test that the get_valid_access_token returns a new access token by refreshing using the refresh token.""" dev = HQSDevice(3, machine=DUMMY_MACHINE, user_email=DUMMY_EMAIL) if access_token_expiry: # Set the token to an outdated token dev._access_token = jwt.encode({"exp": access_token_expiry}, "secret") # Set a refresh token with an expiry date in the future dev._refresh_token = jwt.encode({"exp": now.replace(now.year + 1)}, "secret") mock_response = MockResponseUnsuccessfulRequest() monkeypatch.setattr(requests, "post", lambda *args, **kwargs: mock_response) monkeypatch.setattr(dev, "save_tokens", lambda *args, **kwargs: None) with pytest.raises(RequestFailedError, match="Failed to get access token"): dev.get_valid_access_token()