Skip to content

Commit

Permalink
Merge pull request #81 from andreicalistru/master
Browse files Browse the repository at this point in the history
add endpoint_prefix, update api_prefix
  • Loading branch information
viniciuschiele authored Jul 25, 2018
2 parents 31b7a19 + c6294c9 commit c64b5fe
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 37 deletions.
64 changes: 54 additions & 10 deletions flask_apscheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def __init__(self, scheduler=None, app=None):
self.allowed_hosts = ['*']
self.auth = None
self.api_enabled = False
self.api_prefix = ''
self.api_prefix = '/scheduler'
self.endpoint_prefix = 'scheduler.'
self.app = None

if app:
Expand Down Expand Up @@ -304,6 +305,7 @@ def _load_config(self):
self.api_enabled = self.app.config.get('SCHEDULER_VIEWS_ENABLED', self.api_enabled) # for compatibility reason
self.api_enabled = self.app.config.get('SCHEDULER_API_ENABLED', self.api_enabled)
self.api_prefix = self.app.config.get('SCHEDULER_API_PREFIX', self.api_prefix)
self.endpoint_prefix = self.app.config.get('SCHEDULER_ENDPOINT_PREFIX', self.endpoint_prefix)
self.allowed_hosts = self.app.config.get('SCHEDULER_ALLOWED_HOSTS', self.allowed_hosts)

def _load_jobs(self):
Expand All @@ -323,15 +325,57 @@ def _load_api(self):
"""
Add the routes for the scheduler API.
"""
self.app.add_url_rule(self.api_prefix + '/scheduler', 'get_scheduler_info', self._apply_auth(api.get_scheduler_info))
self.app.add_url_rule(self.api_prefix + '/scheduler/jobs', 'add_job', self._apply_auth(api.add_job), methods=['POST'])
self.app.add_url_rule(self.api_prefix + '/scheduler/jobs', 'get_jobs', self._apply_auth(api.get_jobs))
self.app.add_url_rule(self.api_prefix + '/scheduler/jobs/<job_id>', 'get_job', self._apply_auth(api.get_job))
self.app.add_url_rule(self.api_prefix + '/scheduler/jobs/<job_id>', 'delete_job', self._apply_auth(api.delete_job), methods=['DELETE'])
self.app.add_url_rule(self.api_prefix + '/scheduler/jobs/<job_id>', 'update_job', self._apply_auth(api.update_job), methods=['PATCH'])
self.app.add_url_rule(self.api_prefix + '/scheduler/jobs/<job_id>/pause', 'pause_job', self._apply_auth(api.pause_job), methods=['POST'])
self.app.add_url_rule(self.api_prefix + '/scheduler/jobs/<job_id>/resume', 'resume_job', self._apply_auth(api.resume_job), methods=['POST'])
self.app.add_url_rule(self.api_prefix + '/scheduler/jobs/<job_id>/run', 'run_job', self._apply_auth(api.run_job), methods=['POST'])
self.app.add_url_rule(
self.api_prefix + '',
self.endpoint_prefix + 'get_scheduler_info',
self._apply_auth(api.get_scheduler_info)
)
self.app.add_url_rule(
self.api_prefix + '/jobs',
self.endpoint_prefix + 'add_job',
self._apply_auth(api.add_job),
methods=['POST']
)
self.app.add_url_rule(
self.api_prefix + '/jobs',
self.endpoint_prefix + 'get_jobs',
self._apply_auth(api.get_jobs)
)
self.app.add_url_rule(
self.api_prefix + '/jobs/<job_id>',
self.endpoint_prefix + 'get_job',
self._apply_auth(api.get_job)
)
self.app.add_url_rule(
self.api_prefix + '/jobs/<job_id>',
self.endpoint_prefix + 'delete_job',
self._apply_auth(api.delete_job),
methods=['DELETE']
)
self.app.add_url_rule(
self.api_prefix + '/jobs/<job_id>',
self.endpoint_prefix + 'update_job',
self._apply_auth(api.update_job),
methods=['PATCH']
)
self.app.add_url_rule(
self.api_prefix + '/jobs/<job_id>/pause',
self.endpoint_prefix + 'pause_job',
self._apply_auth(api.pause_job),
methods=['POST']
)
self.app.add_url_rule(
self.api_prefix + '/jobs/<job_id>/resume',
self.endpoint_prefix + 'resume_job',
self._apply_auth(api.resume_job),
methods=['POST']
)
self.app.add_url_rule(
self.api_prefix + '/jobs/<job_id>/run',
self.endpoint_prefix + 'run_job',
self._apply_auth(api.run_job),
methods=['POST']
)

def _apply_auth(self, view_func):
"""
Expand Down
54 changes: 27 additions & 27 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def setUp(self):
self.client = self.app.test_client()

def test_scheduler_info(self):
response = self.client.get('/scheduler')
response = self.client.get(self.scheduler.api_prefix)
self.assertEqual(response.status_code, 200)
info = json.loads(response.get_data(as_text=True))
self.assertIsNotNone(info['current_host'])
Expand All @@ -32,7 +32,7 @@ def test_add_job(self):
'run_date': '2020-12-01T12:30:01+00:00',
}

response = self.client.post('/scheduler/jobs', data=json.dumps(job))
response = self.client.post(self.scheduler.api_prefix + '/jobs', data=json.dumps(job))
self.assertEqual(response.status_code, 200)

job2 = json.loads(response.get_data(as_text=True))
Expand All @@ -50,37 +50,37 @@ def test_add_conflicted_job(self):
'run_date': '2020-12-01T12:30:01+00:00',
}

response = self.client.post('/scheduler/jobs', data=json.dumps(job))
response = self.client.post(self.scheduler.api_prefix + '/jobs', data=json.dumps(job))
self.assertEqual(response.status_code, 200)

response = self.client.post('/scheduler/jobs', data=json.dumps(job))
response = self.client.post(self.scheduler.api_prefix + '/jobs', data=json.dumps(job))
self.assertEqual(response.status_code, 409)

def test_add_invalid_job(self):
job = {
'id': None,
}

response = self.client.post('/scheduler/jobs', data=json.dumps(job))
response = self.client.post(self.scheduler.api_prefix + '/jobs', data=json.dumps(job))
self.assertEqual(response.status_code, 500)

def test_delete_job(self):
self.__add_job()

response = self.client.delete('/scheduler/jobs/job1')
response = self.client.delete(self.scheduler.api_prefix + '/jobs/job1')
self.assertEqual(response.status_code, 204)

response = self.client.get('/scheduler/jobs/job1')
response = self.client.get(self.scheduler.api_prefix + '/jobs/job1')
self.assertEqual(response.status_code, 404)

def test_delete_job_not_found(self):
response = self.client.delete('/scheduler/jobs/job1')
response = self.client.delete(self.scheduler.api_prefix + '/jobs/job1')
self.assertEqual(response.status_code, 404)

def test_get_job(self):
job = self.__add_job()

response = self.client.get('/scheduler/jobs/job1')
response = self.client.get(self.scheduler.api_prefix + '/jobs/job1')
self.assertEqual(response.status_code, 200)

job2 = json.loads(response.get_data(as_text=True))
Expand All @@ -91,13 +91,13 @@ def test_get_job(self):
self.assertEqual(job.get('minutes'), job2.get('minutes'))

def test_get_job_not_found(self):
response = self.client.get('/scheduler/jobs/job1')
response = self.client.get(self.scheduler.api_prefix + '/jobs/job1')
self.assertEqual(response.status_code, 404)

def test_get_all_jobs(self):
job = self.__add_job()

response = self.client.get('/scheduler/jobs')
response = self.client.get(self.scheduler.api_prefix + '/jobs')
self.assertEqual(response.status_code, 200)

jobs = json.loads(response.get_data(as_text=True))
Expand All @@ -121,7 +121,7 @@ def test_update_job(self):
'start_date': '2021-01-01'
}

response = self.client.patch('/scheduler/jobs/job1', data=json.dumps(data_to_update))
response = self.client.patch(self.scheduler.api_prefix + '/jobs/job1', data=json.dumps(data_to_update))
self.assertEqual(response.status_code, 200)

job2 = json.loads(response.get_data(as_text=True))
Expand All @@ -141,7 +141,7 @@ def test_update_job_not_found(self):
'start_date': '2021-01-01'
}

response = self.client.patch('/scheduler/jobs/job1', data=json.dumps(data_to_update))
response = self.client.patch(self.scheduler.api_prefix + '/jobs/job1', data=json.dumps(data_to_update))
self.assertEqual(response.status_code, 404)

def test_update_invalid_job(self):
Expand All @@ -151,39 +151,39 @@ def test_update_invalid_job(self):
'trigger': 'invalid_trigger',
}

response = self.client.patch('/scheduler/jobs/job1', data=json.dumps(data_to_update))
response = self.client.patch(self.scheduler.api_prefix + '/jobs/job1', data=json.dumps(data_to_update))
self.assertEqual(response.status_code, 500)

def test_pause_and_resume_job(self):
self.__add_job()

response = self.client.post('/scheduler/jobs/job1/pause')
response = self.client.post(self.scheduler.api_prefix + '/jobs/job1/pause')
self.assertEqual(response.status_code, 200)
job = json.loads(response.get_data(as_text=True))
self.assertIsNone(job.get('next_run_time'))

response = self.client.post('/scheduler/jobs/job1/resume')
response = self.client.post(self.scheduler.api_prefix + '/jobs/job1/resume')
self.assertEqual(response.status_code, 200)
job = json.loads(response.get_data(as_text=True))
self.assertIsNotNone(job.get('next_run_time'))

def test_pause_and_resume_job_not_found(self):
response = self.client.post('/scheduler/jobs/job1/pause')
response = self.client.post(self.scheduler.api_prefix + '/jobs/job1/pause')
self.assertEqual(response.status_code, 404)

response = self.client.post('/scheduler/jobs/job1/resume')
response = self.client.post(self.scheduler.api_prefix + '/jobs/job1/resume')
self.assertEqual(response.status_code, 404)

def test_run_job(self):
self.__add_job()

response = self.client.post('/scheduler/jobs/job1/run')
response = self.client.post(self.scheduler.api_prefix + '/jobs/job1/run')
self.assertEqual(response.status_code, 200)
job = json.loads(response.get_data(as_text=True))
self.assertIsNotNone(job.get('next_run_time'))

def test_run_job_not_found(self):
response = self.client.post('/scheduler/jobs/job1/run')
response = self.client.post(self.scheduler.api_prefix + '/jobs/job1/run')
self.assertEqual(response.status_code, 404)

def __add_job(self):
Expand All @@ -194,7 +194,7 @@ def __add_job(self):
'minutes': 10,
}

response = self.client.post('/scheduler/jobs', data=json.dumps(job))
response = self.client.post(self.scheduler.api_prefix + '/jobs', data=json.dumps(job))
return json.loads(response.get_data(as_text=True))


Expand All @@ -209,11 +209,11 @@ def setUp(self):
self.client = self.app.test_client()

def test_api_prefix(self):
response = self.client.get('/api/scheduler/jobs')
response = self.client.get(self.scheduler.api_prefix + '/jobs')
self.assertEqual(response.status_code, 200)

def test_invalid_api_prefix(self):
response = self.client.get('/invalidapi/scheduler/jobs')
response = self.client.get('/invalidapi/jobs')
self.assertEqual(response.status_code, 404)


Expand All @@ -233,23 +233,23 @@ def _authenticate(self, auth):

def test_valid_credentials(self):
headers = {'Authorization': 'Basic ' + base64.b64encode(b'test:test').decode('ascii')}
response = self.client.get('/scheduler', headers=headers)
response = self.client.get(self.scheduler.api_prefix + '', headers=headers)
self.assertEqual(response.status_code, 200)

def test_invalid_credentials(self):
headers = {'Authorization': 'Basic ' + base64.b64encode(b'guest:guest').decode('ascii')}
response = self.client.get('/scheduler', headers=headers)
response = self.client.get(self.scheduler.api_prefix + '', headers=headers)
self.assertEqual(response.status_code, 401)
self.assertEqual(response.headers['WWW-Authenticate'], 'Basic realm="Authentication Required"')

def test_invalid_header_format(self):
headers = {'Authorization': 'Basic 1231234'}
response = self.client.get('/scheduler', headers=headers)
response = self.client.get(self.scheduler.api_prefix + '', headers=headers)
self.assertEqual(response.status_code, 401)
self.assertEqual(response.headers['WWW-Authenticate'], 'Basic realm="Authentication Required"')

def test_missing_credentials(self):
response = self.client.get('/scheduler')
response = self.client.get(self.scheduler.api_prefix + '')
self.assertEqual(response.status_code, 401)
self.assertEqual(response.headers['WWW-Authenticate'], 'Basic realm="Authentication Required"')

Expand Down

0 comments on commit c64b5fe

Please sign in to comment.