Skip to content

Add support for updating attachments #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 18, 2024

Conversation

Art4
Copy link
Collaborator

@Art4 Art4 commented Mar 18, 2024

Closes #320.

@Art4 Art4 added this to the v2.6.0 milestone Mar 18, 2024
@Art4 Art4 self-assigned this Mar 18, 2024
@Art4 Art4 linked an issue Mar 18, 2024 that may be closed by this pull request
Copy link

codecov bot commented Mar 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.42%. Comparing base (2b4c451) to head (8d265a4).

Additional details and impacted files
@@            Coverage Diff            @@
##               v2.x     #395   +/-   ##
=========================================
  Coverage     98.41%   98.42%           
- Complexity      604      606    +2     
=========================================
  Files            29       29           
  Lines          1772     1781    +9     
=========================================
+ Hits           1744     1753    +9     
  Misses           28       28           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Art4
Copy link
Collaborator Author

Art4 commented Mar 18, 2024

The endpoint PATCH /attachments/x.json is undocumented so far, but one can see in the source code and tests how the endpoint should work.

https://www.redmine.org/projects/redmine/repository/svn/entry/tags/5.1.2/test/integration/api_test/api_routing_test.rb

  def test_attachments
    should_route 'GET /attachments/1' => 'attachments#show', :id => '1'
    should_route 'PATCH /attachments/1' => 'attachments#update', :id => '1'
    should_route 'DELETE /attachments/1' => 'attachments#destroy', :id => '1'
    should_route 'POST /uploads' => 'attachments#upload'
  end

https://www.redmine.org/projects/redmine/repository/svn/entry/tags/5.1.2/test/integration/api_test/attachments_test.rb

  test "PATCH /attachments/:id.json should update the attachment" do
    patch(
      '/attachments/7.json',
      :params => {:attachment => {:filename => 'renamed.zip', :description => 'updated'}},
      :headers => credentials('jsmith')
    )
    assert_response :no_content
    assert_nil response.media_type
    attachment = Attachment.find(7)
    assert_equal 'renamed.zip', attachment.filename
    assert_equal 'updated', attachment.description
  end

But I have trouble testing this behavior. If I call the endpoint the Redmine server responses with a 200 OK status code and the unchanged body.

Behat test:

    @attachment
    Scenario: Updating the details of an attachment
        Given I have a "NativeCurlClient" client
        And I create a project with name "Test Project" and identifier "test-project"
        And I upload the content of the file "%tests_dir%/Fixtures/testfile_01.txt" with the following data
            | property          | value                |
            | filename          | testfile.txt         |
        When I update the attachment with the id "1" with the following data
            | property          | value                |
            | filename          | testfile2.txt        |
        Then the response has the status code "204"
        And the response has an empty content type
        And the response has the content ""
        And the returned data is true

Test result:

$ sudo docker compose exec php composer behat -- --tags=attachment
> behat --config tests/Behat/behat.yml --format progress '--tags=attachment'
............F---...................................................... 70
.......................F---........................................... 140
......................

--- Failed steps:

001 Scenario: Updating the details of an attachment # features/attachments.feature:30
      Then the response has the status code "204"   # features/attachments.feature:39
        Raw response content: {"attachment":{"id":1,"filename":"testfile.txt","filesize":65,"content_type":"text/plain","description":null,"content_url":"http://redmine-50102:3000/attachments/download/1/testfile.txt","author":{"id":1,"name":"Redmine Admin"},"created_on":"2024-03-18T13:38:43Z"}}
        Failed asserting that 200 is identical to 204.

002 Scenario: Updating the details of an attachment # features/attachments.feature:30
      Then the response has the status code "204"   # features/attachments.feature:39
        Raw response content: {"attachment":{"id":1,"filename":"testfile.txt","filesize":65,"content_type":"text/plain","description":null,"content_url":"http://redmine-50008:3000/attachments/download/1/testfile.txt","author":{"id":1,"name":"Redmine Admin"},"created_on":"2024-03-18T13:38:48Z"}}
        Failed asserting that 200 is identical to 204.

16 scenarios (14 passed, 2 failed)
162 steps (154 passed, 2 failed, 6 skipped)
0m13.65s (13.80Mb)
Script behat --config tests/Behat/behat.yml --format progress handling the behat event returned with error code 1

Update: If I'm using PUT instead of PATCH I can update the attachment successfully. I'm not sure why this works. 🤷

I have working tests for this behavior, so I think we should go with PUT instead of PATCH. I will document this in the code too.

@Art4 Art4 requested a review from kbsali March 18, 2024 15:06
Copy link
Owner

@kbsali kbsali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💪

@Art4 Art4 merged commit 3940781 into v2.x Mar 18, 2024
@Art4 Art4 deleted the 320-add-support-for-updating-attachments-over-rest-api branch March 18, 2024 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for updating attachments over REST API
2 participants