add user_id to poll serializer

fixes CNVS-13360
This commit adds the user_id to the poll json response, only for
teachers.

Test plan
- As a teacher, create a poll
- Request that poll via the poll#show endpoint
- It should have a 'user_id' field with the id of the creator
- As a student, request the same poll via the poll#show endpoint (the
  student won't be able to view it without a published poll session)
- The student should not see the 'user_id' field.

Change-Id: Ib0b09d779f3486edaa38a870d20211268be819d3
Reviewed-on: https://gerrit.instructure.com/35693
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Derek DeVries <ddevries@instructure.com>
QA-Review: Caleb Guanzon <cguanzon@instructure.com>
Product-Review: Josh Simpson <jsimpson@instructure.com>
This commit is contained in:
Josh Simpson 2014-05-30 14:53:08 -06:00
parent f9240e4523
commit a49ba0748c
3 changed files with 24 additions and 2 deletions

View File

@ -45,6 +45,11 @@ module Polling
# "example": "2014-01-07T15:16:18Z",
# "type": "string",
# "format": "date-time"
# },
# "user_id": {
# "description": "The unique identifier for the user that created the poll.",
# "example": 105,
# "type": "integer"
# }
# }
# }

View File

@ -2,7 +2,7 @@ module Polling
class PollSerializer < Canvas::APISerializer
root :poll
attributes :id, :question, :description, :total_results, :created_at
attributes :id, :question, :description, :total_results, :created_at, :user_id
has_many :poll_choices, embed: :ids
@ -24,7 +24,7 @@ module Polling
private
def teacher_keys
[:total_results]
[:total_results, :user_id]
end
def student_keys

View File

@ -80,6 +80,13 @@ describe Polling::PollsController, type: :request do
poll_json = json['polls'].first
poll_json.should have_key("total_results")
end
it "returns the id of the user that created the poll" do
json = get_show
poll_json = json['polls'].first
poll_json.should have_key("user_id")
poll_json['user_id'].should == @teacher.id.to_s
end
end
context "as a student" do
@ -94,6 +101,16 @@ describe Polling::PollsController, type: :request do
poll_json.should_not have_key("total_results")
end
it "shouldn't return the id of the user that created the poll" do
student_in_course(:active_all => true, :course => @course)
session = @poll.poll_sessions.create!(course: @course)
session.publish!
json = get_show
poll_json = json['polls'].first
poll_json.should_not have_key("user_id")
end
it "is unauthorized if there are no published sessions" do
student_in_course(:active_all => true, :course => @course)
section = @course.course_sections.create!(name: 'Section 2')