mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-10 16:54:38 -04:00
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>
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
- @forums.each do |forum|
|
||||
%h2= forum
|
||||
%p= forum.description
|
||||
%p
|
||||
= localize_plural(forum.posts, Post)
|
||||
|
|
||||
|
||||
113
spec/controllers/forums_controller_spec.rb
Normal file
113
spec/controllers/forums_controller_spec.rb
Normal file
@@ -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
|
||||
47
spec/features/forums_spec.rb
Normal file
47
spec/features/forums_spec.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user