-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:geekoala/whatsnews
- Loading branch information
Showing
41 changed files
with
542 additions
and
136 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
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,10 @@ | ||
## Authorization API | ||
|
||
---- | ||
|
||
### Run test | ||
```shell | ||
AUTH_SERVICE_URL=http://example.com:8000 nosetests | ||
``` | ||
where AUTH_SERVICE_URL is only an URL for mock auth service, so it can be a | ||
arbitrary URL. |
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 @@ | ||
from authapi.service import application |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
Author: Wenhua Yang | ||
Date: 9/23/16 | ||
""" | ||
|
||
from flask import Flask, jsonify | ||
from flask_loopback.flask_loopback import FlaskLoopback | ||
from urllib.parse import urlparse | ||
|
||
from authapi import application | ||
|
||
auth_service_url = application.config['AUTH_SERVICE_URL'] | ||
|
||
auth_app = Flask(__name__) | ||
|
||
|
||
@auth_app.route("/token/<client_id>/<secret>") | ||
def get_token(client_id, secret): | ||
if client_id == 'ID123456' and secret == '123456': | ||
ret = {'token': 'TK123456', 'expires_in': 86400} | ||
else: | ||
ret = {'error_code': 3001, 'error_msg': 'invalid client_id or secret'} | ||
return jsonify(ret) | ||
|
||
|
||
# TODO: try to reuse this MockServer, it is duplicated with code in search_api | ||
class MockServer(object): | ||
def __init__(self, app, host, port): | ||
self.app = app | ||
self.host = host | ||
self.port = port | ||
self.mock_server = FlaskLoopback(app) | ||
|
||
def get_server(self): | ||
return self.mock_server.on((self.host, self.port)) | ||
|
||
|
||
# TODO: try to reuse this code, it is duplicated with code in search_api | ||
def parse_host_port(url): | ||
netloc = urlparse(url).netloc | ||
if ':' not in netloc: | ||
netloc = '{}:80'.format(netloc) | ||
_host, _port = netloc.split(':') | ||
_port = int(_port) | ||
return _host, _port | ||
|
||
|
||
# TODO: try to reuse this code, it is duplicated with code in search_api | ||
# this decorator not work for nosetests | ||
def use_mock(mock_server): | ||
def mock_decorator(func): | ||
def wrapper(*args, **kwargs): | ||
with mock_server.get_server(): | ||
func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return mock_decorator | ||
|
||
|
||
auth_host, auth_port = parse_host_port(auth_service_url) | ||
auth_mock = MockServer(auth_app, auth_host, auth_port) | ||
|
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,36 @@ | ||
""" | ||
Author: Wenhua Yang | ||
Date: 9/23/16 | ||
Test cases for auth API. | ||
""" | ||
|
||
import json | ||
import unittest | ||
|
||
from authapi import application | ||
from authapi.tests import auth_mock | ||
|
||
|
||
class TestAuthAPI(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
application.testing = True | ||
cls.app_client = application.test_client() | ||
|
||
def test_get_token(self): | ||
# TODO: use_mock decorator not work for nosetests | ||
with auth_mock.get_server(): | ||
resp = self.app_client.get('/token/ID123456/123456') | ||
result = json.loads(resp.get_data(as_text=True)) | ||
self.assertEqual(result.get('token'), 'TK123456') | ||
|
||
resp = self.app_client.get('/token/ID123456/xxx') | ||
result = json.loads(resp.get_data(as_text=True)) | ||
self.assertEqual(result.get('error_code'), 3001) | ||
|
||
resp = self.app_client.get('/token/xxx/123456') | ||
result = json.loads(resp.get_data(as_text=True)) | ||
self.assertEqual(result.get('error_code'), 3001) | ||
|
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,8 @@ | ||
## Authorization Service | ||
|
||
---- | ||
|
||
### Run test | ||
```shell | ||
nosetests | ||
``` |
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 @@ | ||
from authsvr.service import application |
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
Empty file.
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,45 @@ | ||
""" | ||
Author: Wenhua Yang | ||
Date: 9/23/16 | ||
Test cases for auth service. | ||
""" | ||
|
||
import json | ||
import unittest | ||
|
||
from authsvr import application | ||
|
||
|
||
class TestAuthService(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
application.testing = True | ||
cls.app_client = application.test_client() | ||
|
||
def test_fetch_token(self): | ||
resp = self.app_client.get('/token/ID123456/123456') | ||
result = json.loads(resp.get_data(as_text=True)) | ||
self.assertEqual(result.get('token'), 'TK123456') | ||
|
||
resp = self.app_client.get('/token/ID-Invalid/123456') | ||
result = json.loads(resp.get_data(as_text=True)) | ||
self.assertEqual(result.get('error_code'), 3001) | ||
|
||
resp = self.app_client.get('/token/ID123456/xxx') | ||
result = json.loads(resp.get_data(as_text=True)) | ||
self.assertEqual(result.get('error_code'), 3001) | ||
|
||
def test_verify_token(self): | ||
resp = self.app_client.get('/verify/ID123456/TK123456') | ||
result = json.loads(resp.get_data(as_text=True)) | ||
self.assertEqual(result.get('valid'), True) | ||
|
||
resp = self.app_client.get('/verify/ID123456/xxx') | ||
result = json.loads(resp.get_data(as_text=True)) | ||
self.assertTrue(result.get('error_code'), 3002) | ||
|
||
resp = self.app_client.get('/verify/xxx/TK123456') | ||
result = json.loads(resp.get_data(as_text=True)) | ||
self.assertTrue(result.get('error_code'), 3002) |
Oops, something went wrong.