-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3442 from nebulab/kennyadsl/refactor-api-key-actions
Use RESTful routing for users' API key management
- Loading branch information
Showing
7 changed files
with
124 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
backend/app/controllers/spree/admin/users/api_key_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Admin | ||
module Users | ||
class ApiKeyController < Spree::Admin::BaseController | ||
def create | ||
if user.generate_spree_api_key! | ||
flash[:success] = t('spree.admin.api.key_generated') | ||
end | ||
redirect_to edit_admin_user_path(user) | ||
end | ||
|
||
def destroy | ||
if user.clear_spree_api_key! | ||
flash[:success] = t('spree.admin.api.key_cleared') | ||
end | ||
redirect_to edit_admin_user_path(user) | ||
end | ||
|
||
private | ||
|
||
def user | ||
@user ||= Spree.user_class.find(params[:user_id]) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
backend/spec/controllers/spree/admin/users/api_key_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe Spree::Admin::Users::ApiKeyController, type: :controller do | ||
let(:user) { create(:user) } | ||
|
||
describe '#create' do | ||
context "with ability to manage users and API keys" do | ||
stub_authorization! do |_user| | ||
can [:manage], Spree.user_class | ||
can [:manage], :api_key | ||
end | ||
|
||
it "allows admins to create a new user's API key" do | ||
post :create, params: { user_id: user.id } | ||
|
||
expect(flash[:success]).to eq I18n.t('spree.admin.api.key_generated') | ||
expect(response).to redirect_to(spree.edit_admin_user_path(user)) | ||
end | ||
end | ||
|
||
context "without ability to manage users and API keys" do | ||
stub_authorization! do |_user| | ||
end | ||
|
||
it 'denies access' do | ||
delete :destroy, params: { user_id: user.id } | ||
|
||
expect(flash[:error]).to eq I18n.t('spree.authorization_failure') | ||
expect(response).to redirect_to '/unauthorized' | ||
end | ||
end | ||
end | ||
|
||
describe '#destroy' do | ||
context "with ability to manage users and API keys" do | ||
stub_authorization! do |_user| | ||
can [:manage], Spree.user_class | ||
can [:manage], :api_key | ||
end | ||
|
||
it "allows admins to clear an existing user's API key" do | ||
user.generate_spree_api_key! | ||
delete :destroy, params: { user_id: user.id } | ||
|
||
expect(flash[:success]).to eq I18n.t('spree.admin.api.key_cleared') | ||
expect(response).to redirect_to(spree.edit_admin_user_path(user)) | ||
end | ||
end | ||
|
||
context "without ability to manage users and API keys" do | ||
stub_authorization! do |_user| | ||
end | ||
|
||
it 'denies access' do | ||
delete :destroy, params: { user_id: user.id } | ||
|
||
expect(flash[:error]).to eq I18n.t('spree.authorization_failure') | ||
expect(response).to redirect_to '/unauthorized' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters