2017-12-08 09:58:14 +01:00
defmodule EventosWeb.EventControllerTest do
use EventosWeb.ConnCase
2018-01-13 23:33:03 +01:00
import Eventos.Factory
2017-12-08 09:58:14 +01:00
alias Eventos.Events
2018-01-13 23:33:03 +01:00
alias Eventos.Events.Event
2018-01-15 11:40:01 +01:00
alias Eventos.Export.ICalendar
2017-12-08 09:58:14 +01:00
2018-01-13 23:33:03 +01:00
@create_attrs %{ begins_on : " 2010-04-17 14:00:00.000000Z " , description : " some description " , ends_on : " 2010-04-17 14:00:00.000000Z " , title : " some title " }
@update_attrs %{ begins_on : " 2011-05-18 15:01:01.000000Z " , description : " some updated description " , ends_on : " 2011-05-18 15:01:01.000000Z " , title : " some updated title " }
2018-01-17 11:39:01 +01:00
@invalid_attrs %{ begins_on : nil , description : nil , ends_on : nil , title : nil , address_id : nil }
2018-07-04 16:23:52 +02:00
@create_address_attrs %{ " addressCountry " = > " some addressCountry " , " addressLocality " = > " some addressLocality " , " addressRegion " = > " some addressRegion " , " description " = > " some description " , " floor " = > " some floor " , " postalCode " = > " some postalCode " , " streetAddress " = > " some streetAddress " , " geom " = > %{ " type " = > :point , " data " = > %{ " latitude " = > - 20 , " longitude " = > 30 } } }
2017-12-08 09:58:14 +01:00
def fixture ( :event ) do
{ :ok , event } = Events . create_event ( @create_attrs )
event
end
2018-01-17 11:39:01 +01:00
def address_fixture do
insert ( :address )
end
2018-01-13 23:33:03 +01:00
setup %{ conn : conn } do
2018-06-14 17:25:55 +02:00
actor = insert ( :actor )
user = insert ( :user , actor : actor )
2018-01-13 23:33:03 +01:00
{ :ok , conn : conn , user : user }
end
2017-12-08 09:58:14 +01:00
describe " index " do
test " lists all events " , %{ conn : conn } do
conn = get conn , event_path ( conn , :index )
2018-01-13 23:33:03 +01:00
assert json_response ( conn , 200 ) [ " data " ] == [ ]
2017-12-08 09:58:14 +01:00
end
end
describe " create event " do
2018-01-13 23:33:03 +01:00
test " renders event when data is valid " , %{ conn : conn , user : user } do
2018-06-14 17:25:55 +02:00
attrs = Map . put ( @create_attrs , :organizer_actor_id , user . actor . id )
2018-07-04 16:23:52 +02:00
attrs = Map . put ( attrs , " physical_address " , @create_address_attrs )
2018-01-15 12:04:09 +01:00
category = insert ( :category )
attrs = Map . put ( attrs , :category_id , category . id )
2018-01-13 23:33:03 +01:00
conn = auth_conn ( conn , user )
conn = post conn , event_path ( conn , :create ) , event : attrs
2018-06-14 17:25:55 +02:00
assert %{ " uuid " = > uuid } = json_response ( conn , 201 ) [ " data " ]
2017-12-08 09:58:14 +01:00
2018-06-14 17:25:55 +02:00
conn = get conn , event_path ( conn , :show , uuid )
2018-01-17 11:39:01 +01:00
assert %{
2018-01-13 23:33:03 +01:00
" begins_on " = > " 2010-04-17T14:00:00Z " ,
" description " = > " some description " ,
" ends_on " = > " 2010-04-17T14:00:00Z " ,
2018-01-16 19:45:09 +01:00
" title " = > " some title " ,
2018-01-17 11:39:01 +01:00
" participants " = > [ ] ,
2018-07-04 16:23:52 +02:00
" physical_address " = > %{ " addressCountry " = > " some addressCountry " , " addressLocality " = > " some addressLocality " , " addressRegion " = > " some addressRegion " , " floor " = > " some floor " , " geom " = > %{ " data " = > %{ " latitude " = > - 20.0 , " longitude " = > 30.0 } , " type " = > " point " } , " postalCode " = > " some postalCode " , " streetAddress " = > " some streetAddress " }
2018-01-17 11:39:01 +01:00
} = json_response ( conn , 200 ) [ " data " ]
2017-12-08 09:58:14 +01:00
end
2018-01-13 23:33:03 +01:00
test " renders errors when data is invalid " , %{ conn : conn , user : user } do
conn = auth_conn ( conn , user )
2018-06-14 17:25:55 +02:00
attrs = Map . put ( @invalid_attrs , :organizer_actor_id , user . actor . id )
2018-01-17 11:39:01 +01:00
attrs = Map . put ( attrs , :address , @create_address_attrs )
2018-01-13 23:33:03 +01:00
conn = post conn , event_path ( conn , :create ) , event : attrs
assert json_response ( conn , 422 ) [ " errors " ] != %{ }
2017-12-08 09:58:14 +01:00
end
end
2018-01-15 11:40:01 +01:00
describe " export event " do
setup [ :create_event ]
2018-06-14 17:25:55 +02:00
test " renders ics export of event " , %{ conn : conn , event : % Event { uuid : uuid } = event , user : user } do
2018-01-15 11:40:01 +01:00
conn = auth_conn ( conn , user )
2018-06-14 17:25:55 +02:00
conn = get conn , event_path ( conn , :export_to_ics , uuid )
2018-01-15 11:40:01 +01:00
exported_event = ICalendar . export_event ( event )
assert exported_event == response ( conn , 200 )
end
end
2017-12-08 09:58:14 +01:00
describe " update event " do
setup [ :create_event ]
2018-06-14 17:25:55 +02:00
test " renders event when data is valid " , %{ conn : conn , event : % Event { uuid : uuid } = event , user : user } do
2018-01-13 23:33:03 +01:00
conn = auth_conn ( conn , user )
2018-01-17 11:39:01 +01:00
address = address_fixture ( )
2018-06-14 17:25:55 +02:00
attrs = Map . put ( @update_attrs , :organizer_actor_id , user . actor . id )
2018-01-17 11:39:01 +01:00
attrs = Map . put ( attrs , :address_id , address . id )
2018-06-14 17:25:55 +02:00
conn = put conn , event_path ( conn , :update , uuid ) , event : attrs
assert %{ " uuid " = > uuid } = json_response ( conn , 200 ) [ " data " ]
2017-12-08 09:58:14 +01:00
2018-06-14 17:25:55 +02:00
conn = get conn , event_path ( conn , :show , uuid )
2018-01-17 11:39:01 +01:00
assert %{
" begins_on " = > " 2011-05-18T15:01:01Z " ,
" description " = > " some updated description " ,
" ends_on " = > " 2011-05-18T15:01:01Z " ,
" title " = > " some updated title " ,
" participants " = > [ ] ,
2018-07-04 16:23:52 +02:00
" physical_address " = > %{ " addressCountry " = > " My Country " , " addressLocality " = > " My Locality " , " addressRegion " = > " My Region " , " floor " = > " Myfloor " , " geom " = > %{ " data " = > %{ " latitude " = > 30.0 , " longitude " = > - 90.0 } , " type " = > " point " } , " postalCode " = > " My Postal Code " , " streetAddress " = > " My Street Address " }
2018-01-17 11:39:01 +01:00
} = json_response ( conn , 200 ) [ " data " ]
2017-12-08 09:58:14 +01:00
end
2018-06-14 17:25:55 +02:00
test " renders errors when data is invalid " , %{ conn : conn , event : % Event { uuid : uuid } = event , user : user } do
2018-01-13 23:33:03 +01:00
conn = auth_conn ( conn , user )
2018-06-14 17:25:55 +02:00
attrs = Map . put ( @invalid_attrs , :organizer_actor_id , user . actor . id )
conn = put conn , event_path ( conn , :update , uuid ) , event : attrs
2018-01-13 23:33:03 +01:00
assert json_response ( conn , 422 ) [ " errors " ] != %{ }
2017-12-08 09:58:14 +01:00
end
end
describe " delete event " do
setup [ :create_event ]
2018-06-14 17:25:55 +02:00
test " deletes chosen event " , %{ conn : conn , event : % Event { uuid : uuid } = event , user : user } do
2018-01-13 23:33:03 +01:00
conn = auth_conn ( conn , user )
2018-06-14 17:25:55 +02:00
conn = delete conn , event_path ( conn , :delete , uuid )
2018-01-13 23:33:03 +01:00
assert response ( conn , 204 )
2018-06-14 17:25:55 +02:00
conn = get conn , event_path ( conn , :show , uuid )
assert response ( conn , 404 )
2017-12-08 09:58:14 +01:00
end
end
defp create_event ( _ ) do
2018-06-14 17:25:55 +02:00
actor = insert ( :actor )
event = insert ( :event , organizer_actor : actor )
{ :ok , event : event , actor : actor }
2017-12-08 09:58:14 +01:00
end
end