From 1b5ebdf6feb9934e6152e2095690a0e0039ff29e Mon Sep 17 00:00:00 2001 From: Jose Benito Gonzalez Lopez Date: Tue, 25 Aug 2015 15:46:31 +0200 Subject: [PATCH] tests: add test cases for restful api * Refactores tests in order to share base functionality. (closes #6) * Uses webtest library to test restful api. Signed-off-by: Jose Benito Gonzalez Lopez --- .travis.yml | 6 +++ Dockerfile | 3 +- setup.py | 1 + tests/__init__.py | 21 ++++++++ tests/base.py | 41 +++++++++++++++ tests/test_core_json.py | 17 ++----- tests/test_restful_api.py | 103 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 177 insertions(+), 15 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/base.py create mode 100644 tests/test_restful_api.py diff --git a/.travis.yml b/.travis.yml index 422d101..f35715d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,6 +28,12 @@ language: python python: - "3.4" +addons: + postgresql: "9.4" + +env: + - SQLALCHEMY_DATABASE_URI=postgres://postgres@localhost:5432/postgres + cache: - pip diff --git a/Dockerfile b/Dockerfile index 54dcb1a..5b92966 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,7 +35,8 @@ RUN pip install flask \ pytest-pep8 \ pytest-pep257 \ pytz \ - sphinx + sphinx \ + webtest # Add sources to `code` and work there: WORKDIR /code diff --git a/setup.py b/setup.py index 565688c..07b9bc1 100644 --- a/setup.py +++ b/setup.py @@ -75,6 +75,7 @@ def run_tests(self): 'pytest-pep257', 'pytest', 'coverage', + 'webtest', ] setup( diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..78530cd --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# +# This file is part of ClaimStore. +# Copyright (C) 2015 CERN. +# +# ClaimStore 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. +# +# ClaimStore 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 ClaimStore; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, +# USA. + +"""Test package.""" diff --git a/tests/base.py b/tests/base.py new file mode 100644 index 0000000..125eef7 --- /dev/null +++ b/tests/base.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +# +# This file is part of ClaimStore. +# Copyright (C) 2015 CERN. +# +# ClaimStore 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. +# +# ClaimStore 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 ClaimStore; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, +# USA. + +"""Base test class.""" + +from unittest import TestCase + +from claimstore.app import create_app, db + + +class ClaimStoreTestCase(TestCase): + + """Testing claimstore.core.json.""" + + def setUp(self): + """Set up.""" + self.app = create_app(db_create_all=True) + + def tearDown(self): + """Tear down.""" + with self.app.app_context(): + db.session.remove() + db.drop_all() + self.app = None diff --git a/tests/test_core_json.py b/tests/test_core_json.py index 5ff5500..f6f8928 100644 --- a/tests/test_core_json.py +++ b/tests/test_core_json.py @@ -20,25 +20,14 @@ """claimstore.core.json test suite.""" -import os -from unittest import TestCase - -from claimstore.app import create_app from claimstore.core.json import get_json_schema +from .base import ClaimStoreTestCase -class FlaskTestCase(TestCase): - - """Testing claimstore.core.json.""" - def setUp(self): - """Set up.""" - os.environ['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' - self.app = create_app() +class FlaskTestCase(ClaimStoreTestCase): - def tearDown(self): - """Tear down.""" - self.app = None + """Testing claimstore.core.json.""" def test_get_json_schema(self): """Testing `get_json_schema()`.""" diff --git a/tests/test_restful_api.py b/tests/test_restful_api.py new file mode 100644 index 0000000..2b70da3 --- /dev/null +++ b/tests/test_restful_api.py @@ -0,0 +1,103 @@ +# -*- coding: utf-8 -*- +# +# This file is part of ClaimStore. +# Copyright (C) 2015 CERN. +# +# ClaimStore 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. +# +# ClaimStore 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 ClaimStore; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, +# USA. + +"""claimstore.modules.claims.restful test suite.""" + +import json +import os + +import pytest # isort:skip +from webtest import TestApp # isort:skip + +from .base import ClaimStoreTestCase # isort:skip + + +@pytest.fixture(scope='module') +def claimant_example(app): + """Fixture that returns a JSON example of a claimant.""" + with open(os.path.join( + app.config['BASE_DIR'], + 'claimstore', + 'static', + 'json', + 'examples', + 'claimant.cds.json' + )) as f: + return json.loads(f.read()) + + +@pytest.fixture(scope='module') +def claim_example(app): + """Fixture that returns a JSON example of a claim.""" + with open(os.path.join( + app.config['BASE_DIR'], + 'claimstore', + 'static', + 'json', + 'examples', + 'claim.cds.1.json' + )) as f: + return json.loads(f.read()) + + +class RestfulAPITestCase(ClaimStoreTestCase): + + """Testing claimstore.modules.claims.restful.""" + + def setUp(self): + """Set up.""" + super(RestfulAPITestCase, self).setUp() + + with self.app.app_context(): + self.test_app = TestApp(self.app) + + def test_submit_claimant(self): + """Testing `subscribe` api.""" + resp = self.test_app.post_json( + '/subscribe', + claimant_example(self.app) + ) + self.assertEqual(resp.status_code, 200) + + def test_submit_claim(self): + """Testing POST to `claims` api.""" + # Firstly we need a claimant, so the claim submission should fail. + resp = self.test_app.post_json( + '/claims', + claim_example(self.app), + expect_errors=True + ) + self.assertEqual(resp.status_code, 400) + + # Test when there is a claimant. + resp = self.test_app.post_json( + '/subscribe', + claimant_example(self.app) + ) + resp = self.test_app.post_json( + '/claims', + claim_example(self.app) + ) + self.assertEqual(resp.status_code, 200) + + def test_get_claims(self): + """Testing GET claims api.""" + resp = self.test_app.get('/claims') + self.assertEqual(resp.status_code, 200)