|
1 | 1 | require 'test_helper' |
2 | 2 |
|
3 | 3 | class DocumentsControllerTest < ActionController::TestCase |
4 | | - # test "the truth" do |
5 | | - # assert true |
6 | | - # end |
| 4 | + setup do |
| 5 | + @non_encrypted_user = users(:one) |
| 6 | + @encrypted_user = users(:two) |
| 7 | + @document = documents(:one) |
| 8 | + |
| 9 | + @test_pdf_content = "%PDF-1.4\n1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\nxref\n0 4\n0000000000 65535 f \n0000000009 00000 n \n0000000058 00000 n \n0000000115 00000 n \ntrailer\n<< /Size 4 /Root 1 0 R >>\nstartxref\n192\n%%EOF" |
| 10 | + end |
| 11 | + |
| 12 | + test "should create document without encryption for non-encrypted user" do |
| 13 | + token = JsonWebToken.encode(user_id: @non_encrypted_user.id) |
| 14 | + @request.headers['Authorization'] = "Bearer #{token}" |
| 15 | + |
| 16 | + temp_file = Tempfile.new(['test', '.pdf']) |
| 17 | + temp_file.write(@test_pdf_content) |
| 18 | + temp_file.rewind |
| 19 | + |
| 20 | + uploaded_file = Rack::Test::UploadedFile.new(temp_file.path, 'application/pdf') |
| 21 | + |
| 22 | + assert_difference('Document.count') do |
| 23 | + post :create, params: { |
| 24 | + document: uploaded_file, |
| 25 | + title: 'Test Document', |
| 26 | + description: 'Test description', |
| 27 | + document_date: Date.today.to_s |
| 28 | + } |
| 29 | + end |
| 30 | + |
| 31 | + assert_response :created |
| 32 | + |
| 33 | + new_document = Document.last |
| 34 | + assert_equal false, new_document.encrypted_flag |
| 35 | + assert_equal 'Test Document', new_document.title |
| 36 | + |
| 37 | + assert File.exist?(Settings.document_folder + new_document.document_url) |
| 38 | + |
| 39 | + File.delete(Settings.document_folder + new_document.document_url) if File.exist?(Settings.document_folder + new_document.document_url) |
| 40 | + temp_file.close |
| 41 | + temp_file.unlink |
| 42 | + end |
| 43 | + |
| 44 | + test "should create document with encryption for encrypted user" do |
| 45 | + token = JsonWebToken.encode(user_id: @encrypted_user.id) |
| 46 | + @request.headers['Authorization'] = "Bearer #{token}" |
| 47 | + |
| 48 | + temp_file = Tempfile.new(['test', '.pdf']) |
| 49 | + temp_file.write(@test_pdf_content) |
| 50 | + temp_file.rewind |
| 51 | + |
| 52 | + uploaded_file = Rack::Test::UploadedFile.new(temp_file.path, 'application/pdf') |
| 53 | + |
| 54 | + assert_difference('Document.count') do |
| 55 | + post :create, params: { |
| 56 | + document: uploaded_file, |
| 57 | + title: 'Encrypted Test Document', |
| 58 | + description: 'Encrypted test description', |
| 59 | + document_date: Date.today.to_s |
| 60 | + } |
| 61 | + end |
| 62 | + |
| 63 | + assert_response :created |
| 64 | + |
| 65 | + new_document = Document.last |
| 66 | + assert_equal true, new_document.encrypted_flag |
| 67 | + assert_equal 'Encrypted Test Document', new_document.title |
| 68 | + |
| 69 | + assert File.exist?(Settings.document_folder + new_document.document_url) |
| 70 | + |
| 71 | + stored_content = File.read(Settings.document_folder + new_document.document_url) |
| 72 | + assert_not_equal @test_pdf_content, stored_content |
| 73 | + |
| 74 | + lockbox = Lockbox.new(key: @encrypted_user.secret_key) |
| 75 | + decrypted_content = lockbox.decrypt(stored_content) |
| 76 | + assert_equal @test_pdf_content, decrypted_content |
| 77 | + |
| 78 | + File.delete(Settings.document_folder + new_document.document_url) if File.exist?(Settings.document_folder + new_document.document_url) |
| 79 | + temp_file.close |
| 80 | + temp_file.unlink |
| 81 | + end |
| 82 | + |
| 83 | + test "should download non-encrypted document" do |
| 84 | + token = JsonWebToken.encode(user_id: @non_encrypted_user.id) |
| 85 | + @request.headers['Authorization'] = "Bearer #{token}" |
| 86 | + |
| 87 | + document = Document.create!( |
| 88 | + title: 'Download Test', |
| 89 | + document_date: Date.today, |
| 90 | + document_url: 'download_test.pdf', |
| 91 | + user: @non_encrypted_user, |
| 92 | + encrypted_flag: false |
| 93 | + ) |
| 94 | + |
| 95 | + File.write(Settings.document_folder + 'download_test.pdf', @test_pdf_content, mode: 'w+b') |
| 96 | + |
| 97 | + get :download, params: { id: document.id } |
| 98 | + |
| 99 | + assert_response :success |
| 100 | + assert_equal 'application/pdf', @response.content_type |
| 101 | + assert_equal @test_pdf_content, @response.body |
| 102 | + |
| 103 | + File.delete(Settings.document_folder + 'download_test.pdf') if File.exist?(Settings.document_folder + 'download_test.pdf') |
| 104 | + document.destroy |
| 105 | + end |
| 106 | + |
| 107 | + test "should download and decrypt encrypted document" do |
| 108 | + token = JsonWebToken.encode(user_id: @encrypted_user.id) |
| 109 | + @request.headers['Authorization'] = "Bearer #{token}" |
| 110 | + |
| 111 | + lockbox = Lockbox.new(key: @encrypted_user.secret_key) |
| 112 | + encrypted_content = lockbox.encrypt(@test_pdf_content) |
| 113 | + |
| 114 | + document = Document.create!( |
| 115 | + title: 'Encrypted Download Test', |
| 116 | + document_date: Date.today, |
| 117 | + document_url: 'encrypted_download_test.pdf', |
| 118 | + user: @encrypted_user, |
| 119 | + encrypted_flag: true |
| 120 | + ) |
| 121 | + |
| 122 | + File.write(Settings.document_folder + 'encrypted_download_test.pdf', encrypted_content, mode: 'w+b') |
| 123 | + |
| 124 | + get :download, params: { id: document.id } |
| 125 | + |
| 126 | + assert_response :success |
| 127 | + assert_equal 'application/pdf', @response.content_type |
| 128 | + assert_equal @test_pdf_content, @response.body |
| 129 | + |
| 130 | + File.delete(Settings.document_folder + 'encrypted_download_test.pdf') if File.exist?(Settings.document_folder + 'encrypted_download_test.pdf') |
| 131 | + document.destroy |
| 132 | + end |
| 133 | + |
| 134 | + test "should return unauthorized without token" do |
| 135 | + post :create, params: { |
| 136 | + title: 'Test', |
| 137 | + document_date: Date.today.to_s |
| 138 | + } |
| 139 | + |
| 140 | + assert_response :unauthorized |
| 141 | + end |
7 | 142 | end |
0 commit comments