From a6407aa6629b9224ca4f357d99f387bd76a6fecc Mon Sep 17 00:00:00 2001 From: Daniel M Brasil Date: Wed, 14 Jun 2023 04:48:48 -0300 Subject: [PATCH] Migrate to request specs in `/api/v1/apps` (#25401) --- .../api/v1/apps_spec.rb} | 57 ++++++++++++++----- 1 file changed, 42 insertions(+), 15 deletions(-) rename spec/{controllers/api/v1/apps_controller_spec.rb => requests/api/v1/apps_spec.rb} (69%) diff --git a/spec/controllers/api/v1/apps_controller_spec.rb b/spec/requests/api/v1/apps_spec.rb similarity index 69% rename from spec/controllers/api/v1/apps_controller_spec.rb rename to spec/requests/api/v1/apps_spec.rb index de2a07f20..88f9eee36 100644 --- a/spec/controllers/api/v1/apps_controller_spec.rb +++ b/spec/requests/api/v1/apps_spec.rb @@ -2,16 +2,18 @@ require 'rails_helper' -RSpec.describe Api::V1::AppsController do - render_views +RSpec.describe 'Apps' do + describe 'POST /api/v1/apps' do + subject do + post '/api/v1/apps', params: params + end - describe 'POST #create' do - let(:client_name) { 'Test app' } - let(:scopes) { nil } + let(:client_name) { 'Test app' } + let(:scopes) { nil } let(:redirect_uris) { 'urn:ietf:wg:oauth:2.0:oob' } - let(:website) { nil } + let(:website) { nil } - let(:app_params) do + let(:params) do { client_name: client_name, redirect_uris: redirect_uris, @@ -20,24 +22,26 @@ RSpec.describe Api::V1::AppsController do } end - before do - post :create, params: app_params - end - context 'with valid params' do it 'returns http success' do + subject + expect(response).to have_http_status(200) end it 'creates an OAuth app' do - expect(Doorkeeper::Application.find_by(name: client_name)).to_not be_nil + subject + + expect(Doorkeeper::Application.find_by(name: client_name)).to be_present end it 'returns client ID and client secret' do - json = body_as_json + subject - expect(json[:client_id]).to_not be_blank - expect(json[:client_secret]).to_not be_blank + body = body_as_json + + expect(body[:client_id]).to be_present + expect(body[:client_secret]).to be_present end end @@ -45,6 +49,8 @@ RSpec.describe Api::V1::AppsController do let(:scopes) { 'hoge' } it 'returns http unprocessable entity' do + subject + expect(response).to have_http_status(422) end end @@ -53,10 +59,14 @@ RSpec.describe Api::V1::AppsController do let(:scopes) { (%w(read) * 40).join(' ') } it 'returns http success' do + subject + expect(response).to have_http_status(200) end it 'only saves the scope once' do + subject + expect(Doorkeeper::Application.find_by(name: client_name).scopes.to_s).to eq 'read' end end @@ -65,6 +75,8 @@ RSpec.describe Api::V1::AppsController do let(:client_name) { 'hoge' * 20 } it 'returns http unprocessable entity' do + subject + expect(response).to have_http_status(422) end end @@ -73,6 +85,8 @@ RSpec.describe Api::V1::AppsController do let(:website) { "https://foo.bar/#{'hoge' * 2_000}" } it 'returns http unprocessable entity' do + subject + expect(response).to have_http_status(422) end end @@ -81,6 +95,19 @@ RSpec.describe Api::V1::AppsController do let(:redirect_uris) { "https://foo.bar/#{'hoge' * 2_000}" } it 'returns http unprocessable entity' do + subject + + expect(response).to have_http_status(422) + end + end + + context 'without required params' do + let(:client_name) { '' } + let(:redirect_uris) { '' } + + it 'returns http unprocessable entity' do + subject + expect(response).to have_http_status(422) end end