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