|
9 | 9 | import pytest |
10 | 10 | from click.testing import CliRunner |
11 | 11 |
|
12 | | -from rsconnect.json_web_token import is_jwt_compatible_python_version |
| 12 | +from rsconnect.json_web_token import SECRET_KEY_ENV, is_jwt_compatible_python_version |
13 | 13 |
|
14 | 14 | from .utils import ( |
15 | 15 | apply_common_args, |
@@ -567,6 +567,7 @@ def setUp(self): |
567 | 567 | self.mock_server = "http://localhost:8080" |
568 | 568 | self.mock_uri = "http://localhost:8080/__api__/v1/experimental/bootstrap" |
569 | 569 | self.jwt_keypath = "tests/testdata/jwt/secret.key" |
| 570 | + self.jwt_env_secret = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU=" |
570 | 571 |
|
571 | 572 | self.default_cli_args = [ |
572 | 573 | "bootstrap", |
@@ -617,6 +618,39 @@ def test_bootstrap(self): |
617 | 618 | expected_output = json.loads(open("tests/testdata/initial-admin-responses/success.json", "r").read()) |
618 | 619 | self.assertEqual(json_output, expected_output) |
619 | 620 |
|
| 621 | + @httpretty.activate(verbose=True, allow_net_connect=False) |
| 622 | + def test_bootstrap_env_var(self): |
| 623 | + """ |
| 624 | + Normal initial-admin operation if secret key is configured using an environment variable |
| 625 | + """ |
| 626 | + cli_args = [ |
| 627 | + "bootstrap", |
| 628 | + "--server", |
| 629 | + self.mock_server, |
| 630 | + "--insecure", |
| 631 | + ] |
| 632 | + |
| 633 | + callback = self.create_bootstrap_mock_callback(200, {"api_key": "testapikey123"}) |
| 634 | + |
| 635 | + httpretty.register_uri( |
| 636 | + httpretty.POST, |
| 637 | + self.mock_uri, |
| 638 | + body=callback, |
| 639 | + ) |
| 640 | + |
| 641 | + os.environ[SECRET_KEY_ENV] = self.jwt_env_secret |
| 642 | + |
| 643 | + runner = CliRunner() |
| 644 | + result = runner.invoke(cli, cli_args) |
| 645 | + |
| 646 | + self.assertEqual(result.exit_code, 0, result.output) |
| 647 | + |
| 648 | + json_output = json.loads(result.output) |
| 649 | + expected_output = json.loads(open("tests/testdata/initial-admin-responses/success.json", "r").read()) |
| 650 | + self.assertEqual(json_output, expected_output) |
| 651 | + |
| 652 | + del os.environ[SECRET_KEY_ENV] |
| 653 | + |
620 | 654 | @httpretty.activate(verbose=True, allow_net_connect=False) |
621 | 655 | def test_bootstrap_misc_error(self): |
622 | 656 | """ |
@@ -742,9 +776,46 @@ def test_bootstrap_invalid_server(self): |
742 | 776 | ) |
743 | 777 |
|
744 | 778 | def test_boostrap_missing_jwt_option(self): |
| 779 | + """ |
| 780 | + If jwt keyfile is not specified, it needs to be set using an environment variable |
| 781 | + """ |
| 782 | + runner = CliRunner() |
| 783 | + result = runner.invoke(cli, ["bootstrap", "--server", "http://a_server"]) |
| 784 | + self.assertEqual(result.exit_code, 1, result.output) |
| 785 | + self.assertEqual( |
| 786 | + result.output, "Error: Must specify secret key using either a keyfile or environment variable.\n" |
| 787 | + ) |
| 788 | + |
| 789 | + def test_bootstrap_conflicting_jwt_option(self): |
| 790 | + """ |
| 791 | + If jwt keyfile is specified, it cannot also be set using an environment variable |
| 792 | + """ |
| 793 | + |
| 794 | + os.environ[SECRET_KEY_ENV] = "a_value" |
| 795 | + runner = CliRunner() |
| 796 | + result = runner.invoke(cli, self.default_cli_args) |
| 797 | + self.assertEqual(result.exit_code, 1, result.output) |
| 798 | + self.assertEqual( |
| 799 | + result.output, "Error: Cannot specify secret key using both a keyfile and environment variable.\n" |
| 800 | + ) |
| 801 | + |
| 802 | + del os.environ[SECRET_KEY_ENV] |
| 803 | + |
| 804 | + def test_bootstrap_invalid_env_secret_key(self): |
| 805 | + """ |
| 806 | + If jwt env variable is specified, it needs to be a valid base64-encoded value |
| 807 | + """ |
| 808 | + |
| 809 | + os.environ[SECRET_KEY_ENV] = "a_value" |
745 | 810 | runner = CliRunner() |
746 | 811 | result = runner.invoke(cli, ["bootstrap", "--server", "http://a_server"]) |
747 | | - self.assertEqual(result.exit_code, 2, result.output) |
| 812 | + self.assertEqual(result.exit_code, 1, result.output) |
| 813 | + self.assertEqual( |
| 814 | + result.output, |
| 815 | + "Error: Unable to decode base64 data from environment variable: CONNECT_BOOTSTRAP_SECRETKEY\n", |
| 816 | + ) |
| 817 | + |
| 818 | + del os.environ[SECRET_KEY_ENV] |
748 | 819 |
|
749 | 820 | @httpretty.activate(verbose=True, allow_net_connect=False) |
750 | 821 | def test_bootstrap_raw_output(self): |
|
0 commit comments