@ -21,7 +21,7 @@ from ansible import context
from ansible . errors import AnsibleError
from ansible . galaxy import api as galaxy_api
from ansible . galaxy . api import CollectionVersionMetadata , GalaxyAPI , GalaxyError
from ansible . galaxy . token import GalaxyToken
from ansible . galaxy . token import BasicAuthToken, GalaxyToken, KeycloakToken
from ansible . module_utils . _text import to_native , to_text
from ansible . module_utils . six . moves . urllib import error as urllib_error
from ansible . utils import context_objects as co
@ -53,10 +53,12 @@ def collection_artifact(tmp_path_factory):
yield tar_path
def get_test_galaxy_api ( url , version ) :
def get_test_galaxy_api ( url , version , token_ins = None , token_value = None ) :
token_value = token_value or " my token "
token_ins = token_ins or GalaxyToken ( token_value )
api = GalaxyAPI ( None , " test " , url )
api . _available_api_versions = { version : ' /api/ %s ' % version }
api . token = GalaxyToken( token = " my token " )
api . token = token_ins
return api
@ -79,23 +81,29 @@ def test_api_token_auth():
token = GalaxyToken ( token = u " my_token " )
api = GalaxyAPI ( None , " test " , " https://galaxy.ansible.com " , token = token )
actual = { }
api . _add_auth_token ( actual , " " )
api . _add_auth_token ( actual , " " , required = True )
assert actual == { ' Authorization ' : ' Token my_token ' }
def test_api_token_auth_with_token_type ( ) :
token = GalaxyToken ( token = u " my_token " )
def test_api_token_auth_with_token_type ( monkeypatch ) :
token = KeycloakToken ( auth_url = ' https://api.test/ ' )
mock_token_get = MagicMock ( )
mock_token_get . return_value = ' my_token '
monkeypatch . setattr ( token , ' get ' , mock_token_get )
api = GalaxyAPI ( None , " test " , " https://galaxy.ansible.com " , token = token )
actual = { }
api . _add_auth_token ( actual , " " , token_type = " Bearer " )
api . _add_auth_token ( actual , " " , token_type = " Bearer " , required = True )
assert actual == { ' Authorization ' : ' Bearer my_token ' }
def test_api_token_auth_with_v3_url ( ) :
token = GalaxyToken ( token = u " my_token " )
def test_api_token_auth_with_v3_url ( monkeypatch ) :
token = KeycloakToken ( auth_url = ' https://api.test/ ' )
mock_token_get = MagicMock ( )
mock_token_get . return_value = ' my_token '
monkeypatch . setattr ( token , ' get ' , mock_token_get )
api = GalaxyAPI ( None , " test " , " https://galaxy.ansible.com " , token = token )
actual = { }
api . _add_auth_token ( actual , " https://galaxy.ansible.com/api/v3/resource/name " )
api . _add_auth_token ( actual , " https://galaxy.ansible.com/api/v3/resource/name " , required = True )
assert actual == { ' Authorization ' : ' Bearer my_token ' }
@ -104,28 +112,30 @@ def test_api_token_auth_with_v2_url():
api = GalaxyAPI ( None , " test " , " https://galaxy.ansible.com " , token = token )
actual = { }
# Add v3 to random part of URL but response should only see the v2 as the full URI path segment.
api . _add_auth_token ( actual , " https://galaxy.ansible.com/api/v2/resourcev3/name " )
api . _add_auth_token ( actual , " https://galaxy.ansible.com/api/v2/resourcev3/name " , required = True )
assert actual == { ' Authorization ' : ' Token my_token ' }
def test_api_basic_auth_password ( ) :
api = GalaxyAPI ( None , " test " , " https://galaxy.ansible.com " , username = u " user " , password = u " pass " )
token = BasicAuthToken ( username = u " user " , password = u " pass " )
api = GalaxyAPI ( None , " test " , " https://galaxy.ansible.com " , token = token )
actual = { }
api . _add_auth_token ( actual , " " )
api . _add_auth_token ( actual , " " , required = True )
assert actual == { ' Authorization ' : ' Basic dXNlcjpwYXNz ' }
def test_api_basic_auth_no_password ( ) :
api = GalaxyAPI ( None , " test " , " https://galaxy.ansible.com " , username = u " user " , )
token = BasicAuthToken ( username = u " user " )
api = GalaxyAPI ( None , " test " , " https://galaxy.ansible.com " , token = token )
actual = { }
api . _add_auth_token ( actual , " " )
api . _add_auth_token ( actual , " " , required = True )
assert actual == { ' Authorization ' : ' Basic dXNlcjo= ' }
def test_api_dont_override_auth_header ( ) :
api = GalaxyAPI ( None , " test " , " https://galaxy.ansible.com " )
actual = { ' Authorization ' : ' Custom token ' }
api . _add_auth_token ( actual , " " )
api . _add_auth_token ( actual , " " , required = True )
assert actual == { ' Authorization ' : ' Custom token ' }
@ -167,7 +177,6 @@ def test_initialise_galaxy_with_auth(monkeypatch):
assert actual == { u ' token ' : u ' my token ' }
assert mock_open . call_count == 2
assert mock_open . mock_calls [ 0 ] [ 1 ] [ 0 ] == ' https://galaxy.ansible.com/api '
assert mock_open . mock_calls [ 0 ] [ 2 ] [ ' headers ' ] == { ' Authorization ' : ' Token my_token ' }
assert mock_open . mock_calls [ 1 ] [ 1 ] [ 0 ] == ' https://galaxy.ansible.com/api/v1/tokens/ '
assert mock_open . mock_calls [ 1 ] [ 2 ] [ ' data ' ] == ' github_token=github_token '
@ -175,27 +184,22 @@ def test_initialise_galaxy_with_auth(monkeypatch):
def test_initialise_automation_hub ( monkeypatch ) :
mock_open = MagicMock ( )
mock_open . side_effect = [
urllib_error . HTTPError ( ' https://galaxy.ansible.com/api ' , 401 , ' msg ' , { } , StringIO ( ) ) ,
# AH won't return v1 but we do for authenticate() to work.
StringIO ( u ' { " available_versions " : { " v1 " : " /api/v1 " , " v3 " : " /api/v3 " }} ' ) ,
StringIO ( u ' { " token " : " my token " } ' ) ,
StringIO ( u ' { " available_versions " : { " v2 " : " v2/ " , " v3 " : " v3/ " }} ' ) ,
]
monkeypatch . setattr ( galaxy_api , ' open_url ' , mock_open )
token = KeycloakToken ( auth_url = ' https://api.test/ ' )
mock_token_get = MagicMock ( )
mock_token_get . return_value = ' my_token '
monkeypatch . setattr ( token , ' get ' , mock_token_get )
api = GalaxyAPI ( None , " test " , " https://galaxy.ansible.com " , token = GalaxyToken ( token = ' my_token ' ) )
actual = api . authenticate ( " github_token " )
api = GalaxyAPI ( None , " test " , " https://galaxy.ansible.com " , token = token )
assert len ( api . available_api_versions ) == 2
assert api . available_api_versions [ ' v1 ' ] == u ' /api/v1 '
assert api . available_api_versions [ ' v3 ' ] == u ' /api/v3 '
assert actual == { u ' token ' : u ' my token ' }
assert mock_open . call_count == 3
assert api . available_api_versions [ ' v2 ' ] == u ' v2/ '
assert api . available_api_versions [ ' v3 ' ] == u ' v3/ '
assert mock_open . mock_calls [ 0 ] [ 1 ] [ 0 ] == ' https://galaxy.ansible.com/api '
assert mock_open . mock_calls [ 0 ] [ 2 ] [ ' headers ' ] == { ' Authorization ' : ' Token my_token ' }
assert mock_open . mock_calls [ 1 ] [ 1 ] [ 0 ] == ' https://galaxy.ansible.com/api '
assert mock_open . mock_calls [ 1 ] [ 2 ] [ ' headers ' ] == { ' Authorization ' : ' Bearer my_token ' }
assert mock_open . mock_calls [ 2 ] [ 1 ] [ 0 ] == ' https://galaxy.ansible.com/api/v1/tokens/ '
assert mock_open . mock_calls [ 2 ] [ 2 ] [ ' data ' ] == ' github_token=github_token '
assert mock_open . mock_calls [ 0 ] [ 2 ] [ ' headers ' ] == { ' Authorization ' : ' Bearer my_token ' }
def test_initialise_unknown ( monkeypatch ) :
@ -327,14 +331,19 @@ def test_publish_failure(api_version, collection_url, response, expected, collec
api . publish_collection ( collection_artifact )
@pytest.mark.parametrize ( ' api_version, token_type ' , [
( ' v2 ' , ' Token ' ),
( ' v3 ' , ' Bearer ' ),
@pytest.mark.parametrize ( ' api_version, token_type , token_ins ' , [
( ' v2 ' , ' Token ' , GalaxyToken ( ' my token ' ) ),
( ' v3 ' , ' Bearer ' , KeycloakToken ( auth_url = ' https://api.test/ ' ) ),
] )
def test_wait_import_task ( api_version , token_type , monkeypatch) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version )
def test_wait_import_task ( api_version , token_type , token_ins, monkeypatch) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version , token_ins = token_ins )
import_uri = ' https://galaxy.server.com/api/ %s /task/1234 ' % api_version
if token_ins :
mock_token_get = MagicMock ( )
mock_token_get . return_value = ' my token '
monkeypatch . setattr ( token_ins , ' get ' , mock_token_get )
mock_open = MagicMock ( )
mock_open . return_value = StringIO ( u ' { " state " : " success " , " finished_at " : " time " } ' )
monkeypatch . setattr ( galaxy_api , ' open_url ' , mock_open )
@ -352,14 +361,19 @@ def test_wait_import_task(api_version, token_type, monkeypatch):
assert mock_display . mock_calls [ 0 ] [ 1 ] [ 0 ] == ' Waiting until Galaxy import task %s has completed ' % import_uri
@pytest.mark.parametrize ( ' api_version, token_type ' , [
( ' v2 ' , ' Token ' ),
( ' v3 ' , ' Bearer ' ),
@pytest.mark.parametrize ( ' api_version, token_type , token_ins ' , [
( ' v2 ' , ' Token ' , GalaxyToken ( ' my token ' ) ),
( ' v3 ' , ' Bearer ' , KeycloakToken ( auth_url = ' https://api.test/ ' ) ),
] )
def test_wait_import_task_multiple_requests ( api_version , token_type , monkeypatch) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version )
def test_wait_import_task_multiple_requests ( api_version , token_type , token_ins, monkeypatch) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version , token_ins = token_ins )
import_uri = ' https://galaxy.server.com/api/ %s /task/1234 ' % api_version
if token_ins :
mock_token_get = MagicMock ( )
mock_token_get . return_value = ' my token '
monkeypatch . setattr ( token_ins , ' get ' , mock_token_get )
mock_open = MagicMock ( )
mock_open . side_effect = [
StringIO ( u ' { " state " : " test " } ' ) ,
@ -386,19 +400,24 @@ def test_wait_import_task_multiple_requests(api_version, token_type, monkeypatch
assert mock_display . call_count == 1
assert mock_display . mock_calls [ 0 ] [ 1 ] [ 0 ] == ' Waiting until Galaxy import task %s has completed ' % import_uri
assert mock_vvv . call_count == 2 # 1st is opening Galaxy token file.
assert mock_vvv . mock_calls [ 1 ] [ 1 ] [ 0 ] == \
assert mock_vvv . call_count == 1
assert mock_vvv . mock_calls [ 0 ] [ 1 ] [ 0 ] == \
' Galaxy import process has a status of test, wait 2 seconds before trying again '
@pytest.mark.parametrize ( ' api_version, token_type ' , [
( ' v2 ' , ' Token ' ),
( ' v3 ' , ' Bearer ' ),
@pytest.mark.parametrize ( ' api_version, token_type , token_ins ' , [
( ' v2 ' , ' Token ' , GalaxyToken ( ' my token ' ) ),
( ' v3 ' , ' Bearer ' , KeycloakToken ( auth_url = ' https://api.test/ ' ) ),
] )
def test_wait_import_task_with_failure ( api_version , token_type , monkeypatch) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version )
def test_wait_import_task_with_failure ( api_version , token_type , token_ins, monkeypatch) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version , token_ins = token_ins )
import_uri = ' https://galaxy.server.com/api/ %s /task/1234 ' % api_version
if token_ins :
mock_token_get = MagicMock ( )
mock_token_get . return_value = ' my token '
monkeypatch . setattr ( token_ins , ' get ' , mock_token_get )
mock_open = MagicMock ( )
mock_open . side_effect = [
StringIO ( to_text ( json . dumps ( {
@ -450,8 +469,8 @@ def test_wait_import_task_with_failure(api_version, token_type, monkeypatch):
assert mock_display . call_count == 1
assert mock_display . mock_calls [ 0 ] [ 1 ] [ 0 ] == ' Waiting until Galaxy import task %s has completed ' % import_uri
assert mock_vvv . call_count == 2 # 1st is opening Galaxy token file.
assert mock_vvv . mock_calls [ 1 ] [ 1 ] [ 0 ] == u ' Galaxy import message: info - Somé info '
assert mock_vvv . call_count == 1
assert mock_vvv . mock_calls [ 0 ] [ 1 ] [ 0 ] == u ' Galaxy import message: info - Somé info '
assert mock_warn . call_count == 1
assert mock_warn . mock_calls [ 0 ] [ 1 ] [ 0 ] == u ' Galaxy import warning message: Some wärning '
@ -460,14 +479,19 @@ def test_wait_import_task_with_failure(api_version, token_type, monkeypatch):
assert mock_err . mock_calls [ 0 ] [ 1 ] [ 0 ] == u ' Galaxy import error message: Somé error '
@pytest.mark.parametrize ( ' api_version, token_type ' , [
( ' v2 ' , ' Token ' ),
( ' v3 ' , ' Bearer ' ),
@pytest.mark.parametrize ( ' api_version, token_type , token_ins ' , [
( ' v2 ' , ' Token ' , GalaxyToken ( ' my_token ' ) ),
( ' v3 ' , ' Bearer ' , KeycloakToken ( auth_url = ' https://api.test/ ' ) ),
] )
def test_wait_import_task_with_failure_no_error ( api_version , token_type , monkeypatch) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version )
def test_wait_import_task_with_failure_no_error ( api_version , token_type , token_ins, monkeypatch) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version , token_ins = token_ins )
import_uri = ' https://galaxy.server.com/api/ %s /task/1234 ' % api_version
if token_ins :
mock_token_get = MagicMock ( )
mock_token_get . return_value = ' my token '
monkeypatch . setattr ( token_ins , ' get ' , mock_token_get )
mock_open = MagicMock ( )
mock_open . side_effect = [
StringIO ( to_text ( json . dumps ( {
@ -515,8 +539,8 @@ def test_wait_import_task_with_failure_no_error(api_version, token_type, monkeyp
assert mock_display . call_count == 1
assert mock_display . mock_calls [ 0 ] [ 1 ] [ 0 ] == ' Waiting until Galaxy import task %s has completed ' % import_uri
assert mock_vvv . call_count == 2 # 1st is opening Galaxy token file.
assert mock_vvv . mock_calls [ 1 ] [ 1 ] [ 0 ] == u ' Galaxy import message: info - Somé info '
assert mock_vvv . call_count == 1
assert mock_vvv . mock_calls [ 0 ] [ 1 ] [ 0 ] == u ' Galaxy import message: info - Somé info '
assert mock_warn . call_count == 1
assert mock_warn . mock_calls [ 0 ] [ 1 ] [ 0 ] == u ' Galaxy import warning message: Some wärning '
@ -525,14 +549,19 @@ def test_wait_import_task_with_failure_no_error(api_version, token_type, monkeyp
assert mock_err . mock_calls [ 0 ] [ 1 ] [ 0 ] == u ' Galaxy import error message: Somé error '
@pytest.mark.parametrize ( ' api_version, token_type ' , [
( ' v2 ' , ' Token ' ),
( ' v3 ' , ' Bearer ' ),
@pytest.mark.parametrize ( ' api_version, token_type , token_ins ' , [
( ' v2 ' , ' Token ' , GalaxyToken ( ' my token ' ) ),
( ' v3 ' , ' Bearer ' , KeycloakToken ( auth_url = ' https://api.test/ ' ) ),
] )
def test_wait_import_task_timeout ( api_version , token_type , monkeypatch) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version )
def test_wait_import_task_timeout ( api_version , token_type , token_ins, monkeypatch) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version , token_ins = token_ins )
import_uri = ' https://galaxy.server.com/api/ %s /task/1234 ' % api_version
if token_ins :
mock_token_get = MagicMock ( )
mock_token_get . return_value = ' my token '
monkeypatch . setattr ( token_ins , ' get ' , mock_token_get )
def return_response ( * args , * * kwargs ) :
return StringIO ( u ' { " state " : " waiting " } ' )
@ -561,24 +590,31 @@ def test_wait_import_task_timeout(api_version, token_type, monkeypatch):
assert mock_display . call_count == 1
assert mock_display . mock_calls [ 0 ] [ 1 ] [ 0 ] == ' Waiting until Galaxy import task %s has completed ' % import_uri
expected_wait_msg = ' Galaxy import process has a status of waiting, wait {0} seconds before trying again '
# expected_wait_msg = ' Galaxy import process has a status of waiting, wait {0} seconds before trying again'
assert mock_vvv . call_count > 9 # 1st is opening Galaxy token file.
assert mock_vvv . mock_calls [ 1 ] [ 1 ] [ 0 ] == expected_wait_msg . format ( 2 )
assert mock_vvv . mock_calls [ 2 ] [ 1 ] [ 0 ] == expected_wait_msg . format ( 3 )
assert mock_vvv . mock_calls [ 3 ] [ 1 ] [ 0 ] == expected_wait_msg . format ( 4 )
assert mock_vvv . mock_calls [ 4 ] [ 1 ] [ 0 ] == expected_wait_msg . format ( 6 )
assert mock_vvv . mock_calls [ 5 ] [ 1 ] [ 0 ] == expected_wait_msg . format ( 10 )
assert mock_vvv . mock_calls [ 6 ] [ 1 ] [ 0 ] == expected_wait_msg . format ( 15 )
assert mock_vvv . mock_calls [ 7 ] [ 1 ] [ 0 ] == expected_wait_msg . format ( 22 )
assert mock_vvv . mock_calls [ 8 ] [ 1 ] [ 0 ] == expected_wait_msg . format ( 30 )
@pytest.mark.parametrize ( ' api_version, token_type, version ' , [
( ' v2 ' , ' Token ' , ' v2.1.13 ' ) ,
( ' v3 ' , ' Bearer ' , ' v1.0.0 ' ) ,
# FIXME:
# assert mock_vvv.mock_calls[1][1][0] == expected_wait_msg.format(2)
# assert mock_vvv.mock_calls[2][1][0] == expected_wait_msg.format(3)
# assert mock_vvv.mock_calls[3][1][0] == expected_wait_msg.format(4)
# assert mock_vvv.mock_calls[4][1][0] == expected_wait_msg.format(6)
# assert mock_vvv.mock_calls[5][1][0] == expected_wait_msg.format(10)
# assert mock_vvv.mock_calls[6][1][0] == expected_wait_msg.format(15)
# assert mock_vvv.mock_calls[7][1][0] == expected_wait_msg.format(22)
# assert mock_vvv.mock_calls[8][1][0] == expected_wait_msg.format(30)
@pytest.mark.parametrize ( ' api_version, token_type, version, token_ins ' , [
( ' v2 ' , None , ' v2.1.13 ' , None ) ,
( ' v3 ' , ' Bearer ' , ' v1.0.0 ' , KeycloakToken ( auth_url = ' https://api.test/api/automation-hub/ ' ) ) ,
] )
def test_get_collection_version_metadata_no_version ( api_version , token_type , version , monkeypatch ) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version )
def test_get_collection_version_metadata_no_version ( api_version , token_type , version , token_ins , monkeypatch ) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version , token_ins = token_ins )
if token_ins :
mock_token_get = MagicMock ( )
mock_token_get . return_value = ' my token '
monkeypatch . setattr ( token_ins , ' get ' , mock_token_get )
mock_open = MagicMock ( )
mock_open . side_effect = [
@ -614,11 +650,14 @@ def test_get_collection_version_metadata_no_version(api_version, token_type, ver
assert mock_open . call_count == 1
assert mock_open . mock_calls [ 0 ] [ 1 ] [ 0 ] == ' %s /api/ %s /collections/namespace/collection/versions/ %s ' \
% ( api . api_server , api_version , version )
assert mock_open . mock_calls [ 0 ] [ 2 ] [ ' headers ' ] [ ' Authorization ' ] == ' %s my token ' % token_type
# v2 calls dont need auth, so no authz header or token_type
if token_type :
assert mock_open . mock_calls [ 0 ] [ 2 ] [ ' headers ' ] [ ' Authorization ' ] == ' %s my token ' % token_type
@pytest.mark.parametrize ( ' api_version, token_type, response ' , [
( ' v2 ' , ' Token ' , {
@pytest.mark.parametrize ( ' api_version, token_type, token_ins, response' , [
( ' v2 ' , None , None , {
' count ' : 2 ,
' next ' : None ,
' previous ' : None ,
@ -634,7 +673,7 @@ def test_get_collection_version_metadata_no_version(api_version, token_type, ver
] ,
} ) ,
# TODO: Verify this once Automation Hub is actually out
( ' v3 ' , ' Bearer ' , {
( ' v3 ' , ' Bearer ' , KeycloakToken ( auth_url = ' https://api.test/ ' ) , {
' count ' : 2 ,
' next ' : None ,
' previous ' : None ,
@ -650,8 +689,13 @@ def test_get_collection_version_metadata_no_version(api_version, token_type, ver
] ,
} ) ,
] )
def test_get_collection_versions ( api_version , token_type , response , monkeypatch ) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version )
def test_get_collection_versions ( api_version , token_type , token_ins , response , monkeypatch ) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version , token_ins = token_ins )
if token_ins :
mock_token_get = MagicMock ( )
mock_token_get . return_value = ' my token '
monkeypatch . setattr ( token_ins , ' get ' , mock_token_get )
mock_open = MagicMock ( )
mock_open . side_effect = [
@ -665,11 +709,12 @@ def test_get_collection_versions(api_version, token_type, response, monkeypatch)
assert mock_open . call_count == 1
assert mock_open . mock_calls [ 0 ] [ 1 ] [ 0 ] == ' https://galaxy.server.com/api/ %s /collections/namespace/collection/ ' \
' versions ' % api_version
assert mock_open . mock_calls [ 0 ] [ 2 ] [ ' headers ' ] [ ' Authorization ' ] == ' %s my token ' % token_type
if token_ins :
assert mock_open . mock_calls [ 0 ] [ 2 ] [ ' headers ' ] [ ' Authorization ' ] == ' %s my token ' % token_type
@pytest.mark.parametrize ( ' api_version, token_type, responses' , [
( ' v2 ' , ' Token ' , [
@pytest.mark.parametrize ( ' api_version, token_type, token_ins, responses' , [
( ' v2 ' , None , None , [
{
' count ' : 6 ,
' next ' : ' https://galaxy.server.com/api/v2/collections/namespace/collection/versions/?page=2 ' ,
@ -716,7 +761,7 @@ def test_get_collection_versions(api_version, token_type, response, monkeypatch)
] ,
} ,
] ) ,
( ' v3 ' , ' Bearer ' , [
( ' v3 ' , ' Bearer ' , KeycloakToken ( auth_url = ' https://api.test/ ' ) , [
{
' count ' : 6 ,
' links ' : {
@ -770,24 +815,30 @@ def test_get_collection_versions(api_version, token_type, response, monkeypatch)
} ,
] ) ,
] )
def test_get_collection_versions_pagination ( api_version , token_type , responses , monkeypatch ) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version )
def test_get_collection_versions_pagination ( api_version , token_type , token_ins , responses , monkeypatch ) :
api = get_test_galaxy_api ( ' https://galaxy.server.com ' , api_version , token_ins = token_ins )
if token_ins :
mock_token_get = MagicMock ( )
mock_token_get . return_value = ' my token '
monkeypatch . setattr ( token_ins , ' get ' , mock_token_get )
mock_open = MagicMock ( )
mock_open . side_effect = [ StringIO ( to_text ( json . dumps ( r ) ) ) for r in responses ]
monkeypatch . setattr ( galaxy_api , ' open_url ' , mock_open )
actual = api . get_collection_versions ( ' namespace ' , ' collection ' )
a = ' '
assert actual == [ u ' 1.0.0 ' , u ' 1.0.1 ' , u ' 1.0.2 ' , u ' 1.0.3 ' , u ' 1.0.4 ' , u ' 1.0.5 ' ]
assert mock_open . call_count == 3
assert mock_open . mock_calls [ 0 ] [ 1 ] [ 0 ] == ' https://galaxy.server.com/api/ %s /collections/namespace/collection/ ' \
' versions ' % api_version
assert mock_open . mock_calls [ 0 ] [ 2 ] [ ' headers ' ] [ ' Authorization ' ] == ' %s my token ' % token_type
assert mock_open . mock_calls [ 1 ] [ 1 ] [ 0 ] == ' https://galaxy.server.com/api/ %s /collections/namespace/collection/ ' \
' versions/?page=2 ' % api_version
assert mock_open . mock_calls [ 1 ] [ 2 ] [ ' headers ' ] [ ' Authorization ' ] == ' %s my token ' % token_type
assert mock_open . mock_calls [ 2 ] [ 1 ] [ 0 ] == ' https://galaxy.server.com/api/ %s /collections/namespace/collection/ ' \
' versions/?page=3 ' % api_version
assert mock_open . mock_calls [ 2 ] [ 2 ] [ ' headers ' ] [ ' Authorization ' ] == ' %s my token ' % token_type
if token_type :
assert mock_open . mock_calls [ 0 ] [ 2 ] [ ' headers ' ] [ ' Authorization ' ] == ' %s my token ' % token_type
assert mock_open . mock_calls [ 1 ] [ 2 ] [ ' headers ' ] [ ' Authorization ' ] == ' %s my token ' % token_type
assert mock_open . mock_calls [ 2 ] [ 2 ] [ ' headers ' ] [ ' Authorization ' ] == ' %s my token ' % token_type