@@ -297,4 +297,81 @@ defmodule Livebook.TeamsTest do
297297 refute Livebook.Hubs . hub_exists? ( team . id )
298298 end
299299 end
300+
301+ describe "deploy_app_from_cli/2" do
302+ @ describetag teams_for: :user
303+
304+ @ tag :tmp_dir
305+ test "deploys app to Teams using a CLI session" ,
306+ % { team: team , node: node , tmp_dir: tmp_dir , org: org } do
307+ % { id: id , name: name } =
308+ TeamsRPC . create_deployment_group ( node ,
309+ name: "angry-cat-#{ Ecto.UUID . generate ( ) } " ,
310+ url: "http://localhost:4123" ,
311+ mode: :online ,
312+ org: org
313+ )
314+
315+ id = to_string ( id )
316+ hub_id = "team-#{ org . name } "
317+ slug = Utils . random_short_id ( )
318+ title = "MyNotebook-#{ slug } "
319+ app_settings = % { Notebook.AppSettings . new ( ) | slug: slug }
320+
321+ notebook = % {
322+ Notebook . new ( )
323+ | app_settings: app_settings ,
324+ name: title ,
325+ hub_id: hub_id ,
326+ deployment_group_id: id
327+ }
328+
329+ files_dir = FileSystem.File . local ( tmp_dir )
330+
331+ # stamp the notebook
332+ assert { :ok , app_deployment } = Teams.AppDeployment . new ( notebook , files_dir )
333+
334+ # fetch the cli session
335+ { key , _deploy_key } = TeamsRPC . create_deploy_key ( node , org: org )
336+ config = % { teams_key: team . teams_key , session_token: key }
337+ assert { :ok , team } = Teams . fetch_cli_session ( config )
338+
339+ # deploy the app
340+ assert { :ok , _url } = Teams . deploy_app_from_cli ( team , app_deployment , name )
341+
342+ sha = app_deployment . sha
343+ multi_session = app_settings . multi_session
344+ access_type = app_settings . access_type
345+
346+ assert_receive { :app_deployment_started ,
347+ % Livebook.Teams.AppDeployment {
348+ slug: ^ slug ,
349+ sha: ^ sha ,
350+ title: ^ title ,
351+ deployed_by: "CLI" ,
352+ multi_session: ^ multi_session ,
353+ access_type: ^ access_type ,
354+ deployment_group_id: ^ id
355+ } = app_deployment2 }
356+
357+ assert Teams . deploy_app_from_cli ( team , app_deployment , "foo" ) ==
358+ { :error , % { "name" => [ "does not exist" ] } }
359+
360+ assert Teams . deploy_app_from_cli ( team , % { app_deployment | slug: "@abc" } , name ) ==
361+ { :error , % { "slug" => [ "should only contain alphanumeric characters and dashes" ] } }
362+
363+ assert Teams . deploy_app_from_cli ( team , % { app_deployment | multi_session: nil } , name ) ==
364+ { :error , % { "multi_session" => [ "can't be blank" ] } }
365+
366+ assert Teams . deploy_app_from_cli ( team , % { app_deployment | access_type: nil } , name ) ==
367+ { :error , % { "access_type" => [ "can't be blank" ] } }
368+
369+ assert Teams . deploy_app_from_cli ( team , % { app_deployment | access_type: :abc } , name ) ==
370+ { :error , % { "access_type" => [ "is invalid" ] } }
371+
372+ # force app deployment to be stopped
373+ TeamsRPC . toggle_app_deployment ( node , app_deployment2 . id , team . org_id )
374+ assert_receive { :app_deployment_stopped , ^ app_deployment2 }
375+ end
376+ end
300377end
0 commit comments