diff --git a/app/controllers/administrateurs/api_tokens_controller.rb b/app/controllers/administrateurs/api_tokens_controller.rb index 7b4dd0381..32ca47b16 100644 --- a/app/controllers/administrateurs/api_tokens_controller.rb +++ b/app/controllers/administrateurs/api_tokens_controller.rb @@ -37,20 +37,33 @@ module Administrateurs end def update - if invalid_network? - @invalid_network = true - return render :edit + @libelle_id_procedures = libelle_id_procedures + + h = {} + + if !params[:networks].nil? + if invalid_network? + @invalid_network_message = "vous devez entrer des adresses ipv4 ou ipv6 valides" + return render :edit + end + + if @api_token.eternal? && networks.empty? + @invalid_network_message = "Vous ne pouvez pas supprimer les restrictions d'accès à l'API d'un jeton permanent." + @api_token.reload + return render :edit + end + + h[:authorized_networks] = networks + end end - if @api_token.eternal? && networks.empty? - flash[:alert] = "Vous ne pouvez pas supprimer les restrictions d'accès à l'API d'un jeton permanent." - return render :edit + if params[:name].present? + h[:name] = name end - @api_token.update!(name:, authorized_networks: networks) + @api_token.update!(h) - flash[:notice] = "Le jeton d'API a été mis à jour." - redirect_to profil_path + render :edit end def destroy diff --git a/app/views/administrateurs/api_tokens/edit.html.haml b/app/views/administrateurs/api_tokens/edit.html.haml index 6efa775f5..47f1ac7d4 100644 --- a/app/views/administrateurs/api_tokens/edit.html.haml +++ b/app/views/administrateurs/api_tokens/edit.html.haml @@ -6,41 +6,47 @@ ["Jeton d’API : #{@api_token.name}"]] } .fr-container.fr-mt-2w - %h1 Modification du jeton d'API « #{@api_token.name} » - = form_with url: admin_api_token_path(@api_token), method: :patch, html: { class: 'fr-mt-2w' } do |f| - .fr-input-group - = f.label :name, class: 'fr-label' do - = t('name', scope: [:administrateurs, :api_tokens, :nom]) - %span.fr-hint-text= t('name-hint', scope: [:administrateurs, :api_tokens, :nom]) - = f.text_field :name, - class: 'fr-input width-33', - autocomplete: 'off', - autocapitalize: 'off', - autocorrect: 'off', - spellcheck: false, - required: true, - value: @api_token.name + %turbo-frame#tokenUpdate + %h1 Modification du jeton d'API « #{@api_token.name} » - .fr-input-group.fr-mb-4w{ - class: class_names('fr-input-group--error': @invalid_network) } - = f.label :name, class: 'fr-label' do - = @api_token.eternal? ? "Entrez au moins 1 réseau autorisé" : "Entrez les adresses ip autorisées" - %span.fr-hint-text adresses réseaux séparées par des espaces. ex: 176.31.79.200 192.168.33.0/24 2001:41d0:304:400::52f/128 - = f.text_field :networks, - class: class_names('fr-input': true, 'fr-input--error': @invalid_network), - autocomplete: 'off', - autocapitalize: 'off', - autocorrect: 'off', - spellcheck: false, - required: @api_token.eternal?, - value: @api_token.authorized_networks_for_ui.gsub(/,/, ' ') + = form_with url: admin_api_token_path(@api_token), method: :patch, html: { class: 'fr-mt-2w' } do |f| + .fr-input-group + = f.label :name, class: 'fr-label' do + = t('name', scope: [:administrateurs, :api_tokens, :nom]) + %span.fr-hint-text= t('name-hint', scope: [:administrateurs, :api_tokens, :nom]) + .flex + = f.text_field :name, + class: 'fr-input width-33', + autocomplete: 'off', + autocapitalize: 'off', + autocorrect: 'off', + spellcheck: false, + required: true, + value: @api_token.name - - if @invalid_network - %p.fr-error-text vous devez entrer des adresses ipv4 ou ipv6 valides + %button.fr-btn.fr-btn--secondary.fr-ml-1w Renommer - %ul.fr-btns-group.fr-btns-group--inline - %li - = f.button 'Modifier', type: :submit, class: "fr-btn fr-btn--primary" - %li - = link_to 'Revenir', profil_path, class: "fr-btn fr-btn--secondary" + = form_with url: admin_api_token_path(@api_token), method: :patch, html: { class: 'fr-mt-2w' } do |f| + .fr-input-group.fr-mb-4w{ + class: class_names('fr-input-group--error': @invalid_network_message.present?) } + = f.label :name, class: 'fr-label' do + = @api_token.eternal? ? "Entrez au moins 1 réseau autorisé" : "Entrez les adresses ip autorisées" + %span.fr-hint-text adresses réseaux séparées par des espaces. ex: 176.31.79.200 192.168.33.0/24 2001:41d0:304:400::52f/128 + .flex + = f.text_field :networks, + class: class_names('fr-input': true, 'fr-input--error': @invalid_network_message.present?), + autocomplete: 'off', + autocapitalize: 'off', + autocorrect: 'off', + spellcheck: false, + value: @api_token.authorized_networks_for_ui.gsub(/,/, ' ') + + %button.fr-btn.fr-btn--secondary.fr-ml-1w Modifier + + - if @invalid_network_message.present? + %p.fr-error-text= @invalid_network_message + + %ul.fr-btns-group.fr-btns-group--inline + %li + = link_to 'Revenir', profil_path, class: "fr-btn fr-btn--secondary" diff --git a/spec/controllers/administrateurs/api_tokens_controller_spec.rb b/spec/controllers/administrateurs/api_tokens_controller_spec.rb index b16321cc5..551cdfcbd 100644 --- a/spec/controllers/administrateurs/api_tokens_controller_spec.rb +++ b/spec/controllers/administrateurs/api_tokens_controller_spec.rb @@ -119,9 +119,8 @@ describe Administrateurs::APITokensController, type: :controller do before { subject; token.reload } it 'does not update a token' do - expect(token.name).not_to eq('new name') - expect(assigns(:invalid_network)).to be true - expect(response).to render_template(:edit) + expect(token.authorized_networks).to be_blank + expect(assigns(:invalid_network_message)).to eq('vous devez entrer des adresses ipv4 ou ipv6 valides') end end @@ -135,9 +134,8 @@ describe Administrateurs::APITokensController, type: :controller do let(:networks) { '' } it 'does not update a token' do - expect(token.name).not_to eq('new name') - expect(flash[:alert]).to eq("Vous ne pouvez pas supprimer les restrictions d'accès à l'API d'un jeton permanent.") - expect(response).to render_template(:edit) + expect(token.authorized_networks).to eq([IPAddr.new('118.218.200.200')]) + expect(assigns(:invalid_network_message)).to eq("Vous ne pouvez pas supprimer les restrictions d'accès à l'API d'un jeton permanent.") end end end