Skip to content

Commit

Permalink
tests: addition of test_examples_app.py
Browse files Browse the repository at this point in the history
* Adds test_examples_app.py. (closes #55)

Signed-off-by: Leonardo Rossi <leonardo.r@cern.ch>
  • Loading branch information
Leonardo Rossi committed Sep 21, 2016
1 parent 7fb7654 commit 8a987db
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 25 deletions.
9 changes: 1 addition & 8 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ recursive-include docs *.bat
recursive-include docs *.py
recursive-include docs *.rst
recursive-include docs Makefile
recursive-include examples *.bowerrc
recursive-include examples *.html
recursive-include examples *.json
recursive-include examples *.py
recursive-include instance *.db
recursive-include examples *.py *.sh .json .html *.txt
recursive-include invenio_userprofiles *.html
recursive-include misc *.py
recursive-include misc *.rst
recursive-include tests *.py
recursive-include examples *.txt
16 changes: 16 additions & 0 deletions examples/app-fixtures.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

# quit on errors:
set -o errexit

# quit on unbound symbols:
set -o nounset

DIR=`dirname "$0"`

cd $DIR
export FLASK_APP=app.py

# Create the users
flask users create -a info@inveniosoftware.org --password 123456
flask users create -a another@inveniosoftware.org --password 123456
27 changes: 27 additions & 0 deletions examples/app-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

# quit on errors:
set -o errexit

# quit on unbound symbols:
set -o nounset

DIR=`dirname "$0"`

cd $DIR
export FLASK_APP=app.py
mkdir $DIR/instance

# Install specific dependencies
pip install -r requirements.txt

# Preapare all static files:
npm install -g node-sass clean-css requirejs uglify-js
flask npm
cd static ; npm install ; cd ..
flask collect -v
flask assets build

# Create the database
flask db init
flask db create
11 changes: 11 additions & 0 deletions examples/app-teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

DIR=`dirname "$0"`

cd $DIR
export FLASK_APP=app.py

# clean environment
[ -e "$DIR/instance" ] && rm $DIR/instance -Rf
[ -e "$DIR/static" ] && rm $DIR/static/ -Rf
[ -e "$DIR/cookiefile" ] && rm $DIR/cookiefile -Rf
29 changes: 12 additions & 17 deletions examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,29 @@

"""Minimal Flask application example for development.
Install the Invenio default theme
Start the Redis server.
You should execute these commands in the examples directory.
Install the Invenio default theme and build assets:
.. code-block:: console
$ pip install -r requirements.txt
$ flask -a app.py npm
$ cd static
$ npm install
$ cd ..
$ flask -a app.py collect -v
$ flask -a app.py assets build
$ pip install -e .[all]
$ cd examples
$ ./app-setup.sh
$ ./app-fixtures.sh
Create the database and add a user:
Run the development server:
.. code-block:: console
$ mkdir instance
$ flask -a app.py db init
$ flask -a app.py db create
$ flask -a app.py users create info@inveniosoftware.org -a
$ flask -a app.py users create another@inveniosoftware.org -a
$ FLASK_APP=app.py flask run --debugger -p 5000
Run the development server:
To be able to uninstall the example app:
.. code-block:: console
$ flask -a app.py --debug run
$ ./app-teardown.sh
"""

from __future__ import absolute_import, print_function
Expand Down Expand Up @@ -96,6 +90,7 @@
MAIL_SUPPRESS_SEND=True,
SECRET_KEY='CHANGE_ME',
SQLALCHEMY_TRACK_MODIFICATIONS=True,
WTF_CSRF_ENABLED=False,
)
Babel(app)
InvenioMail(app)
Expand Down
78 changes: 78 additions & 0 deletions tests/test_examples_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Test example app."""

import os
import signal
import subprocess
import time

import pytest


@pytest.yield_fixture
def example_app():
"""Example app fixture."""
current_dir = os.getcwd()
# go to example directory
project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
exampleappdir = os.path.join(project_dir, 'examples')
os.chdir(exampleappdir)
# setup example
cmd = './app-setup.sh'
exit_status = subprocess.call(cmd, shell=True)
assert exit_status == 0
# Starting example web app
cmd = 'FLASK_APP=app.py flask run --debugger -p 5000 -h 0.0.0.0'
webapp = subprocess.Popen(cmd, stdout=subprocess.PIPE,
preexec_fn=os.setsid, shell=True)
time.sleep(10)
# return webapp
yield webapp
# stop server
os.killpg(webapp.pid, signal.SIGTERM)
# tear down example app
cmd = './app-teardown.sh'
subprocess.call(cmd, shell=True)
# return to the original directory
os.chdir(current_dir)


def test_example_app(example_app):
"""Test example app."""
# load fixtures
cmd = './app-fixtures.sh'
exit_status = subprocess.call(cmd, shell=True)
assert exit_status == 0
# open page
email = 'info@inveniosoftware.org'
password = '123456'
cmd = """
curl -c cookiefile http://0.0.0.0:5000/login/ -d \
'csrf_token=None&next=&email={0}&password={1}' && \
curl -b cookiefile http://0.0.0.0:5000/
""".format(email, password)
output = subprocess.check_output(cmd, shell=True).decode('utf-8')
assert 'id="profile-username"' in output

0 comments on commit 8a987db

Please sign in to comment.