From 98ac496aa5b52de75b85699121974d1cd1d88920 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 26 Dec 2017 18:30:36 +0100 Subject: [PATCH] [Fix #1100] Administrateur can renew his API Token --- app/controllers/admin/profile_controller.rb | 6 ++++++ app/views/admin/profile/show.html.haml | 2 ++ config/routes.rb | 1 + spec/controllers/admin/profile_controller_spec.rb | 11 +++++++++++ 4 files changed, 20 insertions(+) diff --git a/app/controllers/admin/profile_controller.rb b/app/controllers/admin/profile_controller.rb index 7da74af64..e9f4d64c8 100644 --- a/app/controllers/admin/profile_controller.rb +++ b/app/controllers/admin/profile_controller.rb @@ -2,4 +2,10 @@ class Admin::ProfileController < AdminController def show @administrateur = current_administrateur end + + def renew_api_token + flash[:notice] = "Votre token d'API a été regénéré." + current_administrateur.renew_api_token + redirect_to admin_profile_path + end end diff --git a/app/views/admin/profile/show.html.haml b/app/views/admin/profile/show.html.haml index 2a2cc723c..78efa24f2 100644 --- a/app/views/admin/profile/show.html.haml +++ b/app/views/admin/profile/show.html.haml @@ -4,3 +4,5 @@ %p API TOKEN : = @administrateur.api_token +%p + = link_to "Regénérer mon token", admin_renew_api_token_path, method: :post, class: "btn btn-default", data: { confirm: "Confirmez-vous la regénération de votre token ? Les applications qui l'utilisent actuellement seront bloquées.", disable_with: "Regénération..." } diff --git a/config/routes.rb b/config/routes.rb index 275c8431c..b6c1b8693 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -110,6 +110,7 @@ Rails.application.routes.draw do get 'procedures/draft' => 'procedures#draft' get 'procedures/path_list' => 'procedures#path_list' get 'profile' => 'profile#show', as: :profile + post 'renew_api_token' => 'profile#renew_api_token', as: :renew_api_token get 'change_dossier_state' => 'change_dossier_state#index' post 'change_dossier_state' => 'change_dossier_state#check' diff --git a/spec/controllers/admin/profile_controller_spec.rb b/spec/controllers/admin/profile_controller_spec.rb index 9e9babf8c..493fb8251 100644 --- a/spec/controllers/admin/profile_controller_spec.rb +++ b/spec/controllers/admin/profile_controller_spec.rb @@ -2,4 +2,15 @@ require 'spec_helper' describe Admin::ProfileController, type: :controller do it { expect(described_class).to be < AdminController } + let(:administrateur) { create(:administrateur) } + + before { sign_in(administrateur) } + + describe 'POST #renew_api_token' do + subject { post :renew_api_token } + + it { expect{ subject }.to change{ administrateur.reload.api_token } } + + it { subject; expect(response.status).to redirect_to(admin_profile_path) } + end end