class TestActFiles

Public Instance Methods

attach(size) click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 705
def attach(size)
  content = '0' * size
  filename = "#{size.byte_format}.txt"
  res = session('POST /test/1.files') {|req|
    req.query.update('content'=>t_make_content(filename, content))
  }
  ok_title('File attachment completed')
  return filename,content
end
test_act_files() click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 474
    def test_act_files
      t_add_user

      page = @site['FrontPage']
      page.store('t')

      # See the form.
      res = session('/test/.files')
      form = res.body.get_path("//div[@class='section']/form")
      ok_eq({:method=>'POST', :action=>'FrontPage.files',
              :enctype=>'multipart/form-data'}, form[1])

      # Put a file.
      res = session('POST /test/.files') {|req|
        req.query.update('content'=>t_make_content('t.txt', 't'))
      }
      ok_title('File attachment completed')

      # Check log.
      eq(",0.000000,user@e.com,file attach,FrontPage\n", @site['_SiteLog'].load)

      # The reference is added.
      ok_eq('t

{{file(t.txt)}}
', page.load)

      # Get the file.
      res = session('/test/.files/t.txt')
      ok_eq('t', res.body)
    end
test_big_file() click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 654
def test_big_file
  t_add_user

  # Set max_file_size to 1MB.
  config = @site['_SiteConfig']
  max_size = 1 * 1024 * 1024        # 1MB
  config.store(":max_file_size:#{max_size}")

  page = @site.create_new
  page.store('t')

  # Try to store a file with 2MB size.
  big_content = '0' * (2 * 1024 * 1024)     # 2MB
  res = session('POST /test/1.files') {|req|
    req.query.update('content'=>t_make_content('t.txt', big_content))
  }
  ok_title('File attachment completed')
  ok_in(['Maximum size exceeded.'],
        "//div[@class='section']/p/em")

  # Get the file.
  res = session('/test/1.files/t.txt')
  ok_title('No such file')
end
test_class_method() click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 630
def test_class_method
  c = Qwik::Action
  eq("a.txt", c.get_basename("/tmp/a.txt"))
  eq("a.txt", c.get_basename("c:\\tmp\\a.txt"))
end
test_download() click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 544
def test_download
  t_add_user

  page = @site.create_new
  page.store('t')

  # Put a file.
  res = session('POST /test/1.files') {|req|
    req.query.update('content'=>t_make_content('t.txt', 't'))
  }
  ok_title('File attachment completed')

  # Error check.
  res = session('/test/1.download')
  ok_title('Error')

  # Download the file.
  res = session('/test/1.download/t.txt')
  ok_eq('text/plain', res['Content-Type'])
  ok_eq("attachment; filename=\"t.txt\"", res['Content-Disposition'])
  ok_eq('t', res.body)

  # Put a file with Japanese filename.
  res = session('POST /test/1.files') {|req|
    req.query.update('content'=>t_make_content('あ.txt', 't'))
  }
  ok_title('File attachment completed')

  # Download the file.
  res = session('/test/1.download/=E3=81=82.txt')
  ok_eq('text/plain', res['Content-Type'])
  ok_eq("attachment; filename=\"あ.txt\"",
        res['Content-Disposition'])
  ok_eq('t', res.body)
end
test_files() click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 317
    def test_files
      t_add_user

      ok_wi([:div, {:class=>'files'},
              [:h5, [:a, {:href=>'1.files'}, 'Files']]],
            '{{show_files}}')

      page = @site['1']
      page.store('t')

      # See the form.
      res = session('/test/1.files')
      form = res.body.get_path("//div[@class='section']/form")
      ok_eq({:method=>'POST', :action=>'1.files',
              :enctype=>'multipart/form-data'}, form[1])
      ok_eq([:div, {:class=>'inputfile'},
              [:input, {:type=>'file', :name=>'content'}]], form[2])
      ok_eq([:div, {:class=>'input_submit'},
              [:input, {:value=>'Attach', :type=>'submit', :class=>'submit'}]],
            form.last)

      # Put a file.
      res = session('POST /test/1.files') {|req|
        req.query.update('content'=>t_make_content('t.txt', 't'))
      }
      ok_title('File attachment completed')

      # Check log.
      eq(",0.000000,user@e.com,file attach,1\n", @site['_SiteLog'].load)

      # The reference is added.
      ok_eq('t

{{file(t.txt)}}
', page.load)

      # Get the file.
      res = session('/test/1.files/t.txt')
      ok_eq('t', res.body)
      ok_eq('text/plain', res['Content-Type'])

      # Put a file with same file name again.
      # The file is saved as another filename.
      res = session('POST /test/1.files') {|req|
        req.query.update('content'=>t_make_content('t.txt', 't2'))
      }
      ok_title('File attachment completed')
      ok_xp([:p, [:strong, '1-t.txt'], ' : ',
              'The file is saved.'],
            "//div[@class='section']/p")

      # Delete the second file.
      res = session('POST /test/1.file_del/1-t.txt')
      ok_title('The file has been deleted.')

      # See the page again.
      ok_wi([:div, {:class=>'files'},
              [:h5, [:a, {:href=>'1.files'}, 'Files']],
              [:ul,
                [:li, [:a, {:href=>'1.files/t.txt'}, 't.txt'],
                  [:span, {:class=>'delete'},
                    " (", [:a, {:href=>"1.file_del/t.txt"}, 'Delete'], ")"],
                  [:span, {:class=>'download'},
                    " (", [:a, {:href=>'1.download/t.txt'}, 'Download'],
                    ")"]]]],
            '{{show_files}}')

      # Put another file.
      res = session('POST /test/1.files') {|req|
        req.query.update('content'=>t_make_content('s.png', TEST_PNG_DATA))
      }
      ok_title('File attachment completed')

      # The reference is added too.
      ok_eq('{{show_files}}

{{file(s.png)}}
', page.load)

      # See the page again.
      res = session('/test/1.html')
      ok_xp([:div, {:class=>'files'},
              [:h5, [:a, {:href=>'1.files'}, 'Files']],
              [:ul,
                [:li, [:a, {:href=>'1.files/s.png'}, 's.png'],
                  [:span, {:class=>'delete'},
                    " (", [:a, {:href=>"1.file_del/s.png"}, 'Delete'], ")"],
                  [:span, {:class=>'download'},
                    " (", [:a, {:href=>'1.download/s.png'},
                      'Download'], ")"]],
                [:li, [:a, {:href=>'1.files/t.txt'}, 't.txt'],
                  [:span, {:class=>'delete'},
                    " (", [:a, {:href=>"1.file_del/t.txt"}, 'Delete'], ")"],
                  [:span, {:class=>'download'},
                    " (", [:a, {:href=>'1.download/t.txt'},
                      'Download'], ")"]]]],
            "//div[@class='files']")

      # Show a form to delete the file.
      res = session('/test/1.file_del/t.txt')
      ok_title('Confirm file deletion')

      # Delete it.
      res = session('POST /test/1.file_del/t.txt')
      ok_title('The file has been deleted.')

      # Try to delete it again.  But the file is already deleted.
      res = session('POST /test/1.file_del/t.txt')
      ok_title('Already deleted.')

      # Try to get file agin.  But the file is already deleted.
      res = session('/test/1.files/t.txt')
      ok_title('No such file')
    end
test_force_download() click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 580
def test_force_download
  t_add_user

  page = @site.create_new
  page.store('t')

  # Put a file.
  res = session('POST /test/1.files') {|req|
    req.query.update('content'=>t_make_content('t.doc', 't'))
  }
  ok_title('File attachment completed')

  # Download the file.
  res = session('/test/1.download/t.doc')
  ok_eq('application/msword', res['Content-Type'])
  ok_eq("attachment; filename=\"t.doc\"", res['Content-Disposition'])
  ok_eq('t', res.body)

  # Download by files extension.
  res = session('/test/1.files/t.doc')
  ok_eq('application/msword', res['Content-Type'])
  ok_eq("attachment; filename=\"t.doc\"", res['Content-Disposition'])
  ok_eq('t', res.body)
end
test_force_download_by_character() click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 679
def test_force_download_by_character
  t_add_user

  page = @site.create_new
  page.store('t')

  # Put a file.
  res = session('POST /test/1.files') {|req|
    req.query.update('content'=>t_make_content('t!.txt', 't'))
  }
  ok_title('File attachment completed')

  # Download the file.
  res = session('/test/1.download/t!.txt')
  ok_eq("attachment; filename=\"t!.txt\"", res['Content-Disposition'])
  ok_eq('t', res.body)

  # Download by files extension.
  res = session('/test/1.files/t!.txt')
  ok_eq("attachment; filename=\"t!.txt\"", res['Content-Disposition'])
  ok_eq('t', res.body)
end
test_force_download_capital_ext() click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 605
def test_force_download_capital_ext
  t_add_user

  page = @site.create_new
  page.store('t')

  # Put a file.
  res = session('POST /test/1.files') {|req|
    req.query.update('content'=>t_make_content('T.DOC', 't'))
  }
  ok_title('File attachment completed')

  # Download the file.
  res = session('/test/1.download/T.DOC')
  ok_eq('application/msword', res['Content-Type'])
  ok_eq("attachment; filename=\"T.DOC\"", res['Content-Disposition'])
  ok_eq('t', res.body)

  # Download by files extension.
  res = session('/test/1.files/T.DOC')
  ok_eq('application/msword', res['Content-Type'])
  ok_eq("attachment; filename=\"T.DOC\"", res['Content-Disposition'])
  ok_eq('t', res.body)
end
test_jfilename() click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 506
    def test_jfilename
      t_add_user

      page = @site.create_new
      page.store('t')

      # Put a file with Japanese filename.
      res = session('POST /test/1.files') {|req|
        req.query.update('content'=>t_make_content('あ.txt', 't'))
      }
      ok_title('File attachment completed')

      # Check log.
      ok_eq(",0.000000,user@e.com,file attach,1\n", @site['_SiteLog'].load)

      # The reference is added.
      ok_eq('t

{{file(あ.txt)}}
', page.load)

      # Get the file.
      res = session('/test/1.files/=E3=81=82.txt')
      ok_eq('t', res.body)

      # You can use SJIS charset.
      res = session('/test/1.files/あ.txt')
      ok_eq('t', res.body)

      # You can use UTF-8 charset.
      res = session('/test/1.files/あ.txt'.set_sourcecode_charset.to_url_charset)
      ok_eq('t', res.body)

      # Delete it.
      res = session('POST /test/1.file_del/=E3=81=82.txt')
      ok_title('The file has been deleted.')
    end
test_multi_files() click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 432
    def test_multi_files
      t_add_user
      page = @site.create_new

      # See the form.
      res = session('/test/1.files')
      form = @res.body.get_path("//div[@class='section']/form")
      ok_eq({:method=>'POST', :action=>'1.files',
              :enctype=>'multipart/form-data'}, form[1])
      ok_eq([:div, {:class=>'inputfile'},
              [:input, {:type=>'file', :name=>'content'}]], form[2])
      ok_eq([:div, {:class=>'inputfile'},
              [:input, { :type=>'file', :name=>'content'}]], form[4])
      ok_eq([:div, {:class=>'input_submit'},
              [:input, {:value=>'Attach', :type=>'submit', :class=>'submit'}]],
            form.last)

      # Put multiple files.
      content1 = t_make_content('t1.txt', 't1')
      content2 = t_make_content('t2.txt', 't2')
      content1.append_data(content2)
      res = session('POST /test/1.files') {|req|
        req.query.update('content'=>content1)
      }

      ok_title('File attachment completed')
      ok_in([
              [:p, [:strong, 't1.txt'], ' : ', 'The file is saved.'],
              [:p, [:strong, 't2.txt'], ' : ', 'The file is saved.'],
              [:hr],
              [:p, 'Go next', ' : ',
                [:a, {:href=>'1.html'}, '1.html']]],
            "//div[@class='section']")

      # The reference is added.
      ok_eq('
{{file(t1.txt)}}

{{file(t2.txt)}}
', page.load)
    end
test_total_file_size_limit() click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 702
def test_total_file_size_limit
  t_add_user

  def attach(size)
    content = '0' * size
    filename = "#{size.byte_format}.txt"
    res = session('POST /test/1.files') {|req|
      req.query.update('content'=>t_make_content(filename, content))
    }
    ok_title('File attachment completed')
    return filename,content
  end
  # Set max_total_file_size to 1MB and warn 256KB
  default_max_total_file_size = @config[:max_total_file_size]
  default_max_total_warn_size = @config[:max_total_warn_size]
  @config[:max_total_file_size] =  1 * 1024 * 1024  # 1MB
  @config[:max_total_warn_size] =  256 * 1024 # 256KB

  page = @site.create_new
  page.store('t')

  ## Try to store a file with 512KB size.
  filename,content = attach(512*1024)
  ok_xp([:p, [:strong, filename], ' : ', 'The file is saved.'],
        "//div[@class='section']/p")

  # Get the file.
  res = session("/test/1.files/#{filename}")
  ok_eq(content,res.body)

  ## Try to store a file with 300KB size. will get the warning
  filename,content = attach(300*1024)
  ok_xp([:p, [:strong, filename], ' : ', 'The file is saved.',
         [:br], [:strong, "Reaching limit. 212KB left"]],
        "//div[@class='section']/p")

  # Get the file.
  res = session("/test/1.files/#{filename}")
  ok_eq(content,res.body)

  ## Try to store a file with 300KB size. will exceeds the limit
  filename,content = attach(256*1024)
  ok_xp([:p, [:strong, filename], ' : ', 'The file is saved.',
         [:br], [:strong, "Exceeded limit."]],
        "//div[@class='section']/p")

  # Get the file.
  res = session("/test/1.files/#{filename}")
  ok_eq(content,res.body)

  ## Try to store a file with 300KB size. will exceeds the limit
  filename,content = attach(1)
  ok_xp([:p, [:strong, filename], ' : ', [:em, 'The file is not saved.'],
        [:br],
        [:strong, "Total file size exceeded."],[:br],
        'Maximum total size', @config[:max_total_file_size].byte_format, [:br],
         'Current total size', ((512+300+256)*1024).byte_format, [:br]],
        "//div[@class='section']/p")

  # Get the file.
  res = session("/test/1.files/#{filename}")
  ok_title('No such file')

  #clean up
  @config[:max_total_file_size] = default_max_total_file_size
  @config[:max_total_warn_size] = default_max_total_warn_size  
end
test_upload_from_windows() click to toggle source
# File vendor/qwik/lib/qwik/act-files.rb, line 636
def test_upload_from_windows
  t_add_user

  page = @site.create_new
  page.store('t')

  # Put a file.
  res = session('POST /test/1.files') {|req|
    req.query.update('content'=>t_make_content("c:\\tmp\\t.txt", 't'))
  }
  ok_title('File attachment completed')

  # Download by files extension.
  res = session('/test/1.files/t.txt')
  ok_eq('text/plain', res['Content-Type'])
  ok_eq('t', res.body)
end