jam-cloud/web/spec/requests/search_api_spec.rb

102 lines
4.2 KiB
Ruby

require 'spec_helper'
describe "Search API", :type => :request do
describe "profile page" do
let(:user) { FactoryBot.create(:user) }
let(:band_params) {
{
name: "The Band",
biography: "Biography",
city: 'Austin',
state: 'TX',
country: 'US',
genres: ['country']
}
}
before(:each) do
Rails.application.reload_routes!
puts "DEBUG: Total routes: #{Rails.application.routes.routes.size}"
puts "DEBUG: Routes: #{Rails.application.routes.routes.map {|r| r.path.spec.to_s if r.defaults[:controller] == 'sessions'}.compact}"
JamRuby::Genre.find_or_create_by(id: 'country', description: 'Country')
BandMusician.delete_all if defined?(BandMusician)
BandSearch.delete_all if defined?(BandSearch)
MusicianSearch.delete_all if defined?(MusicianSearch)
Band.delete_all if defined?(Band)
FriendRequest.delete_all if defined?(FriendRequest)
Friendship.delete_all if defined?(Friendship)
MusicianInstrument.delete_all if defined?(MusicianInstrument)
LessonBooking.delete_all if defined?(LessonBooking)
SaleLineItem.delete_all if defined?(SaleLineItem)
Sale.delete_all if defined?(Sale)
TeacherDistribution.delete_all if defined?(TeacherDistribution)
TeacherPayment.delete_all if defined?(TeacherPayment)
RetailerInvitation.delete_all if defined?(RetailerInvitation)
Teacher.delete_all if defined?(Teacher)
Retailer.delete_all if defined?(Retailer)
InvitedUser.delete_all if defined?(InvitedUser)
User.delete_all
post '/sessions', params: { session: { email: user.email, password: user.password } }
cookies["remember_token"].should == user.remember_token
end
it "empty search" do
get '/api/search.json'
response.status.should == 200
JSON.parse(response.body).should eql({'search_type'=>nil})
end
it "simple search" do
@musician = FactoryBot.create(:user, first_name: "Peach", last_name: "Nothing", email: "user@example.com", musician: true)
@fan = FactoryBot.create(:user, first_name: "Peach Peach", last_name: "Grovery", email: "fan@example.com", musician: false)
band_params[:name] = "Peach pit"
@band = Band.save(user, band_params)
band_params[:name] = "Peach"
@band2 = Band.save(user, band_params)
get '/api/search.json?query=peach&search_text_type=bands'
response.status.should == 200
parsed_response = JSON.parse(response.body)
parsed_response["bands"].length.should == 2
bands = parsed_response["bands"]
bands = [bands[0]["id"], bands[1]["id"]]
bands.should include(@band.id)
bands.should include(@band2.id)
end
it "excludes forever deleted musicians" do
@musician1 = FactoryBot.create(:user, first_name: "Peach Peach", last_name: "Grovery", email: "user@example.com", musician: true)
@musician2 = FactoryBot.create(:user, first_name: "Peach Peach", last_name: "Grovery", email: "user1@example.com", musician: true)
@musician1.permanently_delete
get '/api/search.json?query=peach&search_text_type=musicians'
response.status.should == 200
parsed_response = JSON.parse(response.body)
parsed_response["musicians"].length.should == 1
get '/api/search.json?query=deleted&search_text_type=musicians'
response.status.should == 200
parsed_response = JSON.parse(response.body)
parsed_response["musicians"].length.should == 0
end
it "excludes forever deleted fans" do
@fan1 = FactoryBot.create(:user, first_name: "Peach", last_name: "Nothing", email: "user@example.com", musician: false)
@fan2 = FactoryBot.create(:user, first_name: "Peach Peach", last_name: "Grovery", email: "user1@example.com", musician: false)
@fan1.permanently_delete
get '/api/search.json?query=peach&search_text_type=fans'
response.status.should == 200
parsed_response = JSON.parse(response.body)
parsed_response["fans"].length.should == 1
get '/api/search.json?query=deleted&search_text_type=fans'
response.status.should == 200
parsed_response = JSON.parse(response.body)
parsed_response["fans"].length.should == 0
end
end
end