Skip to content

Commit

Permalink
Use Rails 5 HTTP action methods in controller and request specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
baxang committed Apr 16, 2018
1 parent 3b6119a commit b6c2227
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 106 deletions.
17 changes: 9 additions & 8 deletions spec/controllers/application_metal_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ def index
let(:flag) { true }

it '200 for the correct media type' do
@request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
get :index, {}, as: :url_encoded_form
# @request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
# get :index, {}, as: :url_encoded_form
get :index, params: {}, as: :url_encoded_form
expect(response).to have_http_status 200
end

it 'returns a 415 for an incorrect media type' do
@request.headers['Content-Type'] = 'application/json'
get :index, {}, as: :json
# @request.headers['Content-Type'] = 'application/json'
get :index, as: :json
expect(response).to have_http_status 415
end
end
Expand All @@ -39,14 +40,14 @@ def index
let(:flag) { false }

it 'returns a 200 for the correct media type' do
@request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
get :index, {}, as: :url_encoded_form
# @request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
get :index, as: :url_encoded_form
expect(response).to have_http_status 200
end

it 'returns a 200 for an incorrect media type' do
@request.headers['Content-Type'] = 'application/json'
get :index, {}, as: :json
# @request.headers['Content-Type'] = 'application/json'
get :index, as: :json
expect(response).to have_http_status 200
end
end
Expand Down
42 changes: 28 additions & 14 deletions spec/controllers/applications_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ module Doorkeeper

it 'does not create application' do
expect do
post :create, doorkeeper_application: {
name: 'Example',
redirect_uri: 'https://example.com' }
post :create,
params: {
doorkeeper_application: {
name: 'Example',
redirect_uri: 'https://example.com' }
}
end.not_to change { Doorkeeper::Application.count }
end
end
Expand All @@ -43,30 +46,41 @@ module Doorkeeper

it 'creates application' do
expect do
post :create, doorkeeper_application: {
name: 'Example',
redirect_uri: 'https://example.com' }
post :create,
params: {
doorkeeper_application: {
name: 'Example',
redirect_uri: 'https://example.com'
}
}
end.to change { Doorkeeper::Application.count }.by(1)

expect(response).to be_redirect
end

it 'does not allow mass assignment of uid or secret' do
application = FactoryBot.create(:application)
put :update, id: application.id, doorkeeper_application: {
uid: '1A2B3C4D',
secret: '1A2B3C4D'
}
put :update,
params: {
id: application.id,
doorkeeper_application: {
uid: '1A2B3C4D',
secret: '1A2B3C4D'
}
}

expect(application.reload.uid).not_to eq '1A2B3C4D'
end

it 'updates application' do
application = FactoryBot.create(:application)
put :update, id: application.id, doorkeeper_application: {
name: 'Example',
redirect_uri: 'https://example.com'
}
put :update,
params: {
id: application.id, doorkeeper_application: {
name: 'Example',
redirect_uri: 'https://example.com'
}
}

expect(application.reload.name).to eq 'Example'
end
Expand Down
24 changes: 12 additions & 12 deletions spec/controllers/authorizations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def translated_error_message(key)

describe 'POST #create' do
before do
post :create, client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri
post :create, params: { client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri }
end

it 'redirects after authorization' do
Expand Down Expand Up @@ -76,7 +76,7 @@ def translated_error_message(key)
describe "POST #create in API mode" do
before do
allow(Doorkeeper.configuration).to receive(:api_only).and_return(true)
post :create, client_id: client.uid, response_type: "token", redirect_uri: client.redirect_uri
post :create, params: { client_id: client.uid, response_type: "token", redirect_uri: client.redirect_uri }
end

let(:response_json_body) { JSON.parse(response.body) }
Expand Down Expand Up @@ -114,7 +114,7 @@ def translated_error_message(key)
describe 'POST #create with errors' do
before do
default_scopes_exist :public
post :create, client_id: client.uid, response_type: 'token', scope: 'invalid', redirect_uri: client.redirect_uri
post :create, params: { client_id: client.uid, response_type: 'token', scope: 'invalid', redirect_uri: client.redirect_uri }
end

it 'redirects after authorization' do
Expand Down Expand Up @@ -146,7 +146,7 @@ def translated_error_message(key)
before do
allow(Doorkeeper.configuration).to receive(:api_only).and_return(true)
default_scopes_exist :public
post :create, client_id: client.uid, response_type: 'token', scope: 'invalid', redirect_uri: client.redirect_uri
post :create, params: { client_id: client.uid, response_type: 'token', scope: 'invalid', redirect_uri: client.redirect_uri }
end

let(:response_json_body) { JSON.parse(response.body) }
Expand Down Expand Up @@ -182,7 +182,7 @@ def translated_error_message(key)
allow(Doorkeeper.configuration).to receive(:reuse_access_token).and_return(true)

access_token.save!
post :create, client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri
post :create, params: { client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri }
end

it 'returns the existing access token in a fragment' do
Expand All @@ -200,7 +200,7 @@ def translated_error_message(key)
true
end)
client.update_attribute :redirect_uri, 'urn:ietf:wg:oauth:2.0:oob'
get :new, client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri
get :new, params: { client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri }
end

it 'should redirect immediately' do
Expand All @@ -224,7 +224,7 @@ def translated_error_message(key)
true
end)
client.update_attribute :redirect_uri, 'urn:ietf:wg:oauth:2.0:oob'
get :new, client_id: client.uid, response_type: 'code', redirect_uri: client.redirect_uri
get :new, params: { client_id: client.uid, response_type: 'code', redirect_uri: client.redirect_uri }
end

it 'should redirect immediately' do
Expand All @@ -246,7 +246,7 @@ def translated_error_message(key)
allow(Doorkeeper.configuration).to receive(:skip_authorization).and_return(proc do
true
end)
get :new, client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri
get :new, params: { client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri }
end

it 'should redirect immediately' do
Expand Down Expand Up @@ -278,7 +278,7 @@ def translated_error_message(key)
describe 'GET #new in API mode' do
before do
allow(Doorkeeper.configuration).to receive(:api_only).and_return(true)
get :new, client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri
get :new, params: { client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri }
end

it 'should render success' do
Expand All @@ -303,7 +303,7 @@ def translated_error_message(key)
allow(Doorkeeper.configuration).to receive(:skip_authorization).and_return(proc { true })
allow(Doorkeeper.configuration).to receive(:api_only).and_return(true)

get :new, client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri
get :new, params: { client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri }
end

it 'should render success' do
Expand Down Expand Up @@ -340,7 +340,7 @@ def translated_error_message(key)
describe 'GET #new with errors' do
before do
default_scopes_exist :public
get :new, an_invalid: 'request'
get :new, params: { an_invalid: 'request' }
end

it 'does not redirect' do
Expand All @@ -357,7 +357,7 @@ def translated_error_message(key)
after do
allow(Doorkeeper.configuration).to receive(:skip_authorization).and_return(proc { true })
client.update_attribute :redirect_uri, 'urn:ietf:wg:oauth:2.0:oob'
get :new, client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri
get :new, params: { client_id: client.uid, response_type: 'token', redirect_uri: client.redirect_uri }
end

it 'should call :before_successful_authorization callback' do
Expand Down
28 changes: 14 additions & 14 deletions spec/controllers/protected_resources_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def index

it 'access_token param' do
expect(Doorkeeper::AccessToken).to receive(:by_token).with(token_string).and_return(token)
get :index, access_token: token_string
get :index, params: { access_token: token_string }
end

it 'bearer_token param' do
expect(Doorkeeper::AccessToken).to receive(:by_token).with(token_string).and_return(token)
get :index, bearer_token: token_string
get :index, params: { bearer_token: token_string }
end

it 'Authorization header' do
Expand Down Expand Up @@ -71,25 +71,25 @@ def index

context 'with valid token', token: :valid do
it 'allows into index action' do
get :index, access_token: token_string
get :index, params: { access_token: token_string }
expect(response).to be_successful
end

it 'allows into show action' do
get :show, id: '4', access_token: token_string
get :show, params: { id: '4', access_token: token_string }
expect(response).to be_successful
end
end

context 'with invalid token', token: :invalid do
it 'does not allow into index action' do
get :index, access_token: token_string
get :index, params: { access_token: token_string }
expect(response.status).to eq 401
expect(response.header['WWW-Authenticate']).to match(/^Bearer/)
end

it 'does not allow into show action' do
get :show, id: '4', access_token: token_string
get :show, params: { id: '4', access_token: token_string }
expect(response.status).to eq 401
expect(response.header['WWW-Authenticate']).to match(/^Bearer/)
end
Expand All @@ -115,7 +115,7 @@ def index
Doorkeeper::AccessToken
).to receive(:by_token).with(token_string).and_return(token)

get :index, access_token: token_string
get :index, params: { access_token: token_string }
expect(response).to be_successful
end

Expand All @@ -129,7 +129,7 @@ def index
).to receive(:by_token).with(token_string).and_return(token)
expect(token).to receive(:acceptable?).with([:write]).and_return(false)

get :index, access_token: token_string
get :index, params: { access_token: token_string }
expect(response.status).to eq 403
expect(response.header).to_not include('WWW-Authenticate')
end
Expand Down Expand Up @@ -163,7 +163,7 @@ def doorkeeper_unauthorized_render_options(error: nil)
end

it 'it renders a custom JSON response', token: :invalid do
get :index, access_token: token_string
get :index, params: { access_token: token_string }
expect(response.status).to eq 401
expect(response.content_type).to eq('application/json')
expect(response.header['WWW-Authenticate']).to match(/^Bearer/)
Expand Down Expand Up @@ -193,7 +193,7 @@ def doorkeeper_unauthorized_render_options(error: nil); end
end

it 'it renders a custom text response', token: :invalid do
get :index, access_token: token_string
get :index, params: { access_token: token_string }
expect(response.status).to eq 401
expect(response.content_type).to eq('text/plain')
expect(response.header['WWW-Authenticate']).to match(/^Bearer/)
Expand Down Expand Up @@ -243,7 +243,7 @@ def doorkeeper_forbidden_render_options(*)
end

it 'renders a custom JSON response' do
get :index, access_token: token_string
get :index, params: { access_token: token_string }
expect(response.header).to_not include('WWW-Authenticate')
expect(response.content_type).to eq('application/json')
expect(response.status).to eq 403
Expand All @@ -265,7 +265,7 @@ def doorkeeper_forbidden_render_options(*)
end

it 'overrides the default status code' do
get :index, access_token: token_string
get :index, params: { access_token: token_string }
expect(response.status).to eq 404
end
end
Expand All @@ -282,7 +282,7 @@ def doorkeeper_forbidden_render_options(*)
end

it 'renders a custom status code and text response' do
get :index, access_token: token_string
get :index, params: { access_token: token_string }
expect(response.header).to_not include('WWW-Authenticate')
expect(response.status).to eq 403
expect(response.body).to eq('Forbidden')
Expand All @@ -301,7 +301,7 @@ def doorkeeper_forbidden_render_options(*)
end

it 'overrides the default status code' do
get :index, access_token: token_string
get :index, params: { access_token: token_string }
expect(response.status).to eq 404
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/token_info_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

describe 'successful request' do
it 'responds with token info' do
get :show, access_token: doorkeeper_token.token
get :show, params: { access_token: doorkeeper_token.token }

expect(response.body).to eq(doorkeeper_token.to_json)
end

it 'responds with a 200 status' do
get :show, access_token: doorkeeper_token.token
get :show, params: { access_token: doorkeeper_token.token }

expect(response.status).to eq 200
end
Expand Down
Loading

0 comments on commit b6c2227

Please sign in to comment.