File: test_directory.rb

package info (click to toggle)
ruby-gitlab-fog-azure-rm 1.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 756 kB
  • sloc: ruby: 5,926; sh: 9; makefile: 4
file content (178 lines) | stat: -rw-r--r-- 4,736 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require File.expand_path '../../test_helper', __dir__

# Test class for Storage Container Model
class TestDirectory < Minitest::Test
  def setup
    @service = Fog::AzureRM::Storage.new(storage_account_credentials)
    @directory = directory(@service)

    @container = ApiStub::Models::Storage::Directory.container
    @container_acl = ApiStub::Models::Storage::Directory.container_acl
    @container_https_url = ApiStub::Models::Storage::Directory.container_https_url
  end

  def test_model_methods
    methods = [
      :acl=,
      :acl,
      :destroy,
      :files,
      :public=,
      :public_url,
      :save,
      :persisted?
    ]
    methods.each do |method|
      assert_respond_to @directory, method
    end
  end

  def test_model_attributes
    attributes = [
      :key,
      :etag,
      :last_modified,
      :lease_duration,
      :lease_state,
      :lease_status,
      :metadata
    ]
    @service.stub :create_container, @container do
      attributes.each do |attribute|
        assert_respond_to @directory, attribute
      end
    end
  end

  def test_set_acl_method_success
    result = @directory.acl = 'container'
    assert_equal 'container', result
    assert_equal 'container', @directory.attributes[:acl]
  end

  def test_set_acl_method_exception
    assert_raises(ArgumentError) do
      @directory.acl = 'invalid-acl'
    end
  end

  def test_get_acl_method_success
    # Return 'unknonw' when directory is not persist and acl is 'unknown'
    @directory.acl = 'unknown'
    @directory.stub :persisted?, false do
      assert_equal 'unknown', @directory.acl
    end

    # Return actual value directly when directory is not persist and acl is not 'unknown'
    @directory.acl = 'blob'
    @directory.stub :persisted?, false do
      assert_equal 'blob', @directory.acl
    end

    # Query actual value and return it when directory is persist and acl is 'unknown'
    @directory.acl = 'unknown'
    @directory.stub :persisted?, true do
      @service.stub :get_container_acl, @container_acl do
        assert_equal 'container', @directory.acl
        assert_equal 'container', @directory.attributes[:acl]
      end
    end
  end

  def test_get_acl_method_without_key_exception
    @directory.attributes.delete(:key)
    assert_raises(ArgumentError) do
      @directory.acl
    end
  end

  def test_destroy_method_success
    @service.stub :delete_container, true do
      assert @directory.destroy
    end
  end

  def test_destroy_method_without_key_exception
    @directory.attributes.delete(:key)
    assert_raises(ArgumentError) do
      @directory.destroy
    end
  end

  def test_files_method_success
    assert_instance_of Fog::AzureRM::Storage::Files, @directory.files
  end

  def test_set_public_method_success
    # Set public
    result = @directory.public = true
    assert result
    assert_equal 'container', @directory.attributes[:acl]

    # Set private
    result = @directory.public = false
    assert !result
    assert_equal nil, @directory.attributes[:acl]
  end

  def test_public_url_method_with_public_success
    @directory.stub :acl, 'container' do
      @service.stub :get_container_url, @container_https_url do
        assert_equal @container_https_url, @directory.public_url
      end
    end
  end

  def test_public_url_method_with_private_success
    @directory.stub :acl, nil do
      assert @directory.public_url.nil?
    end
  end

  def test_public_url_method_without_key_exception
    @directory.attributes.delete(:key)
    assert_raises(ArgumentError) do
      @directory.public_url
    end
  end

  def test_save_method_create_success
    @service.stub :create_container, @container do
      assert @directory.save
      assert @directory.attributes[:is_persisted]
    end
  end

  def test_save_method_update_success
    options = { is_create: false }
    @service.stub :put_container_acl, true do
      @service.stub :put_container_metadata, true do
        @service.stub :get_container_properties, @container do
          assert @directory.save(options)
          assert @directory.attributes[:is_persisted]
        end
      end
    end
  end

  def test_save_method_without_key_exception
    @directory.attributes.delete(:key)
    assert_raises(ArgumentError) do
      @directory.save
    end
  end

  def test_persisted_method_success
    @directory.attributes[:is_persisted] = true
    @directory.attributes[:last_modified] = nil
    assert @directory.persisted?

    @directory.attributes[:is_persisted] = false
    @directory.attributes[:last_modified] = 'datetime'
    assert @directory.persisted?

    @directory.attributes[:is_persisted] = false
    @directory.attributes[:last_modified] = nil
    assert !@directory.persisted?
  end
end