class="n">assert_select 'issue > id', :text => issue.id.to_s end test "POST /issues.xml with watcher_user_ids should create issue with watchers" do assert_difference('Issue.count') do post( '/issues.xml', :params => { :issue => { :project_id => 1, :subject => 'Watchers', :tracker_id => 2, :status_id => 3, :watcher_user_ids => [3, 1] } }, :headers => credentials('jsmith')) assert_response :created end issue = Issue.order('id desc').first assert_equal 2, issue.watchers.size assert_equal [1, 3], issue.watcher_user_ids.sort end test "POST /issues.xml with failure should return errors" do assert_no_difference('Issue.count') do post( '/issues.xml', :params => {:issue => {:project_id => 1}}, :headers => credentials('jsmith')) end assert_select 'errors error', :text => "Subject cannot be blank" end test "POST /issues.json should create an issue with the attributes" do payload = <<~JSON { "issue": { "project_id": "1", "tracker_id": "2", "status_id": "3", "category_id": "2", "subject": "API test" } } JSON assert_difference('Issue.count') do post( '/issues.json', :params => payload, :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))) end issue = Issue.order('id DESC').first assert_equal 1, issue.project_id assert_equal 2, issue.tracker_id assert_equal 3, issue.status_id assert_equal 2, issue.category_id assert_equal 'API test', issue.subject end test "POST /issues.json should accept project identifier as project_id" do assert_difference('Issue.count') do post( '/issues.json', :params => {:issue => {:project_id => 'subproject1', :tracker_id => 2, :subject => 'Foo'}}, :headers => credentials('jsmith')) assert_response :created end end test "POST /issues.json without tracker_id should accept custom fields" do field = IssueCustomField.generate!( :field_format => 'list', :multiple => true, :possible_values => ["V1", "V2", "V3"], :default_value => "V2", :is_for_all => true, :trackers => Tracker.all.to_a ) payload = <<~JSON { "issue": { "project_id": "1", "subject": "Multivalued custom field", "custom_field_values":{"#{field.id}":["V1","V3"]} } } JSON assert_difference('Issue.count') do post( '/issues.json', :params => payload, :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))) end assert_response :created issue = Issue.order('id DESC').first assert_equal ["V1", "V3"], issue.custom_field_value(field).sort end test "POST /issues.json with omitted custom field should set default value" do field = IssueCustomField.generate!(:default_value => "Default") issue = new_record(Issue) do post( '/issues.json', :params => { :issue => {:project_id => 1, :subject => 'API', :custom_field_values => {}} }, :headers => credentials('jsmith')) end assert_equal "Default", issue.custom_field_value(field) end test "POST /issues.json with custom field set to blank should not set default value" do field = IssueCustomField.generate!(:default_value => "Default") issue = new_record(Issue) do post( '/issues.json', :params => { :issue => {:project_id => 1, :subject => 'API', :custom_field_values => {field.id.to_s => ""}} }, :headers => credentials('jsmith') ) end assert_equal "", issue.custom_field_value(field) end test "POST /issues.json with failure should return errors" do assert_no_difference('Issue.count') do post( '/issues.json', :params => {:issue => {:project_id => 1}}, :headers => credentials('jsmith')) end json = ActiveSupport::JSON.decode(response.body) assert json['errors'].include?("Subject cannot be blank") end test "POST /issues.json with invalid project_id should respond with 422" do post( '/issues.json', :params => {:issue => {:project_id => 999, :subject => "API"}}, :headers => credentials('jsmith')) assert_response 422 end test "PUT /issues/:id.xml" do assert_difference('Journal.count') do put( '/issues/6.xml', :params => {:issue => {:subject => 'API update', :notes => 'A new note'}}, :headers => credentials('jsmith')) end issue = Issue.find(6) assert_equal "API update", issue.subject journal = Journal.last assert_equal "A new note", journal.notes end test "PUT /issues/:id.xml with custom fields" do put( '/issues/3.xml', :params => { :issue => { :custom_fields => [ {'id' => '1', 'value' => 'PostgreSQL'}, {'id' => '2', 'value' => '150'} ] } }, :headers => credentials('jsmith')) issue = Issue.find(3) assert_equal '150', issue.custom_value_for(2).value assert_equal 'PostgreSQL', issue.custom_value_for(1).value end test "PUT /issues/:id.xml with multi custom fields" do field = CustomField.find(1) field.update_attribute :multiple, true put( '/issues/3.xml', :params => { :issue => { :custom_fields => [ {'id' => '1', 'value' => ['MySQL', 'PostgreSQL']}, {'id' => '2', 'value' => '150'} ] } }, :headers => credentials('jsmith')) issue = Issue.find(3) assert_equal '150', issue.custom_value_for(2).value assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort end test "PUT /issues/:id.xml with project change" do put( '/issues/3.xml', :params => {:issue => {:project_id => 2, :subject => 'Project changed'}}, :headers => credentials('jsmith')) issue = Issue.find(3) assert_equal 2, issue.project_id assert_equal 'Project changed', issue.subject end test "PUT /issues/:id.xml with notes only" do assert_difference('Journal.count') do put( '/issues/6.xml', :params => {:issue => {:notes => 'Notes only'}}, :headers => credentials('jsmith')) end journal = Journal.last assert_equal "Notes only", journal.notes end test "PUT /issues/:id.json with omitted custom field should not change blank value to default value" do field = IssueCustomField.generate!(:default_value => "Default") issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""}) assert_equal "", issue.reload.custom_field_value(field) assert_difference('Journal.count') do put( "/issues/#{issue.id}.json", :params => {:issue => {:custom_field_values => {}, :notes => 'API'}}, :headers => credentials('jsmith')) end assert_equal "", issue.reload.custom_field_value(field) end test "PUT /issues/:id.json with custom field set to blank should not change blank value to default value" do field = IssueCustomField.generate!(:default_value => "Default") issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""}) assert_equal "", issue.reload.custom_field_value(field) assert_difference('Journal.count') do put( "/issues/#{issue.id}.json", :params => {:issue => {:custom_field_values => {field.id.to_s => ""}, :notes => 'API'}}, :headers => credentials('jsmith')) end assert_equal "", issue.reload.custom_field_value(field) end test "PUT /issues/:id.json with tracker change and omitted custom field specific to that tracker should set default value" do field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2]) issue = Issue.generate!(:project_id => 1, :tracker_id => 1) assert_difference('Journal.count') do put( "/issues/#{issue.id}.json", :params => {:issue => {:tracker_id => 2, :custom_field_values => {}, :notes => 'API'}}, :headers => credentials('jsmith')) end assert_equal 2, issue.reload.tracker_id assert_equal "Default", issue.reload.custom_field_value(field) end test "PUT /issues/:id.json with tracker change and custom field specific to that tracker set to blank should not set default value" do field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2]) issue = Issue.generate!(:project_id => 1, :tracker_id => 1) assert_difference('Journal.count') do put( "/issues/#{issue.id}.json", :params => {:issue => {:tracker_id => 2, :custom_field_values => {field.id.to_s => ""}, :notes => 'API'}}, :headers => credentials('jsmith')) end assert_equal 2, issue.reload.tracker_id assert_equal "", issue.reload.custom_field_value(field) end test "PUT /issues/:id.xml with failed update" do put( '/issues/6.xml', :params => {:issue => {:subject => ''}}, :headers => credentials('jsmith')) assert_response :unprocessable_entity assert_select 'errors error', :text => "Subject cannot be blank" end test "PUT /issues/:id.xml with invalid assignee should return error" do user = User.generate! put( '/issues/6.xml', :params => {:issue => {:assigned_to_id => user.id}}, :headers => credentials('jsmith')) assert_response :unprocessable_entity assert_select 'errors error', :text => "Assignee is invalid" end test "PUT /issues/:id.json" do assert_difference('Journal.count') do put( '/issues/6.json', :params => {:issue => {:subject => 'API update', :notes => 'A new note'}}, :headers => credentials('jsmith')) assert_response :no_content assert_equal '', response.body end issue = Issue.find(6) assert_equal "API update", issue.subject journal = Journal.last assert_equal "A new note", journal.notes end test "PUT /issues/:id.json with failed update" do put( '/issues/6.json', :params => {:issue => {:subject => ''}}, :headers => credentials('jsmith')) assert_response :unprocessable_entity json = ActiveSupport::JSON.decode(response.body) assert json['errors'].include?("Subject cannot be blank") end test "DELETE /issues/:id.xml" do assert_difference('Issue.count', -1) do delete '/issues/6.xml', :headers => credentials('jsmith') assert_response :no_content assert_equal '', response.body end assert_nil Issue.find_by_id(6) end test "DELETE /issues/:id.json" do assert_difference('Issue.count', -1) do delete '/issues/6.json', :headers => credentials('jsmith') assert_response :no_content assert_equal '', response.body end assert_nil Issue.find_by_id(6) end test "POST /issues/:id/watchers.xml should add watcher" do assert_difference 'Watcher.count' do post( '/issues/1/watchers.xml', :params => {:user_id => 3}, :headers => credentials('jsmith')) assert_response :no_content assert_equal '', response.body end watcher = Watcher.order('id desc').first assert_equal Issue.find(1), watcher.watchable assert_equal User.find(3), watcher.user end test "DELETE /issues/:id/watchers/:user_id.xml should remove watcher" do Watcher.create!(:user_id => 3, :watchable => Issue.find(1)) assert_difference 'Watcher.count', -1 do delete '/issues/1/watchers/3.xml', :headers => credentials('jsmith') assert_response :no_content assert_equal '', response.body end assert_equal false, Issue.find(1).watched_by?(User.find(3)) end def test_create_issue_with_uploaded_file token = xml_upload('test_create_with_upload', credentials('jsmith')) attachment = Attachment.find_by_token(token) # create the issue with the upload's token assert_difference 'Issue.count' do post( '/issues.xml', :params => {:issue => {:project_id => 1, :subject => 'Uploaded file', :uploads => [{:token => token, :filename => 'test.txt', :content_type => 'text/plain'}]}}, :headers => credentials('jsmith')) assert_response :created end issue = Issue.order('id DESC').first assert_equal 1, issue.attachments.count assert_equal attachment, issue.attachments.first attachment.reload assert_equal 'test.txt', attachment.filename assert_equal 'text/plain', attachment.content_type assert_equal 'test_create_with_upload'.size, attachment.filesize assert_equal 2, attachment.author_id # get the issue with its attachments get "/issues/#{issue.id}.xml?include=attachments" assert_response :success xml = Hash.from_xml(response.body) attachments = xml['issue']['attachments'] assert_kind_of Array, attachments assert_equal 1, attachments.size url = attachments.first['content_url'] assert_not_nil url # download the attachment get url assert_response :success assert_equal 'test_create_with_upload', response.body end def test_create_issue_with_multiple_uploaded_files_as_xml token1 = xml_upload('File content 1', credentials('jsmith')) token2 = xml_upload('File content 2', credentials('jsmith')) payload = <<~XML <?xml version="1.0" encoding="UTF-8" ?> <issue> <project_id>1</project_id> <tracker_id>1</tracker_id> <subject>Issue with multiple attachments</subject> <uploads type="array"> <upload> <token>#{token1}</token> <filename>test1.txt</filename> </upload> <upload> <token>#{token2}</token> <filename>test1.txt</filename> </upload> </uploads> </issue> XML assert_difference 'Issue.count' do post( '/issues.xml', :params => payload, :headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))) assert_response :created end issue = Issue.order('id DESC').first assert_equal 2, issue.attachments.count end def test_create_issue_with_multiple_uploaded_files_as_json token1 = json_upload('File content 1', credentials('jsmith')) token2 = json_upload('File content 2', credentials('jsmith')) payload = <<~JSON { "issue": { "project_id": "1", "tracker_id": "1", "subject": "Issue with multiple attachments", "uploads": [ {"token": "#{token1}", "filename": "test1.txt"}, {"token": "#{token2}", "filename": "test2.txt"} ] } } JSON assert_difference 'Issue.count' do post( '/issues.json', :params => payload, :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))) assert_response :created end issue = Issue.order('id DESC').first assert_equal 2, issue.attachments.count end def test_update_issue_with_uploaded_file token = xml_upload('test_upload_with_upload', credentials('jsmith')) attachment = Attachment.find_by_token(token) # update the issue with the upload's token assert_difference 'Journal.count' do put( '/issues/1.xml', :params => {:issue => {:notes => 'Attachment added', :uploads => [{:token => token, :filename => 'test.txt', :content_type => 'text/plain'}]}}, :headers => credentials('jsmith')) assert_response :no_content assert_equal '', @response.body end issue = Issue.find(1) assert_include attachment, issue.attachments end end