File tree Expand file tree Collapse file tree
test/lib/code_corps/accounts Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -61,15 +61,17 @@ defmodule CodeCorps.Accounts do
6161
6262 case Repo . transaction ( multi ) do
6363 { :ok , % { user: % User { } = user , installations: installations } } ->
64+ user |> upload_github_photo_async
6465 { :ok , user |> Map . put ( :github_app_installations , installations ) }
6566 { :error , :user , % Changeset { } = changeset , _actions_done } ->
6667 { :error , changeset }
6768 end
6869 end
6970
70- defp upload_github_photo_async ( % User { } = user ) do
71+ defp upload_github_photo_async ( % User { cloudinary_public_id: nil } = user ) do
7172 TaskSupervisor . start_child ( :background_processor , fn -> upload_github_photo ( user ) end )
7273 end
74+ defp upload_github_photo_async ( % User { } = user ) , do: user
7375
7476 defp upload_github_photo ( % User { github_avatar_url: github_avatar_url } = user ) do
7577 [ ok: % Cloudex.UploadedImage { public_id: cloudinary_public_id } ] =
Original file line number Diff line number Diff line change @@ -75,10 +75,40 @@ defmodule CodeCorps.AccountsTest do
7575 user
7676 |> Accounts . update_from_github_oauth ( params , token )
7777
78+ wait_for_supervisor ( )
79+
7880 assert user . id
7981 assert user . github_auth_token == token
8082 assert user . sign_up_context == "default"
8183 assert user . type == "user"
8284 end
85+
86+ test "does not update their image if it already exists" do
87+ user = insert ( :user , cloudinary_public_id: "123" )
88+ params = TestHelpers . load_endpoint_fixture ( "user" )
89+
90+ { :ok , % User { } = user } =
91+ user
92+ |> Accounts . update_from_github_oauth ( params , "random_token" )
93+
94+ wait_for_supervisor ( )
95+ user = Repo . get ( User , user . id )
96+
97+ assert user . cloudinary_public_id === "123"
98+ end
99+
100+ test "updates their image if does not exist" do
101+ user = insert ( :user , cloudinary_public_id: nil )
102+ params = TestHelpers . load_endpoint_fixture ( "user" )
103+
104+ { :ok , % User { } = user } =
105+ user
106+ |> Accounts . update_from_github_oauth ( params , "random_token" )
107+
108+ wait_for_supervisor ( )
109+ user = Repo . get ( User , user . id )
110+
111+ assert user . cloudinary_public_id
112+ end
83113 end
84114end
You can’t perform that action at this time.
0 commit comments