Files
tuir/tests/test_page.py
Michael Lazar a7b789bfd9 Squashed commit of the following:
Updated the supported python versions list.
    Fixed regression in displaying xposts. #173.
    Fixing a few style things.
    Added a more robust test for the tornado handler.
    Trying without pytest-cov
    Updated travis for coverage.
    Remove python 3.2 support because no unicode literals, following what praw supports.
    "Side effect is not iterable."
    Added requirements for travis.
    Renamed travis file correctly.
    Adding test configurations, got tox working.
    Adding vcr cassettes to the repo.
    Renamed requirements files.
    Split up tests and cleaned up test names.
    Tests done, still one failure.
    Treat cassettes as binary to prevent bad merging.
    Fixed a few broken tests.
    Added a timeout to notifications.
    Prepping subreddit page.
    Finished submission page tests.
    Working on submission tests.
    Fixed vcr matching on urls with params, started submission tests.
    Log cleanup.
    Still trying to fix a broken test.
    -Fixed a few pytest bugs and tweaked logging.
    Still working on subscription tests.
    Finished page tests, on to subscription page.
    Finished content tests and starting page tests.
    Added the test refresh-token file to gitignore.
    Moved functional test file out of the repository.
    Continuing work on subreddit content tests.
    Tests now match module names, cassettes are split into individual tests for faster loading.
    Linter fixes.
    Cleanup.
    Added support for nested loaders.
    Added pytest options, starting subreddit content tests.
    Back on track with loader, continuing content tests.
    Finishing submission content tests and discovered snag with loader exception handling.
    VCR up and running, continuing to implement content tests.
    Playing around with vcr.py
    Moved helper functions into terminal and new objects.py
    Fixed a few broken tests.
    Working on navigator tests.
    Reorganizing some things.
    Mocked webbrowser._tryorder for terminal test.
    Completed oauth tests.
    Progress on the oauth tests.
    Working on adding fake tornado request.
    Starting on OAuth tool tests.
    Finished curses helpers tests.
    Still working on curses helpers tests.
    Almost finished with tests on curses helpers.
    Adding tests and working on mocking stdscr.
    Starting to add tests for curses functions.
    Merge branch 'future_work' of https://github.com/michael-lazar/rtv into future_work
    Refactoring controller, still in progress.
    Renamed auth handler.
    Rename CursesHelper to CursesBase.
    Added temporary file with a possible template for func testing.
    Mixup between basename and dirname.
    Merge branch 'future_work' of https://github.com/michael-lazar/rtv into future_work
    py3 compatability for mock.
    Beginning to refactor the curses session.
    Started adding tests, improved unicode handling in the config.
    Cleanup, fixed a few typos.
    Major refactor, almost done!.
    Started a config class.
    Merge branch 'master' into future_work
    The editor now handles unicode characters in all situations.
    Fixed a few typos from previous commits.
    __main__.py formatting.
    Cleaned up history logic and moved to the config file.
2015-12-02 22:37:50 -08:00

126 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import pytest
from rtv.page import Page, PageController, logged_in
try:
from unittest import mock
except ImportError:
import mock
def test_page_logged_in(terminal):
page = mock.MagicMock()
page.term = terminal
@logged_in
def func(page):
raise RuntimeError()
# Logged in runs the function
page.reddit.is_oauth_session.return_value = True
with pytest.raises(RuntimeError):
func(page)
message = 'Not logged in'.encode('utf-8')
with pytest.raises(AssertionError):
terminal.stdscr.subwin.addstr.assert_called_with(1, 1, message)
# Logged out skips the function and displays a message
page.reddit.is_oauth_session.return_value = False
func(page)
message = 'Not logged in'.encode('utf-8')
terminal.stdscr.subwin.addstr.assert_called_with(1, 1, message)
def test_page_unauthenticated(reddit, terminal, config, oauth):
page = Page(reddit, terminal, config, oauth)
page.controller = PageController(page)
with mock.patch.object(page, 'refresh_content'), \
mock.patch.object(page, 'content'), \
mock.patch.object(page, 'nav'), \
mock.patch.object(page, 'draw'):
# Loop
def func(ch):
page.active = False
with mock.patch.object(page, 'controller'):
page.controller.trigger = mock.MagicMock(side_effect=func)
page.loop()
assert page.draw.called
# Quit, confirm
terminal.stdscr.getch.return_value = ord('y')
with mock.patch('sys.exit') as sys_exit:
page.controller.trigger('q')
assert sys_exit.called
# Quit, deny
terminal.stdscr.getch.return_value = terminal.ESCAPE
with mock.patch('sys.exit') as sys_exit:
page.controller.trigger('q')
assert not sys_exit.called
# Force quit
terminal.stdscr.getch.return_value = terminal.ESCAPE
with mock.patch('sys.exit') as sys_exit:
page.controller.trigger('Q')
assert sys_exit.called
# Show help
page.controller.trigger('?')
message = 'Basic Commands'.encode('utf-8')
terminal.stdscr.subwin.addstr.assert_any_call(1, 1, message)
# Sort content
page.controller.trigger('1')
page.refresh_content.assert_called_with(order='hot')
page.controller.trigger('2')
page.refresh_content.assert_called_with(order='top')
page.controller.trigger('3')
page.refresh_content.assert_called_with(order='rising')
page.controller.trigger('4')
page.refresh_content.assert_called_with(order='new')
page.controller.trigger('5')
page.refresh_content.assert_called_with(order='controversial')
logged_in_methods = [
'a', # Upvote
'z', # Downvote
'd', # Delete
'e', # Edit
'i', # Get inbox
]
for ch in logged_in_methods:
page.controller.trigger(ch)
message = 'Not logged in'.encode('utf-8')
terminal.stdscr.subwin.addstr.assert_called_with(1, 1, message)
terminal.stdscr.subwin.addstr.reset_mock()
def test_page_authenticated(reddit, terminal, config, oauth, refresh_token):
page = Page(reddit, terminal, config, oauth)
page.controller = PageController(page)
config.refresh_token = refresh_token
# Login
page.controller.trigger('u')
assert reddit.is_oauth_session()
# Get inbox - Call the real method
page.controller.trigger('i')
# Get inbox - Simulate no new messages
reddit.get_unread = mock.Mock(return_value=[])
page.controller.trigger('i')
message = 'No New Messages'.encode('utf-8')
terminal.stdscr.subwin.addstr.assert_called_with(1, 1, message)
# Logout
terminal.stdscr.getch.return_value = ord('y')
page.controller.trigger('u')
assert not reddit.is_oauth_session()