Process assignmet params from create discussion mutation

closes VICE-3799
flag=discussion_create

Test Plan:
- create an assignment on a discussion topic via graqhiql

Sample Mutation:
mutation MyMutation($topicTitle: String!) {
  __typename
  createDiscussionTopic(input: {
    contextId: "1",
    contextType: "Course",
    message: "Well hello there",
    published: true,
    title: $topicTitle,
    assignment: {courseId: "1",
      name: $topicTitle,
      pointsPossible: 15,
      gradingType: percent,
      peerReviews: {
        anonymousReviews: true,
        automaticReviews: true,
        count: 2,
        enabled: true,
        intraReviews: true,
        dueAt: "2023-11-23T17:44:51.014Z"
      }
    }
  }) {
    discussionTopic {
      _id
      title
      message
      assignment {
        _id
        name
        pointsPossible
        gradingType
        peerReviews {
          anonymousReviews
          automaticReviews
          count
          enabled
        }
      }
    }
  }
}

Variables: {
  "topicTitle": "GQL Test 5"
}

Change-Id: Ie19f0032a886f279ccbf7f924bc822e44d2ac480
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/330897
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
QA-Review: Jason Gillett <jason.gillett@instructure.com>
Product-Review: Drake Harper <drake.harper@instructure.com>
Reviewed-by: Caleb Guanzon <cguanzon@instructure.com>
This commit is contained in:
Drake Harper 2023-10-20 13:40:44 -06:00
parent 076f037ca5
commit d698b47c7b
4 changed files with 175 additions and 0 deletions

View File

@ -78,6 +78,7 @@ class Mutations::AssignmentBase < Mutations::BaseMutation
@working_assignment = working_assignment
@session = session
@current_user = current_user
@context = working_assignment.context
end
attr_reader :session

View File

@ -85,8 +85,17 @@ class Mutations::CreateDiscussionTopic < Mutations::DiscussionBase
process_future_date_inputs(input[:delayed_post_at], input[:lock_at], discussion_topic)
process_locked_parameter(input[:locked], discussion_topic)
topic_assignment = discussion_topic.build_assignment(input[:assignment].to_h) if input[:assignment]
return validation_error(I18n.t("You do not have permissions to create assignments in the provided course")) unless topic_assignment.nil? || topic_assignment&.grants_right?(current_user, :create)
discussion_topic.assignment = topic_assignment if topic_assignment&.grants_right?(current_user, :create)
return errors_for(discussion_topic) unless discussion_topic.save
if topic_assignment
return errors_for(topic_assignment) unless topic_assignment.save
end
{ discussion_topic: }
rescue ActiveRecord::RecordNotFound
raise GraphQL::ExecutionError, "Not found"

View File

@ -351,6 +351,40 @@ class DiscussionTopic < ActiveRecord::Base
end
end
def build_assignment(assignment_input = {})
return if deleted?
working_assignment = course.assignments.build
working_assignment.context = context
working_assignment.title = title
working_assignment.description = message
working_assignment.submission_types = "discussion_topic"
working_assignment.workflow_state = (workflow_state == "active") ? "published" : "unpublished"
%i[assignment_group_id assignment_overrides due_at grading_type grading_standard_id lock_at name points_possible unlock_at].each do |field|
working_assignment.send("#{field}=", assignment_input[field]) if assignment_input[field]
end
working_peer_reviews = assignment_input[:peer_reviews].to_h if assignment_input[:peer_reviews]
if working_peer_reviews
peer_review_mappings = {
enabled: :peer_reviews,
count: :peer_review_count,
due_at: :peer_reviews_due_at,
intra_reviews: :intra_group_peer_reviews,
anonymous_reviews: :anonymous_peer_reviews,
automatic_reviews: :automatic_peer_reviews
}
peer_review_mappings.each do |field_from_peer_reviews, attribute_on_assignment|
working_assignment.send("#{attribute_on_assignment}=", working_peer_reviews[field_from_peer_reviews]) if working_peer_reviews[field_from_peer_reviews]
end
end
working_assignment
end
def update_assignment
return if deleted?

View File

@ -66,6 +66,60 @@ describe Mutations::CreateDiscussionTopic do
CanvasSchema.execute(mutation_command, context:)
end
def execute_with_input_with_assignment(create_input, current_user = @teacher)
mutation_command = <<~GQL
mutation {
createDiscussionTopic(input: {
#{create_input}
}){
discussionTopic {
_id
contextType
title
message
published
requireInitialPost
anonymousState
isAnonymousAuthor
delayedPostAt
lockAt
allowRating
onlyGradersCanRate
todoDate
podcastEnabled
podcastHasStudentPosts
isSectionSpecific
groupSet {
_id
}
courseSections{
_id
name
}
assignment {
_id
name
pointsPossible
gradingType
peerReviews {
anonymousReviews
automaticReviews
count
enabled
}
}
}
errors {
attribute
message
}
}
}
GQL
context = { current_user:, request: ActionDispatch::TestRequest.create }
CanvasSchema.execute(mutation_command, context:)
end
it "successfully creates the discussion topic" do
context_type = "Course"
title = "Test Title"
@ -763,4 +817,81 @@ describe Mutations::CreateDiscussionTopic do
expect(discussion_topic["lockAt"]).to be_nil
end
end
context "graded discussion topics" do
it "successfully creates a graded discussion topic" do
context_type = "Course"
title = "Graded Discussion"
message = "Lorem ipsum..."
published = true
query = <<~GQL
contextId: "#{@course.id}"
contextType: "#{context_type}"
title: "#{title}"
message: "#{message}"
published: #{published}
assignment: {
courseId: "1",
name: "#{title}",
pointsPossible: 15,
gradingType: percent,
peerReviews: {
anonymousReviews: true,
automaticReviews: true,
count: 2,
enabled: true,
intraReviews: true,
dueAt: "#{5.days.from_now.iso8601}",
}
}
GQL
result = execute_with_input_with_assignment(query)
discussion_topic = result.dig("data", "createDiscussionTopic", "discussionTopic")
expect(result.dig("data", "discussionTopic", "errors")).to be_nil
expect(discussion_topic["assignment"]["name"]).to eq title
expect(discussion_topic["assignment"]["pointsPossible"]).to eq 15
expect(discussion_topic["assignment"]["gradingType"]).to eq "percent"
expect(discussion_topic["assignment"]["peerReviews"]["anonymousReviews"]).to be true
expect(discussion_topic["assignment"]["peerReviews"]["automaticReviews"]).to be true
expect(discussion_topic["assignment"]["peerReviews"]["count"]).to eq 2
expect(discussion_topic["assignment"]["_id"]).to eq Assignment.last.id.to_s
end
it "student fails to create graded discussion topic" do
context_type = "Course"
title = "Graded Discussion"
message = "Lorem ipsum..."
published = true
query = <<~GQL
contextId: "#{@course.id}"
contextType: "#{context_type}"
title: "#{title}"
message: "#{message}"
published: #{published}
assignment: {
courseId: "1",
name: "#{title}",
pointsPossible: 15,
gradingType: percent,
peerReviews: {
anonymousReviews: true,
automaticReviews: true,
count: 2,
enabled: true,
intraReviews: true,
dueAt: "#{5.days.from_now.iso8601}",
}
}
GQL
student = @course.enroll_student(User.create!, enrollment_state: "active").user
result = execute_with_input_with_assignment(query, student)
discussion_topic = result.dig("data", "createDiscussionTopic", "discussionTopic")
expect(discussion_topic).to be_nil
expect(result["data"]["createDiscussionTopic"]["errors"][0]["message"]).to eq "You do not have permissions to create assignments in the provided course"
end
end
end