cloudflare 4.0.1 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Cloudflare::Accounts, order: :defined, timeout: 30 do
4
+ include_context Cloudflare::Account
5
+
6
+ before do
7
+ account.id # Force a fetch if it hasn't happened yet
8
+ end
9
+
10
+ it 'can list existing accounts' do
11
+ accounts = connection.accounts.to_a
12
+ expect(accounts.any? {|a| a.id == account.id }).to be true
13
+ end
14
+
15
+ it 'can get a specific account' do
16
+ expect(connection.accounts.find_by_id(account.id).id).to eq account.id
17
+ end
18
+
19
+ it 'can generate a representation for the KV namespace endpoint' do
20
+ ns = connection.accounts.find_by_id(account.id).kv_namespaces
21
+ expect(ns).to be_kind_of(Cloudflare::KV::Namespaces)
22
+ expect(ns.resource.reference.path).to end_with("/#{account.id}/storage/kv/namespaces")
23
+ end
24
+ end
@@ -0,0 +1,54 @@
1
+ RSpec.describe Cloudflare::CustomHostname::SSLAttribute::Settings do
2
+
3
+ subject { described_class.new({}) }
4
+
5
+ it 'has an accessor for ciphers' do
6
+ ciphers = double
7
+ expect(subject.ciphers).to be_nil
8
+ subject.ciphers = ciphers
9
+ expect(subject.ciphers).to be ciphers
10
+ end
11
+
12
+ it 'has a boolean accessor for http2' do
13
+ expect(subject.http2).to be_nil
14
+ expect(subject.http2?).to be false
15
+ subject.http2 = true
16
+ expect(subject.http2).to eq 'on'
17
+ expect(subject.http2?).to be true
18
+ subject.http2 = false
19
+ expect(subject.http2).to eq 'off'
20
+ expect(subject.http2?).to be false
21
+ subject.http2 = 'on'
22
+ expect(subject.http2).to eq 'on'
23
+ expect(subject.http2?).to be true
24
+ subject.http2 = 'off'
25
+ expect(subject.http2).to eq 'off'
26
+ expect(subject.http2?).to be false
27
+ end
28
+
29
+ it 'has an accessor for min_tls_version' do
30
+ tls_version = double
31
+ expect(subject.min_tls_version).to be_nil
32
+ subject.min_tls_version = tls_version
33
+ expect(subject.min_tls_version).to be tls_version
34
+ end
35
+
36
+ it 'has a boolean accessor for tls_1_3' do
37
+ expect(subject.tls_1_3).to be_nil
38
+ expect(subject.tls_1_3?).to be false
39
+ subject.tls_1_3 = true
40
+ expect(subject.tls_1_3).to eq 'on'
41
+ expect(subject.tls_1_3?).to be true
42
+ subject.tls_1_3 = false
43
+ expect(subject.tls_1_3).to eq 'off'
44
+ expect(subject.tls_1_3?).to be false
45
+ subject.tls_1_3 = 'on'
46
+ expect(subject.tls_1_3).to eq 'on'
47
+ expect(subject.tls_1_3?).to be true
48
+ subject.tls_1_3 = 'off'
49
+ expect(subject.tls_1_3).to eq 'off'
50
+ expect(subject.tls_1_3?).to be false
51
+ end
52
+
53
+
54
+ end
@@ -0,0 +1,73 @@
1
+ RSpec.describe Cloudflare::CustomHostname::SSLAttribute do
2
+
3
+ accessors = [:cname, :cname_target, :http_body, :http_url, :method, :status, :type, :validation_errors]
4
+
5
+ let(:original_hash) { {} }
6
+
7
+ subject { described_class.new(original_hash) }
8
+
9
+ accessors.each do |key|
10
+
11
+ it "has an accessor for the #{key} value" do
12
+ test_value = double
13
+ expect(subject.send(key)).to be_nil
14
+ original_hash[key] = test_value
15
+ expect(subject.send(key)).to be test_value
16
+ end
17
+
18
+ end
19
+
20
+ it '#active? returns true when the status is "active" and false otherwise' do
21
+ expect(subject.active?).to be false
22
+ original_hash[:status] = 'initializing'
23
+ expect(subject.active?).to be false
24
+ original_hash[:status] = 'pending_validation'
25
+ expect(subject.active?).to be false
26
+ original_hash[:status] = 'pending_deployment'
27
+ expect(subject.active?).to be false
28
+ original_hash[:status] = 'active'
29
+ expect(subject.active?).to be true
30
+ end
31
+
32
+ it '#pending_validation? returns true when the status is "pending_validation" and false otherwise' do
33
+ expect(subject.pending_validation?).to be false
34
+ original_hash[:status] = 'initializing'
35
+ expect(subject.pending_validation?).to be false
36
+ original_hash[:status] = 'active'
37
+ expect(subject.pending_validation?).to be false
38
+ original_hash[:status] = 'pending_deployment'
39
+ expect(subject.pending_validation?).to be false
40
+ original_hash[:status] = 'pending_validation'
41
+ expect(subject.pending_validation?).to be true
42
+ end
43
+
44
+ describe '#settings' do
45
+
46
+ it 'should return a Settings object' do
47
+ expect(subject.settings).to be_kind_of Cloudflare::CustomHostname::SSLAttribute::Settings
48
+ end
49
+
50
+ it 'initailizes the settings object with the value from the settings key' do
51
+ settings = { min_tls_version: double }
52
+ original_hash[:settings] = settings
53
+ expect(subject.settings.min_tls_version).to be settings[:min_tls_version]
54
+ end
55
+
56
+ it 'initializes the settings object with a new hash if the settings key does not exist' do
57
+ expected_value = double
58
+ expect(original_hash[:settings]).to be_nil
59
+ expect(subject.settings.min_tls_version).to be_nil
60
+ expect(original_hash[:settings]).not_to be_nil
61
+ original_hash[:settings][:min_tls_version] = expected_value
62
+ expect(subject.settings.min_tls_version).to be expected_value
63
+ end
64
+
65
+ it 'updates the stored hash with values set on the settings object' do
66
+ expected_value = double
67
+ expect(subject.settings.min_tls_version).to be_nil
68
+ subject.settings.min_tls_version = expected_value
69
+ expect(original_hash[:settings][:min_tls_version]).to be expected_value
70
+ end
71
+ end
72
+
73
+ end
@@ -0,0 +1,213 @@
1
+
2
+ RSpec.xdescribe Cloudflare::CustomHostnames, order: :defined, timeout: 30 do
3
+ include_context Cloudflare::Zone
4
+
5
+ let(:domain) { "www#{ENV['TRAVIS_JOB_ID'] || rand(1..5)}.ourtest.com" }
6
+
7
+ let(:record) { @record = zone.custom_hostnames.create(domain) }
8
+
9
+ let(:custom_origin) do
10
+ id = rand(1...100)
11
+ id += (job_id * 100) if job_id.positive?
12
+ subdomain = "origin-#{id}"
13
+ @dns_record = zone.dns_records.create("A", subdomain, "1.2.3.4") # This needs to exist or the calls will fail
14
+ "#{subdomain}.#{zone.name}"
15
+ end
16
+
17
+ after do
18
+ if defined? @record
19
+ expect(@record.delete).to be_success
20
+ end
21
+
22
+ if defined? @dns_record
23
+ expect(@dns_record.delete).to be_success
24
+ end
25
+ end
26
+
27
+ it 'can create a custom hostname record' do
28
+ expect(record).to be_kind_of Cloudflare::CustomHostname
29
+ expect(record.custom_metadata).to be_nil
30
+ expect(record.hostname).to eq domain
31
+ expect(record.custom_origin).to be_nil
32
+ expect(record.ssl.method).to eq 'http'
33
+ expect(record.ssl.type).to eq 'dv'
34
+ end
35
+
36
+ it 'can create a custom hostname record with a custom origin' do
37
+ begin
38
+ @record = zone.custom_hostnames.create(domain, origin: custom_origin)
39
+
40
+ expect(@record).to be_kind_of Cloudflare::CustomHostname
41
+ expect(@record.custom_metadata).to be_nil
42
+ expect(@record.hostname).to eq domain
43
+ expect(@record.custom_origin).to eq custom_origin
44
+ expect(@record.ssl.method).to eq 'http'
45
+ expect(@record.ssl.type).to eq 'dv'
46
+ rescue Cloudflare::RequestError => e
47
+ if e.message.include?('custom origin server has not been granted')
48
+ skip(e.message) # This currently doesn't work but might start eventually: https://github.com/socketry/async-rspec/issues/7
49
+ else
50
+ raise
51
+ end
52
+ end
53
+ end
54
+
55
+ it 'can create a custom hostname record with different ssl options' do
56
+ @record = zone.custom_hostnames.create(domain, ssl: { method: 'cname' })
57
+
58
+ expect(@record).to be_kind_of Cloudflare::CustomHostname
59
+ expect(@record.custom_metadata).to be_nil
60
+ expect(@record.hostname).to eq domain
61
+ expect(@record.custom_origin).to be_nil
62
+ expect(@record.ssl.method).to eq 'cname'
63
+ expect(@record.ssl.type).to eq 'dv'
64
+ end
65
+
66
+ it 'can create a custom hostname record with additional metadata' do
67
+ metadata = { a: rand(1..10) }
68
+
69
+ begin
70
+ @record = zone.custom_hostnames.create(domain, metadata: metadata)
71
+
72
+ expect(@record).to be_kind_of Cloudflare::CustomHostname
73
+ expect(@record.custom_metadata).to eq metadata
74
+ expect(@record.hostname).to eq domain
75
+ expect(@record.custom_origin).to be_nil
76
+ expect(@record.ssl.method).to eq 'http'
77
+ expect(@record.ssl.type).to eq 'dv'
78
+ rescue Cloudflare::RequestError => e
79
+ if e.message.include?('No custom metadata access has been allocated for this zone')
80
+ skip(e.message) # This currently doesn't work but might start eventually: https://github.com/socketry/async-rspec/issues/7
81
+ else
82
+ raise
83
+ end
84
+ end
85
+ end
86
+
87
+ it 'can look up an existing custom hostname by the hostname or id' do
88
+ expect(zone.custom_hostnames.find_by_hostname(record.hostname).id).to eq record.id
89
+ expect(zone.custom_hostnames.find_by_id(record.id).id).to eq record.id
90
+ end
91
+
92
+ context 'with existing record' do
93
+
94
+ it 'returns the hostname when calling #to_s' do
95
+ expect(record.to_s).to eq domain
96
+ end
97
+
98
+ it 'can update metadata' do
99
+ metadata = { c: rand(1..10) }
100
+
101
+ expect(record.custom_metadata).to be_nil
102
+
103
+ begin
104
+ record.update_settings(metadata: metadata)
105
+
106
+ # Make sure the existing object is updated
107
+ expect(record.custom_metadata).to eq metadata
108
+
109
+ # Verify that the server has the changes
110
+ found_record = zone.custom_hostnames.find_by_id(record.id)
111
+
112
+ expect(found_record.custom_metadata).to eq metadata
113
+ expect(found_record.hostname).to eq domain
114
+ expect(found_record.custom_origin).to be_nil
115
+ rescue Cloudflare::RequestError => e
116
+ if e.message.include?('No custom metadata access has been allocated for this zone')
117
+ skip(e.message) # This currently doesn't work but might start eventually: https://github.com/socketry/async-rspec/issues/7
118
+ else
119
+ raise
120
+ end
121
+ end
122
+ end
123
+
124
+ it 'can update the custom origin' do
125
+ expect(record.custom_origin).to be_nil
126
+
127
+ begin
128
+ record.update_settings(origin: custom_origin)
129
+
130
+ # Make sure the existing object is updated
131
+ expect(record.custom_origin).to eq custom_origin
132
+
133
+ # Verify that the server has the changes
134
+ found_record = zone.custom_hostnames.find_by_id(record.id)
135
+
136
+ expect(found_record.custom_metadata).to be_nil
137
+ expect(found_record.hostname).to eq domain
138
+ expect(found_record.custom_origin).to eq custom_origin
139
+ rescue Cloudflare::RequestError => e
140
+ if e.message.include?('custom origin server has not been granted')
141
+ skip(e.message) # This currently doesn't work but might start eventually: https://github.com/socketry/async-rspec/issues/7
142
+ else
143
+ raise
144
+ end
145
+ end
146
+ end
147
+
148
+ it 'can update ssl information' do
149
+ expect(record.ssl.method).to eq 'http'
150
+
151
+ record.update_settings(ssl: { method: 'cname', type: 'dv' })
152
+
153
+ # Make sure the existing object is updated
154
+ expect(record.ssl.method).to eq 'cname'
155
+
156
+ # Verify that the server has the changes
157
+ found_record = zone.custom_hostnames.find_by_id(record.id)
158
+
159
+ expect(found_record.custom_metadata).to be_nil
160
+ expect(found_record.hostname).to eq domain
161
+ expect(found_record.custom_origin).to be_nil
162
+ expect(found_record.ssl.method).to eq 'cname'
163
+ end
164
+
165
+ context 'has an ssl section' do
166
+
167
+ it 'wraps it in an SSLAttributes object' do
168
+ expect(record.ssl).to be_kind_of Cloudflare::CustomHostname::SSLAttribute
169
+ end
170
+
171
+ it 'has some helpers for commonly used keys' do
172
+ # Make sure our values exist before we check to make sure that they are returned correctly
173
+ expect(record.value[:ssl].values_at(:method, :http_body, :http_url).compact).not_to be_empty
174
+ expect(record.ssl.method).to be record.value[:ssl][:method]
175
+ expect(record.ssl.http_body).to be record.value[:ssl][:http_body]
176
+ expect(record.ssl.http_url).to be record.value[:ssl][:http_url]
177
+ end
178
+
179
+ end
180
+
181
+ describe '#ssl_active?' do
182
+
183
+ it 'returns the result of calling ssl.active?' do
184
+ expected_value = double
185
+ expect(record.ssl).to receive(:active?).and_return(expected_value)
186
+ expect(record).not_to receive(:send_patch)
187
+ expect(record.ssl_active?).to be expected_value
188
+ end
189
+
190
+ it 'returns the result of calling ssl.active? without triggering an update if force_update is true and the ssl is not in the pending_validation state' do
191
+ expected_value = double
192
+ expect(record.ssl).to receive(:active?).and_return(expected_value)
193
+ expect(record.ssl.method).not_to be_nil
194
+ expect(record.ssl.type).not_to be_nil
195
+ expect(record.ssl.pending_validation?).to be false
196
+ expect(record).not_to receive(:send_patch).with(ssl: { method: record.ssl.method, type: record.ssl.type })
197
+ expect(record.ssl_active?(true)).to be expected_value
198
+ end
199
+
200
+ it 'returns the result of calling ssl.active? after triggering an update if force_update is true and the ssl is in the pending_validation state' do
201
+ expected_value = double
202
+ expect(record.ssl).to receive(:active?).and_return(expected_value)
203
+ expect(record.ssl.method).not_to be_nil
204
+ expect(record.ssl.type).not_to be_nil
205
+ record.value[:ssl][:status] = 'pending_validation'
206
+ expect(record).to receive(:send_patch).with(ssl: { method: record.ssl.method, type: record.ssl.type })
207
+ expect(record.ssl_active?(true)).to be expected_value
208
+ end
209
+
210
+ end
211
+
212
+ end
213
+ end
@@ -6,21 +6,31 @@ RSpec.describe Cloudflare::DNS, order: :defined, timeout: 30 do
6
6
 
7
7
  let(:subdomain) {"www#{ENV['TRAVIS_JOB_ID']}"}
8
8
 
9
- let(:record) {@record = zone.dns_records.create("A", subdomain, "1.2.3.4")}
10
-
11
9
  after do
12
10
  if defined? @record
13
11
  expect(@record.delete).to be_success
14
12
  end
15
13
  end
16
14
 
17
- it "can create dns record" do
18
- expect(record.type).to be == "A"
19
- expect(record.name).to be_start_with subdomain
20
- expect(record.content).to be == "1.2.3.4"
15
+ context "new record" do
16
+ it "can create dns record" do
17
+ @record = zone.dns_records.create("A", subdomain, "1.2.3.4")
18
+ expect(@record.type).to be == "A"
19
+ expect(@record.name).to be_start_with subdomain
20
+ expect(@record.content).to be == "1.2.3.4"
21
+ end
22
+
23
+ it "can create dns record with proxied option" do
24
+ @record = zone.dns_records.create("A", subdomain, "1.2.3.4", proxied: true)
25
+ expect(@record.type).to be == "A"
26
+ expect(@record.name).to be_start_with subdomain
27
+ expect(@record.content).to be == "1.2.3.4"
28
+ expect(@record.proxied).to be_truthy
29
+ end
21
30
  end
22
31
 
23
32
  context "with existing record" do
33
+ let(:record) {@record = zone.dns_records.create("A", subdomain, "1.2.3.4")}
24
34
  it "can update dns content" do
25
35
  record.update_content("4.3.2.1")
26
36
  expect(record.content).to be == "4.3.2.1"
@@ -28,5 +38,13 @@ RSpec.describe Cloudflare::DNS, order: :defined, timeout: 30 do
28
38
  fetched_record = zone.dns_records.find_by_name(record.name)
29
39
  expect(fetched_record.content).to be == record.content
30
40
  end
41
+
42
+ it "can update dns content with proxied option" do
43
+ record.update_content("4.3.2.1", proxied: true)
44
+ expect(record.proxied).to be_truthy
45
+
46
+ fetched_record = zone.dns_records.find_by_name(record.name)
47
+ expect(fetched_record.proxied).to be_truthy
48
+ end
31
49
  end
32
50
  end
@@ -0,0 +1,71 @@
1
+
2
+ RSpec.describe Cloudflare::KV::Namespaces, kv_spec: true, order: :defined, timeout: 30 do
3
+ include_context Cloudflare::Account
4
+
5
+ let(:namespace) { @namespace = account.kv_namespaces.create(namespace_title) }
6
+ let(:namespace_title) { "Test NS ##{rand(1..100)}" }
7
+
8
+ after do
9
+ if defined? @namespace
10
+ expect(@namespace.delete).to be_success
11
+ end
12
+ end
13
+
14
+ it 'can create a namespace' do
15
+ expect(namespace).to be_kind_of Cloudflare::KV::Namespace
16
+ expect(namespace.id).not_to be_nil
17
+ expect(namespace.title).to eq namespace_title
18
+ end
19
+
20
+ it 'can find a namespace by title' do
21
+ namespace # Call this so that the namespace gets created
22
+ expect(account.kv_namespaces.find_by_title(namespace_title).id).to eq namespace.id
23
+ end
24
+
25
+ it 'can rename the namespace' do
26
+ new_title = "#{namespace_title}-#{rand(1..100)}"
27
+ namespace.rename(new_title)
28
+ expect(namespace.title).to eq new_title
29
+ expect(account.kv_namespaces.find_by_title(new_title).id).to eq namespace.id
30
+ expect(account.kv_namespaces.find_by_title(namespace_title)).to be_nil
31
+ end
32
+
33
+ it 'can store a key/value, read it back' do
34
+ key = "key-#{rand(1..100)}"
35
+ value = rand(100..999)
36
+ namespace.write_value(key, value)
37
+ expect(account.kv_namespaces.find_by_id(namespace.id).read_value(key)).to eq value.to_s
38
+ end
39
+
40
+ it 'can read a previously stored key' do
41
+ key = "key-#{rand(1..100)}"
42
+ value = rand(100..999)
43
+ expect(account.kv_namespaces.find_by_id(namespace.id).write_value(key, value)).to be true
44
+ expect(namespace.read_value(key)).to eq value.to_s
45
+ end
46
+
47
+ it 'can delete keys' do
48
+ key = "key-#{rand(1..100)}"
49
+ value = rand(100..999)
50
+ expect(namespace.write_value(key, value)).to be true
51
+ expect(namespace.read_value(key)).to eq value.to_s
52
+ expect(namespace.delete_value(key)).to be true
53
+ expect do
54
+ account.kv_namespaces.find_by_id(namespace.id).read_value(key)
55
+ end.to raise_error(Cloudflare::RequestError)
56
+ end
57
+
58
+ it 'can get the keys that exist in the namespace' do
59
+ counter = 0
60
+ keys = Array.new(rand(1..9)) { "key-#{counter += 1}" } # Keep this single digits so ordering works
61
+ keys.each_with_index do |key, i|
62
+ namespace.write_value(key, i)
63
+ end
64
+
65
+ saved_keys = account.kv_namespaces.find_by_id(namespace.id).keys.to_a
66
+ expect(saved_keys.length).to eq keys.length
67
+ saved_keys.each_with_index do |key, i|
68
+ expect(key.name).to eq keys[i]
69
+ end
70
+ end
71
+ end