Add a username slug to the user URL
This way it won't be possible to browse all user URLs by just going to /users/1, /users/2, /users/3, ... and collect usernames, which might not be desirable in some cases. Note we could use the username as a URL parameter and just find the user with `@user = User.find_by!(id: id, username: username)`, but since usernames might contain strange characters, this might lead to strange/ugly URLs. Finally, note we're using `username.to_s` in order to cover the case where the username is `nil` (as is the case with erased users).
This commit is contained in:
39
spec/controllers/users_controller_spec.rb
Normal file
39
spec/controllers/users_controller_spec.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe UsersController do
|
||||
describe "GET show" do
|
||||
let!(:user) { create(:user, username: "James Jameson") }
|
||||
|
||||
it "finds a user by ID and slug" do
|
||||
get :show, params: { id: "#{user.id}-james-jameson" }
|
||||
|
||||
expect(response).to be_successful
|
||||
end
|
||||
|
||||
it "does not find a user by just an ID" do
|
||||
expect do
|
||||
get :show, params: { id: user.id }
|
||||
end.to raise_error ActiveRecord::RecordNotFound
|
||||
end
|
||||
|
||||
it "does not find a user by just a slug" do
|
||||
expect do
|
||||
get :show, params: { id: "james-jameson" }
|
||||
end.to raise_error ActiveRecord::RecordNotFound
|
||||
end
|
||||
|
||||
it "does not find a user with the wrong slug" do
|
||||
expect do
|
||||
get :show, params: { id: "#{user.id}-James Jameson" }
|
||||
end.to raise_error ActiveRecord::RecordNotFound
|
||||
end
|
||||
|
||||
it "finds users without username by ID" do
|
||||
user.erase
|
||||
|
||||
get :show, params: { id: user.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user