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