mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-04-28 17:32:38 +02:00
manage commands testing (#313)
* Rename manage.ConfigTemplate → manage.GenerateConfig To be consistent with the CLI name: `generate-config`. * Add tests for manage.py commands * Run tests from pip-installed package To be able to detect packaging-related issues on test runs. refs #305
This commit is contained in:
parent
3b2e11ab63
commit
2019b398f1
4 changed files with 43 additions and 6 deletions
|
@ -3,3 +3,4 @@ tox
|
||||||
pytest
|
pytest
|
||||||
Flask-Testing
|
Flask-Testing
|
||||||
Flake8
|
Flake8
|
||||||
|
mock; python_version < '3.3'
|
||||||
|
|
|
@ -4,7 +4,7 @@ import os
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
from getpass import getpass
|
import getpass
|
||||||
|
|
||||||
from flask_script import Manager, Command, Option
|
from flask_script import Manager, Command, Option
|
||||||
from flask_migrate import Migrate, MigrateCommand
|
from flask_migrate import Migrate, MigrateCommand
|
||||||
|
@ -20,11 +20,11 @@ class GeneratePasswordHash(Command):
|
||||||
"""Get password from user and hash it without printing it in clear text."""
|
"""Get password from user and hash it without printing it in clear text."""
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
password = getpass(prompt='Password: ')
|
password = getpass.getpass(prompt='Password: ')
|
||||||
print(generate_password_hash(password))
|
print(generate_password_hash(password))
|
||||||
|
|
||||||
|
|
||||||
class ConfigTemplate(Command):
|
class GenerateConfig(Command):
|
||||||
def get_options(self):
|
def get_options(self):
|
||||||
return [
|
return [
|
||||||
Option('config_file', choices=[
|
Option('config_file', choices=[
|
||||||
|
@ -74,7 +74,7 @@ def main():
|
||||||
manager = Manager(app)
|
manager = Manager(app)
|
||||||
manager.add_command('db', MigrateCommand)
|
manager.add_command('db', MigrateCommand)
|
||||||
manager.add_command('generate_password_hash', GeneratePasswordHash)
|
manager.add_command('generate_password_hash', GeneratePasswordHash)
|
||||||
manager.add_command('generate-config', ConfigTemplate)
|
manager.add_command('generate-config', GenerateConfig)
|
||||||
manager.run()
|
manager.run()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,10 @@ try:
|
||||||
import unittest2 as unittest
|
import unittest2 as unittest
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import unittest # NOQA
|
import unittest # NOQA
|
||||||
|
try:
|
||||||
|
from unittest.mock import patch
|
||||||
|
except ImportError:
|
||||||
|
from mock import patch
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
@ -16,6 +20,7 @@ from flask import session
|
||||||
from flask_testing import TestCase
|
from flask_testing import TestCase
|
||||||
|
|
||||||
from ihatemoney.run import create_app, db, load_configuration
|
from ihatemoney.run import create_app, db, load_configuration
|
||||||
|
from ihatemoney.manage import GenerateConfig, GeneratePasswordHash
|
||||||
from ihatemoney import models
|
from ihatemoney import models
|
||||||
from ihatemoney import utils
|
from ihatemoney import utils
|
||||||
|
|
||||||
|
@ -1406,5 +1411,27 @@ class ServerTestCase(IhatemoneyTestCase):
|
||||||
self.assertStatus(200, req)
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
13
tox.ini
13
tox.ini
|
@ -6,22 +6,31 @@ skip_missing_interpreters = True
|
||||||
|
|
||||||
commands =
|
commands =
|
||||||
python --version
|
python --version
|
||||||
py.test ihatemoney/tests/tests.py
|
py.test --pyargs ihatemoney.tests.tests
|
||||||
|
|
||||||
deps =
|
deps =
|
||||||
-rdev-requirements.txt
|
-rdev-requirements.txt
|
||||||
-rrequirements.txt
|
-rrequirements.txt
|
||||||
|
|
||||||
install_command = pip install --pre {opts} {packages}
|
# To be sure we are importing ihatemoney pkg from pip-installed version
|
||||||
|
changedir = /tmp
|
||||||
|
|
||||||
|
install_command =
|
||||||
|
pip install --pre {opts} {packages}
|
||||||
|
pip install .
|
||||||
|
|
||||||
|
|
||||||
[testenv:docs]
|
[testenv:docs]
|
||||||
commands = sphinx-build -a -n -b html -d docs/_build/doctrees docs docs/_build/html
|
commands = sphinx-build -a -n -b html -d docs/_build/doctrees docs docs/_build/html
|
||||||
deps =
|
deps =
|
||||||
-rdocs/requirements.txt
|
-rdocs/requirements.txt
|
||||||
|
changedir = {toxinidir}
|
||||||
|
|
||||||
[testenv:lint]
|
[testenv:lint]
|
||||||
commands = flake8 ihatemoney
|
commands = flake8 ihatemoney
|
||||||
deps =
|
deps =
|
||||||
-rdev-requirements.txt
|
-rdev-requirements.txt
|
||||||
|
changedir = {toxinidir}
|
||||||
|
|
||||||
[flake8]
|
[flake8]
|
||||||
exclude = migrations
|
exclude = migrations
|
||||||
|
|
Loading…
Reference in a new issue