Skip to content

Commit

Permalink
fix: Jupyter support for QRCode auth (Closes #36)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreroggeri committed Apr 9, 2019
1 parent 763e63d commit 439da44
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
7 changes: 3 additions & 4 deletions pynubank/nubank.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import uuid

import requests
from qrcode import QRCode

from pynubank import utils


class NuException(BaseException):
Expand Down Expand Up @@ -60,9 +61,7 @@ def _qr_code_auth(self):
print('Scan the QRCode below with you Nubank application on the following menu:')
print('Nu(Seu Nome) > Perfil > Acesso pelo site')
content = uuid.uuid4()
qr = QRCode()
qr.add_data(content)
qr.print_ascii(invert=True)
utils.print_qr_code(str(content))
input('After the scan, press enter do proceed')
payload = {
'qr_code_id': str(content),
Expand Down
12 changes: 12 additions & 0 deletions pynubank/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from qrcode import QRCode


def print_qr_code(uuid: str):
qr = QRCode()
qr.add_data(uuid)
try:
get_ipython
from IPython.display import display
display(qr.make_image(fill_color="#111", back_color="#ccc"))
except NameError:
qr.print_ascii(invert=True)
3 changes: 2 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest==3.2.2
pytest-cov==2.5.1
python-coveralls==2.9.1
python-coveralls==2.9.1
pillow==6.0.0
31 changes: 31 additions & 0 deletions tests/test_nubank_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import builtins

import pytest
from qrcode import QRCode

from pynubank import utils


@pytest.mark.skip(reason="no way of currently testing this")
def test_print_qr_code_on_jupyter_prints_image(monkeypatch):
def fake_print(x):
raise Exception('Should not call print')

def fake_ipython():
pass

try:
builtins.get_ipython = fake_ipython

monkeypatch.setattr('builtins.print', fake_print)
utils.print_qr_code('some-uuid-1234')
finally:
del (builtins.get_ipython)


def test_print_qr_code_on_terminal_prints_ascii():
def fake_make_image(self, image_factory=None, **kwargs):
raise Exception('Should not call make_image')

QRCode.make_image = fake_make_image
utils.print_qr_code('some-uuid-1234')

0 comments on commit 439da44

Please sign in to comment.