Skip to content

Commit 4c521ca

Browse files
committed
api for deleting identifiers
1 parent 53fd74c commit 4c521ca

File tree

3 files changed

+99
-23
lines changed

3 files changed

+99
-23
lines changed

magma/lib/magma/server.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ def initialize
3333
get '/gnomon/:project_name/rules', action: 'gnomon#project_rules', auth: { user: { can_view?: :project_name } }
3434
get '/gnomon/:project_name', action: 'gnomon#get', auth: { user: { can_view?: :project_name } }
3535
get '/gnomon/:project_name/revisions', action: 'gnomon#revisions', auth: { user: { can_view?: :project_name } }
36-
post '/gnomon/:project_name', action: 'gnomon#set', auth: { user: { is_admin?: :project_name } }
37-
post '/gnomon/:project_name/increment/:rule_name/:identifier_root', action: 'gnomon#increment', auth: { user: { is_admin?: :project_name } }
36+
post '/gnomon/:project_name/delete', action: 'gnomon#delete', auth: { user: { can_edit?: :project_name } }
37+
post '/gnomon/:project_name', action: 'gnomon#set', auth: { user: { can_edit?: :project_name } }
38+
post '/gnomon/:project_name/increment/:rule_name/:identifier_root', action: 'gnomon#increment', auth: { user: { can_edit?: :project_name } }
3839
get '/gnomon/:project_name/decompose/*identifier', action: 'gnomon#decompose', auth: { user: { can_view?: :project_name } }
3940
get '/gnomon/:project_name/list/:rule_name', action: 'gnomon#list', auth: { user: { can_view?: :project_name } }
4041
get '/gnomon/:project_name/rule/:rule_name', action: 'gnomon#rule', auth: { user: { can_view?: :project_name } }

magma/lib/magma/server/gnomon.rb

+25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,31 @@ def get
77
return success_json(grammar.to_hash)
88
end
99

10+
def delete
11+
require_param(:identifiers)
12+
identifiers = Magma::Gnomon::Identifier.where(
13+
project_name: @params[:project_name],
14+
identifier: @params[:identifiers]
15+
).select_map(:identifier)
16+
17+
unknown = @params[:identifiers] - identifiers
18+
19+
unless unknown.empty?
20+
raise Etna::BadRequest, "Unknown identifier(s): #{unknown.join(', ')}"
21+
end
22+
23+
confirmation = Digest::MD5.hexdigest(@params[:identifiers].join)
24+
25+
raise Etna::BadRequest, "Confirm deletion of #{@params[:identifiers].count} identifiers with code #{confirmation}" unless @params[:confirmation] == confirmation
26+
27+
Magma::Gnomon::Identifier.where(
28+
project_name: @params[:project_name],
29+
identifier: @params[:identifiers]
30+
).delete
31+
32+
success_json(success: "Deleted #{@params[:identifiers].count} identifiers")
33+
end
34+
1035
def set
1136
require_param(:config, :comment)
1237
old_grammar = Magma::Gnomon::Grammar.for_project(project_name)

magma/spec/gnomon_spec.rb

+71-21
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def app
3434
grammar = create_grammar
3535

3636
config = VALID_GRAMMAR_CONFIG
37-
auth_header(:admin)
37+
auth_header(:editor)
3838
json_post('/gnomon/labors', config: config, comment: 'eh')
3939

4040
expect(last_response.status).to eq(200)
@@ -51,7 +51,7 @@ def app
5151
config = {
5252
text: "Some content"
5353
}
54-
auth_header(:admin)
54+
auth_header(:editor)
5555
json_post('/gnomon/labors', config: config, comment: 'eh')
5656

5757
expect(last_response.status).to eq(422)
@@ -124,7 +124,7 @@ def app
124124
context 'counter API' do
125125
it 'throws exception with no grammar defined' do
126126
expect(Magma::Gnomon::Identifier.count).to eq(0)
127-
auth_header(:admin)
127+
auth_header(:editor)
128128
post('/gnomon/labors/increment/victim/LABORS-LION-H2-C')
129129
expect(last_response.status).to eq(422)
130130
expect(Magma::Gnomon::Identifier.count).to eq(0)
@@ -137,15 +137,15 @@ def app
137137

138138
context 'generates the next identifier when' do
139139
it 'none exist' do
140-
auth_header(:admin)
140+
auth_header(:editor)
141141
post('/gnomon/labors/increment/victim/LABORS-LION-H2-C')
142142
expect(last_response.status).to eq(200)
143143
expect(last_response.body).to eq("1")
144144
end
145145

146146
it 'does not create the identifier' do
147147
expect(Magma::Gnomon::Identifier.count).to eq(0)
148-
auth_header(:admin)
148+
auth_header(:editor)
149149
post('/gnomon/labors/increment/victim/LABORS-LION-H2-C')
150150
expect(last_response.status).to eq(200)
151151
expect(last_response.body).to eq("1")
@@ -154,23 +154,23 @@ def app
154154

155155
it 'sequence exists' do
156156
identifier = create_identifier("LABORS-LION-H2-C1", rule: 'victim', grammar: @grammar)
157-
auth_header(:admin)
157+
auth_header(:editor)
158158
post('/gnomon/labors/increment/victim/LABORS-LION-H2-C')
159159
expect(last_response.status).to eq(200)
160160
expect(last_response.body).to eq("2")
161161
end
162162

163163
it 'sequence in other token value exists' do
164164
identifier = create_identifier("LABORS-LION-H2-C1", rule: 'victim', grammar: @grammar)
165-
auth_header(:admin)
165+
auth_header(:editor)
166166
post('/gnomon/labors/increment/victim/LABORS-LION-H2-S')
167167
expect(last_response.status).to eq(200)
168168
expect(last_response.body).to eq("1")
169169
end
170170

171171
it 'change in parent token counter' do
172172
identifier = create_identifier("LABORS-LION-H2-C1", rule: 'victim', grammar: @grammar)
173-
auth_header(:admin)
173+
auth_header(:editor)
174174
post('/gnomon/labors/increment/victim/LABORS-LION-H1-C')
175175
expect(last_response.status).to eq(200)
176176
expect(last_response.body).to eq("1")
@@ -185,13 +185,13 @@ def app
185185
end
186186

187187
it 'rule does not exist' do
188-
auth_header(:admin)
188+
auth_header(:editor)
189189
post('/gnomon/labors/increment/habitat/LABORS-MARSH')
190190
expect(last_response.status).to eq(422)
191191
end
192192

193193
it 'identifier_root does not match rule' do
194-
auth_header(:admin)
194+
auth_header(:editor)
195195
post('/gnomon/labors/increment/victim/LABORS-LION-H2-Q')
196196
expect(last_response.status).to eq(422)
197197
post('/gnomon/labors/increment/victim/LABORS-PARROT-H')
@@ -200,7 +200,7 @@ def app
200200

201201
it 'but does not create identifier' do
202202
expect(Magma::Gnomon::Identifier.count).to eq(0)
203-
auth_header(:admin)
203+
auth_header(:editor)
204204
post('/gnomon/labors/increment/victim/LABORS-LION-H2-Q')
205205
expect(last_response.status).to eq(422)
206206
expect(Magma::Gnomon::Identifier.count).to eq(0)
@@ -372,7 +372,7 @@ def app
372372
context 'generate API' do
373373

374374
it 'throws exception when no grammar for project' do
375-
auth_header(:admin)
375+
auth_header(:editor)
376376
post('/gnomon/labors/generate/victim/LABORS-LION-H2-C1')
377377
expect(last_response.status).to eq(422)
378378
expect(Magma::Gnomon::Identifier.count).to eq(0)
@@ -386,7 +386,7 @@ def app
386386
it 'creates the identifier' do
387387
identifier = "LABORS-LION-H2-C1"
388388
expect(Magma::Gnomon::Identifier.count).to eq(0)
389-
auth_header(:admin)
389+
auth_header(:editor)
390390
post("/gnomon/labors/generate/victim/#{identifier}")
391391

392392
rules = {
@@ -406,7 +406,7 @@ def app
406406

407407
it 'ignores identifiers that already exists' do
408408
identifier = create_identifier("LABORS-LION-H2-C1", rule: 'victim', grammar: @grammar)
409-
auth_header(:admin)
409+
auth_header(:editor)
410410
post('/gnomon/labors/generate/victim/LABORS-LION-H2-C1')
411411
expect(last_response.status).to eq(200)
412412
expect(json_body[:rules].size).to eq(4)
@@ -416,7 +416,7 @@ def app
416416

417417
context 'throws exception when' do
418418
it 'invalid rule name provided' do
419-
auth_header(:admin)
419+
auth_header(:editor)
420420
post('/gnomon/labors/generate/alias/LABORS-LION-H2-C1')
421421
expect(last_response.status).to eq(422)
422422
expect(Magma::Gnomon::Identifier.count).to eq(0)
@@ -430,7 +430,7 @@ def app
430430
end
431431

432432
it 'identifier does not match rule' do
433-
auth_header(:admin)
433+
auth_header(:editor)
434434
post('/gnomon/labors/generate/victim/LABORS-LION-H2-X1')
435435
expect(last_response.status).to eq(422)
436436
expect(Magma::Gnomon::Identifier.count).to eq(0)
@@ -439,10 +439,60 @@ def app
439439
end
440440
end
441441

442+
context 'delete identifiers' do
443+
before(:each) do
444+
@grammar = create_grammar(config: VALID_GRAMMAR_CONFIG)
445+
identifier1 = create_identifier("LABORS-LION-H2-C1", rule: 'victim', grammar: @grammar)
446+
identifier2 = create_identifier("LABORS-LION-H2-C2", rule: 'victim', grammar: @grammar)
447+
identifier3 = create_identifier("LABORS-LION-H3-C2", rule: 'victim', grammar: @grammar)
448+
449+
end
450+
451+
it 'allows deletion with confirmation' do
452+
auth_header(:editor)
453+
json_post('/gnomon/labors/delete',
454+
identifiers: [
455+
"LABORS-LION-H2-C1",
456+
"LABORS-LION-H2-C2",
457+
"LABORS-LION-H3-C2",
458+
],
459+
confirmation: "df44036473e8537e83c267c86909bd23"
460+
)
461+
462+
expect(last_response.status).to eq(200)
463+
expect(Magma::Gnomon::Identifier.count).to eq(0)
464+
end
465+
466+
it 'refuses deletion without confirmation' do
467+
auth_header(:editor)
468+
post('/gnomon/labors/delete', identifiers: [
469+
"LABORS-LION-H2-C1",
470+
"LABORS-LION-H2-C2",
471+
"LABORS-LION-H3-C2",
472+
])
473+
474+
expect(last_response.status).to eq(422)
475+
expect(json_body[:error]).to eq("Missing confirmation code df44036473e8537e83c267c86909bd23")
476+
expect(Magma::Gnomon::Identifier.count).to eq(3)
477+
end
478+
479+
it 'refuses deletion with an unknown identifier' do
480+
auth_header(:editor)
481+
post('/gnomon/labors/delete', identifiers: [
482+
"LABORS-LION-H2-C1",
483+
"LABORS-LOON-H2-C2",
484+
"LABORS-LION-H3-C2",
485+
])
486+
487+
expect(last_response.status).to eq(422)
488+
expect(Magma::Gnomon::Identifier.count).to eq(3)
489+
end
490+
end
491+
442492
context 'bulk generate API' do
443493

444494
it 'throws exception when no grammar for project' do
445-
auth_header(:admin)
495+
auth_header(:editor)
446496
json_post('/gnomon/labors/generate', names: [])
447497
expect(last_response.status).to eq(422)
448498
expect(json_body[:error]).to eq('No grammar found for project labors.')
@@ -454,7 +504,7 @@ def app
454504
end
455505

456506
it 'creates identifiers' do
457-
auth_header(:admin)
507+
auth_header(:editor)
458508
json_post("/gnomon/labors/generate", names: [
459509
{
460510
rule_name: 'village',
@@ -482,7 +532,7 @@ def app
482532

483533
it 'ignores identifiers that already exists' do
484534
identifier = create_identifier("LABORS-LION-H2-C1", rule: 'victim', grammar: @grammar)
485-
auth_header(:admin)
535+
auth_header(:editor)
486536
names = [{rule_name: 'victim', name: 'LABORS-LION-H2-C1'}]
487537
json_post('/gnomon/labors/generate', names: names)
488538
expect(last_response.status).to eq(200)
@@ -493,7 +543,7 @@ def app
493543

494544
context 'throws exception when' do
495545
it 'invalid rule name provided' do
496-
auth_header(:admin)
546+
auth_header(:editor)
497547
json_post('/gnomon/labors/generate', names: [{ rule_name: 'alias', name: 'LABORS-LION-H2-C1'}])
498548
expect(last_response.status).to eq(422)
499549
expect(Magma::Gnomon::Identifier.count).to eq(0)
@@ -507,7 +557,7 @@ def app
507557
end
508558

509559
it 'identifier does not match rule' do
510-
auth_header(:admin)
560+
auth_header(:editor)
511561
json_post('/gnomon/labors/generate', names: [{ rule_name: 'victim', name: 'LABORS-LION-H2-X1'}])
512562
expect(last_response.status).to eq(422)
513563
expect(Magma::Gnomon::Identifier.count).to eq(0)

0 commit comments

Comments
 (0)