2023-01-23 12:48:33 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe SlugsController do
|
|
|
|
fab!(:current_user) { Fabricate(:user, trust_level: TrustLevel[4]) }
|
|
|
|
|
|
|
|
describe "#generate" do
|
|
|
|
let(:name) { "Arts & Media" }
|
|
|
|
|
|
|
|
context "when user not logged in" do
|
|
|
|
it "returns a 403 error" do
|
2023-01-27 08:58:33 +08:00
|
|
|
post "/slugs.json", params: { name: name }
|
2023-01-23 12:48:33 +08:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is logged in" do
|
|
|
|
before { sign_in(current_user) }
|
|
|
|
|
|
|
|
it "generates a slug from the name" do
|
2023-01-27 08:58:33 +08:00
|
|
|
post "/slugs.json", params: { name: name }
|
2023-01-23 12:48:33 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["slug"]).to eq(Slug.for(name, ""))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "rate limits" do
|
|
|
|
RateLimiter.enable
|
2023-02-18 01:56:25 +08:00
|
|
|
RateLimiter.clear_all!
|
2023-01-23 12:48:33 +08:00
|
|
|
|
|
|
|
stub_const(SlugsController, "MAX_SLUG_GENERATIONS_PER_MINUTE", 1) do
|
2023-01-27 08:58:33 +08:00
|
|
|
post "/slugs.json?name=#{name}"
|
|
|
|
post "/slugs.json?name=#{name}"
|
2023-01-23 12:48:33 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(response.status).to eq(429)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "requires name" do
|
2023-01-27 08:58:33 +08:00
|
|
|
post "/slugs.json"
|
2023-01-23 12:48:33 +08:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is not TL4 or higher" do
|
|
|
|
before { current_user.change_trust_level!(1) }
|
|
|
|
|
|
|
|
it "returns a 403 error" do
|
2023-01-27 08:58:33 +08:00
|
|
|
post "/slugs.json?name=#{name}"
|
2023-01-23 12:48:33 +08:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is admin" do
|
|
|
|
fab!(:current_user) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
it "generates a slug from the name" do
|
2023-01-27 08:58:33 +08:00
|
|
|
post "/slugs.json", params: { name: name }
|
2023-01-23 12:48:33 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["slug"]).to eq(Slug.for(name, ""))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|