Skip to content

Commit

Permalink
add: new tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Waishnav committed Aug 18, 2024
1 parent ad49ae5 commit a595756
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 21 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ By default, SimpleDiscussion will attempt to send email and slack notifications
SimpleDiscussion.setup do |config|
config.send_email_notifications = false # Default: true
config.send_slack_notifications = false # Default: true

config.markdown_circuit_embed = false # Default: false
config.markdown_user_tagging = false # Default: false
config.markdown_video_embed = false # Default false
end
```

Expand Down
2 changes: 1 addition & 1 deletion app/models/spam_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class SpamReport < ApplicationRecord
belongs_to :user

validates :forum_post_id, :user_id, :reason, presence: true
validates :details, presence: true, if: -> { reason == "other" }
validates :details, presence: true, if: -> { reason == "others" }

enum reason: {
sexual_content: 0,
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class User < ApplicationRecord
:recoverable, :rememberable, :validatable

def moderator?
true
moderator
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddModeratorToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :moderator, :boolean, default: false
end
end
41 changes: 26 additions & 15 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,39 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_07_28_092034) do
ActiveRecord::Schema.define(version: 2024_08_13_072347) do
create_table "forum_categories", force: :cascade do |t|
t.string "name", null: false
t.string "slug", null: false
t.string "color", default: "000000"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
end

create_table "forum_leaderboards", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "points", default: 0, null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.index ["points"], name: "index_forum_leaderboards_on_points"
t.index ["user_id"], name: "index_forum_leaderboards_on_user_id", unique: true
end

create_table "forum_posts", force: :cascade do |t|
t.integer "forum_thread_id"
t.integer "user_id"
t.text "body"
t.boolean "solved", default: false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
end

create_table "forum_subscriptions", force: :cascade do |t|
t.integer "forum_thread_id"
t.integer "user_id"
t.string "subscription_type"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
end

create_table "forum_threads", force: :cascade do |t|
Expand All @@ -44,17 +53,17 @@
t.integer "forum_posts_count", default: 0
t.boolean "pinned", default: false
t.boolean "solved", default: false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
end

create_table "spam_reports", force: :cascade do |t|
t.integer "forum_post_id", null: false
t.integer "user_id", null: false
t.integer "reason", null: false
t.text "details"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.index ["forum_post_id"], name: "index_spam_reports_on_forum_post_id"
t.index ["user_id"], name: "index_spam_reports_on_user_id"
end
Expand All @@ -63,15 +72,17 @@
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "reset_password_sent_at", precision: nil
t.datetime "remember_created_at", precision: nil
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.string "name"
t.boolean "moderator", default: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_foreign_key "forum_leaderboards", "users"
add_foreign_key "forum_posts", "forum_threads"
add_foreign_key "forum_posts", "users"
add_foreign_key "forum_subscriptions", "forum_threads"
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ one:
two:
name: "Second User"
email: "second@example.com"

moderator:
name: "Moderator"
email: "moderator@moderator.com"
moderator: true
71 changes: 67 additions & 4 deletions test/integration/forum_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,34 @@ class ForumTest < ActionDispatch::IntegrationTest
include SimpleDiscussion::Engine.routes.url_helpers

setup do
sign_in users(:one)
@regular_user = users(:one)
@moderator_user = users(:moderator)
@forum_thread = forum_threads(:hello)
@forum_post = forum_posts(:hello)
@filter = LanguageFilter::Filter.new
end

test "threads index" do
sign_in @regular_user
get "/"
assert_response :success
assert_match "Community", response.body
end

test "categories" do
sign_in @regular_user
get forum_category_forum_threads_path(forum_categories(:general))
assert_response :success
end

test "show forum thread" do
get forum_thread_path(forum_threads(:hello))
sign_in @regular_user
get forum_thread_path(@forum_thread)
assert_response :success
end

test "create a forum thread" do
sign_in @regular_user
assert_difference "ForumThread.count" do
assert_difference "ForumPost.count" do
post forum_threads_path, params: {
Expand All @@ -45,8 +52,9 @@ class ForumTest < ActionDispatch::IntegrationTest
end

test "reply to a forum thread" do
sign_in @regular_user
assert_difference "ForumPost.count" do
post forum_thread_forum_posts_path(forum_threads(:hello)), params: {
post forum_thread_forum_posts_path(@forum_thread), params: {
forum_post: {
body: "Reply"
}
Expand All @@ -57,6 +65,7 @@ class ForumTest < ActionDispatch::IntegrationTest
end

test "cannot create a forum thread with inappropriate language in title" do
sign_in @regular_user
inappropriate_word = @filter.matchlist.to_a.sample
assert_no_difference "ForumThread.count" do
assert_no_difference "ForumPost.count" do
Expand All @@ -77,6 +86,8 @@ class ForumTest < ActionDispatch::IntegrationTest
end

test "cannot create a forum thread with inappropriate language in body" do
sign_in @regular_user

inappropriate_word = @filter.matchlist.to_a.sample
assert_no_difference "ForumThread.count" do
assert_no_difference "ForumPost.count" do
Expand All @@ -97,9 +108,11 @@ class ForumTest < ActionDispatch::IntegrationTest
end

test "cannot reply to a forum thread with inappropriate language" do
sign_in @regular_user

inappropriate_word = @filter.matchlist.to_a.sample
assert_no_difference "ForumPost.count" do
post forum_thread_forum_posts_path(forum_threads(:hello)), params: {
post forum_thread_forum_posts_path(@forum_thread), params: {
forum_post: {
body: "contains inappropriate language: #{inappropriate_word}"
}
Expand All @@ -111,6 +124,8 @@ class ForumTest < ActionDispatch::IntegrationTest
end

test "can create a forum thread with appropriate language in title and body" do
sign_in @regular_user

assert_difference "ForumThread.count" do
assert_difference "ForumPost.count" do
post forum_threads_path, params: {
Expand All @@ -127,4 +142,52 @@ class ForumTest < ActionDispatch::IntegrationTest

assert_redirected_to forum_thread_path(ForumThread.last)
end

test "can report a post" do
sign_in @regular_user

assert_difference "SpamReport.count" do
post report_spam_forum_thread_forum_post_path(@forum_thread, @forum_post), params: {
reason: "irrelevant_content"
}
end
assert_redirected_to forum_thread_path(@forum_thread, anchor: dom_id(@forum_post))

spam_report = SpamReport.last
assert_equal @forum_post, spam_report.forum_post
assert_equal @regular_user, spam_report.user
assert_equal "irrelevant_content", spam_report.reason
end

test "can report a post with 'other' reason and details" do
sign_in @regular_user

assert_difference "SpamReport.count" do
post report_spam_forum_thread_forum_post_path(@forum_thread, @forum_post), params: {
reason: "others",
details: "This post contains copyrighted material."
}
end

assert_redirected_to forum_thread_path(@forum_thread, anchor: dom_id(@forum_post))

spam_report = SpamReport.last
assert_equal "others", spam_report.reason
assert_equal "This post contains copyrighted material.", spam_report.details
end

test "modeartor can view spam reports page" do
sign_in @moderator_user

get spam_reports_forum_threads_path
assert_response :success
end

test "regular user can't view spam reports page" do
sign_in @regular_user

get spam_reports_forum_threads_path
assert_response :redirect
assert_redirected_to root_path
end
end

0 comments on commit a595756

Please sign in to comment.