initial v3 api implementation and tests

This commit is contained in:
Olivier Yiptong 2015-04-06 16:59:31 -04:00
Родитель f7c11238cb
Коммит 05ed0b4a4a
5 изменённых файлов: 119 добавлений и 7 удалений

Просмотреть файл

@ -72,9 +72,9 @@ def fetch():
except:
country = "ERROR"
localized = current_app.config['LINKS_LOCALIZATIONS'].get("%s/%s" % (country, locale))
localized = current_app.config['LINKS_LOCALIZATIONS'].get("%s/%s" % (country, locale), {}).get('legacy')
if localized is None:
localized = env.config.LINKS_LOCALIZATIONS.get("STAR/%s" % locale)
localized = env.config.LINKS_LOCALIZATIONS.get("STAR/%s" % locale, {}).get('legacy')
if localized:
# 303 hints to the client to always use GET for the redirect

Просмотреть файл

@ -57,9 +57,9 @@ def fetch(locale=None):
except:
country = "STAR"
localized = env.config.LINKS_LOCALIZATIONS.get("%s/%s" % (country, locale))
localized = env.config.LINKS_LOCALIZATIONS.get("%s/%s" % (country, locale), {}).get('legacy')
if localized is None:
localized = env.config.LINKS_LOCALIZATIONS.get("STAR/%s" % locale)
localized = env.config.LINKS_LOCALIZATIONS.get("STAR/%s" % locale, {}).get('legacy')
if localized is not None:
# 303 hints to the client to always use GET for the redirect
@ -85,7 +85,7 @@ def fetch(locale=None):
return response
def handle_ping(ping_type):
def handle_ping(ping_type, api_version="2"):
"""
A ping handler that just logs the data it receives for further processing
in the backend
@ -111,7 +111,7 @@ def handle_ping(ping_type):
"ip": ip_addr,
"ua": ua,
"locale": locale,
"ver": "2",
"ver": api_version,
})
env.statsd.incr("{0}_error".format(ping_type))

Просмотреть файл

@ -4,3 +4,6 @@ def setup_routes(app):
import onyx.api.v2
onyx.api.v2.register_routes(app)
import onyx.api.v3
onyx.api.v3.register_routes(app)

106
tests/api/test_v3.py Normal file
Просмотреть файл

@ -0,0 +1,106 @@
import json
from flask import url_for
from nose.tools import (
assert_equals,
assert_is_none,
)
from tests.base import BaseTestCase
class TestNewtabServing(BaseTestCase):
def test_unknown_locale(self):
"""
A call with an unknown locale yields an HTTP 204 response
"""
response = self.client.get(url_for('v3_links.fetch', locale='zh-CN', channel='beta'),
content_type='application/json',
headers=[("User-Agent", "TestClient")])
assert_equals(response.status_code, 204)
assert_equals(int(response.headers.get('Content-Length')), 0)
def test_unknown_country(self):
"""
A call with an unknown country, but valid locale is success because of STAR
"""
response = self.client.get(url_for('v3_links.fetch', locale='en-US', channel='beta'),
content_type='application/json',
headers=[("User-Agent", "TestClient")],
environ_base={"REMOTE_ADDR": "202.224.135.69"})
assert_equals(response.status_code, 303)
def test_success(self):
"""
A call with an known geo/locale pair redirects
"""
response = self.client.get(url_for('v3_links.fetch', locale='en-US', channel='beta'),
content_type='application/json',
headers=[("User-Agent", "TestClient")],
environ_base={"REMOTE_ADDR": "173.194.43.105"})
assert_equals(response.status_code, 303)
class TestClickPing(BaseTestCase):
def test_missing_payload(self):
"""
A click ping call without a payload errors
"""
response = self.client.post(url_for('v3_links.click'),
content_type='application/json',
headers=[("User-Agent", "TestClient")])
assert_equals(response.status_code, 400)
def test_empty_payload(self):
"""
A click ping call with an empty payload should pass
"""
response = self.client.post(url_for('v3_links.click'),
content_type='application/json',
headers=[("User-Agent", "TestClient")],
data=json.dumps({}))
assert_equals(response.status_code, 200)
def test_payload_meta(self):
"""
A click ping succeeds
"""
response = self.client.post(url_for('v3_links.click'),
content_type='application/json',
headers=[("User-Agent", "TestClient")],
data=json.dumps({"data": "test"}))
assert_equals(response.status_code, 200)
assert_equals(int(response.headers.get('Content-Length')), 0)
class TestViewPing(BaseTestCase):
def test_missing_payload(self):
"""
A view ping call without a payload errors
"""
response = self.client.post(url_for('v3_links.view'),
content_type='application/json',
headers=[("User-Agent", "TestClient")])
assert_equals(response.status_code, 400)
def test_junk_payload(self):
"""
A view ping with valid json, but illegal payload (not a dict) errors
"""
response = self.client.post(url_for('v3_links.view'),
content_type='application/json',
headers=[("User-Agent", "TestClient")],
data='"hfdsfdsjkl"')
assert_equals(response.status_code, 400)
def test_payload_meta(self):
"""
A view ping succeeds
"""
response = self.client.post(url_for('v3_links.view'),
content_type='application/json',
headers=[("User-Agent", "TestClient")],
data=json.dumps({"data": "test"}))
assert_equals(response.status_code, 200)
assert_equals(int(response.headers.get('Content-Length')), 0)

Просмотреть файл

@ -11,6 +11,9 @@ class BaseTestCase(TestCase):
self.app = environment_manager_create()
env = Environment.instance()
env.config.LINKS_LOCALIZATIONS = {
"STAR/en-US": "http://valid.url.com"
'STAR/en-US': {
'legacy': 'http://valid.url.com',
'ag': 'http://valid.url.again.com',
}
}
return self.app