Skip to content

Commit

Permalink
Merge pull request #113 from OpenTreeOfLife/oauth-accessors
Browse files Browse the repository at this point in the history
Oauth accessors
  • Loading branch information
jar398 authored Oct 6, 2016
2 parents bcb598a + be8cbd0 commit e20426b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
34 changes: 32 additions & 2 deletions ws-tests/opentreetesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ def raise_for_status(resp):
raise e



def api_is_readonly():
return config('host', 'allowwrite', 'true') == 'false'
s = config('host', 'allowwrite', 'false').lower()
return not (s == 'true')

def exit_if_api_is_readonly(fn):
if not api_is_readonly():
Expand All @@ -221,6 +221,36 @@ def exit_if_api_is_readonly(fn):
# This coordinates with run_tests.sh
sys.exit(3)

def github_oauth_for_write_or_exit(fn):
"""Pass in the name of test file and get back the OAUTH token from
the GITHUB_OAUTH_TOKEN environment if the test is not readonly.
Otherwise, exit with the exit code (3) that signals tests skipping.
"""
exit_if_api_is_readonly(fn)
auth_token = os.environ.get('GITHUB_OAUTH_TOKEN')
if auth_token is None:
debug('{} skipped due to lack of GITHUB_OAUTH_TOKEN in env\n'.format(fn))
# This coordinates with run_tests.sh
sys.exit(3)
return auth_token

def writable_api_host_and_oauth_or_exit(fn):
"""Convenience/safety function to makes sure you aren't running
writable phylesystem tests against the production api server.
Returns the domain and GITHUB_OAUTH_TOKEN token if the test configuration
is not readonly, the server is not "https://api.opentree.*" and GITHUB_OAUTH_TOKEN
is in your env. Otherwise exits with the "skip" test code.
"""
apihost = config('host', 'apihost').strip()
if apihost.startswith('https://api.opentree'):
debug('{} skipped because host is set to production api server. This test must be run locally or against devapi.\n'.format(fn))
# This coordinates with run_tests.sh
sys.exit(3)
auth_token = github_oauth_for_write_or_exit(fn)
return apihost, auth_token



# Mimic the behavior of apache so that services can be tested without
# having apache running. See deploy/setup/opentree-shared.conf
Expand Down
7 changes: 6 additions & 1 deletion ws-tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ if [[ ! "$first_config_spec" =~ "=" ]]; then
first_config_spec=host:apihost=$first_config_spec
fi

config_specs="$first_config_spec $* host:allowwrite=false"

config_specs="$first_config_spec $*"
if [[ ! "$config_specs" =~ "host:allowwrite=" ]]; then
config_specs="$first_config_spec $* host:allowwrite=false"
fi


# The python test scripts all use the opentreetesting.py library,
# so its location has to be on PYTHONPATH.
Expand Down

0 comments on commit e20426b

Please sign in to comment.