Commit 5ce9ead2 by wajeeha-khalid

rebased with release to hotpatch

parent 339fd755
......@@ -5,6 +5,10 @@ require_relative 'content'
class CommentThread < Content
include Mongoid::Timestamps
include Mongoid::Attributes::Dynamic
include ActiveModel::MassAssignmentSecurity
include Tire::Model::Search
include Tire::Model::Callbacks
extend Enumerize
voteable self, :up => +1, :down => -1
......@@ -28,8 +32,6 @@ class CommentThread < Content
index({author_id: 1, course_id: 1})
include Tire::Model::Search
include Tire::Model::Callbacks
index_name Content::ES_INDEX_NAME
......@@ -48,12 +50,12 @@ class CommentThread < Content
indexes :commentable_id, type: :string, index: :not_analyzed, included_in_all: false
indexes :author_id, type: :string, as: 'author_id', index: :not_analyzed, included_in_all: false
indexes :group_id, type: :integer, as: 'group_id', index: :not_analyzed, included_in_all: false
indexes :id, :index => :not_analyzed
indexes :thread_id, :analyzer => :keyword, :as => "_id"
indexes :id, :index => :not_analyzed
indexes :thread_id, :analyzer => :keyword, :as => '_id'
end
belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true#, autosave: true
has_many :comments, dependent: :destroy#, autosave: true# Use destroy to envoke callback on the top-level comments TODO async
belongs_to :author, class_name: 'User', inverse_of: :comment_threads, index: true
has_many :comments, dependent: :destroy # Use destroy to invoke callback on the top-level comments
has_many :activities, autosave: true
attr_accessible :title, :body, :course_id, :commentable_id, :anonymous, :anonymous_to_peers, :closed, :thread_type
......@@ -69,24 +71,12 @@ class CommentThread < Content
before_create :set_last_activity_at
before_update :set_last_activity_at, :unless => lambda { closed_changed? }
after_update :clear_endorsements
before_destroy :destroy_subscriptions
scope :active_since, ->(from_time) { where(:last_activity_at => {:$gte => from_time}) }
scope :standalone_context, ->() { where(:context => :standalone) }
scope :course_context, ->() { where(:context => :course) }
def self.new_dumb_thread(options={})
c = self.new
c.title = options[:title] || "title"
c.body = options[:body] || "body"
c.commentable_id = options[:commentable_id] || "commentable_id"
c.course_id = options[:course_id] || "course_id"
c.author = options[:author] || User.first
c.save!
c
end
def activity_since(from_time=nil)
if from_time
activities.where(:created_at => {:$gte => from_time})
......@@ -95,13 +85,21 @@ class CommentThread < Content
end
end
def activity_today; activity_since(Date.today.to_time); end
def activity_today
activity_since(Date.today.to_time)
end
def activity_this_week; activity_since(Date.today.to_time - 1.weeks); end
def activity_this_week
activity_since(Date.today.to_time - 1.weeks)
end
def activity_this_month; activity_since(Date.today.to_time - 1.months); end
def activity_this_month
activity_since(Date.today.to_time - 1.months)
end
def activity_overall; activity_since(nil); end
def activity_overall
activity_since(nil)
end
def root_comments
Comment.roots.where(comment_thread_id: self.id)
......@@ -124,25 +122,26 @@ class CommentThread < Content
end
def to_hash(params={})
as_document.slice(*%w[thread_type title body course_id anonymous anonymous_to_peers commentable_id created_at updated_at at_position_list closed context])
.merge("id" => _id, "user_id" => author_id,
"username" => author_username,
"votes" => votes.slice(*%w[count up_count down_count point]),
"abuse_flaggers" => abuse_flaggers,
"tags" => [],
"type" => "thread",
"group_id" => group_id,
"pinned" => pinned?,
"comments_count" => comment_count)
as_document.slice(*%w[thread_type title body course_id anonymous anonymous_to_peers commentable_id created_at updated_at at_position_list closed context last_activity_at])
.merge('id' => _id,
'user_id' => author_id,
'username' => author_username,
'votes' => votes.slice(*%w[count up_count down_count point]),
'abuse_flaggers' => abuse_flaggers,
'tags' => [],
'type' => 'thread',
'group_id' => group_id,
'pinned' => pinned?,
'comments_count' => comment_count)
end
def comment_thread_id
#so that we can use the comment thread id as a common attribute for flagging
self.id
end
private
end
private
def set_last_activity_at
self.last_activity_at = Time.now.utc unless last_activity_at_changed?
......@@ -154,8 +153,8 @@ private
# the last activity time on the thread. Therefore the callbacks would be mutually recursive and we end up with a
# 'SystemStackError'. The 'set' method skips callbacks and therefore bypasses this issue.
self.comments.each do |comment|
comment.set :endorsed, false
comment.set :endorsement, nil
comment.set(endorsed: false)
comment.set(endorsement: nil)
end
end
end
......@@ -163,5 +162,4 @@ private
def destroy_subscriptions
subscriptions.delete_all
end
end
ENV["SINATRA_ENV"] = "test"
require 'simplecov'
SimpleCov.start
if ENV['CI']=='true'
require 'codecov'
SimpleCov.formatter = SimpleCov::Formatter::Codecov
end
require File.join(File.dirname(__FILE__), '..', 'app')
require 'sinatra'
require 'rack/test'
require 'sinatra'
require 'yajl'
require 'database_cleaner'
require 'support/database_cleaner'
require 'support/elasticsearch'
require 'support/factory_girl'
# setup test environment
set :environment, :test
......@@ -15,6 +23,9 @@ set :run, false
set :raise_errors, true
set :logging, false
Mongoid.logger.level = Logger::WARN
Mongo::Logger.logger.level = ENV["ENABLE_MONGO_DEBUGGING"] ? Logger::DEBUG : Logger::WARN
Delayed::Worker.delay_jobs = false
def app
......@@ -28,36 +39,12 @@ def set_api_key_header
current_session.header "X-Edx-Api-Key", TEST_API_KEY
end
def delete_es_index
Tire.index Content::ES_INDEX_NAME do delete end
end
def create_es_index
new_index = Tire.index Content::ES_INDEX_NAME
new_index.create
[CommentThread, Comment].each do |klass|
klass.put_search_index_mapping
end
end
def refresh_es_index
# we are using the same index for two types, which is against the
# grain of Tire's design. This is why this method works for both
# comment_threads and comments.
CommentThread.tire.index.refresh
end
RSpec.configure do |config|
config.include Rack::Test::Methods
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run focus: true
config.run_all_when_everything_filtered = true
config.before(:each) do
Mongoid::IdentityMap.clear
DatabaseCleaner.clean
delete_es_index
create_es_index
end
end
Mongoid.configure do |config|
......@@ -72,16 +59,24 @@ def create_test_user(id)
User.create!(external_id: id.to_s, username: "user#{id}")
end
def init_without_subscriptions
# Add the given body of text to the list of blocked texts/hashes.
def block_post_body(body='blocked post')
body = body.strip.downcase.gsub(/[^a-z ]/, '').gsub(/\s+/, ' ')
blocked_hash = Digest::MD5.hexdigest(body)
Content.mongo_client[:blocked_hash].insert_one(hash: blocked_hash)
[Comment, CommentThread, User, Notification, Subscription, Activity, Delayed::Backend::Mongoid::Job].each(&:delete_all).each(&:remove_indexes).each(&:create_indexes)
Content.mongo_session[:blocked_hash].drop
delete_es_index
create_es_index
# reload the global holding the blocked hashes
CommentService.blocked_hashes = Content.mongo_client[:blocked_hash].find(nil, projection: {hash: 1}).map do |d|
d['hash']
end
blocked_hash
end
def init_without_subscriptions
commentable = Commentable.new("question_1")
users = (1..10).map{|id| create_test_user(id)}
users = (1..10).map { |id| create_test_user(id) }
user = users.first
thread = CommentThread.new(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1", commentable_id: commentable.id)
......@@ -150,63 +145,15 @@ def init_without_subscriptions
Comment.all.each do |c|
user.vote(c, :up) # make the first user always vote up for convenience
users[2,9].each {|user| user.vote(c, [:up, :down].sample)}
users[2, 9].each { |user| user.vote(c, [:up, :down].sample) }
end
CommentThread.all.each do |c|
user.vote(c, :up) # make the first user always vote up for convenience
users[2,9].each {|user| user.vote(c, [:up, :down].sample)}
users[2, 9].each { |user| user.vote(c, [:up, :down].sample) }
end
Content.mongo_session[:blocked_hash].insert(hash: Digest::MD5.hexdigest("blocked post"))
# reload the global holding the blocked hashes
CommentService.blocked_hashes = Content.mongo_session[:blocked_hash].find.select(hash: 1).each.map {|d| d["hash"]}
end
def init_with_subscriptions
[Comment, CommentThread, User, Notification, Subscription, Activity, Delayed::Backend::Mongoid::Job].each(&:delete_all).each(&:remove_indexes).each(&:create_indexes)
delete_es_index
create_es_index
user1 = create_test_user(1)
user2 = create_test_user(2)
user2.subscribe(user1)
commentable = Commentable.new("question_1")
user1.subscribe(commentable)
user2.subscribe(commentable)
thread = CommentThread.new(title: "I can't solve this problem", body: "can anyone help me?", course_id: "1", commentable_id: commentable.id)
thread.author = user1
user1.subscribe(thread)
user2.subscribe(thread)
thread.save!
thread = thread.reload
comment = thread.comments.new(body: "this problem is so easy", course_id: "1")
comment.author = user2
comment.save!
comment1 = comment.children.new(body: "not for me!", course_id: "1")
comment1.author = user1
comment1.comment_thread = thread
comment1.save!
comment2 = comment1.children.new(body: "not for me neither!", course_id: "1")
comment2.author = user2
comment2.comment_thread = thread
comment2.save!
thread = CommentThread.new(title: "This problem is wrong", body: "it is unsolvable", course_id: "2", commentable_id: commentable.id)
thread.author = user2
user2.subscribe(thread)
thread.save!
thread = CommentThread.new(title: "I don't know what to say", body: "lol", course_id: "2", commentable_id: "something else")
thread.author = user1
thread.save!
block_post_body
end
# this method is used to test results produced using the helper function handle_threads_query
......@@ -215,28 +162,28 @@ def check_thread_result(user, thread, hash, is_json=false)
expected_keys = %w(id thread_type title body course_id commentable_id created_at updated_at context)
expected_keys += %w(anonymous anonymous_to_peers at_position_list closed user_id)
expected_keys += %w(username votes abuse_flaggers tags type group_id pinned)
expected_keys += %w(comments_count unread_comments_count read endorsed)
expected_keys += %w(comments_count unread_comments_count read endorsed last_activity_at)
# these keys are checked separately, when desired, using check_thread_response_paging.
actual_keys = hash.keys - [
"children", "endorsed_responses", "non_endorsed_responses", "resp_skip",
"resp_limit", "resp_total", "non_endorsed_resp_total"
"children", "endorsed_responses", "non_endorsed_responses", "resp_skip",
"resp_limit", "resp_total", "non_endorsed_resp_total"
]
actual_keys.sort.should == expected_keys.sort
hash["title"].should == thread.title
hash["body"].should == thread.body
hash["course_id"].should == thread.course_id
hash["anonymous"].should == thread.anonymous
hash["anonymous_to_peers"].should == thread.anonymous_to_peers
hash["commentable_id"].should == thread.commentable_id
hash["at_position_list"].should == thread.at_position_list
hash["closed"].should == thread.closed
hash["course_id"].should == thread.course_id
hash["anonymous"].should == thread.anonymous
hash["anonymous_to_peers"].should == thread.anonymous_to_peers
hash["commentable_id"].should == thread.commentable_id
hash["at_position_list"].should == thread.at_position_list
hash["closed"].should == thread.closed
hash["user_id"].should == thread.author.id
hash["username"].should == thread.author.username
hash["votes"]["point"].should == thread.votes["point"]
hash["votes"]["count"].should == thread.votes["count"]
hash["votes"]["up_count"].should == thread.votes["up_count"]
hash["votes"]["down_count"].should == thread.votes["down_count"]
hash["votes"]["point"].should == thread.votes["point"]
hash["votes"]["count"].should == thread.votes["count"]
hash["votes"]["up_count"].should == thread.votes["up_count"]
hash["votes"]["down_count"].should == thread.votes["down_count"]
hash["abuse_flaggers"].should == thread.abuse_flaggers
hash["tags"].should == []
hash["type"].should == "thread"
......@@ -249,15 +196,17 @@ def check_thread_result(user, thread, hash, is_json=false)
if is_json
hash["id"].should == thread._id.to_s
hash["created_at"].should == thread.created_at.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
hash["updated_at"].should == thread.updated_at.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
hash["updated_at"].should == thread.updated_at.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
hash["last_activity_at"].should == thread.last_activity_at.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
else
hash["created_at"].should == thread.created_at
hash["updated_at"].should == thread.updated_at
hash["last_activity_at"].should == thread.last_activity_at
end
if user.nil?
hash["unread_comments_count"].should == thread.comments.length
hash["read"].should == false
hash["read"].should == false
else
expected_unread_cnt = thread.comments.length # initially assume nothing has been read
read_states = user.read_states.where(course_id: thread.course_id).to_a
......@@ -265,7 +214,7 @@ def check_thread_result(user, thread, hash, is_json=false)
read_date = read_states.first.last_read_times[thread.id.to_s]
if read_date
thread.comments.each do |c|
if c.author != user and c.updated_at < read_date
if c.updated_at < read_date
expected_unread_cnt -= 1
end
end
......@@ -282,12 +231,18 @@ def check_thread_result_json(user, thread, json_response)
check_thread_result(user, thread, json_response, true)
end
def check_unread_thread_result_json(thread, json_response)
# when thread is unread we do not check if thread matches the user read states data
# and explicitly asserts `read` to false; hence pass user=nil
check_thread_result(nil, thread, json_response, true)
end
def check_thread_response_paging(thread, hash, resp_skip=0, resp_limit=nil, is_json=false, recursive=false)
case thread.thread_type
when "discussion"
check_discussion_response_paging(thread, hash, resp_skip, resp_limit, is_json, recursive)
when "question"
check_question_response_paging(thread, hash, resp_skip, resp_limit, is_json, recursive)
when "discussion"
check_discussion_response_paging(thread, hash, resp_skip, resp_limit, is_json, recursive)
when "question"
check_question_response_paging(thread, hash, resp_skip, resp_limit, is_json, recursive)
end
end
......@@ -298,8 +253,9 @@ def check_comment(comment, hash, is_json, recursive=false)
hash["username"].should == comment.author_username
hash["endorsed"].should == comment.endorsed
hash["endorsement"].should == comment.endorsement
children = Comment.where({"parent_id" => comment.id}).sort({"sk" => 1}).to_a
hash["child_count"].should == children.length
if recursive
children = Comment.where({"parent_id" => comment.id}).sort({"sk" => 1}).to_a
hash["children"].length.should == children.length
hash["children"].each_with_index do |child_hash, i|
check_comment(children[i], child_hash, is_json)
......@@ -313,8 +269,8 @@ def check_discussion_response_paging(thread, hash, resp_skip=0, resp_limit=nil,
total_responses = all_responses.length
hash["resp_total"].should == total_responses
expected_responses = resp_limit.nil? ?
all_responses.drop(resp_skip) :
all_responses.drop(resp_skip).take(resp_limit)
all_responses.drop(resp_skip) :
all_responses.drop(resp_skip).take(resp_limit)
hash["children"].length.should == expected_responses.length
hash["children"].each_with_index do |response_hash, i|
......@@ -339,8 +295,8 @@ def check_question_response_paging(thread, hash, resp_skip=0, resp_limit=nil, is
hash["non_endorsed_resp_total"] == non_endorsed_responses.length
expected_non_endorsed_responses = resp_limit.nil? ?
non_endorsed_responses.drop(resp_skip) :
non_endorsed_responses.drop(resp_skip).take(resp_limit)
non_endorsed_responses.drop(resp_skip) :
non_endorsed_responses.drop(resp_skip).take(resp_limit)
hash["non_endorsed_responses"].length.should == expected_non_endorsed_responses.length
hash["non_endorsed_responses"].each_with_index do |response_hash, i|
check_comment(expected_non_endorsed_responses[i], response_hash, is_json, recursive)
......@@ -377,6 +333,7 @@ def make_comment(author, parent, text)
else
coll = parent.children
thread = parent.comment_thread
parent.set(child_count: coll.length + 1)
end
comment = coll.new(body: text, course_id: parent.course_id)
comment.author = author
......@@ -391,12 +348,12 @@ end
# AKA this will overwrite "standalone t0" each time it is called.
def make_standalone_thread_with_comments(author, index=0)
thread = make_thread(
author,
"standalone thread #{index}",
DFLT_COURSE_ID,
"pdq",
:discussion,
:standalone
author,
"standalone thread #{index}",
DFLT_COURSE_ID,
"pdq",
:discussion,
:standalone
)
3.times do |i|
......@@ -425,5 +382,19 @@ def setup_10_threads
@comments["t#{i} c#{j}"] = comment
end
end
@default_order = 10.times.map {|i| "t#{i}"}.reverse
@default_order = 10.times.map { |i| "t#{i}" }.reverse
end
# Creates a CommentThread with a Comment, and nested child Comment.
# The author of the thread is subscribed to the thread.
def create_comment_thread_and_comments
# Create a new comment thread, and subscribe the author to the thread
thread = create(:comment_thread, :subscribe_author)
# Create a comment along with a nested child comment
comment = create(:comment, comment_thread: thread)
create(:comment, parent: comment)
comment.set(child_count: 1)
thread
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment