Skip to content

Commit

Permalink
Reaise code cov
Browse files Browse the repository at this point in the history
- Reordered imports
- Added test_decode_payload_raises_decoded_error_isatty
- Added test_main_throw_exception
- Monkeypatched sys.stdin.isatty and sys.stdin.read
- Monkeypatched argparse.ArgumentParser.parse_args to force exception in __main__.main
  • Loading branch information
Froilan Irizarry committed May 19, 2017
1 parent e34f447 commit 4466782
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

import argparse
import json

import pytest
import sys

import jwt
from jwt.__main__ import build_argparser, decode_payload, encode_payload, main

import pytest


class TestCli:

Expand Down Expand Up @@ -40,6 +39,23 @@ def test_decode_payload_raises_decoded_error(self):

assert 'There was an error decoding the token' in str(excinfo.value)

def test_decode_payload_raises_decoded_error_isatty(self, monkeypatch):
def patched_sys_stdin_read():
raise jwt.DecodeError()

decode_args = ['--key', '1234', 'decode', 'wrong-token']
parser = build_argparser()

args = parser.parse_args(decode_args)

monkeypatch.setattr(sys.stdin, 'isatty', lambda: True)
monkeypatch.setattr(sys.stdin, 'read', patched_sys_stdin_read)

with pytest.raises(jwt.DecodeError) as excinfo:
decode_payload(args)

assert 'There was an error decoding the token' in str(excinfo.value)

@pytest.mark.parametrize('key,name,job,exp,verify', [
('1234', 'Vader', 'Sith', None, None),
('4567', 'Anakin', 'Jedi', '+1', None),
Expand Down Expand Up @@ -98,3 +114,13 @@ def test_main(self, monkeypatch, key, name, job, exp, verify):
args.append('verify={0}'.format(verify))
monkeypatch.setattr(sys, 'argv', args)
main()

def test_main_throw_exception(self, monkeypatch, capsys):
def patched_argparser_parse_args(self, args):
raise Exception('NOOOOOOOOOOO!')

monkeypatch.setattr(argparse.ArgumentParser, 'parse_args', patched_argparser_parse_args)
main()
out, _ = capsys.readouterr()

assert 'NOOOOOOOOOOO!' in out

0 comments on commit 4466782

Please sign in to comment.