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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
# frozen_string_literal: true
require 'fast_spec_helper'
require 'rspec-parameterized'
load File.expand_path('../../bin/audit-event-type', __dir__)
RSpec.describe 'bin/audit-event-type' do
using RSpec::Parameterized::TableSyntax
describe AuditEventTypeCreator do
let(:argv) { %w[test_audit_event -d test -g govern::compliance -s -t -i https://url -m http://url] }
let(:options) { AuditEventTypeOptionParser.parse(argv) }
let(:creator) { described_class.new(options) }
let(:existing_audit_event_types) do
{ 'existing_audit_event_type' => File.join('config', 'audit_events', 'types', 'existing_audit_event_type.yml') }
end
before do
allow(creator).to receive(:all_audit_event_type_names) { existing_audit_event_types }
allow(creator).to receive(:branch_name).and_return('feature-branch')
allow(creator).to receive(:editor).and_return(nil)
# ignore writes
allow(File).to receive(:write).and_return(true)
# ignore stdin
allow(Readline).to receive(:readline).and_raise('EOF')
end
subject(:create_audit_event_type) { creator.execute }
it 'properly creates an audit event type' do
expect(File).to receive(:write).with(
File.join('config', 'audit_events', 'types', 'test_audit_event.yml'),
anything)
expect do
create_audit_event_type
end.to output(/name: test_audit_event/).to_stdout
end
context 'when running on master' do
it 'requires feature branch' do
expect(creator).to receive(:branch_name).and_return('master')
expect { create_audit_event_type }.to raise_error(AuditEventTypeHelpers::Abort, /Create a branch first/)
end
end
context 'with invalid audit event type names' do
where(:argv, :ex) do
%w[.invalid.audit.type] | /Provide a name for the audit event type that is/
%w[existing_audit_event_type] | /already exists!/
end
with_them do
it do
expect { create_audit_event_type }.to raise_error(ex)
end
end
end
end
describe AuditEventTypeOptionParser do
describe '.parse' do
where(:param, :argv, :result) do
:name | %w[foo] | 'foo'
:amend | %w[foo --amend] | true
:force | %w[foo -f] | true
:force | %w[foo --force] | true
:description | %w[foo -d desc] | 'desc'
:description | %w[foo --description desc] | 'desc'
:group | %w[foo -g govern::compliance] | 'govern::compliance'
:group | %w[foo --group govern::compliance] | 'govern::compliance'
:milestone | %w[foo -M 15.6] | '15.6'
:milestone | %w[foo --milestone 15.6] | '15.6'
:saved_to_database | %w[foo -s] | true
:saved_to_database | %w[foo --saved-to-database] | true
:saved_to_database | %w[foo --no-saved-to-database] | false
:streamed | %w[foo -t] | true
:streamed | %w[foo --streamed] | true
:streamed | %w[foo --no-streamed] | false
:dry_run | %w[foo -n] | true
:dry_run | %w[foo --dry-run] | true
:ee | %w[foo -e] | true
:ee | %w[foo --ee] | true
:jh | %w[foo -j] | true
:jh | %w[foo --jh] | true
:introduced_by_mr | %w[foo -m https://url] | 'https://url'
:introduced_by_mr | %w[foo --introduced-by-mr https://url] | 'https://url'
:introduced_by_issue | %w[foo -i https://url] | 'https://url'
:introduced_by_issue | %w[foo --introduced-by-issue https://url] | 'https://url'
end
with_them do
it do
options = described_class.parse(Array(argv))
expect(options.public_send(param)).to eq(result)
end
end
it 'raises an error when name of the audit event type is missing' do
expect do
expect do
described_class.parse(%w[--amend])
end.to output(/Name for the type of audit event is required/).to_stdout
end.to raise_error(AuditEventTypeHelpers::Abort)
end
it 'parses -h' do
expect do
expect { described_class.parse(%w[foo -h]) }.to output(%r{Usage: bin/audit-event-type}).to_stdout
end.to raise_error(AuditEventTypeHelpers::Done)
end
end
describe '.read_description' do
let(:description) { 'This is a test description for an audit event type.' }
it 'reads description from stdin' do
expect(Readline).to receive(:readline).and_return(description)
expect do
expect(described_class.read_description).to eq('This is a test description for an audit event type.')
end.to output(/Specify a human-readable description of how this event is triggered:/).to_stdout
end
context 'when description is empty' do
let(:description) { '' }
it 'shows error message and retries' do
expect(Readline).to receive(:readline).and_return(description)
expect(Readline).to receive(:readline).and_raise('EOF')
expect do
expect { described_class.read_description }.to raise_error(/EOF/)
end.to output(/Specify a human-readable description of how this event is triggered:/)
.to_stdout.and output(/description is a required field/).to_stderr
end
end
end
describe '.read_group' do
let(:group) { 'govern::compliance' }
it 'reads group from stdin' do
expect(Readline).to receive(:readline).and_return(group)
expect do
expect(described_class.read_group).to eq('govern::compliance')
end.to output(/Specify the group introducing the audit event type, like `govern::compliance`:/).to_stdout
end
context 'when group is empty' do
let(:group) { '' }
it 'shows error message and retries' do
expect(Readline).to receive(:readline).and_return(group)
expect(Readline).to receive(:readline).and_raise('EOF')
expect do
expect { described_class.read_group }.to raise_error(/EOF/)
end.to output(/Specify the group introducing the audit event type, like `govern::compliance`:/)
.to_stdout.and output(/group is a required field/).to_stderr
end
end
end
describe '.read_saved_to_database' do
let(:saved_to_database) { 'true' }
it 'reads saved_to_database from stdin' do
expect(Readline).to receive(:readline).and_return(saved_to_database)
expect do
expect(described_class.read_saved_to_database).to eq(true)
end.to output(/Specify whether to persist events to database and JSON logs \[yes, no\]:/).to_stdout
end
context 'when saved_to_database is invalid' do
let(:saved_to_database) { 'non boolean value' }
it 'shows error message and retries' do
expect(Readline).to receive(:readline).and_return(saved_to_database)
expect(Readline).to receive(:readline).and_raise('EOF')
expect do
expect { described_class.read_saved_to_database }.to raise_error(/EOF/)
end.to output(/Specify whether to persist events to database and JSON logs \[yes, no\]:/)
.to_stdout.and output(/saved_to_database is a required boolean field/).to_stderr
end
end
end
describe '.read_streamed' do
let(:streamed) { 'true' }
it 'reads streamed from stdin' do
expect(Readline).to receive(:readline).and_return(streamed)
expect do
expect(described_class.read_streamed).to eq(true)
end.to output(/Specify if events should be streamed to external services \(if configured\) \[yes, no\]:/)
.to_stdout
end
context 'when streamed is invalid' do
let(:streamed) { 'non boolean value' }
it 'shows error message and retries' do
expect(Readline).to receive(:readline).and_return(streamed)
expect(Readline).to receive(:readline).and_raise('EOF')
expect do
expect { described_class.read_streamed }.to raise_error(/EOF/)
end.to output(/Specify if events should be streamed to external services \(if configured\) \[yes, no\]:/)
.to_stdout.and output(/streamed is a required boolean field/).to_stderr
end
end
end
describe '.read_introduced_by_mr' do
let(:url) { 'https://merge-request' }
it 'reads introduced_by_mr from stdin' do
expect(Readline).to receive(:readline).and_return(url)
expect do
expect(described_class.read_introduced_by_mr).to eq('https://merge-request')
end.to output(/URL to GitLab merge request that added this type of audit event:/).to_stdout
end
context 'when URL is empty' do
let(:url) { '' }
it 'does not raise an error' do
expect(Readline).to receive(:readline).and_return(url)
expect do
expect(described_class.read_introduced_by_mr).to be_nil
end.to output(/URL to GitLab merge request that added this type of audit event:/).to_stdout
end
end
context 'when URL is invalid' do
let(:url) { 'invalid' }
it 'shows error message and retries' do
expect(Readline).to receive(:readline).and_return(url)
expect(Readline).to receive(:readline).and_raise('EOF')
expect do
expect { described_class.read_introduced_by_mr }.to raise_error(/EOF/)
end.to output(/URL to GitLab merge request that added this type of audit event:/)
.to_stdout.and output(/URL needs to start with https/).to_stderr
end
end
end
describe '.read_introduced_by_issue' do
let(:url) { 'https://issue' }
it 'reads type from stdin' do
expect(Readline).to receive(:readline).and_return(url)
expect do
expect(described_class.read_introduced_by_issue).to eq('https://issue')
end.to output(/URL to GitLab issue that added this type of audit event:/).to_stdout
end
context 'when URL is invalid' do
let(:type) { 'invalid' }
it 'shows error message and retries' do
expect(Readline).to receive(:readline).and_return(type)
expect(Readline).to receive(:readline).and_raise('EOF')
expect do
expect { described_class.read_introduced_by_issue }.to raise_error(/EOF/)
end.to output(/URL to GitLab issue that added this type of audit event:/)
.to_stdout.and output(/URL needs to start with https/).to_stderr
end
end
end
describe '.read_milestone' do
before do
allow(File).to receive(:read).with('VERSION').and_return('15.6.0-pre')
allow(File).to receive(:read).and_call_original
end
it 'returns the correct milestone from the VERSION file' do
expect(described_class.read_milestone).to eq('15.6')
end
end
end
end
|