Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
tests: add test cases for restful api
Browse files Browse the repository at this point in the history
* Refactores tests in order to share base functionality. (closes #6)

* Uses webtest library to test restful api.

Signed-off-by: Jose Benito Gonzalez Lopez <jose.benito.gonzalez@cern.ch>
  • Loading branch information
jbenito3 committed Aug 27, 2015
1 parent a388979 commit 1b5ebdf
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ language: python
python:
- "3.4"

addons:
postgresql: "9.4"

env:
- SQLALCHEMY_DATABASE_URI=postgres://postgres@localhost:5432/postgres

cache:
- pip

Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def run_tests(self):
'pytest-pep257',
'pytest',
'coverage',
'webtest',
]

setup(
Expand Down
21 changes: 21 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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."""
41 changes: 41 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 3 additions & 14 deletions tests/test_core_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()`."""
Expand Down
103 changes: 103 additions & 0 deletions tests/test_restful_api.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 1b5ebdf

Please sign in to comment.