def test_config_refresh_token():
    """Ensure that the refresh token can be loaded, saved, and removed"""

    with NamedTemporaryFile(delete=False) as fp:
        config = Config(token_file=fp.name)

        # Write a new token to the file
        config.refresh_token = 'secret_value'
        config.save_refresh_token()

        # Load a valid token from the file
        config.refresh_token = None
        config.load_refresh_token()
        assert config.refresh_token == 'secret_value'

        # Discard the token and delete the file
        config.delete_refresh_token()
        config.delete_refresh_token()
        assert config.refresh_token is None
        assert not os.path.exists(fp.name)

        # Saving should create a new file
        config.refresh_token = 'new_value'
        config.save_refresh_token()

        # Which we can read back to verify
        config.refresh_token = None
        config.load_refresh_token()
        assert config.refresh_token == 'new_value'

        # And delete again to clean up
        config.delete_refresh_token()
        assert not os.path.exists(fp.name)

        # Loading from the non-existent file should return None
        config.refresh_token = 'secret_value'
        config.load_refresh_token()
        assert config.refresh_token is None
Exemple #2
0
def test_config_refresh_token():
    "Ensure that the refresh token can be loaded, saved, and removed"

    with NamedTemporaryFile(delete=False) as fp:
        config = Config(token_file=fp.name)

        # Write a new token to the file
        config.refresh_token = 'secret_value'
        config.save_refresh_token()

        # Load a valid token from the file
        config.refresh_token = None
        config.load_refresh_token()
        assert config.refresh_token == 'secret_value'

        # Discard the token and delete the file
        config.delete_refresh_token()
        assert config.refresh_token is None
        assert not os.path.exists(fp.name)

        # Saving should create a new file
        config.refresh_token = 'new_value'
        config.save_refresh_token()

        # Which we can read back to verify
        config.refresh_token = None
        config.load_refresh_token()
        assert config.refresh_token == 'new_value'

        # And delete again to clean up
        config.delete_refresh_token()
        assert not os.path.exists(fp.name)

        # Loading from the non-existent file should return None
        config.refresh_token = 'secret_value'
        config.load_refresh_token()
        assert config.refresh_token is None
Exemple #3
0
def main():

    locale.setlocale(locale.LC_ALL, '')

    if len(sys.argv) > 1:
        theme = Theme.from_name(sys.argv[1])
    else:
        theme = Theme()

    vcr = initialize_vcr()
    with vcr.use_cassette('demo_theme.yaml') as cassette, \
            curses_session() as stdscr:

        config = Config()
        if vcr.record_mode == 'once':
            config.load_refresh_token()
        else:
            config.refresh_token = 'mock_refresh_token'

        reddit = praw.Reddit(user_agent='RTV Theme Demo',
                             decode_html_entities=False,
                             disable_update_check=True)
        reddit.config.api_request_delay = 0

        config.history.add('https://api.reddit.com/comments/6llvsl/_/djutc3s')
        config.history.add('http://i.imgur.com/Z9iGKWv.gifv')
        config.history.add('https://www.reddit.com/r/Python/comments/6302cj/rpython_official_job_board/')

        term = Terminal(stdscr, config)
        term.set_theme()
        oauth = OAuthHelper(reddit, term, config)
        oauth.authorize()

        theme_list = ThemeList()

        while True:
            term = Terminal(stdscr, config)
            term.set_theme(theme)
            threads = draw_screen(stdscr, reddit, config, theme, oauth)

            try:
                ch = term.show_notification(theme.display_string)
            except KeyboardInterrupt:
                ch = Terminal.ESCAPE

            for thread, term in threads:
                term.pause_getch = False
                thread.join()

            if vcr.record_mode == 'once':
                break
            else:
                cassette.play_counts = Counter()

            theme_list.reload()

            if ch == curses.KEY_RIGHT:
                theme = theme_list.next(theme)
            elif ch == curses.KEY_LEFT:
                theme = theme_list.previous(theme)
            elif ch == Terminal.ESCAPE:
                break
            else:
                # Force the theme to reload
                theme = theme_list.next(theme)
                theme = theme_list.previous(theme)
"""
Initialize an authenticated instance of PRAW to interact with.

$ python -i initialize_session.py
"""
from rtv.docs import AGENT
from rtv.packages import praw
from rtv.content import RequestHeaderRateLimiter
from rtv.config import Config

config = Config()
config.load_refresh_token()

reddit = praw.Reddit(
    user_agent=AGENT.format(version='test_session'),
    decode_html_entities=False,
    disable_update_check=True,
    timeout=10,  # 10 second request timeout
    handler=RequestHeaderRateLimiter())

reddit.set_oauth_app_info(config['oauth_client_id'],
                          config['oauth_client_secret'],
                          config['oauth_redirect_uri'])
reddit.refresh_access_information(config.refresh_token)

inbox = reddit.get_inbox()
items = [next(inbox) for _ in range(20)]
pass