Skip to content

Commit 6ace454

Browse files
committed
Permet l'ajout de nouveaux scopes sur les HabilitationType avec des demandes
1 parent 42fd8e6 commit 6ace454

4 files changed

Lines changed: 60 additions & 9 deletions

File tree

app/components/molecules/admin/scopes_editor_component.html.erb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</div>
88
<% end %>
99

10-
<% unless structural_fields_locked? %>
10+
<% unless disabled? %>
1111
<script type="text/x-template" data-nested-form-target="template">
1212
<div class="nested-fields fr-grid-row fr-grid-row--gutters fr-mb-2w" data-new-record="true">
1313
<div class="fr-col-4">
@@ -57,6 +57,9 @@
5757
<div class="fr-input-group <%= scope_field_error_class(index, :value) %>">
5858
<label class="fr-label" for="scope-<%= index %>-value"><%= t('admin.habilitation_types.form.scopes_editor.value') %> <span class="fr-ml-1w fr-text-error">*</span></label>
5959
<input type="text" id="scope-<%= index %>-value" name="habilitation_type[scopes][][value]" value="<%= scope['value'] %>" class="fr-input" required aria-describedby="scope-<%= index %>-value-error"<%= ' disabled' if structural_fields_locked? %>>
60+
<% if structural_fields_locked? %>
61+
<input type="hidden" name="habilitation_type[scopes][][value]" value="<%= scope['value'] %>">
62+
<% end %>
6063
<% if scope_field_errors(index, :value).any? %>
6164
<p class="fr-messages-group" id="scope-<%= index %>-value-error">
6265
<% scope_field_errors(index, :value).each do |msg| %>
@@ -72,18 +75,18 @@
7275
<input type="text" id="scope-<%= index %>-group" name="habilitation_type[scopes][][group]" value="<%= scope['group'] %>" class="fr-input"<%= ' disabled' if disabled? %>>
7376
</div>
7477
</div>
75-
<% unless structural_fields_locked? %>
76-
<div class="fr-col-2 fr-mt-4w">
78+
<div class="fr-col-2 fr-mt-4w">
79+
<% unless structural_fields_locked? %>
7780
<button type="button" class="fr-btn fr-btn--tertiary fr-btn--sm" data-action="click->nested-form#remove">
7881
<%= t('admin.habilitation_types.form.scopes_editor.remove') %>
7982
</button>
80-
</div>
81-
<% end %>
83+
<% end %>
84+
</div>
8285
</div>
8386
<% end %>
8487
</div>
8588

86-
<% unless structural_fields_locked? %>
89+
<% unless disabled? %>
8790
<button type="button" class="fr-btn fr-btn--secondary fr-btn--sm" data-action="click->nested-form#add">
8891
<%= t('admin.habilitation_types.form.scopes_editor.add') %>
8992
</button>

app/controllers/admin/habilitation_types_controller.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,21 @@ def sanitize_locked_scopes(submitted_scopes)
110110
existing = @habilitation_type.scopes || []
111111
return existing if submitted_scopes.blank?
112112

113-
existing.each_with_index.map do |scope, i|
114-
submitted = (submitted_scopes[i] || {}).stringify_keys
115-
scope.merge(submitted.slice(*HabilitationType::EDITORIAL_SCOPE_PARAMS))
113+
update_existing_scopes(existing, submitted_scopes) +
114+
extract_new_scopes(existing, submitted_scopes)
115+
end
116+
117+
def update_existing_scopes(existing, submitted_scopes)
118+
existing.map do |scope|
119+
submitted = submitted_scopes.find { |s| s[:value] == scope['value'] } || {}
120+
scope.merge(submitted.stringify_keys.slice(*HabilitationType::EDITORIAL_SCOPE_PARAMS))
116121
end
117122
end
123+
124+
def extract_new_scopes(existing, submitted_scopes)
125+
existing_values = existing.to_set { |s| s['value'] }
126+
submitted_scopes
127+
.reject { |s| existing_values.include?(s[:value]) }
128+
.map(&:stringify_keys)
129+
end
118130
end

features/admin/habilitation_types.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,20 @@ Fonctionnalité: Espace admin: types d'habilitation
143143
Alors le champ "Fournisseur" est désactivé
144144
Et le champ radio "API" est désactivé
145145

146+
Scénario: La page de consultation n'affiche pas le bouton d'ajout de scope et les champs sont désactivés
147+
Sachant que je me connecte
148+
Et qu'un type d'habilitation "API Protégée" avec des demandes liées et des scopes existe
149+
Quand je me rends sur le chemin "/admin/types-habilitation/api-protegee"
150+
Alors le champ du scope 1 "name" est désactivé
151+
Et la page ne contient pas "Ajouter un scope"
152+
153+
@javascript
154+
Scénario: Je peux ajouter un scope à un type avec des demandes liées
155+
Sachant que je me connecte
156+
Et qu'un type d'habilitation "API Protégée" avec des demandes liées et des scopes existe
157+
Quand je me rends sur le chemin "/admin/types-habilitation/api-protegee/edit"
158+
Et que je clique sur "Ajouter un scope"
159+
Et que je remplis le scope 2 avec nom "Nouveau scope" valeur "new_scope" groupe "Groupe"
160+
Et que je clique sur "Enregistrer"
161+
Alors il y a un message de succès contenant "mis à jour"
162+

features/step_definitions/habilitation_types_steps.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212
)
1313
end
1414

15+
Sachantque("un type d'habilitation {string} avec des demandes liées et des scopes existe") do |name|
16+
data_provider = DataProvider.first || FactoryBot.create(:data_provider)
17+
habilitation_type = FactoryBot.create(
18+
:habilitation_type,
19+
name:,
20+
data_provider:,
21+
blocks: [{ 'name' => 'basic_infos' }, { 'name' => 'scopes' }],
22+
scopes: [{ 'name' => 'Revenu fiscal', 'value' => 'rfr', 'group' => 'Revenus' }]
23+
)
24+
FactoryBot.create(
25+
:authorization_request,
26+
type: habilitation_type.authorization_request_type
27+
)
28+
end
29+
1530
Quand('je choisis le type {string}') do |kind|
1631
choose kind, allow_label_click: true
1732
end
@@ -32,3 +47,7 @@
3247
Alors('le champ radio {string} est désactivé') do |label|
3348
expect(page).to have_field(label, disabled: true, visible: :all)
3449
end
50+
51+
Alors('le champ du scope {int} {string} est désactivé') do |index, field|
52+
expect(page).to have_css("#scope-#{index - 1}-#{field}[disabled]")
53+
end

0 commit comments

Comments
 (0)