fog-internet-archive 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +53 -0
  3. data/.travis.yml +16 -0
  4. data/Gemfile +3 -0
  5. data/README.md +6 -0
  6. data/Rakefile +19 -0
  7. data/fog-internet-archive.gemspec +32 -0
  8. data/lib/fog/bin/internet_archive.rb +32 -0
  9. data/lib/fog/internet_archive.rb +1 -0
  10. data/lib/fog/internet_archive/core.rb +291 -0
  11. data/lib/fog/internet_archive/models/storage/directories.rb +39 -0
  12. data/lib/fog/internet_archive/models/storage/directory.rb +105 -0
  13. data/lib/fog/internet_archive/models/storage/file.rb +254 -0
  14. data/lib/fog/internet_archive/models/storage/files.rb +119 -0
  15. data/lib/fog/internet_archive/models/storage/ia_attributes.rb +36 -0
  16. data/lib/fog/internet_archive/parsers/storage/access_control_list.rb +42 -0
  17. data/lib/fog/internet_archive/parsers/storage/complete_multipart_upload.rb +20 -0
  18. data/lib/fog/internet_archive/parsers/storage/copy_object.rb +18 -0
  19. data/lib/fog/internet_archive/parsers/storage/cors_configuration.rb +38 -0
  20. data/lib/fog/internet_archive/parsers/storage/delete_multiple_objects.rb +45 -0
  21. data/lib/fog/internet_archive/parsers/storage/get_bucket.rb +58 -0
  22. data/lib/fog/internet_archive/parsers/storage/get_bucket_lifecycle.rb +64 -0
  23. data/lib/fog/internet_archive/parsers/storage/get_bucket_location.rb +16 -0
  24. data/lib/fog/internet_archive/parsers/storage/get_bucket_logging.rb +36 -0
  25. data/lib/fog/internet_archive/parsers/storage/get_bucket_website.rb +22 -0
  26. data/lib/fog/internet_archive/parsers/storage/get_request_payment.rb +16 -0
  27. data/lib/fog/internet_archive/parsers/storage/get_service.rb +28 -0
  28. data/lib/fog/internet_archive/parsers/storage/initiate_multipart_upload.rb +20 -0
  29. data/lib/fog/internet_archive/parsers/storage/list_multipart_uploads.rb +52 -0
  30. data/lib/fog/internet_archive/parsers/storage/list_parts.rb +36 -0
  31. data/lib/fog/internet_archive/requests/storage/abort_multipart_upload.rb +27 -0
  32. data/lib/fog/internet_archive/requests/storage/acl_utils.rb +60 -0
  33. data/lib/fog/internet_archive/requests/storage/complete_multipart_upload.rb +46 -0
  34. data/lib/fog/internet_archive/requests/storage/copy_object.rb +77 -0
  35. data/lib/fog/internet_archive/requests/storage/cors_utils.rb +39 -0
  36. data/lib/fog/internet_archive/requests/storage/delete_bucket.rb +42 -0
  37. data/lib/fog/internet_archive/requests/storage/delete_bucket_cors.rb +26 -0
  38. data/lib/fog/internet_archive/requests/storage/delete_bucket_lifecycle.rb +26 -0
  39. data/lib/fog/internet_archive/requests/storage/delete_bucket_policy.rb +26 -0
  40. data/lib/fog/internet_archive/requests/storage/delete_bucket_website.rb +26 -0
  41. data/lib/fog/internet_archive/requests/storage/delete_multiple_objects.rb +88 -0
  42. data/lib/fog/internet_archive/requests/storage/delete_object.rb +46 -0
  43. data/lib/fog/internet_archive/requests/storage/get_bucket.rb +108 -0
  44. data/lib/fog/internet_archive/requests/storage/get_bucket_acl.rb +65 -0
  45. data/lib/fog/internet_archive/requests/storage/get_bucket_cors.rb +61 -0
  46. data/lib/fog/internet_archive/requests/storage/get_bucket_lifecycle.rb +35 -0
  47. data/lib/fog/internet_archive/requests/storage/get_bucket_location.rb +54 -0
  48. data/lib/fog/internet_archive/requests/storage/get_bucket_logging.rb +45 -0
  49. data/lib/fog/internet_archive/requests/storage/get_bucket_policy.rb +31 -0
  50. data/lib/fog/internet_archive/requests/storage/get_bucket_website.rb +38 -0
  51. data/lib/fog/internet_archive/requests/storage/get_object.rb +163 -0
  52. data/lib/fog/internet_archive/requests/storage/get_object_acl.rb +72 -0
  53. data/lib/fog/internet_archive/requests/storage/get_object_http_url.rb +48 -0
  54. data/lib/fog/internet_archive/requests/storage/get_object_https_url.rb +30 -0
  55. data/lib/fog/internet_archive/requests/storage/get_object_torrent.rb +45 -0
  56. data/lib/fog/internet_archive/requests/storage/get_object_url.rb +49 -0
  57. data/lib/fog/internet_archive/requests/storage/get_request_payment.rb +45 -0
  58. data/lib/fog/internet_archive/requests/storage/get_service.rb +50 -0
  59. data/lib/fog/internet_archive/requests/storage/head_object.rb +57 -0
  60. data/lib/fog/internet_archive/requests/storage/initiate_multipart_upload.rb +42 -0
  61. data/lib/fog/internet_archive/requests/storage/list_multipart_uploads.rb +52 -0
  62. data/lib/fog/internet_archive/requests/storage/list_parts.rb +53 -0
  63. data/lib/fog/internet_archive/requests/storage/post_object_hidden_fields.rb +36 -0
  64. data/lib/fog/internet_archive/requests/storage/put_bucket.rb +70 -0
  65. data/lib/fog/internet_archive/requests/storage/put_bucket_acl.rb +69 -0
  66. data/lib/fog/internet_archive/requests/storage/put_bucket_cors.rb +47 -0
  67. data/lib/fog/internet_archive/requests/storage/put_bucket_lifecycle.rb +76 -0
  68. data/lib/fog/internet_archive/requests/storage/put_bucket_logging.rb +79 -0
  69. data/lib/fog/internet_archive/requests/storage/put_bucket_policy.rb +25 -0
  70. data/lib/fog/internet_archive/requests/storage/put_bucket_website.rb +59 -0
  71. data/lib/fog/internet_archive/requests/storage/put_object.rb +94 -0
  72. data/lib/fog/internet_archive/requests/storage/put_object_acl.rb +73 -0
  73. data/lib/fog/internet_archive/requests/storage/put_object_url.rb +43 -0
  74. data/lib/fog/internet_archive/requests/storage/put_request_payment.rb +45 -0
  75. data/lib/fog/internet_archive/requests/storage/sync_clock.rb +24 -0
  76. data/lib/fog/internet_archive/requests/storage/upload_part.rb +39 -0
  77. data/lib/fog/internet_archive/signaturev4.rb +71 -0
  78. data/lib/fog/internet_archive/storage.rb +381 -0
  79. data/lib/fog/internet_archive/version.rb +5 -0
  80. data/tasks/bundler.rake +3 -0
  81. data/tasks/console.rake +17 -0
  82. data/tasks/lint.rake +3 -0
  83. data/tasks/test.rake +7 -0
  84. data/tests/helper.rb +36 -0
  85. data/tests/helpers/collection_helper.rb +88 -0
  86. data/tests/helpers/formats_helper.rb +98 -0
  87. data/tests/helpers/formats_helper_tests.rb +106 -0
  88. data/tests/helpers/mock_helper.rb +13 -0
  89. data/tests/helpers/model_helper.rb +29 -0
  90. data/tests/helpers/responds_to_helper.rb +11 -0
  91. data/tests/helpers/schema_validator_tests.rb +101 -0
  92. data/tests/helpers/succeeds_helper.rb +9 -0
  93. data/tests/internet_archive/models/storage/directory_tests.rb +42 -0
  94. data/tests/internet_archive/models/storage/file_tests.rb +61 -0
  95. data/tests/internet_archive/models/storage/files_tests.rb +60 -0
  96. data/tests/internet_archive/models/storage/url_tests.rb +28 -0
  97. data/tests/internet_archive/requests/storage/acl_utils_tests.rb +209 -0
  98. data/tests/internet_archive/requests/storage/bucket_tests.rb +324 -0
  99. data/tests/internet_archive/requests/storage/cors_utils_tests.rb +108 -0
  100. data/tests/internet_archive/requests/storage/multipart_upload_tests.rb +132 -0
  101. data/tests/internet_archive/requests/storage/object_tests.rb +166 -0
  102. data/tests/internet_archive/signaturev4_tests.rb +41 -0
  103. data/tests/internet_archive/signed_params_tests.rb +5 -0
  104. data/tests/lorem.txt +1 -0
  105. metadata +322 -0
@@ -0,0 +1,5 @@
1
+ module Fog
2
+ module InternetArchive
3
+ VERSION = "0.0.1".freeze
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ # This installs the tasks that help with gem creation and
2
+ # publishing.
3
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,17 @@
1
+ # From http://erniemiller.org/2014/02/05/7-lines-every-gems-rakefile-should-have/
2
+ # with some modification.
3
+
4
+ desc "Project IRB console"
5
+ task :console do
6
+ require "bundler"
7
+ Bundler.require(:default, :development)
8
+
9
+ # Reload helper to avoid resetting the environment when debugging
10
+ def reload!
11
+ files = $LOADED_FEATURES.select { |feat| feat =~ /\/fog-internet-archive\// }
12
+ files.each { |file| load file }
13
+ end
14
+
15
+ ARGV.clear
16
+ Pry.start
17
+ end
@@ -0,0 +1,3 @@
1
+ require "rubocop/rake_task"
2
+
3
+ RuboCop::RakeTask.new(:lint)
@@ -0,0 +1,7 @@
1
+ namespace :test do
2
+ mock = ENV["FOG_MOCK"] || "true"
3
+ desc "Run tests for travis"
4
+ task :travis do
5
+ sh("export FOG_MOCK=#{mock} && bundle exec shindont")
6
+ end
7
+ end
@@ -0,0 +1,36 @@
1
+ ENV["FOG_RC"] = ENV["FOG_RC"] || File.expand_path("../.fog", __FILE__)
2
+ ENV["FOG_CREDENTIAL"] = ENV["FOG_CREDENTIAL"] || "default"
3
+
4
+ require "fog/internet_archive"
5
+ require "securerandom"
6
+
7
+ Excon.defaults.merge!(:debug_request => true, :debug_response => true)
8
+
9
+ require File.expand_path(File.join(File.dirname(__FILE__), "helpers", "mock_helper"))
10
+
11
+ # This overrides the default 600 seconds timeout during live test runs
12
+ if Fog.mocking?
13
+ FOG_TESTING_TIMEOUT = ENV["FOG_TEST_TIMEOUT"] || 2000
14
+ Fog.timeout = 2000
15
+ Fog::Logger.warning "Setting default fog timeout to #{Fog.timeout} seconds"
16
+ else
17
+ FOG_TESTING_TIMEOUT = Fog.timeout
18
+ end
19
+
20
+ def lorem_file
21
+ File.open(File.dirname(__FILE__) + "/lorem.txt", "r")
22
+ end
23
+
24
+ def array_differences(array_a, array_b)
25
+ (array_a - array_b) | (array_b - array_a)
26
+ end
27
+
28
+ def generate_unique_domain(with_trailing_dot = false)
29
+ # get time (with 1/100th of sec accuracy)
30
+ # want unique domain name and if provider is fast, this can be called more than once per second
31
+ time = (Time.now.to_f * 100).to_i
32
+ domain = "test-" + time.to_s + ".com"
33
+ domain += "." if with_trailing_dot
34
+
35
+ domain
36
+ end
@@ -0,0 +1,88 @@
1
+ # TODO: REMOVE this testing functionality is now covered in `spec/helpers/model_helper.rb`
2
+
3
+ def collection_tests(collection, params = {}, mocks_implemented = true)
4
+ tests("success") do
5
+ tests("#new(#{params.inspect})").succeeds do
6
+ pending if Fog.mocking? && !mocks_implemented
7
+ collection.new(params)
8
+ end
9
+
10
+ tests("#create(#{params.inspect})").succeeds do
11
+ pending if Fog.mocking? && !mocks_implemented
12
+ @instance = collection.create(params)
13
+ end
14
+ # FIXME: work around for timing issue on AWS describe_instances mocks
15
+
16
+ if Fog.mocking? && @instance.respond_to?(:ready?)
17
+ @instance.wait_for { ready? }
18
+ end
19
+
20
+ tests("#all").succeeds do
21
+ pending if Fog.mocking? && !mocks_implemented
22
+ collection.all
23
+ end
24
+
25
+ @identity = @instance.identity if !Fog.mocking? || mocks_implemented
26
+
27
+ tests("#get(#{@identity})").succeeds do
28
+ pending if Fog.mocking? && !mocks_implemented
29
+ collection.get(@identity)
30
+ end
31
+
32
+ tests("Enumerable") do
33
+ pending if Fog.mocking? && !mocks_implemented
34
+
35
+ methods = [
36
+ "all?", "any?", "find", "detect", "collect", "map",
37
+ "find_index", "flat_map", "collect_concat", "group_by",
38
+ "none?", "one?"
39
+ ]
40
+
41
+ # JRuby 1.7.5+ issue causes a SystemStackError: stack level too deep
42
+ # https://github.com/jruby/jruby/issues/1265
43
+ if RUBY_PLATFORM == "java" && JRUBY_VERSION =~ /1\.7\.[5-8]/
44
+ methods.delete("all?")
45
+ end
46
+
47
+ methods.each do |enum_method|
48
+ next unless collection.respond_to?(enum_method)
49
+ tests("##{enum_method}").succeeds do
50
+ block_called = false
51
+ collection.send(enum_method) { |_| block_called = true }
52
+ block_called
53
+ end
54
+ end
55
+
56
+ %w(
57
+ max_by min_by).each do |enum_method|
58
+ next unless collection.respond_to?(enum_method)
59
+ tests("##{enum_method}").succeeds do
60
+ block_called = false
61
+ collection.send(enum_method) do |_|
62
+ block_called = true
63
+ 0
64
+ end
65
+ block_called
66
+ end
67
+ end
68
+ end
69
+
70
+ yield(@instance) if block_given?
71
+
72
+ @instance.destroy if !Fog.mocking? || mocks_implemented
73
+ end
74
+
75
+ tests("failure") do
76
+ if !Fog.mocking? || mocks_implemented
77
+ @identity = @identity.to_s
78
+ @identity = @identity.gsub(/[a-zA-Z]/) { Fog::Mock.random_letters(1) }
79
+ @identity = @identity.gsub(/\d/) { Fog::Mock.random_numbers(1) }
80
+ @identity
81
+ end
82
+
83
+ tests("#get('#{@identity}')").returns(nil) do
84
+ pending if Fog.mocking? && !mocks_implemented
85
+ collection.get(@identity)
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,98 @@
1
+ require "fog/schema/data_validator"
2
+
3
+ # format related hackery
4
+ # allows both true.is_a?(Fog::Boolean) and false.is_a?(Fog::Boolean)
5
+ # allows both nil.is_a?(Fog::Nullable::String) and ''.is_a?(Fog::Nullable::String)
6
+ module Fog
7
+ module Boolean; end
8
+ module Nullable
9
+ module Boolean; end
10
+ module Integer; end
11
+ module String; end
12
+ module Time; end
13
+ module Float; end
14
+ module Hash; end
15
+ module Array; end
16
+ end
17
+ end
18
+ [FalseClass, TrueClass].each { |klass| klass.send(:include, Fog::Boolean) }
19
+ [FalseClass, TrueClass, NilClass, Fog::Boolean].each { |klass| klass.send(:include, Fog::Nullable::Boolean) }
20
+ [NilClass, String].each { |klass| klass.send(:include, Fog::Nullable::String) }
21
+ [NilClass, Time].each { |klass| klass.send(:include, Fog::Nullable::Time) }
22
+ [Integer, NilClass].each { |klass| klass.send(:include, Fog::Nullable::Integer) }
23
+ [Float, NilClass].each { |klass| klass.send(:include, Fog::Nullable::Float) }
24
+ [Hash, NilClass].each { |klass| klass.send(:include, Fog::Nullable::Hash) }
25
+ [Array, NilClass].each { |klass| klass.send(:include, Fog::Nullable::Array) }
26
+
27
+ module Shindo
28
+ class Tests
29
+ # Generates a Shindo test that compares a hash schema to the result
30
+ # of the passed in block returning true if they match.
31
+ #
32
+ # The schema that is passed in is a Hash or Array of hashes that
33
+ # have Classes in place of values. When checking the schema the
34
+ # value should match the Class.
35
+ #
36
+ # Strict mode will fail if the data has additional keys. Setting
37
+ # +strict+ to +false+ will allow additional keys to appear.
38
+ #
39
+ # @param [Hash] schema A Hash schema
40
+ # @param [Hash] options Options to change validation rules
41
+ # @option options [Boolean] :allow_extra_keys
42
+ # If +true+ does not fail when keys are in the data that are
43
+ # not specified in the schema. This allows new values to
44
+ # appear in API output without breaking the check.
45
+ # @option options [Boolean] :allow_optional_rules
46
+ # If +true+ does not fail if extra keys are in the schema
47
+ # that do not match the data. Not recommended!
48
+ # @yield Data to check with schema
49
+ #
50
+ # @example Using in a test
51
+ # Shindo.tests("comparing welcome data against schema") do
52
+ # data = {:welcome => "Hello" }
53
+ # data_matches_schema(:welcome => String) { data }
54
+ # end
55
+ #
56
+ # comparing welcome data against schema
57
+ # + data matches schema
58
+ #
59
+ # @example Example schema
60
+ # {
61
+ # "id" => String,
62
+ # "ram" => Integer,
63
+ # "disks" => [
64
+ # {
65
+ # "size" => Float
66
+ # }
67
+ # ],
68
+ # "dns_name" => Fog::Nullable::String,
69
+ # "active" => Fog::Boolean,
70
+ # "created" => DateTime
71
+ # }
72
+ #
73
+ # @return [Boolean]
74
+ def data_matches_schema(schema, options = {})
75
+ test("data matches schema") do
76
+ validator = Fog::Schema::DataValidator.new
77
+ valid = validator.validate(yield, schema, options)
78
+ @message = validator.message unless valid
79
+ valid
80
+ end
81
+ end
82
+
83
+ # @deprecated #formats is deprecated. Use #data_matches_schema instead
84
+ def formats(format, strict = true)
85
+ test("has proper format") do
86
+ if strict
87
+ options = { :allow_extra_keys => false, :allow_optional_rules => true }
88
+ else
89
+ options = { :allow_extra_keys => true, :allow_optional_rules => true }
90
+ end
91
+ validator = Fog::Schema::DataValidator.new
92
+ valid = validator.validate(yield, format, options)
93
+ @message = validator.message unless valid
94
+ valid
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,106 @@
1
+ Shindo.tests("test_helper", "meta") do
2
+ tests("comparing welcome data against schema") do
3
+ data = { :welcome => "Hello" }
4
+ data_matches_schema(:welcome => String) { data }
5
+ end
6
+
7
+ tests('#data_matches_schema') do
8
+ tests("when value matches schema expectation") do
9
+ data_matches_schema("key" => String) { { "key" => "Value" } }
10
+ end
11
+
12
+ tests("when values within an array all match schema expectation") do
13
+ data_matches_schema({ "key" => [Integer] }) { { "key" => [1, 2] } }
14
+ end
15
+
16
+ tests("when nested values match schema expectation") do
17
+ data_matches_schema("key" => { :nested_key => String }) { { "key" => { :nested_key => "Value" } } }
18
+ end
19
+
20
+ tests("when collection of values all match schema expectation") do
21
+ data_matches_schema([{ "key" => String }]) { [{ "key" => "Value" }, { "key" => "Value" }] }
22
+ end
23
+
24
+ tests("when collection is empty although schema covers optional members") do
25
+ data_matches_schema([{ "key" => String }], :allow_optional_rules => true) { [] }
26
+ end
27
+
28
+ tests("when additional keys are passed and not strict") do
29
+ data_matches_schema({ "key" => String }, :allow_extra_keys => true) { { "key" => "Value", :extra => "Bonus" } }
30
+ end
31
+
32
+ tests("when value is nil and schema expects NilClass") do
33
+ data_matches_schema("key" => NilClass) { { "key" => nil } }
34
+ end
35
+
36
+ tests("when value and schema match as hashes") do
37
+ data_matches_schema({}) { {} }
38
+ end
39
+
40
+ tests("when value and schema match as arrays") do
41
+ data_matches_schema([]) { [] }
42
+ end
43
+
44
+ tests("when value is a Time") do
45
+ data_matches_schema("time" => Time) { { "time" => Time.now } }
46
+ end
47
+
48
+ tests('when key is missing but value should be NilClass (#1477)') do
49
+ data_matches_schema({ "key" => NilClass }, :allow_optional_rules => true) { {} }
50
+ end
51
+
52
+ tests('when key is missing but value is nullable (#1477)') do
53
+ data_matches_schema({ "key" => Fog::Nullable::String }, :allow_optional_rules => true) { {} }
54
+ end
55
+ end
56
+
57
+ tests('#formats backwards compatible changes') do
58
+ tests("when value matches schema expectation") do
59
+ formats("key" => String) { { "key" => "Value" } }
60
+ end
61
+
62
+ tests("when values within an array all match schema expectation") do
63
+ formats("key" => [Integer]) { { "key" => [1, 2] } }
64
+ end
65
+
66
+ tests("when nested values match schema expectation") do
67
+ formats("key" => { :nested_key => String }) { { "key" => { :nested_key => "Value" } } }
68
+ end
69
+
70
+ tests("when collection of values all match schema expectation") do
71
+ formats([{ "key" => String }]) { [{ "key" => "Value" }, { "key" => "Value" }] }
72
+ end
73
+
74
+ tests("when collection is empty although schema covers optional members") do
75
+ formats([{ "key" => String }]) { [] }
76
+ end
77
+
78
+ tests("when additional keys are passed and not strict") do
79
+ formats({ "key" => String }, false) { { "key" => "Value", :extra => "Bonus" } }
80
+ end
81
+
82
+ tests("when value is nil and schema expects NilClass") do
83
+ formats("key" => NilClass) { { "key" => nil } }
84
+ end
85
+
86
+ tests("when value and schema match as hashes") do
87
+ formats({}) { {} }
88
+ end
89
+
90
+ tests("when value and schema match as arrays") do
91
+ formats([]) { [] }
92
+ end
93
+
94
+ tests("when value is a Time") do
95
+ formats("time" => Time) { { "time" => Time.now } }
96
+ end
97
+
98
+ tests('when key is missing but value should be NilClass (#1477)') do
99
+ formats("key" => NilClass) { {} }
100
+ end
101
+
102
+ tests('when key is missing but value is nullable (#1477)') do
103
+ formats({ "key" => Fog::Nullable::String }) { {} }
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,13 @@
1
+ # Use so you can run in mock mode from the command line
2
+ #
3
+ # FOG_MOCK=true fog
4
+
5
+ Fog.mock! if ENV["FOG_MOCK"] == "true"
6
+
7
+ # if in mocked mode, fill in some fake credentials for us
8
+ if Fog.mock?
9
+ Fog.credentials = {
10
+ ia_access_key_id: "fake_id",
11
+ ia_secret_access_key: "fake_key",
12
+ }.merge(Fog.credentials)
13
+ end
@@ -0,0 +1,29 @@
1
+ # TODO: REMOVE this testing functionality is now covered in `spec/helpers/model_helper.rb`
2
+
3
+ def model_tests(collection, params = {}, mocks_implemented = true)
4
+ tests("success") do
5
+ @instance = collection.new(params)
6
+
7
+ tests("#save").succeeds do
8
+ pending if Fog.mocking? && !mocks_implemented
9
+ @instance.save
10
+ end
11
+
12
+ yield(@instance) if block_given?
13
+
14
+ tests("#destroy").succeeds do
15
+ pending if Fog.mocking? && !mocks_implemented
16
+ @instance.destroy
17
+ end
18
+ end
19
+ end
20
+
21
+ # Generates a unique identifier with a random differentiator.
22
+ # Useful when rapidly re-running tests, so we don't have to wait
23
+ # serveral minutes for deleted objects to disappear from the API
24
+ # E.g. 'fog-test-1234'
25
+ def uniq_id(base_name = "fog-test")
26
+ # random_differentiator
27
+ suffix = rand(65_536).to_s(16).rjust(4, "0")
28
+ [base_name, suffix].join("-")
29
+ end
@@ -0,0 +1,11 @@
1
+ module Shindo
2
+ class Tests
3
+ def responds_to(method_names)
4
+ for method_name in [*method_names]
5
+ tests("#respond_to?(:#{method_name})").returns(true) do
6
+ @instance.respond_to?(method_name)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,101 @@
1
+ Shindo.tests("Fog::Schema::DataValidator", "meta") do
2
+ validator = Fog::Schema::DataValidator.new
3
+
4
+ tests('#validate') do
5
+ tests("returns true") do
6
+ returns(true, "when value matches schema expectation") do
7
+ validator.validate({ "key" => "Value" }, "key" => String)
8
+ end
9
+
10
+ returns(true, "when values within an array all match schema expectation") do
11
+ validator.validate({ "key" => [1, 2] }, "key" => [Integer])
12
+ end
13
+
14
+ returns(true, "when nested values match schema expectation") do
15
+ validator.validate({ "key" => { :nested_key => "Value" } }, "key" => { :nested_key => String })
16
+ end
17
+
18
+ returns(true, "when collection of values all match schema expectation") do
19
+ validator.validate([{ "key" => "Value" }, { "key" => "Value" }], [{ "key" => String }])
20
+ end
21
+
22
+ returns(true, "when collection is empty although schema covers optional members") do
23
+ validator.validate([], [{ "key" => String }])
24
+ end
25
+
26
+ returns(true, "when additional keys are passed and not strict") do
27
+ validator.validate({ "key" => "Value", :extra => "Bonus" }, { "key" => String }, :allow_extra_keys => true)
28
+ end
29
+
30
+ returns(true, "when value is nil and schema expects NilClass") do
31
+ validator.validate({ "key" => nil }, "key" => NilClass)
32
+ end
33
+
34
+ returns(true, "when value and schema match as hashes") do
35
+ validator.validate({}, {})
36
+ end
37
+
38
+ returns(true, "when value and schema match as arrays") do
39
+ validator.validate([], [])
40
+ end
41
+
42
+ returns(true, "when value is a Time") do
43
+ validator.validate({ "time" => Time.now }, "time" => Time)
44
+ end
45
+
46
+ returns(true, 'when key is missing but value should be NilClass (#1477)') do
47
+ validator.validate({}, { "key" => NilClass }, :allow_optional_rules => true)
48
+ end
49
+
50
+ returns(true, 'when key is missing but value is nullable (#1477)') do
51
+ validator.validate({}, { "key" => Fog::Nullable::String }, :allow_optional_rules => true)
52
+ end
53
+ end
54
+
55
+ tests("returns false") do
56
+ returns(false, "when value does not match schema expectation") do
57
+ validator.validate({ "key" => nil }, { "key" => String })
58
+ end
59
+
60
+ returns(false, "when key formats do not match") do
61
+ validator.validate({ "key" => "Value" }, :key => String)
62
+ end
63
+
64
+ returns(false, "when additional keys are passed and strict") do
65
+ validator.validate({ "key" => "Missing" }, {})
66
+ end
67
+
68
+ returns(false, "when some keys do not appear") do
69
+ validator.validate({}, "key" => String)
70
+ end
71
+
72
+ returns(false, "when collection contains a member that does not match schema") do
73
+ validator.validate([{ "key" => "Value" }, { "key" => 5 }], [{ "key" => String }])
74
+ end
75
+
76
+ returns(false, "when collection has multiple schema patterns") do
77
+ validator.validate([{ "key" => "Value" }], [{ "key" => Integer }, { "key" => String }])
78
+ end
79
+
80
+ returns(false, "when hash and array are compared") do
81
+ validator.validate({}, [])
82
+ end
83
+
84
+ returns(false, "when array and hash are compared") do
85
+ validator.validate([], {})
86
+ end
87
+
88
+ returns(false, "when a hash is expected but another data type is found") do
89
+ validator.validate({ "key" => { :nested_key => [] } }, "key" => { :nested_key => {} })
90
+ end
91
+
92
+ returns(false, 'when key is missing but value should be NilClass (#1477)') do
93
+ validator.validate({}, { "key" => NilClass }, :allow_optional_rules => false)
94
+ end
95
+
96
+ returns(false, 'when key is missing but value is nullable (#1477)') do
97
+ validator.validate({}, { "key" => Fog::Nullable::String }, :allow_optional_rules => false)
98
+ end
99
+ end
100
+ end
101
+ end