overrides_tracker 0.1.9 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,227 @@
1
+ require 'spec_helper'
2
+ require 'webmock'
3
+ require 'vcr'
4
+
5
+ describe OverridesTracker::Api do
6
+ describe '.report_build' do
7
+ let(:api_token) { 'abc123' }
8
+ let(:branch_name) { 'master' }
9
+ let(:last_commit_id) { '123456' }
10
+ let(:last_commit_name) { 'Commit message' }
11
+ let(:file_path) { "#{Dir.pwd}/spec/result_files/master.otf" }
12
+
13
+ let(:uri) { URI(OverridesTracker::Api::API_DOMAIN) }
14
+ let(:client) { double('client') }
15
+ let(:request) { double('request') }
16
+ let(:file) { double('file') }
17
+ let(:response) { double('response') }
18
+ let(:file_hash) { double('file_hash') }
19
+
20
+ let(:form_data) do
21
+ [['api_token', api_token],
22
+ ['branch_name', branch_name],
23
+ ['build_provider_id', last_commit_id],
24
+ ['build_name', last_commit_name],
25
+ ['result_file', file],
26
+ ['file_hash', file_hash]
27
+ ]
28
+ end
29
+
30
+ before do
31
+ allow(OverridesTracker::Api).to receive(:build_client).and_return(client)
32
+ allow(Net::HTTP::Post).to receive(:new).and_return(request)
33
+ allow(request).to receive(:set_form).with(form_data, 'multipart/form-data')
34
+ allow(client).to receive(:request).and_return(response)
35
+ allow(response).to receive(:body).and_return('{"success": true}')
36
+ allow(File).to receive(:open).and_return(file)
37
+ allow(Digest::SHA256).to receive(:hexdigest).and_return(file_hash)
38
+ allow(described_class).to receive(:disable_net_blockers!)
39
+ end
40
+
41
+ context 'when reference build is not found' do
42
+ before do
43
+ allow(OverridesTracker::Api).to receive(:find_or_report_build).with(api_token, branch_name, last_commit_id, last_commit_name, file_hash).and_return(false)
44
+ end
45
+
46
+ it 'sends a POST request to the API with the correct form data' do
47
+ expect(request).to receive(:set_form).with(form_data, 'multipart/form-data')
48
+ expect(client).to receive(:request).with(request)
49
+
50
+ expect(OverridesTracker::Api.report_build(api_token, branch_name, last_commit_id, last_commit_name,
51
+ file_path)).to be true
52
+ end
53
+
54
+ context 'when the API request fails' do
55
+ before do
56
+ allow(client).to receive(:request).and_raise(SocketError)
57
+ end
58
+
59
+ it 'raises an error' do
60
+ expect(OverridesTracker::Api.report_build(api_token, branch_name, last_commit_id, last_commit_name,
61
+ file_path)).to be false
62
+ end
63
+ end
64
+ end
65
+
66
+ context 'when reference build is found' do
67
+ before do
68
+ allow(OverridesTracker::Api).to receive(:find_or_report_build).with(api_token, branch_name, last_commit_id, last_commit_name, file_hash).and_return(true)
69
+ end
70
+
71
+ it 'does not send a POST request to the API with the correct form data' do
72
+ expect(request).to_not receive(:set_form).with(form_data, 'multipart/form-data')
73
+ expect(client).to_not receive(:request).with(request)
74
+
75
+ expect(OverridesTracker::Api.report_build(api_token, branch_name, last_commit_id, last_commit_name,
76
+ file_path)).to be true
77
+ end
78
+ end
79
+ end
80
+
81
+ describe '.find_or_report_build' do
82
+ let(:api_token) { 'abc123' }
83
+ let(:branch_name) { 'master' }
84
+ let(:last_commit_id) { '123456' }
85
+ let(:last_commit_name) { 'Commit message' }
86
+ let(:file_path) { "#{Dir.pwd}/spec/result_files/master.otf" }
87
+
88
+ let(:uri) { URI(OverridesTracker::Api::API_DOMAIN) }
89
+ let(:client) { double('client') }
90
+ let(:request) { double('request') }
91
+ let(:file) { double('file') }
92
+ let(:response) { double('response') }
93
+ let(:file_hash) { double('file_hash') }
94
+
95
+ let(:form_data) do
96
+ [['api_token', api_token],
97
+ ['branch_name', branch_name],
98
+ ['build_provider_id', last_commit_id],
99
+ ['build_name', last_commit_name],
100
+ ['file_hash', file_hash]
101
+ ]
102
+ end
103
+
104
+ before do
105
+ allow(OverridesTracker::Api).to receive(:build_client).and_return(client)
106
+ allow(Net::HTTP::Post).to receive(:new).and_return(request)
107
+ allow(request).to receive(:set_form).with(form_data)
108
+ allow(client).to receive(:request).and_return(response)
109
+ allow(response).to receive(:body).and_return('{"success": true}')
110
+ allow(File).to receive(:open).and_return(file)
111
+ allow(Digest::SHA256).to receive(:hexdigest).and_return(file_hash)
112
+ allow(described_class).to receive(:disable_net_blockers!)
113
+ end
114
+
115
+ context 'when reference build is not found' do
116
+ before do
117
+ allow(response).to receive(:code).and_return('404')
118
+ end
119
+
120
+ it 'sends a POST request to the API with the correct form data and returns false' do
121
+ expect(request).to receive(:set_form).with(form_data)
122
+ expect(client).to receive(:request).with(request)
123
+
124
+ expect(OverridesTracker::Api.find_or_report_build(api_token, branch_name, last_commit_id, last_commit_name,
125
+ file_hash)).to be false
126
+ end
127
+ end
128
+
129
+ context 'when reference build is found' do
130
+ before do
131
+ allow(response).to receive(:code).and_return('200')
132
+ end
133
+
134
+ it 'sends a POST request to the API with the correct form data and returns true' do
135
+ expect(request).to receive(:set_form).with(form_data)
136
+ expect(client).to receive(:request).with(request)
137
+
138
+ expect(OverridesTracker::Api.find_or_report_build(api_token, branch_name, last_commit_id, last_commit_name,
139
+ file_hash)).to be true
140
+ end
141
+ end
142
+
143
+ context 'when the API request fails' do
144
+ before do
145
+ allow(client).to receive(:request).and_raise(SocketError)
146
+ end
147
+
148
+ it 'raises an error' do
149
+ expect(OverridesTracker::Api.find_or_report_build(api_token, branch_name, last_commit_id, last_commit_name,
150
+ file_hash)).to be false
151
+ end
152
+ end
153
+ end
154
+
155
+ describe '.build_client' do
156
+ context 'when the port is 443' do
157
+ it 'creates a new Net::HTTP object with SSL enabled' do
158
+ uri = URI('https://example.com')
159
+ client = OverridesTracker::Api.build_client(uri)
160
+
161
+ expect(client).to be_a(Net::HTTP)
162
+ expect(client.use_ssl?).to be(true)
163
+ end
164
+ end
165
+
166
+ context 'when the port is not 443' do
167
+ it 'creates a new Net::HTTP object with SSL disabled' do
168
+ uri = URI('http://example.com')
169
+ client = OverridesTracker::Api.build_client(uri)
170
+
171
+ expect(client).to be_a(Net::HTTP)
172
+ expect(client.use_ssl?).to be(false)
173
+ end
174
+ end
175
+ end
176
+
177
+ describe '#disable_net_blockers!' do
178
+ context 'when the WebMock library is loaded' do
179
+ it 'allows the API host' do
180
+ # Set up a stub for the WebMock Config instance to return an array of allowed hosts
181
+ allow(WebMock::Config.instance).to receive(:allow).and_return(OverridesTracker::Api::API_HOST)
182
+
183
+ # Call the disable_net_blockers! method
184
+ OverridesTracker::Api.send(:disable_net_blockers!)
185
+
186
+ # Verify that the API host was added to the array of allowed hosts
187
+ expect(WebMock::Config.instance.allow).to include(OverridesTracker::Api::API_HOST)
188
+ end
189
+ end
190
+
191
+ context 'when the VCR library is loaded' do
192
+ context 'when VCR version is 2 or greater' do
193
+ let(:version) { double('version') }
194
+
195
+ it 'ignores the API host' do
196
+ allow(VCR).to receive(:version).and_return(version)
197
+ allow(version).to receive(:major).and_return(2)
198
+ # Set up a stub for the VCR configure method
199
+ allow(VCR).to receive(:configure).and_call_original
200
+ # Call the disable_net_blockers! method
201
+ OverridesTracker::Api.send(:disable_net_blockers!)
202
+
203
+ # Verify that the VCR configure method was called with the API host added to the list of ignored hosts
204
+ expect(VCR).to have_received(:configure)
205
+ expect(VCR.configuration.ignore_hosts.first).to eq(OverridesTracker::Api::API_HOST)
206
+ end
207
+ end
208
+
209
+ context 'when VCR version is under 2' do
210
+ let(:version) { double('version') }
211
+
212
+ it 'ignores the API host' do
213
+ allow(VCR).to receive(:version).and_return(version)
214
+ allow(version).to receive(:major).and_return(1)
215
+ # Set up a stub for the VCR configure method
216
+ allow(VCR).to receive(:config).and_call_original
217
+ # Call the disable_net_blockers! method
218
+ OverridesTracker::Api.send(:disable_net_blockers!)
219
+
220
+ # Verify that the VCR config method was called with the API host added to the list of ignored hosts
221
+ expect(VCR).to have_received(:config)
222
+ expect(VCR.configuration.ignore_hosts.first).to eq(OverridesTracker::Api::API_HOST)
223
+ end
224
+ end
225
+ end
226
+ end
227
+ end