aboutsummaryrefslogtreecommitdiffstats
path: root/test/sites/shared_site_tests.rb
blob: de28d7ae635b7925a863706932d4905558e3282d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require "test_helper"
require "active_support/core_ext/securerandom"
require "yaml"

SITE_CONFIGURATIONS = begin
  YAML.load_file(File.expand_path("../configurations.yml", __FILE__)).deep_symbolize_keys 
rescue Errno::ENOENT
  puts "Missing site configuration file in test/sites/configurations.yml"
end

module ActiveFile::Sites::SharedSiteTests
  extend ActiveSupport::Concern

  FIXTURE_KEY  = SecureRandom.base58(24)
  FIXTURE_FILE = StringIO.new("Hello world!")

  included do
    setup do
      @site = self.class.const_get(:SITE)
      @site.upload FIXTURE_KEY, FIXTURE_FILE
      FIXTURE_FILE.rewind
    end

    teardown do
      @site.delete FIXTURE_KEY
      FIXTURE_FILE.rewind
    end

    test "uploading" do
      begin
        key  = SecureRandom.base58(24)
        data = "Something else entirely!"
        @site.upload(key, StringIO.new(data))

        assert_equal data, @site.download(key)
      ensure
        @site.delete key
      end
    end

    test "downloading" do
      assert_equal FIXTURE_FILE.read, @site.download(FIXTURE_KEY)
    end

    test "existing" do
      assert @site.exist?(FIXTURE_KEY)
      assert_not @site.exist?(FIXTURE_KEY + "nonsense")
    end

    test "deleting" do
      @site.delete FIXTURE_KEY
      assert_not @site.exist?(FIXTURE_KEY)
    end

    test "sizing" do
      assert_equal FIXTURE_FILE.size, @site.byte_size(FIXTURE_KEY)
    end
  
    test "checksumming" do
      assert_equal Digest::MD5.hexdigest(FIXTURE_FILE.read), @site.checksum(FIXTURE_KEY)
    end
  end
end