Fix request specs

This commit is contained in:
Eugene Burmakin 2025-09-02 23:12:10 +02:00
parent 71bb224524
commit d4c0eaa549
2 changed files with 64 additions and 28 deletions

View file

@ -52,7 +52,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations', params: { q: search_query }, headers: headers get '/api/v1/locations', params: { q: search_query }, headers: headers
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response['query']).to eq(search_query) expect(json_response['query']).to eq(search_query)
expect(json_response['locations']).to be_an(Array) expect(json_response['locations']).to be_an(Array)
@ -150,7 +150,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations', params: { q: 'NonexistentPlace' }, headers: headers get '/api/v1/locations', params: { q: 'NonexistentPlace' }, headers: headers
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response['locations']).to be_empty expect(json_response['locations']).to be_empty
expect(json_response['total_locations']).to eq(0) expect(json_response['total_locations']).to eq(0)
@ -162,9 +162,9 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations', headers: headers get '/api/v1/locations', headers: headers
expect(response).to have_http_status(:bad_request) expect(response).to have_http_status(:bad_request)
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response['error']).to eq('Search query parameter (q) is required') expect(json_response['error']).to eq('Search query parameter (q) or coordinates (lat, lon) are required')
end end
end end
@ -173,9 +173,9 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations', params: { q: ' ' }, headers: headers get '/api/v1/locations', params: { q: ' ' }, headers: headers
expect(response).to have_http_status(:bad_request) expect(response).to have_http_status(:bad_request)
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response['error']).to eq('Search query parameter (q) is required') expect(json_response['error']).to eq('Search query parameter (q) or coordinates (lat, lon) are required')
end end
end end
@ -186,7 +186,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations', params: { q: long_query }, headers: headers get '/api/v1/locations', params: { q: long_query }, headers: headers
expect(response).to have_http_status(:bad_request) expect(response).to have_http_status(:bad_request)
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response['error']).to eq('Search query too long (max 200 characters)') expect(json_response['error']).to eq('Search query too long (max 200 characters)')
end end
@ -202,7 +202,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations', params: { q: 'test' }, headers: headers get '/api/v1/locations', params: { q: 'test' }, headers: headers
expect(response).to have_http_status(:internal_server_error) expect(response).to have_http_status(:internal_server_error)
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response['error']).to eq('Search failed. Please try again.') expect(json_response['error']).to eq('Search failed. Please try again.')
end end
@ -283,11 +283,11 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations/suggestions', params: { q: 'Kaufland' }, headers: headers get '/api/v1/locations/suggestions', params: { q: 'Kaufland' }, headers: headers
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response['suggestions']).to be_an(Array) expect(json_response['suggestions']).to be_an(Array)
expect(json_response['suggestions'].length).to eq(2) expect(json_response['suggestions'].length).to eq(2)
first_suggestion = json_response['suggestions'].first first_suggestion = json_response['suggestions'].first
expect(first_suggestion).to include( expect(first_suggestion).to include(
'name' => 'Kaufland Mitte', 'name' => 'Kaufland Mitte',
@ -307,7 +307,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
type: 'place' type: 'place'
} }
end end
allow_any_instance_of(LocationSearch::GeocodingService) allow_any_instance_of(LocationSearch::GeocodingService)
.to receive(:search).and_return(large_suggestions) .to receive(:search).and_return(large_suggestions)
@ -323,7 +323,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations/suggestions', params: { q: 'a' }, headers: headers get '/api/v1/locations/suggestions', params: { q: 'a' }, headers: headers
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response['suggestions']).to be_empty expect(json_response['suggestions']).to be_empty
end end
@ -334,7 +334,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations/suggestions', params: { q: '' }, headers: headers get '/api/v1/locations/suggestions', params: { q: '' }, headers: headers
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response['suggestions']).to be_empty expect(json_response['suggestions']).to be_empty
end end
@ -350,7 +350,7 @@ RSpec.describe Api::V1::LocationsController, type: :request do
get '/api/v1/locations/suggestions', params: { q: 'test' }, headers: headers get '/api/v1/locations/suggestions', params: { q: 'test' }, headers: headers
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response['suggestions']).to be_empty expect(json_response['suggestions']).to be_empty
end end
@ -365,4 +365,4 @@ RSpec.describe Api::V1::LocationsController, type: :request do
end end
end end
end end
end end

View file

@ -6,10 +6,11 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
let(:user) { create(:user) } let(:user) { create(:user) }
before do before do
driven_by(:selenium_headless_chrome) driven_by(:selenium_chrome_headless)
sign_in user # Alternative for debugging: driven_by(:rack_test)
login_as user, scope: :user
# Create some test points near Berlin # Create some test points near Berlin with proper PostGIS point format
create(:point, create(:point,
user: user, user: user,
latitude: 52.5200, latitude: 52.5200,
@ -27,6 +28,9 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
city: 'Berlin', city: 'Berlin',
country: 'Germany' country: 'Germany'
) )
# Ensure points are properly saved and accessible
expect(user.points.count).to eq(2)
# Mock the geocoding service to avoid external API calls # Mock the geocoding service to avoid external API calls
allow_any_instance_of(LocationSearch::GeocodingService).to receive(:search) do |_service, query| allow_any_instance_of(LocationSearch::GeocodingService).to receive(:search) do |_service, query|
@ -59,11 +63,23 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
describe 'Search Bar' do describe 'Search Bar' do
before do before do
visit map_path visit '/map'
# Wait for map to load # Debug: check what's actually on the page
expect(page).to have_css('#map') puts "Page title: #{page.title}"
sleep(2) # Give time for JavaScript to initialize puts "Page body contains 'Map': #{page.has_content?('Map')}"
puts "Page has #map element: #{page.has_css?('#map')}"
puts "Page HTML preview: #{page.html[0..500]}..."
# Check if map container exists
if page.has_css?('#map')
puts "Map element found!"
else
puts "Map element NOT found - checking for any div elements:"
puts "Number of div elements: #{page.all('div').count}"
end
sleep(3) # Give time for JavaScript to initialize
end end
it 'displays search toggle button on the map' do it 'displays search toggle button on the map' do
@ -282,7 +298,7 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
describe 'Search API Integration' do describe 'Search API Integration' do
it 'makes authenticated requests to the search API' do it 'makes authenticated requests to the search API' do
# Test that the frontend makes proper API calls # Test that the frontend makes proper API calls
visit map_path visit '/map'
fill_in 'location-search-input', with: 'Kaufland' fill_in 'location-search-input', with: 'Kaufland'
@ -298,7 +314,7 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
describe 'Real-world Search Scenarios' do describe 'Real-world Search Scenarios' do
context 'with business name search' do context 'with business name search' do
it 'finds visits to business locations' do it 'finds visits to business locations' do
visit map_path visit '/map'
fill_in 'location-search-input', with: 'Kaufland' fill_in 'location-search-input', with: 'Kaufland'
click_button '🔍' click_button '🔍'
@ -310,7 +326,7 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
context 'with address search' do context 'with address search' do
it 'handles street address searches' do it 'handles street address searches' do
visit map_path visit '/map'
fill_in 'location-search-input', with: 'Alexanderplatz 1' fill_in 'location-search-input', with: 'Alexanderplatz 1'
click_button '🔍' click_button '🔍'
@ -323,7 +339,7 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
context 'with multiple search terms' do context 'with multiple search terms' do
it 'handles complex search queries' do it 'handles complex search queries' do
visit map_path visit '/map'
fill_in 'location-search-input', with: 'Kaufland Berlin' fill_in 'location-search-input', with: 'Kaufland Berlin'
click_button '🔍' click_button '🔍'
@ -448,8 +464,28 @@ RSpec.describe 'Location Search Feature', type: :system, js: true do
def sign_in(user) def sign_in(user)
visit '/users/sign_in' visit '/users/sign_in'
fill_in 'Email', with: user.email # Try different selectors for email field
fill_in 'Password', with: user.password if page.has_field?('Email')
fill_in 'Email', with: user.email
elsif page.has_field?('user_email')
fill_in 'user_email', with: user.email
elsif page.has_css('input[type="email"]')
find('input[type="email"]').fill_in with: user.email
else
raise "Could not find email field"
end
# Try different selectors for password field
if page.has_field?('Password')
fill_in 'Password', with: user.password
elsif page.has_field?('user_password')
fill_in 'user_password', with: user.password
elsif page.has_css('input[type="password"]')
find('input[type="password"]').fill_in with: user.password
else
raise "Could not find password field"
end
click_button 'Log in' click_button 'Log in'
end end
end end