diff --git a/Gemfile b/Gemfile index c48887a..1eac189 100644 --- a/Gemfile +++ b/Gemfile @@ -78,6 +78,7 @@ group :test do gem "launchy" gem "simplecov", require: false gem "rubocop" + gem "webmock" end diff --git a/Gemfile.lock b/Gemfile.lock index 1bc4b13..be860d1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -86,6 +86,8 @@ GEM xpath (~> 3.2) coderay (1.1.3) concurrent-ruby (1.2.2) + crack (0.4.5) + rexml crass (1.0.6) date (3.3.4) debug (1.8.0) @@ -103,6 +105,7 @@ GEM railties (>= 5.0.0) globalid (1.2.1) activesupport (>= 6.1) + hashdiff (1.0.1) httparty (0.21.0) mini_mime (>= 1.0.0) multi_xml (>= 0.5.2) @@ -282,6 +285,10 @@ GEM nokogiri (~> 1.6) rubyzip (>= 1.3.0) selenium-webdriver (~> 4.0, < 4.11) + webmock (3.19.1) + addressable (>= 2.8.0) + crack (>= 0.3.2) + hashdiff (>= 0.4.0, < 2.0.0) websocket (1.2.10) websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) @@ -321,6 +328,7 @@ DEPENDENCIES tzinfo-data web-console webdrivers + webmock RUBY VERSION ruby 3.1.4p223 diff --git a/spec/features/places_spec.rb b/spec/features/places_spec.rb new file mode 100644 index 0000000..8cb4104 --- /dev/null +++ b/spec/features/places_spec.rb @@ -0,0 +1,41 @@ +require "rails_helper" + +describe "Places" do + it "displays a place if one is returned from the api" do + allow(BeermappingApi).to receive(:places_in).with("kumpula").and_return( + [ Place.new( name: "Oljenkorsi", id: 1 ) ] + ) + + visit places_path + fill_in("city", with: "kumpula") + click_button "Search" + + expect(page).to have_content "Oljenkorsi" + end + + it "displays all places if multiple are returned from the api" do + allow(BeermappingApi).to receive(:places_in).with("kumpula").and_return( + [ Place.new( name: "Oljenkorsi", id: 1 ), Place.new( name: "Emt mitä paikkoja kumpulassa on", id: 2), Place.new( name: ":D", id: 3) ] + ) + + visit places_path + fill_in("city", with: "kumpula") + click_button "Search" + + expect(page).to have_content "Oljenkorsi" + expect(page).to have_content "Emt mitä paikkoja kumpulassa on" + expect(page).to have_content ":D" + end + + it "displays 'No locations in _city_' if none is returned by the api" do + allow(BeermappingApi).to receive(:places_in).with("kumpula").and_return( + [] + ) + + visit places_path + fill_in("city", with: "kumpula") + click_button "Search" + + expect(page).to have_content "No locations in kumpula" + end +end diff --git a/spec/lib/beermapping_api_spec.rb b/spec/lib/beermapping_api_spec.rb new file mode 100644 index 0000000..0899296 --- /dev/null +++ b/spec/lib/beermapping_api_spec.rb @@ -0,0 +1,39 @@ +require "rails_helper" + +describe "Beermapping API" do + it "when HTTP GET returns one entry, it is parsed and returned" do + + http_response_body = <<-END_OF_STRING +18856Panimoravintola KouluBrewpubhttps://beermapping.com/location/18856http://beermapping.com/maps/proxymaps.php?locid=18856&d=5http://beermapping.com/maps/blogproxy.php?locid=18856&d=1&type=normEerikinkatu 18Turku20100Finland(02) 274 575700 + END_OF_STRING + stub_request(:get, /.*turku/).to_return(body: http_response_body, headers: { "Content-Type" => "text/xml" }) + + places = BeermappingApi.places_in("turku") + + expect(places.size).to eq 1 + + place = places.first + expect(place.name).to eq "Panimoravintola Koulu" + expect(place.street).to eq "Eerikinkatu 18" + end + + it "should return empty array when no places are returned from api" do + http_response_body = <<-END_OF_STRING +6742Pullman BarBeer Barhttps://beermapping.com/location/6742http://beermapping.com/maps/proxymaps.php?locid=6742&d=5http://beermapping.com/maps/blogproxy.php?locid=6742&d=1&type=normKaivokatu 1Helsinki00100Finland+358 9 0307 2272.50002506743BelgeBeer Barhttps://beermapping.com/location/6743http://beermapping.com/maps/proxymaps.php?locid=6743&d=5http://beermapping.com/maps/blogproxy.php?locid=6743&d=1&type=normKluuvikatu 5Helsinki00100Finland+358 10 766 3567.4999251 + END_OF_STRING + stub_request(:get, /.*helsinki/).to_return(body: http_response_body, headers: { "Content-Type": "text/xml" }) + + places = BeermappingApi.places_in("helsinki") + first_place = places[0] + second_place = places[1] + + expect(first_place.name).to eq "Pullman Bar" + expect(first_place.street).to eq "Kaivokatu 1" + + expect(second_place.name).to eq "Belge" + expect(second_place.street).to eq "Kluuvikatu 5" + + expect(first_place.class).to eq Place + expect(second_place.class).to eq Place + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index c718176..26df3a3 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -7,6 +7,8 @@ abort("The Rails environment is running in production mode!") if Rails.env.produ require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! +require 'webmock/rspec' + # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are # run as spec files by default. This means that files in spec/support that end