datadog_backup 3.0.0.alpha.2 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,24 +23,6 @@ describe DatadogBackup::Dashboards do
23
23
  'title' => 'foo'
24
24
  }
25
25
  end
26
- let(:all_dashboards) do
27
- [
28
- 200,
29
- {},
30
- {
31
- 'dashboards' => [
32
- dashboard_description
33
- ]
34
- }
35
- ]
36
- end
37
- let(:example_dashboard) do
38
- [
39
- 200,
40
- {},
41
- board_abc_123_def
42
- ]
43
- end
44
26
  let(:board_abc_123_def) do
45
27
  {
46
28
  'graphs' => [
@@ -61,6 +43,8 @@ describe DatadogBackup::Dashboards do
61
43
  'title' => 'example dashboard'
62
44
  }
63
45
  end
46
+ let(:all_dashboards) { respond_with200({ 'dashboards' => [dashboard_description] }) }
47
+ let(:example_dashboard) { respond_with200(board_abc_123_def) }
64
48
 
65
49
  before do
66
50
  stubs.get('/api/v1/dashboard') { all_dashboards }
@@ -71,25 +55,32 @@ describe DatadogBackup::Dashboards do
71
55
  subject { dashboards.backup }
72
56
 
73
57
  it 'is expected to create a file' do
74
- file = double('file')
58
+ file = instance_double(File)
75
59
  allow(File).to receive(:open).with(dashboards.filename('abc-123-def'), 'w').and_return(file)
76
- expect(file).to receive(:write).with(::JSON.pretty_generate(board_abc_123_def.deep_sort))
60
+ allow(file).to receive(:write)
77
61
  allow(file).to receive(:close)
78
62
 
79
63
  dashboards.backup
64
+ expect(file).to have_received(:write).with(::JSON.pretty_generate(board_abc_123_def.deep_sort))
80
65
  end
81
66
  end
82
67
 
83
- describe '#all_dashboards' do
84
- subject { dashboards.all_dashboards }
68
+ describe '#filename' do
69
+ subject { dashboards.filename('abc-123-def') }
70
+
71
+ it { is_expected.to eq("#{tempdir}/dashboards/abc-123-def.json") }
72
+ end
73
+
74
+ describe '#get_by_id' do
75
+ subject { dashboards.get_by_id('abc-123-def') }
85
76
 
86
- it { is_expected.to eq [dashboard_description] }
77
+ it { is_expected.to eq board_abc_123_def }
87
78
  end
88
79
 
89
80
  describe '#diff' do
90
81
  it 'calls the api only once' do
91
82
  dashboards.write_file('{"a":"b"}', dashboards.filename('abc-123-def'))
92
- expect(dashboards.diff('abc-123-def')).to eq(<<~EOF
83
+ expect(dashboards.diff('abc-123-def')).to eq(<<~EODASH
93
84
  ---
94
85
  -description: example dashboard
95
86
  -graphs:
@@ -101,8 +92,8 @@ describe DatadogBackup::Dashboards do
101
92
  - title: example graph
102
93
  -title: example dashboard
103
94
  +a: b
104
- EOF
105
- )
95
+ EODASH
96
+ .chomp)
106
97
  end
107
98
  end
108
99
 
@@ -111,10 +102,4 @@ describe DatadogBackup::Dashboards do
111
102
 
112
103
  it { is_expected.to eq({ a: :b }) }
113
104
  end
114
-
115
- describe '#get_by_id' do
116
- subject { dashboards.get_by_id('abc-123-def') }
117
-
118
- it { is_expected.to eq board_abc_123_def }
119
- end
120
105
  end
@@ -3,7 +3,9 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe DatadogBackup::Deprecations do
6
- let(:logger) { double }
6
+ subject(:check) { described_class.check }
7
+
8
+ let(:logger) { instance_double(Logger) }
7
9
 
8
10
  before do
9
11
  stub_const('LOGGER', logger)
@@ -12,24 +14,20 @@ describe DatadogBackup::Deprecations do
12
14
 
13
15
  %w[2.4.10 2.5.9 2.6.8].each do |ruby_version|
14
16
  describe "#check#{ruby_version}" do
15
- subject { described_class.check }
16
-
17
17
  it 'does warn' do
18
18
  stub_const('RUBY_VERSION', ruby_version)
19
- expect(logger).to receive(:warn).with(/ruby-#{ruby_version} is deprecated./)
20
- subject
19
+ check
20
+ expect(logger).to have_received(:warn).with(/ruby-#{ruby_version} is deprecated./)
21
21
  end
22
22
  end
23
23
  end
24
24
 
25
25
  %w[2.7.4 3.0.4 3.1.2 3.2.0-preview1].each do |ruby_version|
26
26
  describe "#check#{ruby_version}" do
27
- subject { described_class.check }
28
-
29
27
  it 'does not warn' do
30
28
  stub_const('RUBY_VERSION', ruby_version)
31
- expect(logger).to_not receive(:warn).with(/ruby-#{ruby_version} is deprecated./)
32
- subject
29
+ check
30
+ expect(logger).not_to have_received(:warn).with(/ruby-#{ruby_version} is deprecated./)
33
31
  end
34
32
  end
35
33
  end
@@ -4,16 +4,16 @@ require 'spec_helper'
4
4
 
5
5
  describe DatadogBackup::LocalFilesystem do
6
6
  let(:tempdir) { Dir.mktmpdir }
7
- let(:core) do
8
- DatadogBackup::Core.new(
7
+ let(:resources) do
8
+ DatadogBackup::Resources.new(
9
9
  action: 'backup',
10
10
  backup_dir: tempdir,
11
11
  resources: [DatadogBackup::Dashboards],
12
12
  output_format: :json
13
13
  )
14
14
  end
15
- let(:core_yaml) do
16
- DatadogBackup::Core.new(
15
+ let(:resources_yaml) do
16
+ DatadogBackup::Resources.new(
17
17
  action: 'backup',
18
18
  backup_dir: tempdir,
19
19
  resources: [],
@@ -22,7 +22,7 @@ describe DatadogBackup::LocalFilesystem do
22
22
  end
23
23
 
24
24
  describe '#all_files' do
25
- subject { core.all_files }
25
+ subject { resources.all_files }
26
26
 
27
27
  before do
28
28
  File.new("#{tempdir}/all_files.json", 'w')
@@ -36,7 +36,7 @@ describe DatadogBackup::LocalFilesystem do
36
36
  end
37
37
 
38
38
  describe '#all_file_ids_for_selected_resources' do
39
- subject { core.all_file_ids_for_selected_resources }
39
+ subject { resources.all_file_ids_for_selected_resources }
40
40
 
41
41
  before do
42
42
  Dir.mkdir("#{tempdir}/dashboards")
@@ -54,49 +54,49 @@ describe DatadogBackup::LocalFilesystem do
54
54
  end
55
55
 
56
56
  describe '#class_from_id' do
57
- subject { core.class_from_id('abc-123-def') }
57
+ subject { resources.class_from_id('abc-123-def') }
58
58
 
59
59
  before do
60
- core.write_file('abc', "#{tempdir}/core/abc-123-def.json")
60
+ resources.write_file('abc', "#{tempdir}/resources/abc-123-def.json")
61
61
  end
62
62
 
63
63
  after do
64
- FileUtils.rm "#{tempdir}/core/abc-123-def.json"
64
+ FileUtils.rm "#{tempdir}/resources/abc-123-def.json"
65
65
  end
66
66
 
67
- it { is_expected.to eq DatadogBackup::Core }
67
+ it { is_expected.to eq DatadogBackup::Resources }
68
68
  end
69
69
 
70
70
  describe '#dump' do
71
- context ':json' do
72
- subject { core.dump({ a: :b }) }
71
+ context 'when mode is :json' do
72
+ subject { resources.dump({ a: :b }) }
73
73
 
74
74
  it { is_expected.to eq(%({\n "a": "b"\n})) }
75
75
  end
76
76
 
77
- context ':yaml' do
78
- subject { core_yaml.dump({ 'a' => 'b' }) }
77
+ context 'when mode is :yaml' do
78
+ subject { resources_yaml.dump({ 'a' => 'b' }) }
79
79
 
80
80
  it { is_expected.to eq(%(---\na: b\n)) }
81
81
  end
82
82
  end
83
83
 
84
84
  describe '#filename' do
85
- context ':json' do
86
- subject { core.filename('abc-123-def') }
85
+ context 'when mode is :json' do
86
+ subject { resources.filename('abc-123-def') }
87
87
 
88
- it { is_expected.to eq("#{tempdir}/core/abc-123-def.json") }
88
+ it { is_expected.to eq("#{tempdir}/resources/abc-123-def.json") }
89
89
  end
90
90
 
91
- context ':yaml' do
92
- subject { core_yaml.filename('abc-123-def') }
91
+ context 'when mode is :yaml' do
92
+ subject { resources_yaml.filename('abc-123-def') }
93
93
 
94
- it { is_expected.to eq("#{tempdir}/core/abc-123-def.yaml") }
94
+ it { is_expected.to eq("#{tempdir}/resources/abc-123-def.yaml") }
95
95
  end
96
96
  end
97
97
 
98
98
  describe '#file_type' do
99
- subject { core.file_type("#{tempdir}/file_type.json") }
99
+ subject { resources.file_type("#{tempdir}/file_type.json") }
100
100
 
101
101
  before do
102
102
  File.new("#{tempdir}/file_type.json", 'w')
@@ -110,7 +110,7 @@ describe DatadogBackup::LocalFilesystem do
110
110
  end
111
111
 
112
112
  describe '#find_file_by_id' do
113
- subject { core.find_file_by_id('find_file') }
113
+ subject { resources.find_file_by_id('find_file') }
114
114
 
115
115
  before do
116
116
  File.new("#{tempdir}/find_file.json", 'w')
@@ -124,63 +124,63 @@ describe DatadogBackup::LocalFilesystem do
124
124
  end
125
125
 
126
126
  describe '#load_from_file' do
127
- context ':json' do
128
- subject { core.load_from_file(%({\n "a": "b"\n}), :json) }
127
+ context 'when mode is :json' do
128
+ subject { resources.load_from_file(%({\n "a": "b"\n}), :json) }
129
129
 
130
130
  it { is_expected.to eq('a' => 'b') }
131
131
  end
132
132
 
133
- context ':yaml' do
134
- subject { core.load_from_file(%(---\na: b\n), :yaml) }
133
+ context 'when mode is :yaml' do
134
+ subject { resources.load_from_file(%(---\na: b\n), :yaml) }
135
135
 
136
136
  it { is_expected.to eq('a' => 'b') }
137
137
  end
138
138
  end
139
139
 
140
140
  describe '#load_from_file_by_id' do
141
- context 'written in json read in yaml mode' do
142
- subject { core_yaml.load_from_file_by_id('abc-123-def') }
141
+ context 'when the backup is in json but the mode is :yaml' do
142
+ subject { resources_yaml.load_from_file_by_id('abc-123-def') }
143
143
 
144
- before { core.write_file(%({"a": "b"}), "#{tempdir}/core/abc-123-def.json") }
144
+ before { resources.write_file(%({"a": "b"}), "#{tempdir}/resources/abc-123-def.json") }
145
145
 
146
- after { FileUtils.rm "#{tempdir}/core/abc-123-def.json" }
146
+ after { FileUtils.rm "#{tempdir}/resources/abc-123-def.json" }
147
147
 
148
148
  it { is_expected.to eq('a' => 'b') }
149
149
  end
150
150
 
151
- context 'written in yaml read in json mode' do
152
- subject { core.load_from_file_by_id('abc-123-def') }
151
+ context 'when the backup is in yaml but the mode is :json' do
152
+ subject { resources.load_from_file_by_id('abc-123-def') }
153
153
 
154
- before { core.write_file(%(---\na: b), "#{tempdir}/core/abc-123-def.yaml") }
154
+ before { resources.write_file(%(---\na: b), "#{tempdir}/resources/abc-123-def.yaml") }
155
155
 
156
- after { FileUtils.rm "#{tempdir}/core/abc-123-def.yaml" }
156
+ after { FileUtils.rm "#{tempdir}/resources/abc-123-def.yaml" }
157
157
 
158
158
  it { is_expected.to eq('a' => 'b') }
159
159
  end
160
160
 
161
- context 'Integer as parameter' do
162
- subject { core.load_from_file_by_id(12_345) }
161
+ context 'with Integer as parameter' do
162
+ subject { resources.load_from_file_by_id(12_345) }
163
163
 
164
- before { core.write_file(%(---\na: b), "#{tempdir}/core/12345.yaml") }
164
+ before { resources.write_file(%(---\na: b), "#{tempdir}/resources/12345.yaml") }
165
165
 
166
- after { FileUtils.rm "#{tempdir}/core/12345.yaml" }
166
+ after { FileUtils.rm "#{tempdir}/resources/12345.yaml" }
167
167
 
168
168
  it { is_expected.to eq('a' => 'b') }
169
169
  end
170
170
  end
171
171
 
172
172
  describe '#write_file' do
173
- subject { core.write_file('abc123', "#{tempdir}/core/abc-123-def.json") }
173
+ subject(:write_file) { resources.write_file('abc123', "#{tempdir}/resources/abc-123-def.json") }
174
174
 
175
- let(:file_like_object) { double }
175
+ let(:file_like_object) { instance_double(File) }
176
176
 
177
177
  it 'writes a file to abc-123-def.json' do
178
178
  allow(File).to receive(:open).and_call_original
179
- allow(File).to receive(:open).with("#{tempdir}/core/abc-123-def.json", 'w').and_return(file_like_object)
179
+ allow(File).to receive(:open).with("#{tempdir}/resources/abc-123-def.json", 'w').and_return(file_like_object)
180
180
  allow(file_like_object).to receive(:write)
181
181
  allow(file_like_object).to receive(:close)
182
182
 
183
- subject
183
+ write_file
184
184
 
185
185
  expect(file_like_object).to have_received(:write).with('abc123')
186
186
  end
@@ -16,7 +16,6 @@ describe DatadogBackup::Monitors do
16
16
  allow(monitors).to receive(:api_service).and_return(api_client_double)
17
17
  return monitors
18
18
  end
19
-
20
19
  let(:monitor_description) do
21
20
  {
22
21
  'query' => 'bar',
@@ -35,30 +34,16 @@ describe DatadogBackup::Monitors do
35
34
  'query' => 'bar'
36
35
  }
37
36
  end
38
- let(:all_monitors) do
39
- [
40
- 200,
41
- {},
42
- [
43
- monitor_description
44
- ]
45
- ]
46
- end
47
- let(:example_monitor) do
48
- [
49
- 200,
50
- {},
51
- monitor_description
52
- ]
53
- end
37
+ let(:all_monitors) { respond_with200([monitor_description]) }
38
+ let(:example_monitor) { respond_with200(monitor_description) }
54
39
 
55
40
  before do
56
41
  stubs.get('/api/v1/monitor') { all_monitors }
57
42
  stubs.get('/api/v1/dashboard/123455') { example_monitor }
58
43
  end
59
44
 
60
- describe '#all_monitors' do
61
- subject { monitors.all_monitors }
45
+ describe '#get_all' do
46
+ subject { monitors.get_all }
62
47
 
63
48
  it { is_expected.to eq [monitor_description] }
64
49
  end
@@ -67,22 +52,21 @@ describe DatadogBackup::Monitors do
67
52
  subject { monitors.backup }
68
53
 
69
54
  it 'is expected to create a file' do
70
- file = double('file')
55
+ file = instance_double(File)
71
56
  allow(File).to receive(:open).with(monitors.filename(123_455), 'w').and_return(file)
72
- expect(file).to receive(:write).with(::JSON.pretty_generate(clean_monitor_description))
57
+ allow(file).to receive(:write)
73
58
  allow(file).to receive(:close)
74
59
 
75
60
  monitors.backup
61
+ expect(file).to have_received(:write).with(::JSON.pretty_generate(clean_monitor_description))
76
62
  end
77
63
  end
78
64
 
79
65
  describe '#diff and #except' do
80
66
  example 'it ignores `overall_state` and `overall_state_modified`' do
81
67
  monitors.write_file(monitors.dump(monitor_description), monitors.filename(123_455))
82
- stubs.get('/api/v1/dashboard/123455') {
83
- [
84
- 200,
85
- {},
68
+ stubs.get('/api/v1/dashboard/123455') do
69
+ respond_with200(
86
70
  [
87
71
  {
88
72
  'query' => 'bar',
@@ -93,8 +77,8 @@ describe DatadogBackup::Monitors do
93
77
  'overall_state_modified' => '9999-07-27T22:55:55+00:00'
94
78
  }
95
79
  ]
96
- ]
97
- }
80
+ )
81
+ end
98
82
 
99
83
  expect(monitors.diff(123_455)).to eq ''
100
84
 
@@ -109,13 +93,13 @@ describe DatadogBackup::Monitors do
109
93
  end
110
94
 
111
95
  describe '#get_by_id' do
112
- context 'Integer' do
96
+ context 'when Integer' do
113
97
  subject { monitors.get_by_id(123_455) }
114
98
 
115
99
  it { is_expected.to eq monitor_description }
116
100
  end
117
101
 
118
- context 'String' do
102
+ context 'when String' do
119
103
  subject { monitors.get_by_id('123455') }
120
104
 
121
105
  it { is_expected.to eq monitor_description }
@@ -0,0 +1,258 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe DatadogBackup::Synthetics do
6
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
7
+ let(:api_client_double) { Faraday.new { |f| f.adapter :test, stubs } }
8
+ let(:tempdir) { Dir.mktmpdir } # TODO: delete afterward
9
+ let(:synthetics) do
10
+ synthetics = described_class.new(
11
+ action: 'backup',
12
+ backup_dir: tempdir,
13
+ output_format: :json,
14
+ resources: []
15
+ )
16
+ allow(synthetics).to receive(:api_service).and_return(api_client_double)
17
+ return synthetics
18
+ end
19
+ let(:api_test) do
20
+ { 'config' => { 'assertions' => [{ 'operator' => 'contains', 'property' => 'set-cookie', 'target' => '_user_id', 'type' => 'header' },
21
+ { 'operator' => 'contains', 'target' => 'body message', 'type' => 'body' },
22
+ { 'operator' => 'is', 'property' => 'content-type', 'target' => 'text/html; charset=utf-8', 'type' => 'header' },
23
+ { 'operator' => 'is', 'target' => 200, 'type' => 'statusCode' },
24
+ { 'operator' => 'lessThan', 'target' => 5000, 'type' => 'responseTime' }],
25
+ 'request' => { 'headers' => { 'User-Agent' => 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:65.0) Gecko/20100101 Firefox/65.0',
26
+ 'cookie' => '_a=12345; _example_session=abc123' },
27
+ 'method' => 'GET',
28
+ 'url' => 'https://www.example.com/' } },
29
+ 'creator' => { 'email' => 'user@example.com', 'handle' => 'user@example.com', 'name' => 'Hugh Zer' },
30
+ 'locations' => ['aws:ap-northeast-1', 'aws:eu-central-1', 'aws:eu-west-2', 'aws:us-west-2'],
31
+ 'message' => 'TEST: This is a test',
32
+ 'monitor_id' => 12_345,
33
+ 'name' => 'TEST: This is a test',
34
+ 'options' => { 'follow_redirects' => true,
35
+ 'httpVersion' => 'http1',
36
+ 'min_failure_duration' => 120,
37
+ 'min_location_failed' => 2,
38
+ 'monitor_options' => { 'renotify_interval' => 0 },
39
+ 'monitor_priority' => 1,
40
+ 'retry' => { 'count' => 1, 'interval' => 500 },
41
+ 'tick_every' => 120 },
42
+ 'public_id' => 'abc-123-def',
43
+ 'status' => 'live',
44
+ 'subtype' => 'http',
45
+ 'tags' => ['env:test'],
46
+ 'type' => 'api' }
47
+ end
48
+ let(:browser_test) do
49
+ { 'config' => { 'assertions' => [],
50
+ 'configVariables' => [],
51
+ 'request' => { 'headers' => {}, 'method' => 'GET', 'url' => 'https://www.example.com' },
52
+ 'setCookie' => nil,
53
+ 'variables' => [] },
54
+ 'creator' => { 'email' => 'user@example.com',
55
+ 'handle' => 'user@example.com',
56
+ 'name' => 'Hugh Zer' },
57
+ 'locations' => ['aws:us-east-2'],
58
+ 'message' => 'Test message',
59
+ 'monitor_id' => 12_345,
60
+ 'name' => 'www.example.com',
61
+ 'options' => { 'ci' => { 'executionRule' => 'non_blocking' },
62
+ 'device_ids' => ['chrome.laptop_large', 'chrome.mobile_small'],
63
+ 'disableCors' => false,
64
+ 'disableCsp' => false,
65
+ 'ignoreServerCertificateError' => false,
66
+ 'min_failure_duration' => 300,
67
+ 'min_location_failed' => 1,
68
+ 'monitor_options' => { 'renotify_interval' => 0 },
69
+ 'noScreenshot' => false,
70
+ 'retry' => { 'count' => 0, 'interval' => 1000 },
71
+ 'tick_every' => 900 },
72
+ 'public_id' => '456-ghi-789',
73
+ 'status' => 'live',
74
+ 'tags' => ['env:test'],
75
+ 'type' => 'browser' }
76
+ end
77
+ let(:all_synthetics) { respond_with200({ 'tests' => [api_test, browser_test] }) }
78
+ let(:api_synthetic) { respond_with200(api_test) }
79
+ let(:browser_synthetic) { respond_with200(browser_test) }
80
+
81
+ before do
82
+ stubs.get('/api/v1/synthetics/tests') { all_synthetics }
83
+ stubs.get('/api/v1/synthetics/tests/api/abc-123-def') { api_synthetic }
84
+ stubs.get('/api/v1/synthetics/tests/browser/456-ghi-789') { browser_synthetic }
85
+ end
86
+
87
+ describe '#all' do
88
+ subject { synthetics.all }
89
+
90
+ it { is_expected.to contain_exactly(api_test, browser_test) }
91
+ end
92
+
93
+ describe '#backup' do
94
+ subject(:backup) { synthetics.backup }
95
+
96
+ let(:apifile) { instance_double(File) }
97
+ let(:browserfile) { instance_double(File) }
98
+
99
+ before do
100
+ allow(File).to receive(:open).with(synthetics.filename('abc-123-def'), 'w').and_return(apifile)
101
+ allow(File).to receive(:open).with(synthetics.filename('456-ghi-789'), 'w').and_return(browserfile)
102
+ allow(apifile).to receive(:write)
103
+ allow(apifile).to receive(:close)
104
+ allow(browserfile).to receive(:write)
105
+ allow(browserfile).to receive(:close)
106
+ end
107
+
108
+ it 'is expected to write the API test' do
109
+ backup
110
+ expect(apifile).to have_received(:write).with(::JSON.pretty_generate(api_test))
111
+ end
112
+
113
+ it 'is expected to write the browser test' do
114
+ backup
115
+ expect(browserfile).to have_received(:write).with(::JSON.pretty_generate(browser_test))
116
+ end
117
+ end
118
+
119
+ describe '#filename' do
120
+ subject { synthetics.filename('abc-123-def') }
121
+
122
+ it { is_expected.to eq("#{tempdir}/synthetics/abc-123-def.json") }
123
+ end
124
+
125
+ describe '#get_by_id' do
126
+ context 'when the type is api' do
127
+ subject { synthetics.get_by_id('abc-123-def') }
128
+
129
+ it { is_expected.to eq api_test }
130
+ end
131
+
132
+ context 'when the type is browser' do
133
+ subject { synthetics.get_by_id('456-ghi-789') }
134
+
135
+ it { is_expected.to eq browser_test }
136
+ end
137
+ end
138
+
139
+ describe '#diff' do # TODO: migrate to resources_spec.rb, since #diff is not defined here.
140
+ subject { synthetics.diff('abc-123-def') }
141
+
142
+ before do
143
+ synthetics.write_file(synthetics.dump(api_test), synthetics.filename('abc-123-def'))
144
+ end
145
+
146
+ context 'when the test is identical' do
147
+ it { is_expected.to be_empty }
148
+ end
149
+
150
+ context 'when the remote is not found' do
151
+ subject(:invalid_diff) { synthetics.diff('invalid-id') }
152
+
153
+ before do
154
+ synthetics.write_file(synthetics.dump({ 'name' => 'invalid-diff' }), synthetics.filename('invalid-id'))
155
+ end
156
+
157
+ it {
158
+ expect(invalid_diff).to eq(%(---- {}\n+---\n+name: invalid-diff))
159
+ }
160
+ end
161
+
162
+ context 'when there is a local update' do
163
+ before do
164
+ different_test = api_test.dup
165
+ different_test['message'] = 'Different message'
166
+ synthetics.write_file(synthetics.dump(different_test), synthetics.filename('abc-123-def'))
167
+ end
168
+
169
+ it { is_expected.to include(%(-message: 'TEST: This is a test'\n+message: Different message)) }
170
+ end
171
+ end
172
+
173
+ describe '#create' do
174
+ context 'when the type is api' do
175
+ subject(:create) { synthetics.create({ 'type' => 'api' }) }
176
+
177
+ before do
178
+ stubs.post('/api/v1/synthetics/tests/api') { respond_with200({ 'public_id' => 'api-create-abc' }) }
179
+ end
180
+
181
+ it { is_expected.to eq({ 'public_id' => 'api-create-abc' }) }
182
+ end
183
+
184
+ context 'when the type is browser' do
185
+ subject(:create) { synthetics.create({ 'type' => 'browser' }) }
186
+
187
+ before do
188
+ stubs.post('/api/v1/synthetics/tests/browser') { respond_with200({ 'public_id' => 'browser-create-abc' }) }
189
+ end
190
+
191
+ it { is_expected.to eq({ 'public_id' => 'browser-create-abc' }) }
192
+ end
193
+ end
194
+
195
+ describe '#update' do
196
+ context 'when the type is api' do
197
+ subject(:update) { synthetics.update('api-update-abc', { 'type' => 'api' }) }
198
+
199
+ before do
200
+ stubs.put('/api/v1/synthetics/tests/api/api-update-abc') { respond_with200({ 'public_id' => 'api-update-abc' }) }
201
+ end
202
+
203
+ it { is_expected.to eq({ 'public_id' => 'api-update-abc' }) }
204
+ end
205
+
206
+ context 'when the type is browser' do
207
+ subject(:update) { synthetics.update('browser-update-abc', { 'type' => 'browser' }) }
208
+
209
+ before do
210
+ stubs.put('/api/v1/synthetics/tests/browser/browser-update-abc') { respond_with200({ 'public_id' => 'browser-update-abc' }) }
211
+ end
212
+
213
+ it { is_expected.to eq({ 'public_id' => 'browser-update-abc' }) }
214
+ end
215
+ end
216
+
217
+ describe '#restore' do
218
+ context 'when the id exists' do
219
+ subject { synthetics.restore('abc-123-def') }
220
+
221
+ before do
222
+ synthetics.write_file(synthetics.dump({ 'name' => 'restore-valid-id', 'type' => 'api' }), synthetics.filename('abc-123-def'))
223
+ stubs.put('/api/v1/synthetics/tests/api/abc-123-def') { respond_with200({ 'public_id' => 'abc-123-def', 'type' => 'api' }) }
224
+ end
225
+
226
+ it { is_expected.to eq({ 'public_id' => 'abc-123-def', 'type' => 'api' }) }
227
+ end
228
+
229
+ context 'when the id does not exist' do
230
+ subject(:restore) { synthetics.restore('restore-invalid-id') }
231
+
232
+ before do
233
+ synthetics.write_file(synthetics.dump({ 'name' => 'restore-invalid-id', 'type' => 'api' }), synthetics.filename('restore-invalid-id'))
234
+ stubs.put('/api/v1/synthetics/tests/api/restore-invalid-id') { [404, {}, ''] }
235
+ stubs.post('/api/v1/synthetics/tests/api') { respond_with200({ 'public_id' => 'restore-valid-id' }) }
236
+ allow(synthetics).to receive(:create).and_call_original
237
+ allow(synthetics).to receive(:all).and_return([api_test, browser_test, { 'public_id' => 'restore-valid-id', 'type' => 'api' }])
238
+ end
239
+
240
+ it { is_expected.to eq({ 'type' => 'api' }) }
241
+
242
+ it 'calls create with the contents of the original file' do
243
+ restore
244
+ expect(synthetics).to have_received(:create).with({ 'name' => 'restore-invalid-id', 'type' => 'api' })
245
+ end
246
+
247
+ it 'deletes the original file' do
248
+ restore
249
+ expect(File.exist?(synthetics.filename('restore-invalid-id'))).to be false
250
+ end
251
+
252
+ it 'creates a new file with the restored contents' do
253
+ restore
254
+ expect(File.exist?(synthetics.filename('restore-valid-id'))).to be true
255
+ end
256
+ end
257
+ end
258
+ end