2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-15 08:13:50 +08:00
|
|
|
RSpec.describe Admin::SearchLogsController do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:moderator)
|
|
|
|
fab!(:user)
|
2017-11-15 08:13:50 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
before { SearchLog.log(term: "ruby", search_type: :header, ip_address: "127.0.0.1") }
|
2017-11-15 08:13:50 +08:00
|
|
|
|
2018-04-23 10:00:37 +08:00
|
|
|
after { SearchLog.clear_debounce_cache! }
|
|
|
|
|
2022-07-27 18:21:10 +08:00
|
|
|
describe "#index" do
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "search logs accessible" do
|
|
|
|
it "returns search logs" do
|
|
|
|
get "/admin/logs/search_logs.json"
|
2017-11-15 08:13:50 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json[0]["term"]).to eq("ruby")
|
|
|
|
expect(json[0]["searches"]).to eq(1)
|
|
|
|
expect(json[0]["ctr"]).to eq(0)
|
|
|
|
end
|
2017-11-15 08:13:50 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
include_examples "search logs accessible"
|
|
|
|
end
|
2017-11-15 08:13:50 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2017-11-15 08:13:50 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "search logs accessible"
|
2017-11-15 08:13:50 +08:00
|
|
|
end
|
2022-10-31 20:02:26 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2022-10-31 20:02:26 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "denies access with a 404 response" do
|
|
|
|
get "/admin/logs/search_logs.json"
|
2022-10-31 20:02:26 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
2022-10-31 20:02:26 +08:00
|
|
|
end
|
2017-11-15 08:13:50 +08:00
|
|
|
end
|
2017-12-20 10:41:31 +08:00
|
|
|
|
2022-07-27 18:21:10 +08:00
|
|
|
describe "#term" do
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "search log term accessible" do
|
|
|
|
it "returns search log term" do
|
|
|
|
get "/admin/logs/search_logs/term.json", params: { term: "ruby" }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["term"]["type"]).to eq("search_log_term")
|
|
|
|
expect(json["term"]["search_result"]).to be_present
|
|
|
|
end
|
2017-12-20 10:41:31 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2019-03-29 09:48:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "search log term accessible"
|
2017-12-20 10:41:31 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2017-12-20 10:41:31 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "search log term accessible"
|
2017-12-20 10:41:31 +08:00
|
|
|
end
|
2022-10-31 20:02:26 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2022-10-31 20:02:26 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "denies access with a 404 response" do
|
|
|
|
get "/admin/logs/search_logs/term.json", params: { term: "ruby" }
|
2022-10-31 20:02:26 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
2022-10-31 20:02:26 +08:00
|
|
|
end
|
2017-12-20 10:41:31 +08:00
|
|
|
end
|
2017-11-15 08:13:50 +08:00
|
|
|
end
|