11defmodule CodebattleWeb.ExtApi.UserControllerTest do
2- use CodebattleWeb.ConnCase , async: true
2+ use CodebattleWeb.ConnCase , async: false
33
44 alias Codebattle.Clan
55 alias Codebattle.Repo
66 alias Codebattle.User
7+ alias Codebattle.UserEvent
78
89 describe "create/2" do
910 test "checks auth" , % { conn: conn } do
@@ -15,7 +16,14 @@ defmodule CodebattleWeb.ExtApi.UserControllerTest do
1516 test "creates user with clan and auth token" , % { conn: conn } do
1617 conn
1718 |> put_req_header ( "x-auth-key" , "x-key" )
18- |> post ( Routes . ext_api_user_path ( conn , :create , % { name: "lol" , clan: "S2xhbg==" , UID: "asdf" , category: "cat" } ) )
19+ |> post (
20+ Routes . ext_api_user_path ( conn , :create , % {
21+ name: "lol" ,
22+ clan: "S2xhbg==" ,
23+ UID: "asdf" ,
24+ category: "cat"
25+ } )
26+ )
1927 |> json_response ( 200 )
2028
2129 user = Repo . get_by ( User , name: "lol" )
@@ -97,11 +105,25 @@ defmodule CodebattleWeb.ExtApi.UserControllerTest do
97105
98106 test "updates user by UID" , % { conn: conn } do
99107 clan = insert ( :clan , name: "Kek" , long_name: "lOl_kEk" )
100- user = insert ( :user , name: "whatever" , clan_id: nil , subscription_type: :free , external_oauth_id: "asdf" )
108+
109+ user =
110+ insert ( :user ,
111+ name: "whatever" ,
112+ clan_id: nil ,
113+ subscription_type: :free ,
114+ external_oauth_id: "asdf"
115+ )
101116
102117 conn
103118 |> put_req_header ( "x-auth-key" , "x-key" )
104- |> post ( Routes . ext_api_user_path ( conn , :create , % { category: "lol" , name: "oiblz" , clan: "Kek " , UID: "asdf" } ) )
119+ |> post (
120+ Routes . ext_api_user_path ( conn , :create , % {
121+ category: "lol" ,
122+ name: "oiblz" ,
123+ clan: "Kek " ,
124+ UID: "asdf"
125+ } )
126+ )
105127 |> json_response ( 200 )
106128
107129 user = Repo . get ( User , user . id )
@@ -113,17 +135,39 @@ defmodule CodebattleWeb.ExtApi.UserControllerTest do
113135 category: "lol" ,
114136 external_oauth_id: "asdf" ,
115137 subscription_type: :premium
116- } == Map . take ( user , [ :id , :name , :clan_id , :external_oauth_id , :subscription_type , :category ] )
138+ } ==
139+ Map . take ( user , [
140+ :id ,
141+ :name ,
142+ :clan_id ,
143+ :external_oauth_id ,
144+ :subscription_type ,
145+ :category
146+ ] )
117147 end
118148
119149 test "updates user with duplicated name by UID" , % { conn: conn } do
120150 clan = insert ( :clan , name: "Kek" , long_name: "lOl_kEk" )
121151 insert ( :user , name: "oiblz" )
122- user = insert ( :user , name: "whatever" , clan_id: nil , subscription_type: :free , external_oauth_id: "asdf" )
152+
153+ user =
154+ insert ( :user ,
155+ name: "whatever" ,
156+ clan_id: nil ,
157+ subscription_type: :free ,
158+ external_oauth_id: "asdf"
159+ )
123160
124161 conn
125162 |> put_req_header ( "x-auth-key" , "x-key" )
126- |> post ( Routes . ext_api_user_path ( conn , :create , % { category: "lol" , name: "oiblz" , clan: "Kek " , UID: "asdf" } ) )
163+ |> post (
164+ Routes . ext_api_user_path ( conn , :create , % {
165+ category: "lol" ,
166+ name: "oiblz" ,
167+ clan: "Kek " ,
168+ UID: "asdf"
169+ } )
170+ )
127171 |> json_response ( 200 )
128172
129173 user = Repo . get ( User , user . id )
@@ -135,7 +179,50 @@ defmodule CodebattleWeb.ExtApi.UserControllerTest do
135179 external_oauth_id: "asdf" ,
136180 category: "lol" ,
137181 subscription_type: :premium
138- } == Map . take ( user , [ :id , :clan_id , :external_oauth_id , :subscription_type , :category ] )
182+ } ==
183+ Map . take ( user , [ :id , :clan_id , :external_oauth_id , :subscription_type , :category ] )
184+ end
185+
186+ test "finds or creates user with user_event" , % { conn: conn } do
187+ Application . put_env ( :codebattle , :main_event_slug , "e" )
188+
189+ insert ( :event , slug: "e" )
190+
191+ conn
192+ |> put_req_header ( "x-auth-key" , "x-key" )
193+ |> post (
194+ Routes . ext_api_user_path ( conn , :create , % {
195+ name: "lol" ,
196+ clan: "S2xhbg==" ,
197+ UID: "asdf" ,
198+ category: "cat"
199+ } )
200+ )
201+ |> json_response ( 200 )
202+
203+ user = Repo . get_by ( User , name: "lol" )
204+ user_event = Repo . get_by ( UserEvent , user_id: user . id )
205+
206+ assert user_event
207+ assert user_event . state == % { }
208+
209+ conn
210+ |> put_req_header ( "x-auth-key" , "x-key" )
211+ |> post (
212+ Routes . ext_api_user_path ( conn , :create , % {
213+ name: "lol" ,
214+ clan: "S2xhbg==" ,
215+ UID: "asdf" ,
216+ category: "cat"
217+ } )
218+ )
219+ |> json_response ( 200 )
220+
221+ user_event = Repo . get_by ( UserEvent , user_id: user . id )
222+
223+ assert user_event . state == % { }
224+
225+ Application . delete_env ( :codebattle , :main_event_slug )
139226 end
140227 end
141228end
0 commit comments