Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Make Azure functions call for access token #13

Merged
merged 1 commit into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/rhelocator/update_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,15 @@ def get_azure_access_token() -> str:
return str(resp.json().get("access_token", None))


def get_azure_locations(access_token: str) -> list[str]:
def get_azure_locations() -> list[str]:
"""Get a list of all Azure locations.

Azure API docs:
https://learn.microsoft.com/en-us/rest/api/resources/subscriptions/list-locations?tabs=HTTP

Args:
access_token: Valid Azure access token from get_azure_access_token().
https://learn.microsoft.com/en-us/rest/api/resources/subscriptions/list-locations?tabs=HTTP

Returns:
List of valid Azure regions.
"""
access_token = get_azure_access_token()
headers = {"Authorization": f"Bearer {access_token}"}
params = {"api-version": "2020-01-01"}
url = (
Expand Down
22 changes: 13 additions & 9 deletions tests/test_update_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from unittest.mock import Mock
from unittest.mock import patch

from src.rhelocator import update_images
from rhelocator import update_images


def test_get_aws_regions() -> None:
Expand All @@ -26,8 +26,8 @@ def test_get_aws_hourly_images() -> None:
)


@patch("src.rhelocator.update_images.get_aws_hourly_images")
@patch("src.rhelocator.update_images.get_aws_regions")
@patch("rhelocator.update_images.get_aws_hourly_images")
@patch("rhelocator.update_images.get_aws_regions")
def test_get_aws_all_hourly_images(
mock_regions: MagicMock, mock_images: MagicMock
) -> None:
Expand All @@ -45,14 +45,14 @@ def test_get_aws_all_hourly_images(


@patch("rhelocator.update_images.requests.post")
def test_get_azure_access_token(mock_requests: MagicMock) -> None:
def test_get_azure_access_token(mock_post) -> None:
"""Test retrieving Azure locations."""
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"foo": "bar", "access_token": "secrete"}
mock_requests.return_value = mock_response
auth_response = {"foo": "bar", "access_token": "secrete"}
mock_post.return_value = Mock(ok=True)
mock_post.return_value.json.return_value = auth_response

access_token = update_images.get_azure_access_token()

assert access_token == "secrete" # nosec B105


Expand Down Expand Up @@ -87,7 +87,11 @@ def test_get_azure_locations(mock_requests: MagicMock) -> None:
}
mock_requests.return_value = mock_response

regions = update_images.get_azure_locations("secrete")
# Fake the access token.
with patch("rhelocator.update_images.get_azure_access_token") as mock_token:
mock_token.return_value = "secrete"
regions = update_images.get_azure_locations()

assert regions == ["eastus"]


Expand Down