Skip to content

Commit

Permalink
Fix unittest mocks to support us checking geturl() after opening conn…
Browse files Browse the repository at this point in the history
…ections.
  • Loading branch information
dannon committed Sep 22, 2023
1 parent 5ff729d commit d01b039
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 3 additions & 1 deletion test/unit/files/test_drs.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ def drs_repo_handler(request):
content_type="application/json",
)

test_url = "drs://drs.example.org/314159"

def check_specific_header(request, **kwargs):
assert request.full_url == "https://my.respository.org/myfile.txt"
assert request.headers["Authorization"] == "Basic Z2E0Z2g6ZHJz"
response: Any = io.StringIO("hello drs world")
response.headers = {}
response.geturl = lambda: test_url
return response

with mock.patch.object(urllib.request, "urlopen", new=check_specific_header):
test_url = "drs://drs.example.org/314159"
user_context = user_context_fixture()
file_sources = configured_file_sources(FILE_SOURCES_CONF)
file_source_pair = file_sources.get_file_source_path(test_url)
Expand Down
20 changes: 15 additions & 5 deletions test/unit/files/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@


def test_file_source_http_specific():
test_url = "https://www.usegalaxy.org/myfile.txt"

def check_specific_header(request, **kwargs):
assert request.headers["Authorization"] == "Bearer IBearTokens"
response: Any = io.StringIO("hello specific world")
response.headers = {}
response.geturl = lambda: test_url
return response

with mock.patch.object(urllib.request, "urlopen", new=check_specific_header):
test_url = "https://www.usegalaxy.org/myfile.txt"
user_context = user_context_fixture()
file_sources = configured_file_sources(FILE_SOURCES_CONF)
file_source_pair = file_sources.get_file_source_path(test_url)
Expand All @@ -39,14 +41,16 @@ def check_specific_header(request, **kwargs):


def test_file_source_another_http_specific():
test_url = "http://www.galaxyproject.org/anotherfile.txt"

def check_another_header(request, **kwargs):
assert request.headers["Another_header"] == "found"
response: Any = io.StringIO("hello another world")
response.geturl = lambda: test_url
response.headers = {}
return response

with mock.patch.object(urllib.request, "urlopen", new=check_another_header):
test_url = "http://www.galaxyproject.org/anotherfile.txt"
user_context = user_context_fixture()
file_sources = configured_file_sources(FILE_SOURCES_CONF)
file_source_pair = file_sources.get_file_source_path(test_url)
Expand All @@ -58,14 +62,16 @@ def check_another_header(request, **kwargs):


def test_file_source_http_generic():
test_url = "https://www.elsewhere.org/myfile.txt"

def check_generic_headers(request, **kwargs):
assert not request.headers
response: Any = io.StringIO("hello generic world")
response.headers = {}
response.geturl = lambda: test_url
return response

with mock.patch.object(urllib.request, "urlopen", new=check_generic_headers):
test_url = "https://www.elsewhere.org/myfile.txt"
user_context = user_context_fixture()
file_sources = configured_file_sources(FILE_SOURCES_CONF)
file_source_pair = file_sources.get_file_source_path(test_url)
Expand All @@ -77,14 +83,16 @@ def check_generic_headers(request, **kwargs):


def test_file_source_ftp_url():
test_url = "ftp://ftp.gnu.org/README"

def check_generic_headers(request, **kwargs):
assert not request.headers
response: Any = io.StringIO("This is ftp.gnu.org, the FTP server of the the GNU project.")
response.headers = {}
response.geturl = lambda: test_url
return response

with mock.patch.object(urllib.request, "urlopen", new=check_generic_headers):
test_url = "ftp://ftp.gnu.org/README"
user_context = user_context_fixture()
file_sources = configured_file_sources(FILE_SOURCES_CONF)
file_source_pair = file_sources.get_file_source_path(test_url)
Expand All @@ -108,14 +116,16 @@ def test_file_source_http_without_stock_generic():


def test_file_source_http_without_stock_specific():
test_url = "https://www.usegalaxy.org/myfile2.txt"

def check_specific_header(request, **kwargs):
assert request.headers["Authorization"] == "Bearer IBearTokens"
response: Any = io.StringIO("hello specific world 2")
response.headers = {}
response.geturl = lambda: test_url
return response

with mock.patch.object(urllib.request, "urlopen", new=check_specific_header):
test_url = "https://www.usegalaxy.org/myfile2.txt"
user_context = user_context_fixture()
file_sources = configured_file_sources(FILE_SOURCES_CONF_WITHOUT_STOCK)
file_source_pair = file_sources.get_file_source_path(test_url)
Expand Down

0 comments on commit d01b039

Please sign in to comment.