|
| 1 | +import json |
| 2 | +from unittest.mock import MagicMock, AsyncMock, patch |
| 3 | + |
| 4 | + |
| 5 | +""" |
| 6 | +Verify commands: |
| 7 | +* btcli s create |
| 8 | +* btcli s set-identity |
| 9 | +* btcli s get-identity |
| 10 | +""" |
| 11 | + |
| 12 | + |
| 13 | +def test_set_id(local_chain, wallet_setup): |
| 14 | + """ |
| 15 | + Tests that the user is prompted to confirm that the incorrect text/html URL is |
| 16 | + indeed the one they wish to set as their logo URL, and that when the MIME type is 'image/jpeg' |
| 17 | + they are not given this prompt. |
| 18 | + """ |
| 19 | + wallet_path_alice = "//Alice" |
| 20 | + netuid = 2 |
| 21 | + |
| 22 | + # Create wallet for Alice |
| 23 | + keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = wallet_setup( |
| 24 | + wallet_path_alice |
| 25 | + ) |
| 26 | + # Register a subnet with sudo as Alice |
| 27 | + result = exec_command_alice( |
| 28 | + command="subnets", |
| 29 | + sub_command="create", |
| 30 | + extra_args=[ |
| 31 | + "--wallet-path", |
| 32 | + wallet_path_alice, |
| 33 | + "--chain", |
| 34 | + "ws://127.0.0.1:9945", |
| 35 | + "--wallet-name", |
| 36 | + wallet_alice.name, |
| 37 | + "--wallet-hotkey", |
| 38 | + wallet_alice.hotkey_str, |
| 39 | + "--subnet-name", |
| 40 | + "Test Subnet", |
| 41 | + "--repo", |
| 42 | + "https://github.com/username/repo", |
| 43 | + "--contact", |
| 44 | + "alice@opentensor.dev", |
| 45 | + "--url", |
| 46 | + "https://testsubnet.com", |
| 47 | + "--discord", |
| 48 | + "alice#1234", |
| 49 | + "--description", |
| 50 | + "A test subnet for e2e testing", |
| 51 | + "--additional-info", |
| 52 | + "Created by Alice", |
| 53 | + "--logo-url", |
| 54 | + "https://testsubnet.com/logo.png", |
| 55 | + "--no-prompt", |
| 56 | + "--json-output", |
| 57 | + ], |
| 58 | + ) |
| 59 | + result_output = json.loads(result.stdout) |
| 60 | + assert result_output["success"] is True |
| 61 | + |
| 62 | + mock_response = MagicMock() |
| 63 | + mock_response.status = 200 |
| 64 | + mock_response.content_type = "text/html" # bad MIME type |
| 65 | + mock_response.__aenter__ = AsyncMock(return_value=mock_response) |
| 66 | + mock_response.__aexit__ = AsyncMock(return_value=None) |
| 67 | + |
| 68 | + mock_session = MagicMock() |
| 69 | + mock_session.get = MagicMock(return_value=mock_response) |
| 70 | + mock_session.__aenter__ = AsyncMock(return_value=mock_session) |
| 71 | + mock_session.__aexit__ = AsyncMock(return_value=None) |
| 72 | + |
| 73 | + with patch("aiohttp.ClientSession", return_value=mock_session): |
| 74 | + set_identity = exec_command_alice( |
| 75 | + "subnets", |
| 76 | + "set-identity", |
| 77 | + extra_args=[ |
| 78 | + "--wallet-path", |
| 79 | + wallet_path_alice, |
| 80 | + "--wallet-name", |
| 81 | + wallet_alice.name, |
| 82 | + "--hotkey", |
| 83 | + wallet_alice.hotkey_str, |
| 84 | + "--chain", |
| 85 | + "ws://127.0.0.1:9945", |
| 86 | + "--netuid", |
| 87 | + str(netuid), |
| 88 | + "--subnet-name", |
| 89 | + sn_name := "Test Subnet", |
| 90 | + "--github-repo", |
| 91 | + sn_github := "https://github.com/username/repo", |
| 92 | + "--subnet-contact", |
| 93 | + sn_contact := "alice@opentensor.dev", |
| 94 | + "--subnet-url", |
| 95 | + sn_url := "https://testsubnet.com", |
| 96 | + "--discord", |
| 97 | + sn_discord := "alice#1234", |
| 98 | + "--description", |
| 99 | + sn_description := "A test subnet for e2e testing", |
| 100 | + "--logo-url", |
| 101 | + sn_logo_url := "https://testsubnet.com/logo.png", |
| 102 | + "--additional-info", |
| 103 | + sn_add_info := "Created by Alice", |
| 104 | + "--prompt", |
| 105 | + ], |
| 106 | + inputs=["Y", "Y"], |
| 107 | + ) |
| 108 | + assert ( |
| 109 | + f"Are you sure you want to use {sn_logo_url} as your image URL?" |
| 110 | + in set_identity.stdout |
| 111 | + ) |
| 112 | + get_identity = exec_command_alice( |
| 113 | + "subnets", |
| 114 | + "get-identity", |
| 115 | + extra_args=[ |
| 116 | + "--chain", |
| 117 | + "ws://127.0.0.1:9945", |
| 118 | + "--netuid", |
| 119 | + netuid, |
| 120 | + "--json-output", |
| 121 | + ], |
| 122 | + ) |
| 123 | + get_identity_output = json.loads(get_identity.stdout) |
| 124 | + assert get_identity_output["subnet_name"] == sn_name |
| 125 | + assert get_identity_output["github_repo"] == sn_github |
| 126 | + assert get_identity_output["subnet_contact"] == sn_contact |
| 127 | + assert get_identity_output["subnet_url"] == sn_url |
| 128 | + assert get_identity_output["discord"] == sn_discord |
| 129 | + assert get_identity_output["description"] == sn_description |
| 130 | + assert get_identity_output["logo_url"] == sn_logo_url |
| 131 | + assert get_identity_output["additional"] == sn_add_info |
| 132 | + |
| 133 | + mock_response = MagicMock() |
| 134 | + mock_response.status = 200 |
| 135 | + mock_response.content_type = "image/jpeg" # good MIME type |
| 136 | + mock_response.__aenter__ = AsyncMock(return_value=mock_response) |
| 137 | + mock_response.__aexit__ = AsyncMock(return_value=None) |
| 138 | + |
| 139 | + mock_session = MagicMock() |
| 140 | + mock_session.get = MagicMock(return_value=mock_response) |
| 141 | + mock_session.__aenter__ = AsyncMock(return_value=mock_session) |
| 142 | + mock_session.__aexit__ = AsyncMock(return_value=None) |
| 143 | + with patch("aiohttp.ClientSession", return_value=mock_session): |
| 144 | + set_identity = exec_command_alice( |
| 145 | + "subnets", |
| 146 | + "set-identity", |
| 147 | + extra_args=[ |
| 148 | + "--wallet-path", |
| 149 | + wallet_path_alice, |
| 150 | + "--wallet-name", |
| 151 | + wallet_alice.name, |
| 152 | + "--hotkey", |
| 153 | + wallet_alice.hotkey_str, |
| 154 | + "--chain", |
| 155 | + "ws://127.0.0.1:9945", |
| 156 | + "--netuid", |
| 157 | + str(netuid), |
| 158 | + "--subnet-name", |
| 159 | + sn_name := "Test Subnet", |
| 160 | + "--github-repo", |
| 161 | + sn_github := "https://github.com/username/repo", |
| 162 | + "--subnet-contact", |
| 163 | + sn_contact := "alice@opentensor.dev", |
| 164 | + "--subnet-url", |
| 165 | + sn_url := "https://testsubnet.com", |
| 166 | + "--discord", |
| 167 | + sn_discord := "alice#1234", |
| 168 | + "--description", |
| 169 | + sn_description := "A test subnet for e2e testing", |
| 170 | + "--logo-url", |
| 171 | + sn_logo_url := "https://testsubnet.com/logo.png", |
| 172 | + "--additional-info", |
| 173 | + sn_add_info := "Created by Alice", |
| 174 | + "--prompt", |
| 175 | + ], |
| 176 | + inputs=["Y"], |
| 177 | + ) |
| 178 | + assert ( |
| 179 | + f"Are you sure you want to use {sn_logo_url} as your image URL?" |
| 180 | + not in set_identity.stdout |
| 181 | + ) |
| 182 | + get_identity = exec_command_alice( |
| 183 | + "subnets", |
| 184 | + "get-identity", |
| 185 | + extra_args=[ |
| 186 | + "--chain", |
| 187 | + "ws://127.0.0.1:9945", |
| 188 | + "--netuid", |
| 189 | + netuid, |
| 190 | + "--json-output", |
| 191 | + ], |
| 192 | + ) |
| 193 | + get_identity_output = json.loads(get_identity.stdout) |
| 194 | + assert get_identity_output["subnet_name"] == sn_name |
| 195 | + assert get_identity_output["github_repo"] == sn_github |
| 196 | + assert get_identity_output["subnet_contact"] == sn_contact |
| 197 | + assert get_identity_output["subnet_url"] == sn_url |
| 198 | + assert get_identity_output["discord"] == sn_discord |
| 199 | + assert get_identity_output["description"] == sn_description |
| 200 | + assert get_identity_output["logo_url"] == sn_logo_url |
| 201 | + assert get_identity_output["additional"] == sn_add_info |
0 commit comments