[deploy] Create write_articles oauth scope (#7834)

This commit is contained in:
Mac Siri 2020-05-14 09:55:53 -04:00 committed by GitHub
parent 815248884c
commit 321b24d2a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 23 deletions

View file

@ -1,9 +1,9 @@
module Api
module V0
class ArticlesController < ApiController
before_action :authenticate_with_api_key_or_current_user!, only: %i[create update]
before_action :authenticate!, only: :me
before_action :authenticate!, only: %i[create update me]
before_action -> { doorkeeper_authorize! :public }, only: %w[index show], if: -> { doorkeeper_token }
before_action -> { doorkeeper_authorize! :write_articles }, only: %w[create update], if: -> { doorkeeper_token }
before_action :validate_article_param_is_hash, only: %w[create update]

View file

@ -211,7 +211,7 @@ Doorkeeper.configure do
# https://doorkeeper.gitbook.io/guides/ruby-on-rails/scopes
#
default_scopes :public, :read_articles
# optional_scopes :write, :update
optional_scopes :write_articles
# Allows to restrict only certain scopes for grant_type.
# By default, all the scopes will be available for all the grant types.
@ -226,7 +226,7 @@ Doorkeeper.configure do
# not in configuration, i.e. +default_scopes+ or +optional_scopes+.
# (disabled by default)
#
# enforce_configured_scopes
enforce_configured_scopes
# Change the way client credentials are retrieved from the request object.
# By default it retrieves first from the `HTTP_AUTHORIZATION` header, then

View file

@ -22,6 +22,7 @@ en:
scopes:
public: 'Access public profile data and published articles'
read_articles: 'Access all articles including drafts'
write_articles: 'Access to creating and updating articles'
applications:
confirmations:

View file

@ -462,14 +462,6 @@ RSpec.describe "Api::V0::Articles", type: :request do
post api_articles_path, headers: { "api-key" => api_secret.secret, "content-type" => "application/json" }
expect(response).to have_http_status(:unauthorized)
end
it "fails when oauth's access_token" do
access_token = create(:doorkeeper_access_token, resource_owner_id: user.id)
headers = { "authorization" => "Bearer #{access_token.token}", "content-type" => "application/json" }
post api_articles_path, params: { article: { title: Faker::Book.title } }.to_json, headers: headers
expect(response).to have_http_status(:unauthorized)
end
end
describe "when authorized" do
@ -481,6 +473,22 @@ RSpec.describe "Api::V0::Articles", type: :request do
post api_articles_path, params: { article: params }.to_json, headers: headers
end
it "returns a 403 if :write_articles scope is missing (oauth)" do
access_token = create(:doorkeeper_access_token, resource_owner_id: user.id, scopes: "public")
headers = { "authorization" => "Bearer #{access_token.token}", "content-type" => "application/json" }
post api_articles_path, params: { article: { title: Faker::Book.title } }.to_json, headers: headers
expect(response).to have_http_status(:forbidden)
end
it "returns a 201 if :write_articles scope is provided (oauth)" do
access_token = create(:doorkeeper_access_token, resource_owner_id: user.id, scopes: "write_articles")
headers = { "authorization" => "Bearer #{access_token.token}", "content-type" => "application/json" }
post api_articles_path, params: { article: { title: Faker::Book.title, body_markdown: "" } }.to_json, headers: headers
expect(response).to have_http_status(:created)
end
it "returns a 429 status code if the rate limit is reached" do
rate_limit_checker = instance_double(RateLimitChecker)
retry_after_val = RateLimitChecker::ACTION_LIMITERS.dig(:published_article_creation, :retry_after)
@ -765,17 +773,6 @@ RSpec.describe "Api::V0::Articles", type: :request do
put path, headers: { "api-key" => api_secret.secret, "content-type" => "application/json" }
expect(response).to have_http_status(:unauthorized)
end
it "fails with oauth's access_token" do
access_token = create(:doorkeeper_access_token, resource_owner_id: user.id)
headers = { "authorization" => "Bearer #{access_token.token}", "content-type" => "application/json" }
title = Faker::Book.title
body_markdown = "foobar"
params = { title: title, body_markdown: body_markdown }
put path, params: { article: params }.to_json, headers: headers
expect(response).to have_http_status(:unauthorized)
end
end
describe "when authorized" do
@ -786,6 +783,28 @@ RSpec.describe "Api::V0::Articles", type: :request do
put path, params: { article: params }.to_json, headers: headers
end
it "returns a 403 if :write_articles scope is missing (oauth)" do
access_token = create(:doorkeeper_access_token, resource_owner_id: user.id)
headers = { "authorization" => "Bearer #{access_token.token}", "content-type" => "application/json" }
title = Faker::Book.title
body_markdown = "foobar"
params = { title: title, body_markdown: body_markdown }
put path, params: { article: params }.to_json, headers: headers
expect(response).to have_http_status(:forbidden)
end
it "returns a 200 if :write_articles scope is provided (oauth)" do
access_token = create(:doorkeeper_access_token, resource_owner_id: user.id, scopes: "write_articles")
headers = { "authorization" => "Bearer #{access_token.token}", "content-type" => "application/json" }
title = Faker::Book.title
body_markdown = "foobar"
params = { title: title, body_markdown: body_markdown }
put path, params: { article: params }.to_json, headers: headers
expect(response).to have_http_status(:ok)
end
it "returns a 429 status code if the rate limit is reached" do
rate_limit_checker = instance_double(RateLimitChecker)
retry_after_val = RateLimitChecker::ACTION_LIMITERS.dig(:article_update, :retry_after)