Updated job endpoint tests with authorization header

This commit is contained in:
Rebecca Law
2016-01-15 17:11:02 +00:00
parent b05ad17029
commit 96ba2d13b3
2 changed files with 24 additions and 9 deletions

View File

@@ -88,6 +88,7 @@ def sample_job(notify_db,
save_job(job) save_job(job)
return job return job
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def sample_admin_service_id(notify_db, notify_db_session): def sample_admin_service_id(notify_db, notify_db_session):
admin_user = sample_user(notify_db, notify_db_session, email="notify_admin@digital.cabinet-office.gov.uk") admin_user = sample_user(notify_db, notify_db_session, email="notify_admin@digital.cabinet-office.gov.uk")

View File

@@ -1,18 +1,20 @@
import json import json
import uuid import uuid
from flask import url_for from flask import url_for
from tests import create_authorization_header
from tests.app.conftest import sample_job as create_job from tests.app.conftest import sample_job as create_job
def test_get_jobs(notify_api, notify_db, notify_db_session, sample_template): def test_get_jobs(notify_api, notify_db, notify_db_session, sample_template):
_setup_jobs(notify_db, notify_db_session, sample_template) _setup_jobs(notify_db, notify_db_session, sample_template)
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client:
response = client.get(url_for('job.get_job')) auth_header = create_authorization_header(service_id=sample_template.service.id,
path=url_for('job.get_job'),
method='GET')
response = client.get(url_for('job.get_job'), headers=[auth_header])
assert response.status_code == 200 assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True)) resp_json = json.loads(response.get_data(as_text=True))
assert len(resp_json['data']) == 5 assert len(resp_json['data']) == 5
@@ -23,7 +25,10 @@ def test_get_job_with_invalid_id_returns400(notify_api, notify_db,
sample_template): sample_template):
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client:
response = client.get(url_for('job.get_job', job_id='invalid_id')) auth_header = create_authorization_header(service_id=sample_template.service.id,
path=url_for('job.get_job', job_id='invalid_id'),
method='GET')
response = client.get(url_for('job.get_job', job_id='invalid_id'), headers=[auth_header])
assert response.status_code == 400 assert response.status_code == 400
resp_json = json.loads(response.get_data(as_text=True)) resp_json = json.loads(response.get_data(as_text=True))
assert resp_json == {'message': 'Invalid job id', assert resp_json == {'message': 'Invalid job id',
@@ -36,11 +41,13 @@ def test_get_job_with_unknown_id_returns404(notify_api, notify_db,
random_id = str(uuid.uuid4()) random_id = str(uuid.uuid4())
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client:
response = client.get(url_for('job.get_job', job_id=random_id)) auth_header = create_authorization_header(service_id=sample_template.service.id,
path=url_for('job.get_job', job_id=random_id),
method='GET')
response = client.get(url_for('job.get_job', job_id=random_id), headers=[auth_header])
assert response.status_code == 404 assert response.status_code == 404
resp_json = json.loads(response.get_data(as_text=True)) resp_json = json.loads(response.get_data(as_text=True))
assert resp_json == {'message': 'Job not found', 'result': assert resp_json == {'message': 'Job not found', 'result': 'error'}
'error'}
def test_get_job_by_id(notify_api, notify_db, notify_db_session, def test_get_job_by_id(notify_api, notify_db, notify_db_session,
@@ -48,7 +55,10 @@ def test_get_job_by_id(notify_api, notify_db, notify_db_session,
job_id = str(sample_job.id) job_id = str(sample_job.id)
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client:
response = client.get(url_for('job.get_job', job_id=job_id)) auth_header = create_authorization_header(service_id=sample_job.service.id,
path=url_for('job.get_job', job_id=job_id),
method='GET')
response = client.get(url_for('job.get_job', job_id=job_id), headers=[auth_header])
assert response.status_code == 200 assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True)) resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['data']['id'] == job_id assert resp_json['data']['id'] == job_id
@@ -65,9 +75,13 @@ def test_post_job(notify_api, notify_db, notify_db_session, sample_template):
'template': template_id, 'template': template_id,
'original_file_name': original_file_name 'original_file_name': original_file_name
} }
headers = [('Content-Type', 'application/json')]
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client:
auth_header = create_authorization_header(service_id=sample_template.service.id,
path=url_for('job.create_job'),
method='POST',
request_body=json.dumps(data))
headers = [('Content-Type', 'application/json'), auth_header]
response = client.post( response = client.post(
url_for('job.create_job'), url_for('job.create_job'),
data=json.dumps(data), data=json.dumps(data),