diff --git a/spec/controllers/booking_spec.cr b/spec/controllers/booking_spec.cr index 6eb576f..38caba9 100644 --- a/spec/controllers/booking_spec.cr +++ b/spec/controllers/booking_spec.cr @@ -55,25 +55,24 @@ describe Bookings do it "should return a list of bookings" do # instantiate the controller - response = IO::Memory.new - starting = 5.minutes.from_now.to_unix ending = 40.minutes.from_now.to_unix route = "/api/staff/v1/bookings?period_start=#{starting}&period_end=#{ending}&type=desk" - app = Bookings.new(context("GET", route, HEADERS, response_io: response)) + ctx = context("GET", route, HEADERS) + ctx.response.output = IO::Memory.new + Bookings.new(ctx).index # Test the instance method of the controller - app.index - data = response.to_s - data = JSON.parse(data.split("\r\n").reject(&.empty?)[-1]) + data = JSON.parse(ctx.response.output.to_s).not_nil! data.as_a.size.should eq(2) # filter by zones - response = IO::Memory.new route = "/api/staff/v1/bookings?period_start=#{starting}&period_end=#{ending}&type=desk&zones=zone-890,zone-4127" - Bookings.new(context("GET", route, HEADERS, response_io: response)).index + ctx = context("GET", route, HEADERS) + ctx.response.output = IO::Memory.new + Bookings.new(ctx).index - data = JSON.parse(response.to_s.split("\r\n").reject(&.empty?)[-1]) + data = JSON.parse(ctx.response.output.to_s).not_nil! data.as_a.size.should eq(1) end @@ -89,13 +88,14 @@ describe Bookings do app.destroy # Check only one is returned - response = IO::Memory.new starting = 5.minutes.from_now.to_unix ending = 40.minutes.from_now.to_unix route = "/api/staff/v1/bookings?period_start=#{starting}&period_end=#{ending}&type=desk" - Bookings.new(context("GET", route, HEADERS, response_io: response)).index + ctx = context("GET", route, HEADERS) + ctx.response.output = IO::Memory.new + Bookings.new(ctx).index - data = JSON.parse(response.to_s.split("\r\n").reject(&.empty?)[-1]) + data = JSON.parse(ctx.response.output.to_s).not_nil! data.as_a.size.should eq(1) end @@ -107,15 +107,13 @@ describe Bookings do body = IO::Memory.new body << %({"asset_id":"some_desk","booking_start":#{starting},"booking_end":#{ending},"booking_type":"desk"}) body.rewind - response = IO::Memory.new - context = context("POST", "/api/staff/v1/bookings/", HEADERS, body, response_io: response) - app = Bookings.new(context) - app.create + ctx = context("POST", "/api/staff/v1/bookings/", HEADERS, body) + ctx.response.output = IO::Memory.new + Bookings.new(ctx).create WebMock.stub(:post, "https://example.place.technology/api/engine/v2/signal").to_return(body: "") - data = response.to_s.split("\r\n").reject(&.empty?)[-1] - created = Booking.from_json(data) + created = Booking.from_json(ctx.response.output.to_s).not_nil! created.asset_id.should eq("some_desk") created.booking_start.should eq(starting) created.booking_end.should eq(ending) @@ -124,13 +122,12 @@ describe Bookings do body = IO::Memory.new body << %({"extension_data":{"other":"stuff"}}) body.rewind - response = IO::Memory.new - context = context("PATCH", "/api/staff/v1/bookings/#{created.id}", HEADERS, body, response_io: response) - context.route_params = {"id" => created.id.to_s} - app = Bookings.new(context) - app.update + ctx = context("PATCH", "/api/staff/v1/bookings/#{created.id}", HEADERS, body) + ctx.route_params = {"id" => created.id.to_s} + ctx.response.output = IO::Memory.new + Bookings.new(ctx).update - updated = Booking.from_json(response.to_s.split("\r\n").reject(&.empty?)[-1]) + updated = Booking.from_json(ctx.response.output.to_s).not_nil! updated.extension_data["other"].should eq("stuff") end end diff --git a/spec/controllers/events_spec.cr b/spec/controllers/events_spec.cr index c92f76a..6915e29 100644 --- a/spec/controllers/events_spec.cr +++ b/spec/controllers/events_spec.cr @@ -23,16 +23,16 @@ describe Events do later = 1588422097 # instantiate the controller - response = IO::Memory.new - app = Events.new(context( + ctx = context( "GET", "/api/staff/v1/events?zone_ids=z1&period_start=#{now}&period_end=#{later}", - HEADERS, response_io: response - )) + HEADERS + ) + ctx.response.output = IO::Memory.new + Events.new(ctx).index # Test the instance method of the controller - app.index - response.to_s.split("\r\n").reject(&.empty?)[-1].should start_with( + ctx.response.output.to_s.should start_with( "[{\"id\":\"123456789\",\"status\":null,\"calendar\":\"room2@example.com\",\"title\":null,\"body\":null,\"location\":null,\"host\":null,\"creator\":\"test@example.com\",\"private\":false,\"event_start\"" ) end @@ -73,15 +73,15 @@ describe Events do result.should eq true # instantiate the controller - response = IO::Memory.new - app = Events.new(context( + ctx = context( "GET", "/api/staff/v1/events?zone_ids=z1&period_start=#{now}&period_end=#{later}", - HEADERS, response_io: response - )) + HEADERS + ) + ctx.response.output = IO::Memory.new + Events.new(ctx).index # Test the instance method of the controller - app.index - response.to_s.includes?(%({"breakdown":15,"cleaned":false})).should eq(true) + ctx.response.output.to_s.includes?(%({"breakdown":15,"cleaned":false})).should eq(true) end end diff --git a/spec/controllers/guests_spec.cr b/spec/controllers/guests_spec.cr index 279ed1a..43e5f17 100644 --- a/spec/controllers/guests_spec.cr +++ b/spec/controllers/guests_spec.cr @@ -23,24 +23,24 @@ describe Guests do it "should return a list of guests" do # instantiate the controller - response = IO::Memory.new - app = Guests.new(context("GET", "/api/staff/v1/guests", HEADERS, response_io: response)) + ctx = context("GET", "/api/staff/v1/guests", HEADERS) + ctx.response.output = IO::Memory.new + Guests.new(ctx).index # Test the instance method of the controller - app.index - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq( + ctx.response.output.to_s.should eq( %([{"email":"jon@place.techn","name":"Jon","preferred_name":null,"phone":null,"organisation":null,"notes":null,"photo":null,"banned":false,"dangerous":false,"extension_data":{},"checked_in":false,"visit_expected":false},{"email":"steve@place.techn","name":"Steve","preferred_name":null,"phone":null,"organisation":null,"notes":null,"photo":null,"banned":false,"dangerous":false,"extension_data":{},"checked_in":false,"visit_expected":false}]) ) end it "should return a filtered list of guests" do # instantiate the controller - response = IO::Memory.new - app = Guests.new(context("GET", "/api/staff/v1/guests?q=stev", HEADERS, response_io: response)) + ctx = context("GET", "/api/staff/v1/guests?q=stev", HEADERS) + ctx.response.output = IO::Memory.new + Guests.new(ctx).index # Test the instance method of the controller - app.index - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq( + ctx.response.output.to_s.should eq( %([{"email":"steve@place.techn","name":"Steve","preferred_name":null,"phone":null,"organisation":null,"notes":null,"photo":null,"banned":false,"dangerous":false,"extension_data":{},"checked_in":false,"visit_expected":false}]) ) end @@ -49,25 +49,26 @@ describe Guests do now = Time.utc.to_unix later = 4.hours.from_now.to_unix route = "/api/staff/v1/guests?period_start=#{now}&period_end=#{later}" - response = IO::Memory.new - app = Guests.new(context("GET", route, HEADERS, response_io: response)) + ctx = context("GET", route, HEADERS) + ctx.response.output = IO::Memory.new + Guests.new(ctx).index # Test the instance method of the controller - app.index - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq("[]") + ctx.response.output.to_s.should eq("[]") meta = generate_event guest = generate_guest guest.attendee_for(meta.id.not_nil!) # instantiate the controller - response = IO::Memory.new - app = Guests.new(context("GET", route, HEADERS, response_io: response)) + ctx2 = context("GET", route, HEADERS) + ctx2.response.output = IO::Memory.new + app = Guests.new(ctx2) begin # Test the instance method of the controller app.index - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq( + ctx2.response.output.to_s.should eq( "[{\"email\":\"bob@outside.com\",\"name\":null,\"preferred_name\":null,\"phone\":null,\"organisation\":null,\"notes\":null,\"photo\":null,\"banned\":false,\"dangerous\":false,\"extension_data\":{},\"checked_in\":false,\"visit_expected\":true}]" ) ensure @@ -94,21 +95,23 @@ describe Guests do now = Time.utc.to_unix later = 4.hours.from_now.to_unix route = "/api/staff/v1/guests?period_start=#{now}&period_end=#{later}&system_ids=sys-rJQQlR4Cn7" - response = IO::Memory.new - app = Guests.new(context("GET", route, HEADERS, response_io: response)) + + ctx = context("GET", route, HEADERS) + ctx.response.output = IO::Memory.new + Guests.new(ctx).index # Test the instance method of the controller - app.index - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq("[]") + ctx.response.output.to_s.should eq("[]") # instantiate the controller - response = IO::Memory.new + route = "/api/staff/v1/guests?period_start=#{now}&period_end=#{later}&system_ids=sys-rJQQlR4Cn7,sys_id" - app = Guests.new(context("GET", route, HEADERS, response_io: response)) + ctx = context("GET", route, HEADERS) + ctx.response.output = IO::Memory.new + Guests.new(ctx).index # Test the instance method of the controller - app.index - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq( + ctx.response.output.to_s.should eq( "[{\"email\":\"bob@outside.com\",\"name\":null,\"preferred_name\":null,\"phone\":null,\"organisation\":null,\"notes\":null,\"photo\":null,\"banned\":false,\"dangerous\":false,\"extension_data\":{},\"checked_in\":false,\"visit_expected\":true}]" ) ensure @@ -119,51 +122,49 @@ describe Guests do it "should show a guests details" do # instantiate the controller - response = IO::Memory.new - context = context("GET", "/api/staff/v1/guests/#{guest1.email}/", HEADERS, response_io: response) - context.route_params = {"id" => guest1.email.not_nil!} - app = Guests.new(context) + ctx = context("GET", "/api/staff/v1/guests/#{guest1.email}/", HEADERS) + ctx.route_params = {"id" => guest1.email.not_nil!} + ctx.response.output = IO::Memory.new + Guests.new(ctx).show # Test the instance method of the controller - app.show - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq( + ctx.response.output.to_s.should eq( %({"email":"steve@place.techn","name":"Steve","preferred_name":null,"phone":null,"organisation":null,"notes":null,"photo":null,"banned":false,"dangerous":false,"extension_data":{},"checked_in":false,"visit_expected":false}) ) end it "should show a guests details when visiting today" do # instantiate the controller - response = IO::Memory.new - context = context("GET", "/api/staff/v1/guests/#{guest1.email}/", HEADERS, response_io: response) - context.route_params = {"id" => guest1.email.not_nil!} - app = Guests.new(context) + ctx = context("GET", "/api/staff/v1/guests/#{guest1.email}/", HEADERS) + ctx.route_params = {"id" => guest1.email.not_nil!} + ctx.response.output = IO::Memory.new + app = Guests.new(ctx) meta = generate_event guest1.attendee_for(meta.id.not_nil!) # Test the instance method of the controller app.show - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq( + ctx.response.output.to_s.should eq( %({"email":"steve@place.techn","name":"Steve","preferred_name":null,"phone":null,"organisation":null,"notes":null,"photo":null,"banned":false,"dangerous":false,"extension_data":{},"checked_in":false,"visit_expected":true}) ) end it "should delete a guest" do # instantiate the controller - context = context("DELETE", "/api/staff/v1/guests/#{guest1.email}/", HEADERS) - context.route_params = {"id" => guest1.email.not_nil!} - app = Guests.new(context) + ctx = context("DELETE", "/api/staff/v1/guests/#{guest1.email}/", HEADERS) + ctx.route_params = {"id" => guest1.email.not_nil!} # Test the instance method of the controller - app.destroy + Guests.new(ctx).destroy # Check only one is returned - response = IO::Memory.new - app = Guests.new(context("GET", "/api/staff/v1/guests", HEADERS, response_io: response)) + ctx = context("GET", "/api/staff/v1/guests", HEADERS) + ctx.response.output = IO::Memory.new # Test the instance method of the controller - app.index - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq( + Guests.new(ctx).index + ctx.response.output.to_s.should eq( %([{"email":"jon@place.techn","name":"Jon","preferred_name":null,"phone":null,"organisation":null,"notes":null,"photo":null,"banned":false,"dangerous":false,"extension_data":{},"checked_in":false,"visit_expected":false}]) ) end @@ -173,12 +174,11 @@ describe Guests do body = IO::Memory.new body << %({"email":"bob@jane.com","banned":true,"extension_data":{"test":"data"}}) body.rewind - response = IO::Memory.new - context = context("POST", "/api/staff/v1/guests/", HEADERS, body, response_io: response) - app = Guests.new(context) - app.create + ctx = context("POST", "/api/staff/v1/guests/", HEADERS, body) + ctx.response.output = IO::Memory.new + Guests.new(ctx).create - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq( + ctx.response.output.to_s.should eq( %({"email":"bob@jane.com","name":null,"preferred_name":null,"phone":null,"organisation":null,"notes":null,"photo":null,"banned":true,"dangerous":false,"extension_data":{"test":"data"},"checked_in":false,"visit_expected":false}) ) @@ -186,13 +186,12 @@ describe Guests do body = IO::Memory.new body << %({"name":"Bob Jane","extension_data":{"other":"stuff"}}) body.rewind - response = IO::Memory.new - context = context("PATCH", "/api/staff/v1/guests/bob@jane.com", HEADERS, body, response_io: response) - context.route_params = {"id" => "bob@jane.com"} - app = Guests.new(context) - app.update + ctx = context("PATCH", "/api/staff/v1/guests/bob@jane.com", HEADERS, body) + ctx.route_params = {"id" => "bob@jane.com"} + ctx.response.output = IO::Memory.new + Guests.new(ctx).update - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq( + ctx.response.output.to_s.should eq( %({"email":"bob@jane.com","name":"Bob Jane","preferred_name":null,"phone":null,"organisation":null,"notes":null,"photo":null,"banned":false,"dangerous":false,"extension_data":{"test":"data","other":"stuff"},"checked_in":false,"visit_expected":false}) ) end diff --git a/spec/controllers/staff_spec.cr b/spec/controllers/staff_spec.cr index af232a5..7f34868 100644 --- a/spec/controllers/staff_spec.cr +++ b/spec/controllers/staff_spec.cr @@ -3,27 +3,28 @@ require "../spec_helper" describe Staff do it "should return a list of users" do # instantiate the controller - response = IO::Memory.new - app = Staff.new(context("GET", "/api/staff/v1/people", HEADERS, response_io: response)) + ctx = context("GET", "/api/staff/v1/people", HEADERS) + ctx.response.output = IO::Memory.new + app = Staff.new(ctx) DirectoryHelper.mock_token DirectoryHelper.mock_user_query # Test the instance method of the controller app.index - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq %([{"name":"John Smith","email":"test@example.com"}]) + ctx.response.output.to_s.should eq %([{"name":"John Smith","email":"test@example.com"}]) end it "should the requested user" do # instantiate the controller - response = IO::Memory.new - ctx = context("GET", "/api/staff/v1/people/test@example.com", HEADERS, response_io: response) + ctx = context("GET", "/api/staff/v1/people/test@example.com", HEADERS) ctx.route_params = {"id" => "test@example.com"} + ctx.response.output = IO::Memory.new app = Staff.new(ctx) DirectoryHelper.mock_token DirectoryHelper.mock_lookup # Test the instance method of the controller app.show - response.to_s.split("\r\n").reject(&.empty?)[-1].should eq %({"name":"John Smith","email":"test@example.com"}) + ctx.response.output.to_s.should eq %({"name":"John Smith","email":"test@example.com"}) end end