Commit f78fdd94 by cahrens

Add group_id to payload from notifications.

TNL-299
parent 2d2a05a1
......@@ -255,14 +255,14 @@ helpers do
end
end
def notifications_by_date_range_and_user_ids start_date_time, end_date_time, user_ids
def notifications_by_date_range_and_user_ids(start_date_time, end_date_time, user_ids)
#given a date range and a user, find all of the notifiable content
#key by thread id, and return notification messages for each user
#first, find the subscriptions for the users
subscriptions = Subscription.where(:subscriber_id.in => user_ids)
#get the thhread ids
#get the thread ids
thread_ids = subscriptions.collect{|t| t.source_id}.uniq
#find all the comments
......@@ -308,6 +308,9 @@ helpers do
t["content"] = []
t["title"] = current_thread.title
t["commentable_id"] = current_thread.commentable_id
unless current_thread.group_id.nil?
t["group_id"] = current_thread.group_id
end
else
t = notification_map[u][c.course_id][c.comment_thread_id.to_s]
end
......
......@@ -8,23 +8,43 @@ describe "app" do
set_api_key_header
end
describe "POST /api/v1/notifications" do
it "returns notifications by class and user" do
start_time = Time.now
user = User.create(:external_id => 1,:username => "example")
def create_thread(user, options = {})
# Create a CommentThread with the given user.
# Can optionally specify a cohort group_id via options.
# Returns the created CommentThread.
commentable = Commentable.new("question_1")
random_string = (0...8).map{ ('a'..'z').to_a[rand(26)] }.join
thread = CommentThread.new(title: "Test title", body: "elephant otter", course_id: "1", commentable_id: commentable.id, comments_text_dummy: random_string)
thread = CommentThread.new(
title: "Test title", body: "elephant otter", course_id: "1",
commentable_id: commentable.id, comments_text_dummy: random_string
)
thread.thread_type = :discussion
thread.author = user
if options[:group_id]
thread.group_id = options[:group_id]
end
thread.save!
subscription = Subscription.create({:subscriber_id => user._id.to_s, :source_id => thread._id.to_s})
return thread
end
def get_thread_notification(comment_body, options = {})
# Creates a thread and comment with the specified comment_body.
# Can optionally specify a cohort group_id via options.
# Calls the notifications API to retrieve the notification for the thread
# and returns the response hash for the single comment thread within the course.
# Keys for the returned hash: content, title, commentable_id, group_id (only present if cohorted).
start_time = Time.now
user = User.create(:external_id => 1,:username => "example")
thread = create_thread(user, options)
subscription = Subscription.create(:subscriber_id => user._id.to_s, :source_id => thread._id.to_s)
dummy = random_string = (0..5).map{ ('a'..'z').to_a[rand(26)] }.join
comment = Comment.new
comment.comment_thread_id = thread.id
comment.body = dummy
comment.body = comment_body
comment.author_id = user.id
comment.course_id = 'test course'
comment.save!
......@@ -33,10 +53,36 @@ describe "app" do
end_time = Time.now
post "/api/v1/notifications", from: CGI::escape(start_time.to_s), to: CGI::escape(end_time.to_s), user_ids: subscription.subscriber_id
post(
"/api/v1/notifications",
{
from: CGI::escape(start_time.to_s),
to: CGI::escape(end_time.to_s),
user_ids: subscription.subscriber_id
}
)
last_response.should be_ok
last_response.body.to_s.include?(dummy).should == true
response_hash = JSON.parse(last_response.body)
return response_hash[user.id][comment.course_id][thread.id.to_s]
end
describe "POST /api/v1/notifications" do
it "returns notifications by class and user" do
expected_comment_body = random_string = (0..5).map{ ('a'..'z').to_a[rand(26)] }.join
thread_notification = get_thread_notification(expected_comment_body)
actual_comment_body = thread_notification["content"][0]["body"]
actual_comment_body.should eq(expected_comment_body)
end
it "contains cohort group_id if defined" do
thread_notification = get_thread_notification("dummy comment content", :group_id => 1974)
thread_notification["group_id"].should be(1974)
end
it "does not contain cohort group_id if not defined" do
thread_notification = get_thread_notification("dummy comment content")
thread_notification.has_key?("group_id").should be_false
end
it "returns only threads subscribed to by user" do
......@@ -44,16 +90,11 @@ describe "app" do
# first make a dummy thread and comment and a subscription
commentable = Commentable.new("question_1")
user = User.create(:external_id => 1,:username => "example")
random_string = (0...8).map{ ('a'..'z').to_a[rand(26)] }.join
thread = CommentThread.new(title: "Test title", body: "elephant otter", course_id: "1", commentable_id: commentable.id, comments_text_dummy: random_string)
thread.thread_type = :discussion
thread.author = user
thread.save!
thread = create_thread(user)
subscription = Subscription.create({:subscriber_id => user._id.to_s, :source_id => thread._id.to_s})
comment = Comment.new(body: random_string, course_id: "1", commentable_id: commentable.id)
comment = Comment.new(body: "dummy body text", course_id: "1", commentable_id: commentable.id)
comment.author = user
comment.comment_thread = thread
comment.save!
......
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