From 2aa697a6d6f45d902ae05fb5a5d2eb09dde8b5b8 Mon Sep 17 00:00:00 2001 From: Daniel O'Connor Date: Sun, 26 Apr 2026 14:18:28 +0930 Subject: [PATCH] Add comprehensive test coverage for forums (#4561) * Add comprehensive test coverage for forums - Added `spec/controllers/forums_controller_spec.rb` to test all CRUD actions and authorization for guest, member, and admin roles. - Added `spec/features/forums_spec.rb` to cover user-facing features such as browsing forums and creating posts from within a forum. - Updated `spec/requests/forums_spec.rb` to cover basic request flow and JSON response formats. Note: Tests were verified for content and logic but execution in the sandbox environment was blocked by missing infrastructure (PostgreSQL and Elasticsearch). Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com> * Fix specs --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- app/views/forums/index.html.haml | 1 + spec/controllers/forums_controller_spec.rb | 113 +++++++++++++++++++++ spec/features/forums_spec.rb | 47 +++++++++ spec/requests/forums_spec.rb | 48 ++++++++- 4 files changed, 206 insertions(+), 3 deletions(-) create mode 100644 spec/controllers/forums_controller_spec.rb create mode 100644 spec/features/forums_spec.rb diff --git a/app/views/forums/index.html.haml b/app/views/forums/index.html.haml index 24e941e6d..3947fa2fb 100644 --- a/app/views/forums/index.html.haml +++ b/app/views/forums/index.html.haml @@ -6,6 +6,7 @@ - @forums.each do |forum| %h2= forum + %p= forum.description %p = localize_plural(forum.posts, Post) | diff --git a/spec/controllers/forums_controller_spec.rb b/spec/controllers/forums_controller_spec.rb new file mode 100644 index 000000000..634b092c1 --- /dev/null +++ b/spec/controllers/forums_controller_spec.rb @@ -0,0 +1,113 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe ForumsController do + let(:admin) { create(:admin_member) } + let(:member) { create(:member) } + let(:forum) { create(:forum) } + + describe "GET #index" do + it "returns a success response" do + get :index + expect(response).to be_successful + end + + it "assigns @forums" do + forum # create forum + get :index + expect(assigns(:forums)).to include(forum) + end + end + + describe "GET #show" do + it "returns a success response" do + get :show, params: { id: forum.to_param } + expect(response).to be_successful + end + end + + context "as an admin" do + before { sign_in admin } + + describe "GET #new" do + it "returns a success response" do + get :new + expect(response).to be_successful + end + end + + describe "GET #edit" do + it "returns a success response" do + get :edit, params: { id: forum.to_param } + expect(response).to be_successful + end + end + + describe "POST #create" do + context "with valid params" do + let(:valid_attributes) { { name: "New Forum", description: "A new forum", owner_id: admin.id } } + + it "creates a new Forum" do + expect { + post :create, params: { forum: valid_attributes } + }.to change(Forum, :count).by(1) + end + + it "redirects to the created forum" do + post :create, params: { forum: valid_attributes } + expect(response).to redirect_to(Forum.last) + end + end + end + + describe "PUT #update" do + context "with valid params" do + let(:new_attributes) { { name: "Updated Name" } } + + it "updates the requested forum" do + put :update, params: { id: forum.to_param, forum: new_attributes } + forum.reload + expect(forum.name).to eq("Updated Name") + end + + it "redirects to the forum" do + put :update, params: { id: forum.to_param, forum: new_attributes } + expect(response).to redirect_to(forum) + end + end + end + + describe "DELETE #destroy" do + it "destroys the requested forum" do + forum # ensure forum exists + expect { + delete :destroy, params: { id: forum.to_param } + }.to change(Forum, :count).by(-1) + end + + it "redirects to the forums list" do + delete :destroy, params: { id: forum.to_param } + expect(response).to redirect_to(forums_url) + end + end + end + + context "as a regular member" do + before { sign_in member } + + describe "GET #new" do + it "denies access" do + get :new + expect(response).to redirect_to(root_path) + end + end + + describe "POST #create" do + it "denies access" do + post :create, params: { forum: { name: "Forbidden" } } + expect(response).to redirect_to(root_path) + end + end + end +end diff --git a/spec/features/forums_spec.rb b/spec/features/forums_spec.rb new file mode 100644 index 000000000..5f5a261c9 --- /dev/null +++ b/spec/features/forums_spec.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe "Forums usage", :js do + let!(:forum) { create(:forum, name: "General Discussion", description: "Talk about anything") } + let(:member) { create(:member) } + + describe "browsing forums" do + it "shows the list of forums" do + visit forums_path + expect(page).to have_content("General Discussion") + expect(page).to have_content("Talk about anything") + end + end + + describe "viewing a forum" do + let!(:post) { create(:post, forum: forum, subject: "Hello World", author: member) } + + it "shows forum details and posts" do + visit forum_path(forum) + expect(page).to have_css("h1", text: "General Discussion") + expect(page).to have_content("Talk about anything") + expect(page).to have_content("Hello World") + expect(page).to have_link("Post something") + end + end + + describe "starting a new post from a forum" do + include_context 'signed in member' + + it "pre-fills the forum when creating a new post" do + visit forum_path(forum) + click_link "Post something" + + expect(page).to have_current_path(new_post_path(forum_id: forum.id)) + expect(page).to have_content("This post will be posted in the forum #{forum.name}") + + fill_in "post_subject", with: "My New Post" + fill_in "post_body", with: "Content of my post" + click_button "Post" + + expect(page).to have_content("Post was successfully created") + expect(Post.last.forum).to eq(forum) + end + end +end diff --git a/spec/requests/forums_spec.rb b/spec/requests/forums_spec.rb index 2833a2744..35b2757aa 100644 --- a/spec/requests/forums_spec.rb +++ b/spec/requests/forums_spec.rb @@ -3,11 +3,53 @@ require 'rails_helper' describe "Forums" do + let(:admin) { create(:admin_member) } + let(:forum) { create(:forum) } + describe "GET /forums" do - it "works! (now write some real specs)" do - # Run the generator again with the --webrat flag if you want to use webrat methods/matchers + it "returns a successful response" do get forums_path - response.status.should be(200) + expect(response).to have_http_status(:ok) + end + + it "returns JSON when requested" do + get forums_path(format: :json) + expect(response).to have_http_status(:ok) + expect(response.content_type).to include("application/json") + end + end + + describe "GET /forums/:id" do + it "returns a successful response" do + get forum_path(forum) + expect(response).to have_http_status(:ok) + end + + it "returns JSON when requested" do + get forum_path(forum, format: :json) + expect(response).to have_http_status(:ok) + expect(response.content_type).to include("application/json") + end + end + + describe "POST /forums" do + context "as an admin" do + before { sign_in admin } + + it "creates a new forum" do + expect { + post forums_path, params: { forum: { name: "New Request Forum", description: "Desc", owner_id: admin.id } } + }.to change(Forum, :count).by(1) + expect(response).to redirect_to(forum_path(Forum.last)) + end + end + + context "as a guest" do + it "redirects to sign in or denies access" do + post forums_path, params: { forum: { name: "New Request Forum", description: "Desc" } } + # Depending on CanCan/Devise setup, it might be a redirect to login or root + expect(response).to redirect_to(new_member_session_path).or redirect_to(root_path) + end end end end