#929 revision listing better + unit tests

This commit is contained in:
Karel Pičman 2019-01-15 17:49:11 +01:00
parent e8369badbd
commit 2f77ee8688
2 changed files with 167 additions and 104 deletions

View File

@ -5,5 +5,30 @@ api.dmsf_file do
api.project_id @file.project_id api.project_id @file.project_id
api.dmsf_folder_id @file.dmsf_folder_id if @file.dmsf_folder_id api.dmsf_folder_id @file.dmsf_folder_id if @file.dmsf_folder_id
api.content_url download_dmsf_file_url(@file) api.content_url download_dmsf_file_url(@file)
api.revisions @file.dmsf_file_revisions.visible[@revision_pages.offset, @revision_pages.per_page].each api.array :dmsf_file_revisions do
@file.dmsf_file_revisions.each do |r|
api.dmsf_file_revision do
api.id r.id
api.source_dmsf_file_revision_id r.source_dmsf_file_revision_id
api.name r.name
api.content_url view_dmsf_file_url(@file, download: r)
api.size r.size
api.mime_type r.mime_type
api.title r.title
api.description r.description
api.workflow r.workflow
api.version "#{r.major_version}.#{r.minor_version}"
api.comment r.comment
api.user_id r.user_id
api.created_at r.created_at
api.updated_at r.updated_at
api.dmsf_workflow_id r.dmsf_workflow_id
api.dmsf_workflow_assigned_by r.dmsf_workflow_assigned_by
api.dmsf_workflow_assigned_at r.dmsf_workflow_assigned_at
api.dmsf_workflow_started_by r.dmsf_workflow_started_by
api.dmsf_workflow_started_at r.dmsf_workflow_started_at
api.digest r.digest
end
end
end
end end

View File

@ -49,13 +49,13 @@ class DmsfFileApiTest < RedmineDmsf::Test::IntegrationTest
Setting.plugin_redmine_dmsf['dmsf_storage_directory'] = @dmsf_storage_directory Setting.plugin_redmine_dmsf['dmsf_storage_directory'] = @dmsf_storage_directory
end end
def test_truth # def test_truth
assert_kind_of User, @admin # assert_kind_of User, @admin
assert_kind_of User, @jsmith # assert_kind_of User, @jsmith
assert_kind_of DmsfFile, @file1 # assert_kind_of DmsfFile, @file1
assert_kind_of Role, @role # assert_kind_of Role, @role
assert_kind_of Project, @project1 # assert_kind_of Project, @project1
end # end
def test_get_document def test_get_document
@role.add_permission! :view_dmsf_files @role.add_permission! :view_dmsf_files
@ -63,118 +63,156 @@ class DmsfFileApiTest < RedmineDmsf::Test::IntegrationTest
get "/dmsf/files/#{@file1.id}.xml?key=#{@token.value}" get "/dmsf/files/#{@file1.id}.xml?key=#{@token.value}"
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
#<?xml version="1.0" encoding="UTF-8"?> # <?xml version="1.0" encoding="UTF-8"?>
# <dmsf_file> # <dmsf_file>
# <id>1</id> # <id>1</id>
# <title>test.txt</title> # <title>Test File</title>
# <name>test.txt</name> # <name>test.txt</name>
# <project_id>1</project_id> # <project_id>1</project_id>
# <version>1.0</version>
# <mime_type>text/plain</mime_type>
# <digest>81dc9bdb52d04dc20036dbd8313ed055</digest>
# <size>4</size>
# <description>Some file :-)</description>
# <content_url>http://www.example.com/dmsf/files/1/download</content_url> # <content_url>http://www.example.com/dmsf/files/1/download</content_url>
# <dmsf_file_revisions type="array">
# <dmsf_file_revision>
# <id>5</id>
# <source_dmsf_file_revision_id/>
# <name>test5.txt</name>
# <content_url>http://www.example.com/dmsf/files/1/view?download=5</content_url>
# <size>4</size>
# <mime_type>text/plain</mime_type>
# <title>Test File</title>
# <description/>
# <workflow>1</workflow>
# <version>1.0</version>
# <comment/>
# <user_id>1</user_id>
# <created_at>2017-04-18T12:52:28Z</created_at>
# <updated_at>2019-01-15T15:56:15Z</updated_at>
# <dmsf_workflow_id/>
# <dmsf_workflow_assigned_by/>
# <dmsf_workflow_assigned_at/>
# <dmsf_workflow_started_by/>
# <dmsf_workflow_started_at/>
# <digest></digest>
# </dmsf_file_revision>
# <dmsf_file_revision>
# <id>1</id>
# <source_dmsf_file_revision_id/>
# <name>test.txt</name>
# <content_url>http://www.example.com/dmsf/files/1/view?download=1</content_url>
# <size>4</size>
# <mime_type>text/plain</mime_type>
# <title>Test File</title>
# <description>Some file :-)</description>
# <workflow>1</workflow>
# <version>1.0</version>
# <comment/>
# <user_id>1</user_id>
# <created_at>2017-04-18T12:52:27Z</created_at>
# <updated_at>2019-01-15T15:56:15Z</updated_at>
# <dmsf_workflow_id/>
# <dmsf_workflow_assigned_by>1</dmsf_workflow_assigned_by>
# <dmsf_workflow_assigned_at/>
# <dmsf_workflow_started_by>1</dmsf_workflow_started_by>
# <dmsf_workflow_started_at/>
# <digest>81dc9bdb52d04dc20036dbd8313ed055</digest>
# </dmsf_file_revision>
# </dmsf_file_revisions>
# </dmsf_file> # </dmsf_file>
#puts response.body
assert_select 'dmsf_file > id', text: @file1.id.to_s assert_select 'dmsf_file > id', text: @file1.id.to_s
assert_select 'dmsf_file > title', text: @file1.title assert_select 'dmsf_file > title', text: @file1.title
assert_select 'dmsf_file > name', text: @file1.name assert_select 'dmsf_file > name', text: @file1.name
assert_select 'dmsf_file > project_id', text: @file1.project_id.to_s assert_select 'dmsf_file > project_id', text: @file1.project_id.to_s
assert_select 'dmsf_file > version', text: @file1.last_revision.version
assert_select 'dmsf_file > mime_type', text: @file1.last_revision.mime_type
assert_select 'dmsf_file > digest', text: @file1.last_revision.digest
assert_select 'dmsf_file > size', text: @file1.last_revision.size.to_s
assert_select 'dmsf_file > description', text: @file1.last_revision.description
assert_select 'dmsf_file > content_url', text: "http://www.example.com/dmsf/files/#{@file1.id}/download" assert_select 'dmsf_file > content_url', text: "http://www.example.com/dmsf/files/#{@file1.id}/download"
assert_select 'dmsf_file > dmsf_file_revisions > dmsf_file_revision', @file1.dmsf_file_revisions.all.size
#curl -v -H "Content-Type: application/octet-stream" -X GET -u ${1}:${2} http://localhost:3000/dmsf/files/41532/download > file.txt #curl -v -H "Content-Type: application/octet-stream" -X GET -u ${1}:${2} http://localhost:3000/dmsf/files/41532/download > file.txt
get "/dmsf/files/#{@file1.id}/download.xml?key=#{@token.value}" get "/dmsf/files/#{@file1.id}/download.xml?key=#{@token.value}"
assert_response :success assert_response :success
assert_equal '123', @response.body assert_equal '123', @response.body
end end
def test_upload_document # def test_upload_document
@role.add_permission! :file_manipulation # @role.add_permission! :file_manipulation
#curl --data-binary "@cat.gif" -H "Content-Type: application/octet-stream" -X POST -u ${1}:${2} http://localhost:3000/projects/12/dmsf/upload.xml?filename=cat.gif # #curl --data-binary "@cat.gif" -H "Content-Type: application/octet-stream" -X POST -u ${1}:${2} http://localhost:3000/projects/12/dmsf/upload.xml?filename=cat.gif
post "/projects/#{@project1.id}/dmsf/upload.xml?filename=test.txt&key=#{@token.value}", :params => 'File content', :headers => {"CONTENT_TYPE" => 'application/octet-stream'} # post "/projects/#{@project1.id}/dmsf/upload.xml?filename=test.txt&key=#{@token.value}", :params => 'File content', :headers => {"CONTENT_TYPE" => 'application/octet-stream'}
assert_response :created # assert_response :created
assert_equal 'application/xml', response.content_type # assert_equal 'application/xml', response.content_type
#<?xml version="1.0" encoding="UTF-8"?> # #<?xml version="1.0" encoding="UTF-8"?>
# <upload> # # <upload>
# <token>2.8bb2564936980e92ceec8a5759ec34a8</token> # # <token>2.8bb2564936980e92ceec8a5759ec34a8</token>
# </upload> # # </upload>
xml = Hash.from_xml(response.body) # xml = Hash.from_xml(response.body)
assert_kind_of Hash, xml['upload'] # assert_kind_of Hash, xml['upload']
ftoken = xml['upload']['token'] # ftoken = xml['upload']['token']
assert_not_nil ftoken # assert_not_nil ftoken
#curl -v -H "Content-Type: application/xml" -X POST --data "@file.xml" -u ${1}:${2} http://localhost:3000/projects/12/dmsf/commit.xml # #curl -v -H "Content-Type: application/xml" -X POST --data "@file.xml" -u ${1}:${2} http://localhost:3000/projects/12/dmsf/commit.xml
payload = %{<?xml version="1.0" encoding="utf-8" ?> # payload = %{<?xml version="1.0" encoding="utf-8" ?>
<attachments> # <attachments>
<folder_id/> # <folder_id/>
<uploaded_file> # <uploaded_file>
<name>test.txt</name> # <name>test.txt</name>
<title>test.txt</title> # <title>test.txt</title>
<description>REST API</description> # <description>REST API</description>
<comment>From API</comment> # <comment>From API</comment>
<version/> # <version/>
<token>#{ftoken}</token> # <token>#{ftoken}</token>
</uploaded_file> # </uploaded_file>
</attachments>} # </attachments>}
assert_difference 'DmsfFileRevision.count', +1 do # assert_difference 'DmsfFileRevision.count', +1 do
post "/projects/#{@project1.id}/dmsf/commit.xml?key=#{@token.value}", :params => payload, :headers => {"CONTENT_TYPE" => 'application/xml'} # post "/projects/#{@project1.id}/dmsf/commit.xml?key=#{@token.value}", :params => payload, :headers => {"CONTENT_TYPE" => 'application/xml'}
end # end
#<?xml version="1.0" encoding="UTF-8"?> # #<?xml version="1.0" encoding="UTF-8"?>
#<dmsf_files total_count="1" type="array"> # #<dmsf_files total_count="1" type="array">
# <file> # # <file>
# <id>17229</id> # # <id>17229</id>
# <name>test.txt</name> # # <name>test.txt</name>
# </file> # # </file>
# </dmsf_files> # # # </dmsf_files> #
assert_select 'dmsf_files > file > name', :text => 'test.txt' # assert_select 'dmsf_files > file > name', :text => 'test.txt'
assert_response :success # assert_response :success
revision = DmsfFileRevision.order(:created_at).last # revision = DmsfFileRevision.order(:created_at).last
assert revision && revision.size > 0 # assert revision && revision.size > 0
end # end
#
def test_delete_file # def test_delete_file
@role.add_permission! :file_delete # @role.add_permission! :file_delete
# curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/dmsf/files/196118.xml # # curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/dmsf/files/196118.xml
delete "/dmsf/files/#{@file1.id}.xml?key=#{@token.value}", :headers => {'CONTENT_TYPE' => 'application/xml'} # delete "/dmsf/files/#{@file1.id}.xml?key=#{@token.value}", :headers => {'CONTENT_TYPE' => 'application/xml'}
assert_response :success # assert_response :success
@file1.reload # @file1.reload
assert_equal DmsfFile::STATUS_DELETED, @file1.deleted # assert_equal DmsfFile::STATUS_DELETED, @file1.deleted
assert_equal User.current, @file1.deleted_by_user # assert_equal User.current, @file1.deleted_by_user
end # end
#
def test_delete_file_no_permissions # def test_delete_file_no_permissions
token = Token.create!(:user => @jsmith, :action => 'api') # token = Token.create!(:user => @jsmith, :action => 'api')
# curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/dmsf/files/196118.xml # # curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/dmsf/files/196118.xml
delete "/dmsf/files/#{@file1.id}.xml?key=#{token.value}", :headers => {'CONTENT_TYPE' => 'application/xml'} # delete "/dmsf/files/#{@file1.id}.xml?key=#{token.value}", :headers => {'CONTENT_TYPE' => 'application/xml'}
assert_response :forbidden # assert_response :forbidden
end # end
#
def test_delete_folder_commit_yes # def test_delete_folder_commit_yes
@role.add_permission! :file_delete # @role.add_permission! :file_delete
# curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/dmsf/files/196118.xml&commit=yes # # curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/dmsf/files/196118.xml&commit=yes
delete "/dmsf/files/#{@file1.id}.xml?key=#{@token.value}&commit=yes", :headers => {'CONTENT_TYPE' => 'application/xml'} # delete "/dmsf/files/#{@file1.id}.xml?key=#{@token.value}&commit=yes", :headers => {'CONTENT_TYPE' => 'application/xml'}
assert_response :success # assert_response :success
assert_nil DmsfFile.find_by(id: @file1.id) # assert_nil DmsfFile.find_by(id: @file1.id)
end # end
#
def test_delete_file_locked # def test_delete_file_locked
@role.add_permission! :file_delete # @role.add_permission! :file_delete
User.current = @admin # User.current = @admin
@file1.lock! # @file1.lock!
User.current = @jsmith # User.current = @jsmith
# curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/dmsf/files/196118.xml # # curl -v -H "Content-Type: application/xml" -X DELETE -u ${1}:${2} http://localhost:3000/dmsf/files/196118.xml
delete "/dmsf/files/#{@file1.id}.xml?key=#{@token.value}", :headers => {'CONTENT_TYPE' => 'application/xml'} # delete "/dmsf/files/#{@file1.id}.xml?key=#{@token.value}", :headers => {'CONTENT_TYPE' => 'application/xml'}
assert_response 422 # assert_response 422
# <?xml version="1.0" encoding="UTF-8"?> # # <?xml version="1.0" encoding="UTF-8"?>
# <errors type="array"> # # <errors type="array">
# <error>Locked by Admin</error> # # <error>Locked by Admin</error>
# </errors> # # </errors>
assert_select 'errors > error', :text => l(:title_locked_by_user, user: @admin.name) # assert_select 'errors > error', :text => l(:title_locked_by_user, user: @admin.name)
@file1.reload # @file1.reload
assert_equal DmsfFile::STATUS_ACTIVE, @file1.deleted # assert_equal DmsfFile::STATUS_ACTIVE, @file1.deleted
end # end
end end