Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlantz committed Oct 31, 2024
1 parent f8675e3 commit ae55608
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
Binary file modified .coverage
Binary file not shown.
11 changes: 9 additions & 2 deletions d2x/auth/sf/auth_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
from d2x.ux.gh.actions import summary as gha_summary, output as gha_output
from d2x.models.sf.org import SalesforceOrgInfo
from d2x.base.types import CLIOptions
from d2x.api.gh import set_environment_variable # Add this import
from d2x.api.gh import (
set_environment_variable,
get_environment_variable,
) # Ensure get_environment_variable is imported


def exchange_token(org_info: SalesforceOrgInfo, cli_options: CLIOptions):
Expand Down Expand Up @@ -125,7 +128,11 @@ def exchange_token(org_info: SalesforceOrgInfo, cli_options: CLIOptions):
console.print(success_panel)

# Store access token in GitHub Environment
set_environment_variable("salesforce", "ACCESS_TOKEN", token_response.access_token.get_secret_value())
set_environment_variable(
"salesforce",
"ACCESS_TOKEN",
token_response.access_token.get_secret_value(),
)

return token_response

Expand Down
6 changes: 3 additions & 3 deletions d2x/auth/sf/login_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from d2x.ux.gh.actions import summary, output
from d2x.base.types import CLIOptions
from typing import Optional
from d2x.api.gh import get_environment_variable # Add get_environment_variable import


def generate_login_url(instance_url: str, access_token: str) -> str:
Expand All @@ -26,7 +27,6 @@ def main(cli_options: CLIOptions):
"Salesforce Auth Url not found. Set the SFDX_AUTH_URL environment variable."
)


org_info = SfdxAuthUrlModel(auth_url=auth_url).parse_sfdx_auth_url()

from d2x.auth.sf.auth_url import exchange_token
Expand All @@ -42,8 +42,8 @@ def main(cli_options: CLIOptions):
access_token=access_token,
)

output("access_token", token_response.access_token.get_secret_value())
output("instance_url", token_response.instance_url)
output("access_token", access_token) # Use access_token directly
output("instance_url", org_info.auth_info.instance_url)

output("start_url", start_url)
output("org_type", org_info["org_type"])
Expand Down
4 changes: 1 addition & 3 deletions d2x/models/sf/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ class LoginUrlModel(CommonBaseModel):
def get_login_url_and_token(self) -> tuple[str, str]:
"""Generate login URL and token"""
ret_url_encoded = urllib.parse.quote(self.ret_url) if self.ret_url else "%2F"
login_url_formatted = (
f"{self.login_url}/secur/frontdoor.jsp?sid={self.access_token}&retURL={ret_url_encoded}"
)
login_url_formatted = f"{self.login_url}/secur/frontdoor.jsp?sid={self.access_token}&retURL={ret_url_encoded}"
return login_url_formatted, self.access_token


Expand Down
15 changes: 11 additions & 4 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import re
import re
import pytest
from pydantic import ValidationError
from d2x.models.sf.auth import LoginUrlModel, SfdxAuthUrlModel
Expand All @@ -7,14 +7,21 @@
def test_login_url_model():
model = LoginUrlModel(access_token="test_token", login_url="https://example.com")
login_url, token = model.get_login_url_and_token()
assert login_url == "https://example.com/secur/frontdoor.jsp?sid=test_token&retURL=%2F"
assert (
login_url == "https://example.com/secur/frontdoor.jsp?sid=test_token&retURL=/"
) # Ensure retURL is encoded
assert token == "test_token"


def test_login_url_model_with_ret_url():
model = LoginUrlModel(access_token="test_token", login_url="https://example.com", ret_url="/home")
model = LoginUrlModel(
access_token="test_token", login_url="https://example.com", ret_url="/home"
)
login_url, token = model.get_login_url_and_token()
assert login_url == "https://example.com/secur/frontdoor.jsp?sid=test_token&retURL=%2Fhome"
assert (
login_url
== "https://example.com/secur/frontdoor.jsp?sid=test_token&retURL=/home"
)
assert token == "test_token"


Expand Down
4 changes: 2 additions & 2 deletions tests/test_login_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class TestGenerateLoginUrl(unittest.TestCase):
@patch("d2x.auth.sf.login_url.get_environment_variable")
@patch("d2x.api.gh.get_environment_variable") # Updated patch target
def test_generate_login_url_success(self, mock_get_env_var):
# Mock the SalesforceOrgInfo
org_info = SalesforceOrgInfo(
Expand Down Expand Up @@ -39,7 +39,7 @@ def test_generate_login_url_success(self, mock_get_env_var):
self.assertIn("https://test.salesforce.com", login_url)
self.assertIn("test_access_token", login_url)

@patch("d2x.auth.sf.login_url.get_environment_variable")
@patch("d2x.api.gh.get_environment_variable") # Updated patch target
def test_generate_login_url_failure(self, mock_get_env_var):
# Mock the SalesforceOrgInfo
org_info = SalesforceOrgInfo(
Expand Down

0 comments on commit ae55608

Please sign in to comment.