Commit 9034de54 by Clinton Blackburn Committed by Clinton Blackburn

Added acceptance tests for API gateway

ECOM-3957
parent 71812e34
.DEFAULT_GOAL := test
.PHONY: clean compile_translations dummy_translations extract_translations fake_translations help html_coverage \
.PHONY: accept clean compile_translations dummy_translations extract_translations fake_translations help html_coverage \
migrate pull_translations push_translations quality requirements production-requirements test \
update_translations validate
help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " help display this help message"
@echo " make accept run acceptance tests"
@echo " clean delete generated byte code and coverage reports"
@echo " compile_translations compile translation files, outputting .po files for each supported language"
@echo " dummy_translations generate dummy translation (.po) files"
......@@ -48,8 +49,8 @@ test: clean
coverage report
quality:
pep8 --config=.pep8 course_discovery *.py
pylint --rcfile=pylintrc course_discovery *.py
pep8 --config=.pep8 acceptance_tests course_discovery *.py
pylint --rcfile=pylintrc acceptance_tests course_discovery *.py
validate: test quality
......@@ -84,3 +85,6 @@ start-devstack:
open-devstack:
docker-compose --x-networking up -d
docker exec -it course-discovery env TERM=$(TERM) /edx/app/discovery/devstack.sh open
accept:
nosetests --with-ignore-docstrings -v acceptance_tests
import os
API_GATEWAY_DISCOVERY_ROOT = os.environ.get('API_GATEWAY_DISCOVERY_ROOT')
if not API_GATEWAY_DISCOVERY_ROOT:
raise RuntimeError('API_GATEWAY_DISCOVERY_ROOT (e.g. https://api.edx.org/discovery/v1) must be supplied!')
DISCOVERY_API_ACCESS_TOKEN = os.environ.get('DISCOVERY_API_ACCESS_TOKEN')
if not DISCOVERY_API_ACCESS_TOKEN:
raise RuntimeError('DISCOVERY_API_ACCESS_TOKEN must be supplied!')
CATALOG_ID = int(os.environ.get('CATALOG_ID', 1))
COURSE_ID = os.environ.get('COURSE_ID', 'edX/DemoX')
COURSE_RUN_ID = os.environ.get('COURSE_RUN_ID', 'course-v1:edX+DemoX+Demo_Course')
""" Tests to validate configuration of the API gateway. """
from unittest import TestCase
import ddt
import requests
from acceptance_tests.config import API_GATEWAY_DISCOVERY_ROOT, DISCOVERY_API_ACCESS_TOKEN, CATALOG_ID
@ddt.ddt
class ApiGatewayTests(TestCase):
PATHS = (
'catalogs/',
'catalogs/{id}/'.format(id=CATALOG_ID),
)
def get_discovery_api_gateway_url(self, path):
""" Returns a complete URL for the given path, routed through the API gateway. """
return '{root}/{path}'.format(root=API_GATEWAY_DISCOVERY_ROOT.rstrip('/'), path=path)
def assert_api_response(self, path, expected_status_code=200, **headers):
"""
Verify the API returns HTTP 200.
Arguments:
path(str) -- Path of the API endpoint to call.
expected_status_code (int) -- Expected HTTP status code of the API response.
headers (dict) -- Headers to pass with the request.
"""
url = self.get_discovery_api_gateway_url(path)
response = requests.get(url, headers=headers)
self.assertEqual(response.status_code, expected_status_code)
@ddt.data(*PATHS)
def test_endpoint_ok(self, path):
""" Verify the endpoint returns HTTP 200 for valid requests. """
headers = {
'Authorization': 'Bearer {token}'.format(token=DISCOVERY_API_ACCESS_TOKEN)
}
self.assert_api_response(path, **headers)
@ddt.data(*PATHS)
def test_endpoint_not_authorized(self, path):
""" Verify the endpoint returns HTTP 403 for unauthorized requests. """
self.assert_api_response(path, expected_status_code=403)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment