Add tests for manage.py commands

This commit is contained in:
Jocelyn Delalande 2018-01-06 02:53:06 +01:00
parent 7e60354c14
commit 9bc1a3f09f
3 changed files with 30 additions and 2 deletions

View file

@ -3,3 +3,4 @@ tox
pytest
Flask-Testing
Flake8
mock; python_version < '3.3'

View file

@ -4,7 +4,7 @@ import os
import pkgutil
import random
import sys
from getpass import getpass
import getpass
from flask_script import Manager, Command, Option
from flask_migrate import Migrate, MigrateCommand
@ -20,7 +20,7 @@ class GeneratePasswordHash(Command):
"""Get password from user and hash it without printing it in clear text."""
def run(self):
password = getpass(prompt='Password: ')
password = getpass.getpass(prompt='Password: ')
print(generate_password_hash(password))

View file

@ -4,6 +4,10 @@ try:
import unittest2 as unittest
except ImportError:
import unittest # NOQA
try:
from unittest.mock import patch
except ImportError:
from mock import patch
import os
import json
@ -16,6 +20,7 @@ from flask import session
from flask_testing import TestCase
from ihatemoney.run import create_app, db, load_configuration
from ihatemoney.manage import GenerateConfig, GeneratePasswordHash
from ihatemoney import models
from ihatemoney import utils
@ -1406,5 +1411,27 @@ class ServerTestCase(IhatemoneyTestCase):
self.assertStatus(200, req)
class CommandTestCase(BaseTestCase):
def test_generate_config(self):
""" Simply checks that all config file generation
- raise no exception
- produce something non-empty
"""
cmd = GenerateConfig()
for config_file in cmd.get_options()[0].kwargs['choices']:
with patch('sys.stdout', new=six.StringIO()) as stdout:
cmd.run(config_file)
print(stdout.getvalue())
self.assertNotEqual(len(stdout.getvalue().strip()), 0)
def test_generate_password_hash(self):
cmd = GeneratePasswordHash()
with patch('sys.stdout', new=six.StringIO()) as stdout, \
patch('getpass.getpass', new=lambda prompt: 'secret'): # NOQA
cmd.run()
print(stdout.getvalue())
self.assertEqual(len(stdout.getvalue().strip()), 187)
if __name__ == "__main__":
unittest.main()