|
6 | 6 | import sys |
7 | 7 | import io |
8 | 8 | from rsconnect.exception import RSConnectException |
| 9 | +from rsconnect.models import AppModes |
9 | 10 | from .utils import ( |
10 | 11 | require_api_key, |
11 | 12 | require_connect, |
12 | 13 | ) |
13 | | -from rsconnect.api import RSConnectClient, RSConnectExecutor, RSConnectServer, _to_server_check_list |
| 14 | +from rsconnect.api import ( |
| 15 | + RSConnectClient, |
| 16 | + RSConnectExecutor, |
| 17 | + RSConnectServer, |
| 18 | + _to_server_check_list, |
| 19 | + CloudService, |
| 20 | + PositClient, |
| 21 | + CloudServer, |
| 22 | +) |
14 | 23 |
|
15 | 24 |
|
16 | 25 | class TestAPI(TestCase): |
@@ -207,3 +216,185 @@ def test_deploy_existing_application_with_failure(self): |
207 | 216 | app_id = Mock() |
208 | 217 | with self.assertRaises(RSConnectException): |
209 | 218 | client.deploy(app_id, app_name=None, app_title=None, title_is_default=None, tarball=None) |
| 219 | + |
| 220 | + |
| 221 | +class CloudServiceTestCase(TestCase): |
| 222 | + def test_prepare_new_deploy(self): |
| 223 | + cloud_client = Mock(spec=PositClient) |
| 224 | + server = CloudServer("https://api.posit.cloud", "the_account", "the_token", "the_secret") |
| 225 | + project_application_id = "20" |
| 226 | + cloud_service = CloudService( |
| 227 | + cloud_client=cloud_client, server=server, project_application_id=project_application_id |
| 228 | + ) |
| 229 | + |
| 230 | + app_id = None |
| 231 | + app_name = "my app" |
| 232 | + bundle_size = 5000 |
| 233 | + bundle_hash = "the_hash" |
| 234 | + app_mode = AppModes.PYTHON_SHINY |
| 235 | + |
| 236 | + cloud_client.get_application.return_value = { |
| 237 | + "content_id": 2, |
| 238 | + } |
| 239 | + cloud_client.get_content.return_value = { |
| 240 | + "space_id": 1000, |
| 241 | + } |
| 242 | + cloud_client.create_output.return_value = { |
| 243 | + "id": 1, |
| 244 | + "source_id": 10, |
| 245 | + "url": "https://posit.cloud/content/1", |
| 246 | + } |
| 247 | + cloud_client.create_bundle.return_value = { |
| 248 | + "id": 100, |
| 249 | + "presigned_url": "https://presigned.url", |
| 250 | + "presigned_checksum": "the_checksum", |
| 251 | + } |
| 252 | + |
| 253 | + prepare_deploy_result = cloud_service.prepare_deploy( |
| 254 | + app_id=app_id, |
| 255 | + app_name=app_name, |
| 256 | + bundle_size=bundle_size, |
| 257 | + bundle_hash=bundle_hash, |
| 258 | + app_mode=app_mode, |
| 259 | + app_store_version=1, |
| 260 | + ) |
| 261 | + |
| 262 | + cloud_client.get_application.assert_called_with(project_application_id) |
| 263 | + cloud_client.get_content.assert_called_with(2) |
| 264 | + cloud_client.create_output.assert_called_with( |
| 265 | + name=app_name, application_type="connect", project_id=2, space_id=1000 |
| 266 | + ) |
| 267 | + cloud_client.create_bundle.assert_called_with(10, "application/x-tar", bundle_size, bundle_hash) |
| 268 | + |
| 269 | + assert prepare_deploy_result.app_id == 1 |
| 270 | + assert prepare_deploy_result.application_id == 10 |
| 271 | + assert prepare_deploy_result.app_url == "https://posit.cloud/content/1" |
| 272 | + assert prepare_deploy_result.bundle_id == 100 |
| 273 | + assert prepare_deploy_result.presigned_url == "https://presigned.url" |
| 274 | + assert prepare_deploy_result.presigned_checksum == "the_checksum" |
| 275 | + |
| 276 | + def test_prepare_redeploy(self): |
| 277 | + cloud_client = Mock(spec=PositClient) |
| 278 | + server = CloudServer("https://api.posit.cloud", "the_account", "the_token", "the_secret") |
| 279 | + project_application_id = "20" |
| 280 | + cloud_service = CloudService( |
| 281 | + cloud_client=cloud_client, server=server, project_application_id=project_application_id |
| 282 | + ) |
| 283 | + |
| 284 | + app_id = 1 |
| 285 | + app_name = "my app" |
| 286 | + bundle_size = 5000 |
| 287 | + bundle_hash = "the_hash" |
| 288 | + app_mode = AppModes.PYTHON_SHINY |
| 289 | + |
| 290 | + cloud_client.get_content.return_value = {"id": 1, "source_id": 10, "url": "https://posit.cloud/content/1"} |
| 291 | + cloud_client.create_bundle.return_value = { |
| 292 | + "id": 100, |
| 293 | + "presigned_url": "https://presigned.url", |
| 294 | + "presigned_checksum": "the_checksum", |
| 295 | + } |
| 296 | + |
| 297 | + prepare_deploy_result = cloud_service.prepare_deploy( |
| 298 | + app_id=app_id, |
| 299 | + app_name=app_name, |
| 300 | + bundle_size=bundle_size, |
| 301 | + bundle_hash=bundle_hash, |
| 302 | + app_mode=app_mode, |
| 303 | + app_store_version=1, |
| 304 | + ) |
| 305 | + cloud_client.get_content.assert_called_with(1) |
| 306 | + cloud_client.create_bundle.assert_called_with(10, "application/x-tar", bundle_size, bundle_hash) |
| 307 | + |
| 308 | + assert prepare_deploy_result.app_id == 1 |
| 309 | + assert prepare_deploy_result.application_id == 10 |
| 310 | + assert prepare_deploy_result.app_url == "https://posit.cloud/content/1" |
| 311 | + assert prepare_deploy_result.bundle_id == 100 |
| 312 | + assert prepare_deploy_result.presigned_url == "https://presigned.url" |
| 313 | + assert prepare_deploy_result.presigned_checksum == "the_checksum" |
| 314 | + |
| 315 | + def test_prepare_redeploy_static(self): |
| 316 | + cloud_client = Mock(spec=PositClient) |
| 317 | + server = CloudServer("https://api.posit.cloud", "the_account", "the_token", "the_secret") |
| 318 | + project_application_id = "20" |
| 319 | + cloud_service = CloudService( |
| 320 | + cloud_client=cloud_client, server=server, project_application_id=project_application_id |
| 321 | + ) |
| 322 | + |
| 323 | + app_id = 1 |
| 324 | + app_name = "my app" |
| 325 | + bundle_size = 5000 |
| 326 | + bundle_hash = "the_hash" |
| 327 | + app_mode = AppModes.STATIC |
| 328 | + |
| 329 | + cloud_client.get_content.return_value = {"id": 1, "source_id": 10, "url": "https://posit.cloud/content/1"} |
| 330 | + cloud_client.create_revision.return_value = { |
| 331 | + "application_id": 11, |
| 332 | + } |
| 333 | + cloud_client.create_bundle.return_value = { |
| 334 | + "id": 100, |
| 335 | + "presigned_url": "https://presigned.url", |
| 336 | + "presigned_checksum": "the_checksum", |
| 337 | + } |
| 338 | + |
| 339 | + prepare_deploy_result = cloud_service.prepare_deploy( |
| 340 | + app_id=app_id, |
| 341 | + app_name=app_name, |
| 342 | + bundle_size=bundle_size, |
| 343 | + bundle_hash=bundle_hash, |
| 344 | + app_mode=app_mode, |
| 345 | + app_store_version=1, |
| 346 | + ) |
| 347 | + cloud_client.get_content.assert_called_with(1) |
| 348 | + cloud_client.create_revision.assert_called_with(1) |
| 349 | + cloud_client.create_bundle.assert_called_with(11, "application/x-tar", bundle_size, bundle_hash) |
| 350 | + |
| 351 | + assert prepare_deploy_result.app_id == 1 |
| 352 | + assert prepare_deploy_result.application_id == 11 |
| 353 | + assert prepare_deploy_result.app_url == "https://posit.cloud/content/1" |
| 354 | + assert prepare_deploy_result.bundle_id == 100 |
| 355 | + assert prepare_deploy_result.presigned_url == "https://presigned.url" |
| 356 | + assert prepare_deploy_result.presigned_checksum == "the_checksum" |
| 357 | + |
| 358 | + def test_prepare_redeploy_preversioned_app_store(self): |
| 359 | + cloud_client = Mock(spec=PositClient) |
| 360 | + server = CloudServer("https://api.posit.cloud", "the_account", "the_token", "the_secret") |
| 361 | + project_application_id = "20" |
| 362 | + cloud_service = CloudService( |
| 363 | + cloud_client=cloud_client, server=server, project_application_id=project_application_id |
| 364 | + ) |
| 365 | + |
| 366 | + app_id = 10 |
| 367 | + app_name = "my app" |
| 368 | + bundle_size = 5000 |
| 369 | + bundle_hash = "the_hash" |
| 370 | + app_mode = AppModes.PYTHON_SHINY |
| 371 | + |
| 372 | + cloud_client.get_application.return_value = { |
| 373 | + "id": 10, |
| 374 | + "content_id": 1, |
| 375 | + } |
| 376 | + cloud_client.get_content.return_value = {"id": 1, "source_id": 10, "url": "https://posit.cloud/content/1"} |
| 377 | + cloud_client.create_bundle.return_value = { |
| 378 | + "id": 100, |
| 379 | + "presigned_url": "https://presigned.url", |
| 380 | + "presigned_checksum": "the_checksum", |
| 381 | + } |
| 382 | + |
| 383 | + prepare_deploy_result = cloud_service.prepare_deploy( |
| 384 | + app_id=app_id, |
| 385 | + app_name=app_name, |
| 386 | + bundle_size=bundle_size, |
| 387 | + bundle_hash=bundle_hash, |
| 388 | + app_mode=app_mode, |
| 389 | + app_store_version=None, |
| 390 | + ) |
| 391 | + cloud_client.get_application.assert_called_with(10) |
| 392 | + cloud_client.get_content.assert_called_with(1) |
| 393 | + cloud_client.create_bundle.assert_called_with(10, "application/x-tar", bundle_size, bundle_hash) |
| 394 | + |
| 395 | + assert prepare_deploy_result.app_id == 1 |
| 396 | + assert prepare_deploy_result.application_id == 10 |
| 397 | + assert prepare_deploy_result.app_url == "https://posit.cloud/content/1" |
| 398 | + assert prepare_deploy_result.bundle_id == 100 |
| 399 | + assert prepare_deploy_result.presigned_url == "https://presigned.url" |
| 400 | + assert prepare_deploy_result.presigned_checksum == "the_checksum" |
0 commit comments