2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-15 08:13:50 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Admin::SearchLogsController do
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SearchLog.log(term: 'ruby', search_type: :header, ip_address: '127.0.0.1')
|
|
|
|
end
|
|
|
|
|
2018-04-23 10:00:37 +08:00
|
|
|
after do
|
|
|
|
SearchLog.clear_debounce_cache!
|
|
|
|
end
|
|
|
|
|
2017-11-15 08:13:50 +08:00
|
|
|
context "#index" do
|
|
|
|
it "raises an error if you aren't logged in" do
|
2018-01-12 11:15:10 +08:00
|
|
|
get '/admin/logs/search_logs.json'
|
|
|
|
expect(response.status).to eq(404)
|
2017-11-15 08:13:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error if you aren't an admin" do
|
|
|
|
sign_in(user)
|
2018-01-12 11:15:10 +08:00
|
|
|
get '/admin/logs/search_logs.json'
|
|
|
|
expect(response.status).to eq(404)
|
2017-11-15 08:13:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should work if you are an admin" do
|
|
|
|
sign_in(admin)
|
|
|
|
get '/admin/logs/search_logs.json'
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-11-15 08:13:50 +08:00
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json[0]['term']).to eq('ruby')
|
2018-12-18 21:43:46 +08:00
|
|
|
expect(json[0]['searches']).to eq(1)
|
|
|
|
expect(json[0]['ctr']).to eq(0)
|
2017-11-15 08:13:50 +08:00
|
|
|
end
|
|
|
|
end
|
2017-12-20 10:41:31 +08:00
|
|
|
|
|
|
|
context "#term" do
|
|
|
|
it "raises an error if you aren't logged in" do
|
2019-03-29 09:48:20 +08:00
|
|
|
get '/admin/logs/search_logs/term.json', params: {
|
|
|
|
term: "ruby"
|
|
|
|
}
|
|
|
|
|
2018-01-12 11:15:10 +08:00
|
|
|
expect(response.status).to eq(404)
|
2017-12-20 10:41:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error if you aren't an admin" do
|
|
|
|
sign_in(user)
|
2019-03-29 09:48:20 +08:00
|
|
|
|
|
|
|
get '/admin/logs/search_logs/term.json', params: {
|
|
|
|
term: "ruby"
|
|
|
|
}
|
|
|
|
|
2018-01-12 11:15:10 +08:00
|
|
|
expect(response.status).to eq(404)
|
2017-12-20 10:41:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should work if you are an admin" do
|
|
|
|
sign_in(admin)
|
2019-03-29 09:48:20 +08:00
|
|
|
|
|
|
|
get '/admin/logs/search_logs/term.json', params: {
|
|
|
|
term: "ruby"
|
|
|
|
}
|
2017-12-20 10:41:31 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-12-20 10:41:31 +08:00
|
|
|
|
|
|
|
json = ::JSON.parse(response.body)
|
|
|
|
expect(json['term']['type']).to eq('search_log_term')
|
2018-01-16 18:29:22 +08:00
|
|
|
expect(json['term']['search_result']).to be_present
|
2017-12-20 10:41:31 +08:00
|
|
|
end
|
|
|
|
end
|
2017-11-15 08:13:50 +08:00
|
|
|
end
|