REFACTOR: admin api controller specs to requests (#5972)

This commit is contained in:
OsamaSayegh 2018-06-11 07:35:45 +03:00 committed by Guo Xiang Tan
parent 2c8a9d36af
commit 65241c6778
2 changed files with 63 additions and 56 deletions

View File

@ -1,56 +0,0 @@
require 'rails_helper'
describe Admin::ApiController do
it "is a subclass of AdminController" do
expect(Admin::ApiController < Admin::AdminController).to eq(true)
end
let!(:user) { log_in(:admin) }
context '.index' do
it "succeeds" do
get :index, format: :json
expect(response.status).to eq(200)
end
end
context '.regenerate_key' do
let(:api_key) { Fabricate(:api_key) }
it "returns 404 when there is no key" do
put :regenerate_key, params: { id: 1234 }, format: :json
expect(response).not_to be_successful
expect(response.status).to eq(404)
end
it "delegates to the api key's `regenerate!` method" do
ApiKey.any_instance.expects(:regenerate!)
put :regenerate_key, params: { id: api_key.id }, format: :json
end
end
context '.revoke_key' do
let(:api_key) { Fabricate(:api_key) }
it "returns 404 when there is no key" do
delete :revoke_key, params: { id: 1234 }, format: :json
expect(response).not_to be_successful
expect(response.status).to eq(404)
end
it "delegates to the api key's `regenerate!` method" do
ApiKey.any_instance.expects(:destroy)
delete :revoke_key, params: { id: api_key.id }, format: :json
end
end
context '.create_master_key' do
it "creates a record" do
expect do
post :create_master_key, format: :json
end.to change(ApiKey, :count).by(1)
end
end
end

View File

@ -0,0 +1,63 @@
require 'rails_helper'
describe Admin::ApiController do
it "is a subclass of AdminController" do
expect(Admin::ApiController < Admin::AdminController).to eq(true)
end
let(:admin) { Fabricate(:admin) }
before do
sign_in(admin)
end
describe '#index' do
it "succeeds" do
get "/admin/api/keys.json"
expect(response.status).to eq(200)
end
end
describe '#regenerate_key' do
let(:api_key) { Fabricate(:api_key) }
it "returns 404 when there is no key" do
put "/admin/api/key.json", params: { id: 1234 }
expect(response.status).to eq(404)
end
it "delegates to the api key's `regenerate!` method" do
prev_value = api_key.key
put "/admin/api/key.json", params: { id: api_key.id }
expect(response.status).to eq(200)
api_key.reload
expect(api_key.key).not_to eq(prev_value)
expect(api_key.created_by.id).to eq(admin.id)
end
end
describe '#revoke_key' do
let(:api_key) { Fabricate(:api_key) }
it "returns 404 when there is no key" do
delete "/admin/api/key.json", params: { id: 1234 }
expect(response.status).to eq(404)
end
it "delegates to the api key's `regenerate!` method" do
delete "/admin/api/key.json", params: { id: api_key.id }
expect(response.status).to eq(200)
expect(ApiKey.where(key: api_key.key).count).to eq(0)
end
end
describe '#create_master_key' do
it "creates a record" do
expect do
post "/admin/api/key.json"
end.to change(ApiKey, :count).by(1)
expect(response.status).to eq(200)
end
end
end