Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor response.to_s.split to crystal context.response.output #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions spec/controllers/booking_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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
24 changes: 12 additions & 12 deletions spec/controllers/events_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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\":\"[email protected]\",\"title\":null,\"body\":null,\"location\":null,\"host\":null,\"creator\":\"[email protected]\",\"private\":false,\"event_start\""
)
end
Expand Down Expand Up @@ -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
105 changes: 52 additions & 53 deletions spec/controllers/guests_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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":"[email protected]","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":"[email protected]","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":"[email protected]","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
Expand All @@ -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\":\"[email protected]\",\"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
Expand All @@ -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\":\"[email protected]\",\"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
Expand All @@ -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":"[email protected]","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":"[email protected]","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":"[email protected]","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
Expand All @@ -173,26 +174,24 @@ describe Guests do
body = IO::Memory.new
body << %({"email":"[email protected]","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":"[email protected]","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})
)

# instantiate the controller
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/[email protected]", HEADERS, body, response_io: response)
context.route_params = {"id" => "[email protected]"}
app = Guests.new(context)
app.update
ctx = context("PATCH", "/api/staff/v1/guests/[email protected]", HEADERS, body)
ctx.route_params = {"id" => "[email protected]"}
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":"[email protected]","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
Expand Down
13 changes: 7 additions & 6 deletions spec/controllers/staff_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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":"[email protected]"}])
ctx.response.output.to_s.should eq %([{"name":"John Smith","email":"[email protected]"}])
end

it "should the requested user" do
# instantiate the controller
response = IO::Memory.new
ctx = context("GET", "/api/staff/v1/people/[email protected]", HEADERS, response_io: response)
ctx = context("GET", "/api/staff/v1/people/[email protected]", HEADERS)
ctx.route_params = {"id" => "[email protected]"}
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":"[email protected]"})
ctx.response.output.to_s.should eq %({"name":"John Smith","email":"[email protected]"})
end
end