|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +import snowflake.connector.aio |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.skipolddriver |
| 12 | +async def test_spcs_token_included_in_login_request_async(monkeypatch): |
| 13 | + """Verify that SPCS_TOKEN is injected into async login request body when present.""" |
| 14 | + |
| 15 | + custom_path = "/custom/path/to/spcs_token" |
| 16 | + monkeypatch.setenv("SF_SPCS_TOKEN_PATH", custom_path) |
| 17 | + monkeypatch.setattr( |
| 18 | + "snowflake.connector._utils.os.path.isfile", |
| 19 | + lambda path: path == custom_path, |
| 20 | + raising=False, |
| 21 | + ) |
| 22 | + mock_open = mock.mock_open(read_data="TEST_SPCS_TOKEN_ASYNC") |
| 23 | + monkeypatch.setattr("snowflake.connector._utils.open", mock_open, raising=False) |
| 24 | + |
| 25 | + captured_requests: list[tuple[str, dict]] = [] |
| 26 | + |
| 27 | + async def mock_post_request(url, headers, body, **kwargs): |
| 28 | + captured_requests.append((url, json.loads(body))) |
| 29 | + return { |
| 30 | + "success": True, |
| 31 | + "message": None, |
| 32 | + "data": { |
| 33 | + "token": "TOKEN_ASYNC", |
| 34 | + "masterToken": "MASTER_TOKEN_ASYNC", |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + with mock.patch( |
| 39 | + "snowflake.connector.aio._network.SnowflakeRestful._post_request", |
| 40 | + side_effect=mock_post_request, |
| 41 | + ): |
| 42 | + conn = snowflake.connector.aio.SnowflakeConnection( |
| 43 | + account="testaccount", |
| 44 | + user="testuser", |
| 45 | + password="testpwd", |
| 46 | + host="testaccount.snowflakecomputing.com", |
| 47 | + ) |
| 48 | + await conn.connect() |
| 49 | + assert conn._rest.token == "TOKEN_ASYNC" |
| 50 | + assert conn._rest.master_token == "MASTER_TOKEN_ASYNC" |
| 51 | + await conn.close() |
| 52 | + |
| 53 | + # Exactly one login-request should have been sent for this simple flow |
| 54 | + login_bodies = [body for (url, body) in captured_requests if "login-request" in url] |
| 55 | + assert len(login_bodies) == 1 |
| 56 | + body = login_bodies[0] |
| 57 | + assert body["data"]["SPCS_TOKEN"] == "TEST_SPCS_TOKEN_ASYNC" |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.skipolddriver |
| 61 | +async def test_spcs_token_not_included_when_file_missing_async(monkeypatch): |
| 62 | + """Verify that SPCS_TOKEN is not added to async login request when file does not exist.""" |
| 63 | + |
| 64 | + monkeypatch.delenv("SF_SPCS_TOKEN_PATH", raising=False) |
| 65 | + |
| 66 | + captured_requests: list[tuple[str, dict]] = [] |
| 67 | + |
| 68 | + async def mock_post_request(url, headers, body, **kwargs): |
| 69 | + captured_requests.append((url, json.loads(body))) |
| 70 | + return { |
| 71 | + "success": True, |
| 72 | + "message": None, |
| 73 | + "data": { |
| 74 | + "token": "TOKEN_ASYNC", |
| 75 | + "masterToken": "MASTER_TOKEN_ASYNC", |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + with mock.patch( |
| 80 | + "snowflake.connector.aio._network.SnowflakeRestful._post_request", |
| 81 | + side_effect=mock_post_request, |
| 82 | + ): |
| 83 | + conn = snowflake.connector.aio.SnowflakeConnection( |
| 84 | + account="testaccount", |
| 85 | + user="testuser", |
| 86 | + password="testpwd", |
| 87 | + host="testaccount.snowflakecomputing.com", |
| 88 | + ) |
| 89 | + await conn.connect() |
| 90 | + assert conn._rest.token == "TOKEN_ASYNC" |
| 91 | + assert conn._rest.master_token == "MASTER_TOKEN_ASYNC" |
| 92 | + await conn.close() |
| 93 | + |
| 94 | + # Exactly one login-request should have been sent for this simple flow |
| 95 | + login_bodies = [body for (url, body) in captured_requests if "login-request" in url] |
| 96 | + assert len(login_bodies) == 1 |
| 97 | + body = login_bodies[0] |
| 98 | + assert "SPCS_TOKEN" not in body["data"] |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.skipolddriver |
| 102 | +async def test_spcs_token_default_path_used_when_env_unset_async( |
| 103 | + monkeypatch, |
| 104 | +): |
| 105 | + """When SF_SPCS_TOKEN_PATH is not set, default path should be used (async).""" |
| 106 | + |
| 107 | + monkeypatch.delenv("SF_SPCS_TOKEN_PATH", raising=False) |
| 108 | + |
| 109 | + default_path = "/snowflake/session/spcs_token" |
| 110 | + monkeypatch.setattr( |
| 111 | + "snowflake.connector._utils.os.path.isfile", |
| 112 | + lambda path: path == default_path, |
| 113 | + raising=False, |
| 114 | + ) |
| 115 | + mock_open = mock.mock_open(read_data="DEFAULT_PATH_SPCS_TOKEN_ASYNC") |
| 116 | + monkeypatch.setattr("snowflake.connector._utils.open", mock_open, raising=False) |
| 117 | + |
| 118 | + captured_requests: list[tuple[str, dict]] = [] |
| 119 | + |
| 120 | + async def mock_post_request(url, headers, body, **kwargs): |
| 121 | + captured_requests.append((url, json.loads(body))) |
| 122 | + return { |
| 123 | + "success": True, |
| 124 | + "message": None, |
| 125 | + "data": { |
| 126 | + "token": "TOKEN_ASYNC", |
| 127 | + "masterToken": "MASTER_TOKEN_ASYNC", |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + with mock.patch( |
| 132 | + "snowflake.connector.aio._network.SnowflakeRestful._post_request", |
| 133 | + side_effect=mock_post_request, |
| 134 | + ): |
| 135 | + conn = snowflake.connector.aio.SnowflakeConnection( |
| 136 | + account="testaccount", |
| 137 | + user="testuser", |
| 138 | + password="testpwd", |
| 139 | + host="testaccount.snowflakecomputing.com", |
| 140 | + ) |
| 141 | + await conn.connect() |
| 142 | + assert conn._rest.token == "TOKEN_ASYNC" |
| 143 | + assert conn._rest.master_token == "MASTER_TOKEN_ASYNC" |
| 144 | + await conn.close() |
| 145 | + |
| 146 | + # Exactly one login-request should have been sent for this simple flow |
| 147 | + login_bodies = [body for (url, body) in captured_requests if "login-request" in url] |
| 148 | + assert len(login_bodies) == 1 |
| 149 | + body = login_bodies[0] |
| 150 | + assert body["data"]["SPCS_TOKEN"] == "DEFAULT_PATH_SPCS_TOKEN_ASYNC" |
0 commit comments