<commit_before># frozen_string_literal: true

#
# Copyright (C) 2018 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

require "spec_helper"
require_relative "../graphql_spec_helper"

describe Mutations::SetCoursePostPolicy do
  let(:assignment) { course.assignments.create! }
  let(:course) { Course.create!(workflow_state: "available") }
  let(:student) { course.enroll_user(User.create!, "StudentEnrollment", enrollment_state: "active").user }
  let(:teacher) { course.enroll_user(User.create!, "TeacherEnrollment", enrollment_state: "active").user }

  def mutation_str(course_id: nil, post_manually: nil)
    input_string = course_id ? "courseId: #{course_id}" : ""
    input_string += " postManually: #{post_manually}" if post_manually.present?

    <<~GQL
      mutation {
        setCoursePostPolicy(input: {
          #{input_string}
        }) {
          postPolicy {
            course {
              _id
            }
            _id
            postManually
          }
          errors {
            attribute
            message
          }
        }
      }
    GQL
  end

  def execute_query(mutation_str, context)
    CanvasSchema.execute(mutation_str, context: context)
  end

  context "when user has manage_grades permission" do
    let(:context) { { current_user: teacher } }

    it "requires that courseId be passed in the query" do
      result = execute_query(mutation_str(post_manually: true), context)
      expected_error = "'courseId' on InputObject 'SetCoursePostPolicyInput' is required"
      expect(result.dig("errors", 0, "message")).to include expected_error
    end

    it "requires that postManually be passed in the query" do
      result = execute_query(mutation_str(course_id: course.id), context)
      expected_error = "'postManually' on InputObject 'SetCoursePostPolicyInput' is required"
      expect(result.dig("errors", 0, "message")).to include expected_error
    end

    it "returns an error if the provided course id does not exist" do
      bad_id = (Course.last&.id || 0) + 1
      result = execute_query(mutation_str(course_id: bad_id, post_manually: true), context)
      expected_error = "A course with that id does not exist"
      expect(result.dig("errors", 0, "message")).to eql expected_error
    end

    it "returns the related post policy" do
      result = execute_query(mutation_str(course_id: course.id, post_manually: true), context)
      policy = PostPolicy.find_by(course: course, assignment: nil)
      expect(result.dig("data", "setCoursePostPolicy", "postPolicy", "_id").to_i).to be policy.id
    end

    it "updates an existing course post policy when one exists" do
      skip "DEMO-23 (8/17/20)"

      policy = PostPolicy.create!(course: course, post_manually: false)
      result = execute_query(mutation_str(course_id: course.id, post_manually: true), context)
      expect(result.dig("data", "setCoursePostPolicy", "postPolicy", "_id").to_i).to be policy.id
    end

    describe "updating the post policy of assignments in the course" do
      let(:policy) { PostPolicy.create!(course: course, post_manually: false) }
      let(:post_manually_mutation) { mutation_str(course_id: course.id, post_manually: true) }

      let(:assignment) { course.assignments.create! }
      let(:anonymous_assignment) { course.assignments.create!(anonymous_grading: true) }
      let(:moderated_assignment) do
        course.assignments.create!(
          final_grader: teacher,
          grader_count: 2,
          moderated_grading: true
        )<commit_msg>spec: remove outdated course post policy spec<commit_after># frozen_string_literal: true

#
# Copyright (C) 2018 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

require "spec_helper"
require_relative "../graphql_spec_helper"

describe Mutations::SetCoursePostPolicy do
  let(:assignment) { course.assignments.create! }
  let(:course) { Course.create!(workflow_state: "available") }
  let(:student) { course.enroll_user(User.create!, "StudentEnrollment", enrollment_state: "active").user }
  let(:teacher) { course.enroll_user(User.create!, "TeacherEnrollment", enrollment_state: "active").user }

  def mutation_str(course_id: nil, post_manually: nil)
    input_string = course_id ? "courseId: #{course_id}" : ""
    input_string += " postManually: #{post_manually}" if post_manually.present?

    <<~GQL
      mutation {
        setCoursePostPolicy(input: {
          #{input_string}
        }) {
          postPolicy {
            course {
              _id
            }
            _id
            postManually
          }
          errors {
            attribute
            message
          }
        }
      }
    GQL
  end

  def execute_query(mutation_str, context)
    CanvasSchema.execute(mutation_str, context: context)
  end

  context "when user has manage_grades permission" do
    let(:context) { { current_user: teacher } }

    it "requires that courseId be passed in the query" do
      result = execute_query(mutation_str(post_manually: true), context)
      expected_error = "'courseId' on InputObject 'SetCoursePostPolicyInput' is required"
      expect(result.dig("errors", 0, "message")).to include expected_error
    end

    it "requires that postManually be passed in the query" do
      result = execute_query(mutation_str(course_id: course.id), context)
      expected_error = "'postManually' on InputObject 'SetCoursePostPolicyInput' is required"
      expect(result.dig("errors", 0, "message")).to include expected_error
    end

    it "returns an error if the provided course id does not exist" do
      bad_id = (Course.last&.id || 0) + 1
      result = execute_query(mutation_str(course_id: bad_id, post_manually: true), context)
      expected_error = "A course with that id does not exist"
      expect(result.dig("errors", 0, "message")).to eql expected_error
    end

    it "returns the related post policy" do
      result = execute_query(mutation_str(course_id: course.id, post_manually: true), context)
      policy = PostPolicy.find_by(course: course, assignment: nil)
      expect(result.dig("data", "setCoursePostPolicy", "postPolicy", "_id").to_i).to be policy.id
    end

    describe "updating the post policy of assignments in the course" do
      let(:policy) { PostPolicy.create!(course: course, post_manually: false) }
      let(:post_manually_mutation) { mutation_str(course_id: course.id, post_manually: true) }

      let(:assignment) { course.assignments.create! }
      let(:anonymous_assignment) { course.assignments.create!(anonymous_grading: true) }
      let(:moderated_assignment) do
        course.assignments.create!(
          final_grader: teacher,
          grader_count: 2,
          moderated_grading: true
        )
{'fraction_non_alphanumeric': 0.09789593045231196, 'fraction_numerical': 0.003653773466045105, 'mean_word_length': 3.390486725663717, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5624212', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2274, 'n_words': 690}