Skip to content

Commit e185458

Browse files
cboraChristian
authored andcommitted
adding tests
1 parent 460d0dd commit e185458

File tree

8 files changed

+140
-9
lines changed

8 files changed

+140
-9
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.pyc
2+
*.egg-info
3+
*~
4+
ENV

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: python
2+
python:
3+
- "2.7"
4+
5+
install:
6+
- pip install -e . --process-dependency-links
7+
8+
script:
9+
- tests/run_test.sh

recastrestapi/apicli.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ def server(config):
1414
from server import app
1515
port = int(os.environ.get("PORT", 5000))
1616
app.run(host='0.0.0.0', port=port)
17+
18+
@apicli.command()
19+
@click.option('--config', '-c')
20+
def test(config):
21+
if config:
22+
os.environ['RECASTCONTROLCENTER_CONFIG'] = config
23+
import unittest
24+
tests = unittest.TestLoader().discover('tests')
25+
unittest.TextTestRunner(verbosity=2).run(tests)

recastrestapi/server.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
from eve_sqlalchemy.decorators import registerSchema
99
from sqlalchemy.orm.exc import NoResultFound
1010
from sqlalchemy.orm.exc import MultipleResultsFound
11+
from eve_docs import eve_docs
12+
from flask.ext.bootstrap import Bootstrap
1113

1214
from recastrestapi.apiconfig import config as apiconf
13-
from settings import DOMAIN, SQLALCHEMY_DATABASE_URI, DEBUG, XML, JSON
15+
from settings import DOMAIN, SQLALCHEMY_DATABASE_URI, DEBUG, XML, JSON, RESOURCE_METHODS, PUBLIC_ITEM_METHODS, HATEOAS, IF_MATCH, LAST_UPDATED, DATE_CREATED, ID_FIELD, ITEM_LOOKUP_FIELD
1416

1517

1618
class TokenAuth(TokenAuth):
@@ -32,10 +34,18 @@ def check_auth(self, token, allowed_roles, resource, method):
3234
'DEBUG': DEBUG,
3335
'XML': XML,
3436
'JSON': JSON,
37+
'RESOURCE_METHODS': RESOURCE_METHODS,
38+
'PUBLIC_ITEM_METHODS': PUBLIC_ITEM_METHODS,
39+
'HATEOAS': HATEOAS,
40+
'IF_MATCH': IF_MATCH,
41+
'LAST_UPDATED': LAST_UPDATED,
42+
'DATE_CREATED': DATE_CREATED,
43+
'ID_FIELD': ID_FIELD,
44+
'ITEM_LOOKUP_FIELD': ITEM_LOOKUP_FIELD,
3545
}
3646

3747
app = Eve(auth=TokenAuth, settings=SETTINGS, validator=ValidatorSQL, data=SQL)
38-
48+
app.register_blueprint(eve_docs, url_prefix='/docs')
3949
Base = recastdb.database.db.Model
4050

4151
#bind SQLAlchemy

recastrestapi/settings.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,28 @@
22
from recastdb.database import db
33

44
from eve_sqlalchemy.decorators import registerSchema
5-
5+
from eve.utils import config
66
from recastrestapi.apiconfig import config as apiconf
77

88

9+
DEBUG = True
10+
11+
SQLALCHEMY_DATABASE_URI = apiconf['DBPATH']
12+
RESOURCE_METHODS = ['GET', 'POST', 'DELETE']
13+
PUBLIC_ITEM_METHODS = ['GET', 'POST']
14+
HATEOAS = True
15+
IF_MATCH = False
16+
LAST_UPDATED = '_updated'
17+
DATE_CREATED = '_created'
18+
19+
ID_FIELD = 'id'
20+
ITEM_LOOKUP_FIELD = ID_FIELD
21+
config.ID_FIELD = ID_FIELD
22+
config.ITEM_LOOKUP_FIELD = ID_FIELD
23+
24+
XML = True
25+
JSON = True
26+
927
registerSchema('users')(recastdb.models.User)
1028
registerSchema('analysis')(recastdb.models.Analysis)
1129
registerSchema('subscriptions')(recastdb.models.Subscription)
@@ -21,13 +39,8 @@
2139
registerSchema('point_responses')(recastdb.models.PointResponse)
2240
registerSchema('basic_responses')(recastdb.models.BasicResponse)
2341
registerSchema('histograms')(recastdb.models.Histogram)
42+
registerSchema('access_tokens')(recastdb.models.AccessToken)
2443

25-
DEBUG = True
26-
27-
SQLALCHEMY_DATABASE_URI = apiconf['DBPATH']
28-
29-
XML = True
30-
JSON = False
3144

3245
DOMAIN = {
3346
'users': recastdb.models.User._eve_schema['users'],
@@ -45,6 +58,7 @@
4558
'point_responses': recastdb.models.PointResponse._eve_schema['point_responses'],
4659
'basic_responses': recastdb.models.BasicResponse._eve_schema['basic_responses'],
4760
'histograms': recastdb.models.Histogram._eve_schema['histograms'],
61+
'access_tokens': recastdb.models.AccessToken._eve_schema['access_tokens'],
4862
}
4963

5064
DOMAIN['users'].update({
@@ -55,6 +69,30 @@
5569
},
5670
'cache_control': 'max-age=10,must-revalidate',
5771
'cache_expires': 10,
72+
'resource_methods': ['GET', 'POST', 'DELETE'],
73+
'item_methods': ['GET', 'PUT', 'PATCH']
74+
})
75+
76+
DOMAIN['analysis'].update({
77+
'item_lookup_field': 'id',
78+
'additional_lookup': {
79+
'url': 'regex("[\w]+")',
80+
'field': 'collaboration'
81+
},
82+
'cache_control': 'max-age=10,must-revalidate',
83+
'cache_expires': 10,
5884
'resource_methods': ['GET', 'POST', 'DELETE']
5985
})
6086

87+
DOMAIN['run_conditions'].update({
88+
'item_lookup_field': 'id',
89+
'additional_lookup': {
90+
'url': 'regex("[0-9]+")',
91+
'field': 'id'
92+
},
93+
'cache_control': 'max-age=10,must-revalidate',
94+
'cache_expires': 10,
95+
'resource_methods': ['GET', 'POST', 'DELETE'],
96+
'item_methods': ['GET', 'PUT', 'PATCH']
97+
})
98+

tests/__init__.py

Whitespace-only changes.

tests/run_test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
recast-api test

tests/test.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import unittest
2+
import json
3+
from recastrestapi.server import app
4+
from recastrestapi.server import db
5+
from recastdb.models import AccessToken, User
6+
import random
7+
import string
8+
9+
class APITestCase(unittest.TestCase):
10+
def setUp(self):
11+
self.app = app
12+
self.app.app_context().push()
13+
db.create_all()
14+
self.client = self.app.test_client()
15+
self.token = self.random_string()
16+
17+
def tearDown(self):
18+
db.session.remove()
19+
#self.app.app_context().pop()
20+
21+
def test_add_token(self):
22+
test_user = User(name="Rest API test user", email="testapi@email.com")
23+
db.session.add(test_user)
24+
db.session.commit()
25+
token = AccessToken(token=self.token, user_id=test_user.id)
26+
db.session.add(token)
27+
db.session.commit()
28+
29+
def get_api_header(self, token="test_token"):
30+
return {
31+
'Authorization': 'Basic ' + token,
32+
'Accept': 'application/json',
33+
'Content-Type': 'application/json'
34+
}
35+
36+
def test_404(self):
37+
response = self.client.get(
38+
'/wrong/url',
39+
headers=self.get_api_header())
40+
self.assertTrue(response.status_code == 404)
41+
json_response = json.loads(response.data.decode('utf-8'))
42+
print json_response['_error']['code']
43+
self.assertEquals(json_response['_error']['code'],404)
44+
45+
46+
def test_token_auth(self):
47+
pass
48+
49+
def test_GET(self):
50+
pass
51+
52+
def test_POST(self):
53+
pass
54+
55+
def random_string(self, size=10, chars=string.ascii_uppercase + string.digits):
56+
return ''.join(random.choice(chars) for _ in range(size))
57+
58+

0 commit comments

Comments
 (0)