def test_constructor(): with mock.patch.object(community.CommunityAuthentication, "login", mock.Mock()) as login_mock: community.CommunityAuthentication(AUTH_URL) login_mock.assert_not_called() community.CommunityAuthentication(AUTH_URL, "username", "password") login_mock.assert_called_with("username", "password")
def logged_in_auth(): with mock.patch.object( requests, "post", mock.Mock(return_value=MockedResponse(json=AUTH_RETURN))): auth = community.CommunityAuthentication(AUTH_URL) auth.login("username", "login") return auth
def test_ensure_token_validity(): with pytest.raises(authentication.AuthenticationRequired): community.CommunityAuthentication(AUTH_URL).ensure_token_validity() with pytest.raises(authentication.AuthenticationRequired): auth = community.CommunityAuthentication(AUTH_URL) auth._auth_token = "1" auth.refresh_token = "1" auth._expire_at = None community.CommunityAuthentication(AUTH_URL).ensure_token_validity() with pytest.raises(authentication.AuthenticationRequired): auth = community.CommunityAuthentication(AUTH_URL) auth._auth_token = "1" auth.refresh_token = "1" auth._expire_at = "1" auth._reset_tokens() community.CommunityAuthentication(AUTH_URL).ensure_token_validity() auth = community.CommunityAuthentication(AUTH_URL) with mock.patch.object(auth, "is_logged_in", mock.Mock(return_value=False)) as is_logged_in_mock, \ mock.patch.object(auth, "_try_auto_login", mock.Mock()) as _try_auto_login_mock: with pytest.raises(authentication.AuthenticationRequired): auth.ensure_token_validity() assert is_logged_in_mock.call_count == 2 _try_auto_login_mock.assert_called_once() with mock.patch.object(auth, "_try_auto_login", mock.Mock()) as _try_auto_login_mock: with pytest.raises(authentication.AuthenticationRequired): auth.ensure_token_validity() _try_auto_login_mock.assert_called_once() auth = community.CommunityAuthentication(AUTH_URL) auth._auth_token = "1" auth.refresh_token = "1" # refresh required auth._expire_at = 1 with mock.patch.object(auth, "_refresh_auth", mock.Mock()) as refresh_mock, \ mock.patch.object(auth, "_try_auto_login", mock.Mock()) as _try_auto_login_mock: auth.ensure_token_validity() refresh_mock.assert_called_once() _try_auto_login_mock.assert_not_called() # refresh not required auth._expire_at = time.time() + 10 with mock.patch.object(auth, "_refresh_auth", mock.Mock()) as refresh_mock: auth.ensure_token_validity() refresh_mock.assert_not_called() with mock.patch.object(auth, "ensure_token_validity", mock.Mock()) as ensure_mock: auth.ensure_token_validity() ensure_mock.assert_called_once()
def auth(): return community.CommunityAuthentication(AUTH_URL)