discourse/spec/requests/slugs_controller_spec.rb
Alan Guo Xiang Tan ed6c9d1545
DEV: Call Discourse.redis.flushdb after the end of each test (#29117)
There have been too many flaky tests as a result of leaking state in
Redis so it is easier to resolve them by ensuring we flush Redis'
database.

Locally on my machine, calling `Discourse.redis.flushdb` takes around
0.1ms which means this change will have very little impact on test
runtimes.
2024-10-09 07:19:31 +08:00

64 lines
1.7 KiB
Ruby

# 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
post "/slugs.json", params: { name: name }
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
post "/slugs.json", params: { name: name }
expect(response.status).to eq(200)
expect(response.parsed_body["slug"]).to eq(Slug.for(name, ""))
end
it "requires name" do
post "/slugs.json"
expect(response.status).to eq(400)
end
describe "rate limiting" do
before { RateLimiter.enable }
it "rate limits" do
stub_const(SlugsController, "MAX_SLUG_GENERATIONS_PER_MINUTE", 1) do
post "/slugs.json?name=#{name}"
post "/slugs.json?name=#{name}"
end
expect(response.status).to eq(429)
end
end
context "when user is not TL4 or higher" do
before { current_user.change_trust_level!(1) }
it "returns a 403 error" do
post "/slugs.json?name=#{name}"
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
post "/slugs.json", params: { name: name }
expect(response.status).to eq(200)
expect(response.parsed_body["slug"]).to eq(Slug.for(name, ""))
end
end
end
end
end