domain/users controller spec

This commit is contained in:
Dylan Knutson
2025-07-27 17:26:42 +00:00
parent 8b2ee14ef7
commit 1f44ec2fa2
3 changed files with 107 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
<%# nasty hack, otherwise postgres uses a bad query plan %>
<% if user.is_a?(Domain::User::FaUser) %>
<% if user.is_a?(Domain::User::FaUser) || user.is_a?(Domain::User::InkbunnyUser) %>
<% fav_posts = user.faved_posts.includes(:creator).limit(5) %>
<% else %>
<% fav_posts = user.faved_posts.limit(5) %>

View File

@@ -0,0 +1,106 @@
# typed: false
require "rails_helper"
RSpec.describe Domain::UsersController, type: :controller do
render_views
# Create a real user with admin role for tests that require authentication
let(:admin_user) { create(:user, :admin) }
# Shared examples for common show behavior across all user types
shared_examples "show action for user type" do |user_factory, param_prefix, param_attr|
let(:domain_user) { create(user_factory) }
let(:composite_param) { "#{param_prefix}@#{domain_user.send(param_attr)}" }
context "when user exists" do
it "returns a successful response" do
get :show, params: { id: composite_param }
expect(response).to be_successful
end
it "renders the show template" do
get :show, params: { id: composite_param }
expect(response).to render_template(:show)
end
it "sets the @user instance variable" do
get :show, params: { id: composite_param }
expect(assigns(:user)).to eq(domain_user)
end
it "authorizes the user" do
expect(controller).to receive(:authorize).with(domain_user)
get :show, params: { id: composite_param }
end
it "does not require authentication" do
# Ensure no user is signed in
sign_out :user
get :show, params: { id: composite_param }
expect(response).to be_successful
end
end
context "when user does not exist" do
let(:invalid_param) { "#{param_prefix}@nonexistent" }
it "raises ActiveRecord::RecordNotFound" do
expect { get :show, params: { id: invalid_param } }.to raise_error(
ActiveRecord::RecordNotFound,
)
end
end
context "with invalid composite parameter format" do
it "raises ActionController::BadRequest for malformed param" do
expect { get :show, params: { id: "invalid_format" } }.to raise_error(
ActionController::BadRequest,
/invalid id/,
)
end
it "raises ActionController::BadRequest for unknown model type" do
expect { get :show, params: { id: "unknown@test" } }.to raise_error(
ActionController::BadRequest,
/unknown model type/,
)
end
end
end
describe "GET #show" do
before do
# Mock authorization to allow all actions for these tests
allow(controller).to receive(:authorize).and_return(true)
end
context "for Domain::User::FaUser" do
include_examples "show action for user type",
:domain_user_fa_user,
"fa",
:url_name
end
context "for Domain::User::E621User" do
include_examples "show action for user type",
:domain_user_e621_user,
"e621",
:name
end
context "for Domain::User::InkbunnyUser" do
include_examples "show action for user type",
:domain_user_inkbunny_user,
"ib",
:name
end
context "param configuration" do
it "uses the correct param config for user_id_param" do
param_config = described_class.param_config
expect(param_config.user_id_param).to eq(:id)
end
end
end
end

View File

@@ -80,7 +80,6 @@ RSpec.configure do |config|
# Add FactoryBot methods
config.include FactoryBot::Syntax::Methods
config.render_views
end