This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <jose.benito.gonzalez@cern.ch>
- Loading branch information
Showing
7 changed files
with
177 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,7 @@ def run_tests(self): | |
'pytest-pep257', | ||
'pytest', | ||
'coverage', | ||
'webtest', | ||
] | ||
|
||
setup( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |