| 1 |
###################################################################### |
| 2 |
# |
| 3 |
# File: test/unit/console_tool/test_authorize_account.py |
| 4 |
# |
| 5 |
# Copyright 2023 Backblaze Inc. All Rights Reserved. |
| 6 |
# |
| 7 |
# License https://www.backblaze.com/using_b2_code.html |
| 8 |
# |
| 9 |
###################################################################### |
| 10 |
from unittest import mock |
| 11 |
|
| 12 |
import pytest |
| 13 |
from b2sdk.v2 import ALL_CAPABILITIES |
| 14 |
|
| 15 |
from b2._internal._cli.const import ( |
| 16 |
B2_APPLICATION_KEY_ENV_VAR, |
| 17 |
B2_APPLICATION_KEY_ID_ENV_VAR, |
| 18 |
B2_ENVIRONMENT_ENV_VAR, |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture |
| 23 |
def b2_cli_is_authorized_afterwards(b2_cli): |
| 24 |
assert b2_cli.account_info.get_account_auth_token() is None |
| 25 |
yield b2_cli |
| 26 |
assert b2_cli.account_info.get_account_auth_token() is not None |
| 27 |
|
| 28 |
|
| 29 |
def test_authorize_with_bad_key(b2_cli): |
| 30 |
expected_stdout = "" |
| 31 |
expected_stderr = """ |
| 32 |
ERROR: unable to authorize account: Invalid authorization token. Server said: secret key is wrong (unauthorized) |
| 33 |
""" |
| 34 |
|
| 35 |
b2_cli._run_command( |
| 36 |
["authorize-account", b2_cli.account_id, "bad-app-key"], |
| 37 |
expected_stdout, |
| 38 |
expected_stderr, |
| 39 |
1, |
| 40 |
) |
| 41 |
assert b2_cli.account_info.get_account_auth_token() is None |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.parametrize( |
| 45 |
"command", |
| 46 |
[ |
| 47 |
"authorize-account", |
| 48 |
"authorize_account", |
| 49 |
], |
| 50 |
) |
| 51 |
def test_authorize_with_good_key(b2_cli, b2_cli_is_authorized_afterwards, command): |
| 52 |
assert b2_cli.account_info.get_account_auth_token() is None |
| 53 |
|
| 54 |
expected_stderr = """ |
| 55 |
""" |
| 56 |
|
| 57 |
b2_cli._run_command([command, b2_cli.account_id, b2_cli.master_key], None, expected_stderr, 0) |
| 58 |
|
| 59 |
assert b2_cli.account_info.get_account_auth_token() is not None |
| 60 |
|
| 61 |
|
| 62 |
def test_authorize_using_env_variables(b2_cli): |
| 63 |
assert b2_cli.account_info.get_account_auth_token() is None |
| 64 |
|
| 65 |
expected_stderr = """ |
| 66 |
""" |
| 67 |
|
| 68 |
with mock.patch.dict( |
| 69 |
"os.environ", |
| 70 |
{ |
| 71 |
B2_APPLICATION_KEY_ID_ENV_VAR: b2_cli.account_id, |
| 72 |
B2_APPLICATION_KEY_ENV_VAR: b2_cli.master_key, |
| 73 |
}, |
| 74 |
): |
| 75 |
b2_cli._run_command(["authorize-account"], None, expected_stderr, 0) |
| 76 |
|
| 77 |
assert b2_cli.account_info.get_account_auth_token() is not None |
| 78 |
|
| 79 |
|
| 80 |
@pytest.mark.parametrize( |
| 81 |
"flags,realm_url", |
| 82 |
[ |
| 83 |
([], "http://production.example.com"), |
| 84 |
(["--debug-logs"], "http://production.example.com"), |
| 85 |
(["--environment", "http://custom.example.com"], "http://custom.example.com"), |
| 86 |
(["--environment", "production"], "http://production.example.com"), |
| 87 |
(["--dev"], "http://api.backblazeb2.xyz:8180"), |
| 88 |
(["--staging"], "https://api.backblaze.net"), |
| 89 |
], |
| 90 |
) |
| 91 |
def test_authorize_towards_realm( |
| 92 |
b2_cli, b2_cli_is_authorized_afterwards, flags, realm_url, cwd_path, b2_cli_log_fix |
| 93 |
): |
| 94 |
expected_stderr = f"Using {realm_url}\n" if any(f != "--debug-logs" for f in flags) else "" |
| 95 |
|
| 96 |
b2_cli._run_command( |
| 97 |
["authorize-account", *flags, b2_cli.account_id, b2_cli.master_key], |
| 98 |
None, |
| 99 |
expected_stderr, |
| 100 |
0, |
| 101 |
) |
| 102 |
log_path = cwd_path / "b2_cli.log" |
| 103 |
if "--debug-logs" in flags: |
| 104 |
assert f"Using {realm_url}\n" in log_path.read_text() |
| 105 |
else: |
| 106 |
assert not log_path.exists() |
| 107 |
|
| 108 |
|
| 109 |
def test_authorize_towards_custom_realm_using_env(b2_cli, b2_cli_is_authorized_afterwards): |
| 110 |
expected_stderr = """ |
| 111 |
Using http://custom2.example.com |
| 112 |
""" |
| 113 |
|
| 114 |
with mock.patch.dict( |
| 115 |
"os.environ", |
| 116 |
{ |
| 117 |
B2_ENVIRONMENT_ENV_VAR: "http://custom2.example.com", |
| 118 |
}, |
| 119 |
): |
| 120 |
b2_cli._run_command( |
| 121 |
["authorize-account", b2_cli.account_id, b2_cli.master_key], |
| 122 |
None, |
| 123 |
expected_stderr, |
| 124 |
0, |
| 125 |
) |
| 126 |
|
| 127 |
|
| 128 |
def test_authorize_account_prints_account_info(b2_cli): |
| 129 |
expected_json = { |
| 130 |
'accountAuthToken': 'auth_token_0', |
| 131 |
'accountFilePath': None, |
| 132 |
'accountId': 'account-0', |
| 133 |
'allowed': |
| 134 |
{ |
| 135 |
'bucketId': None, |
| 136 |
'bucketName': None, |
| 137 |
'capabilities': sorted(ALL_CAPABILITIES), |
| 138 |
'namePrefix': None, |
| 139 |
}, |
| 140 |
'apiUrl': 'http://api.example.com', |
| 141 |
'applicationKey': 'masterKey-0', |
| 142 |
'applicationKeyId': 'account-0', |
| 143 |
'downloadUrl': 'http://download.example.com', |
| 144 |
'isMasterKey': True, |
| 145 |
's3endpoint': 'http://s3.api.example.com' |
| 146 |
} |
| 147 |
|
| 148 |
b2_cli._run_command( |
| 149 |
['authorize-account', b2_cli.account_id, b2_cli.master_key], |
| 150 |
expected_stderr='', |
| 151 |
expected_status=0, |
| 152 |
expected_json_in_stdout=expected_json, |
| 153 |
) |