Make Doorkeeper inherit from ApplicationController (#3591)
* Resolve doorkeeper issue * Refactor spec * Remove comment
This commit is contained in:
parent
06e41f38a9
commit
fc9272fe71
2 changed files with 58 additions and 76 deletions
|
|
@ -25,7 +25,7 @@ Doorkeeper.configure do
|
||||||
if current_user
|
if current_user
|
||||||
head :forbidden unless current_user.admin?
|
head :forbidden unless current_user.admin?
|
||||||
else
|
else
|
||||||
redirect_to sign_in_url
|
warden.authenticate!(scope: :user)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -74,7 +74,7 @@ Doorkeeper.configure do
|
||||||
# Defaults to ActionController::Base.
|
# Defaults to ActionController::Base.
|
||||||
# See https://doorkeeper.gitbook.io/guides/configuration/other-configurations#custom-base-controller
|
# See https://doorkeeper.gitbook.io/guides/configuration/other-configurations#custom-base-controller
|
||||||
#
|
#
|
||||||
# base_controller 'ApplicationController'
|
base_controller "ApplicationController"
|
||||||
|
|
||||||
# Reuse access token for the same resource owner within an application (disabled by default).
|
# Reuse access token for the same resource owner within an application (disabled by default).
|
||||||
#
|
#
|
||||||
|
|
|
||||||
|
|
@ -1,145 +1,126 @@
|
||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
RSpec.describe "Api::V0::Articles", type: :request do
|
RSpec.describe "Api::V0::Articles", type: :request do
|
||||||
let(:user1) { create(:user) }
|
|
||||||
let(:user2) { create(:user) }
|
|
||||||
|
|
||||||
def json_response
|
def json_response
|
||||||
JSON.parse(response.body)
|
JSON.parse(response.body)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "GET /api/articles" do
|
describe "GET /api/articles" do
|
||||||
|
let_it_be(:article) { create(:article) }
|
||||||
|
|
||||||
it "returns json response" do
|
it "returns json response" do
|
||||||
get "/api/articles"
|
get api_articles_path
|
||||||
expect(response.content_type).to eq("application/json")
|
expect(response.content_type).to eq("application/json")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns featured articles with no params" do
|
it "returns featured articles if no param is given" do
|
||||||
create(:article)
|
|
||||||
create(:article, featured: true)
|
create(:article, featured: true)
|
||||||
create(:article, featured: true)
|
get api_articles_path
|
||||||
get "/api/articles"
|
expect(json_response.size).to eq(1)
|
||||||
expect(JSON.parse(response.body).size).to eq(2)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns user articles if username param is present" do
|
it "returns user's articles for the given username" do
|
||||||
create(:article, user_id: user1.id)
|
user = create(:user)
|
||||||
create(:article, user_id: user1.id)
|
create_list(:article, 2, user: user)
|
||||||
create(:article, user_id: user2.id)
|
get api_articles_path(username: user.username)
|
||||||
get "/api/articles?username=#{user1.username}"
|
expect(json_response.size).to eq(2)
|
||||||
expect(JSON.parse(response.body).size).to eq(2)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns no articles if username param is unknown" do
|
it "returns nothing if given user is not found" do
|
||||||
create(:article, user_id: user1.id)
|
get api_articles_path(username: "foobar")
|
||||||
get "/api/articles?username=foobar"
|
expect(json_response.size).to eq(0)
|
||||||
expect(JSON.parse(response.body).size).to eq(0)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns organization articles if username param is present" do
|
it "returns org's articles if org's slug is given" do
|
||||||
|
user = article.user
|
||||||
org = create(:organization)
|
org = create(:organization)
|
||||||
create(:article, user_id: user1.id)
|
create(:article, user: user)
|
||||||
create(:article, user_id: user1.id, organization_id: org.id)
|
create(:article, user: user, organization: org)
|
||||||
create(:article, user_id: user1.id, organization_id: org.id)
|
get api_articles_path(username: org.slug)
|
||||||
create(:article, user_id: user1.id)
|
expect(json_response.size).to eq(1)
|
||||||
create(:article, user_id: user2.id)
|
|
||||||
get "/api/articles?username=#{org.slug}"
|
|
||||||
expect(JSON.parse(response.body).size).to eq(2)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns tag articles if tag param is present" do
|
it "returns tag's articles" do
|
||||||
article = create(:article)
|
get api_articles_path(tag: article.tag_list.first)
|
||||||
get "/api/articles?tag=#{article.tag_list.first}"
|
expect(json_response.size).to eq(1)
|
||||||
expect(JSON.parse(response.body).size).to eq(1)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns top tag articles if tag param is present" do
|
it "returns top tag articles if tag and top param is present" do
|
||||||
article = create(:article)
|
get api_articles_path(tag: article.tag_list.first, top: "7")
|
||||||
get "/api/articles?tag=#{article.tag_list.first}&top=7"
|
expect(json_response.size).to eq(1)
|
||||||
expect(JSON.parse(response.body).size).to eq(1)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns top articles if tag param is present" do
|
it "returns top articles if top param is present" do
|
||||||
create(:article)
|
old_article = create(:article)
|
||||||
article = create(:article)
|
old_article.update_column(:published_at, 10.days.ago)
|
||||||
article.update_column(:published_at, 10.days.ago)
|
get api_articles_path(top: "7")
|
||||||
get "/api/articles?top=7"
|
expect(json_response.size).to eq(1)
|
||||||
expect(JSON.parse(response.body).size).to eq(1)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns not tag articles if article and tag are not approved" do
|
it "returns not tag articles if article and tag are not approved" do
|
||||||
article = create(:article, approved: false)
|
article.update_column(:approved, false)
|
||||||
tag = Tag.find_by(name: article.tag_list.first)
|
tag = Tag.find_by(name: article.tag_list.first)
|
||||||
tag.update(requires_approval: true)
|
tag.update(requires_approval: true)
|
||||||
get "/api/articles?tag=#{tag.name}"
|
|
||||||
|
get api_articles_path(tag: tag.name)
|
||||||
expect(JSON.parse(response.body).size).to eq(0)
|
expect(JSON.parse(response.body).size).to eq(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "GET /api/articles/:id" do
|
describe "GET /api/articles/:id" do
|
||||||
let(:article) { create(:article, published: true) }
|
let_it_be(:article) { create(:article) }
|
||||||
|
|
||||||
it "gets article based on ID" do
|
it "returns proper article" do
|
||||||
get "/api/articles/#{article.id}"
|
get api_article_path(article.id)
|
||||||
expect(json_response["title"]).to eq(article.title)
|
expect(json_response["title"]).to eq(article.title)
|
||||||
end
|
|
||||||
|
|
||||||
it "contains article markdown content" do
|
|
||||||
get "/api/articles/#{article.id}"
|
|
||||||
expect(json_response["body_markdown"]).to eq(article.body_markdown)
|
expect(json_response["body_markdown"]).to eq(article.body_markdown)
|
||||||
end
|
|
||||||
|
|
||||||
it "fails with an unpublished article" do
|
|
||||||
article.update_columns(published: false)
|
|
||||||
get "/api/articles/#{article.id}"
|
|
||||||
expect(response).to have_http_status(:not_found)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "fails with an unknown article ID" do
|
|
||||||
get "/api/articles/99999"
|
|
||||||
expect(response).to have_http_status(:not_found)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "contains tags as an array" do
|
|
||||||
get "/api/articles/#{article.id}"
|
|
||||||
expect(json_response["tags"]).to eq(article.decorate.cached_tag_list_array)
|
expect(json_response["tags"]).to eq(article.decorate.cached_tag_list_array)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "contains all the relevant datetimes" do
|
it "returns all the relevant datetimes" do
|
||||||
article.update_columns(
|
article.update_columns(
|
||||||
edited_at: 1.minute.from_now,
|
edited_at: 1.minute.from_now,
|
||||||
crossposted_at: 2.minutes.ago, last_comment_at: 30.seconds.ago
|
crossposted_at: 2.minutes.ago, last_comment_at: 30.seconds.ago
|
||||||
)
|
)
|
||||||
get "/api/articles/#{article.id}"
|
get api_article_path(article.id)
|
||||||
expect(json_response["created_at"]).to eq(article.created_at.utc.iso8601)
|
expect(json_response["created_at"]).to eq(article.created_at.utc.iso8601)
|
||||||
expect(json_response["edited_at"]).to eq(article.edited_at.utc.iso8601)
|
expect(json_response["edited_at"]).to eq(article.edited_at.utc.iso8601)
|
||||||
expect(json_response["crossposted_at"]).to eq(article.crossposted_at.utc.iso8601)
|
expect(json_response["crossposted_at"]).to eq(article.crossposted_at.utc.iso8601)
|
||||||
expect(json_response["published_at"]).to eq(article.published_at.utc.iso8601)
|
expect(json_response["published_at"]).to eq(article.published_at.utc.iso8601)
|
||||||
expect(json_response["last_comment_at"]).to eq(article.last_comment_at.utc.iso8601)
|
expect(json_response["last_comment_at"]).to eq(article.last_comment_at.utc.iso8601)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "fails with an unpublished article" do
|
||||||
|
article.update_columns(published: false)
|
||||||
|
get api_article_path(article.id)
|
||||||
|
expect(response).to have_http_status(:not_found)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "fails with an unknown article ID" do
|
||||||
|
get api_article_path("9999")
|
||||||
|
expect(response).to have_http_status(:not_found)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "POST /api/articles" do
|
describe "POST /api/articles" do
|
||||||
let!(:api_secret) { create(:api_secret) }
|
let!(:api_secret) { create(:api_secret) }
|
||||||
let!(:user) { api_secret.user }
|
let!(:user) { api_secret.user }
|
||||||
let!(:path) { "/api/articles" }
|
|
||||||
let(:organization) { create(:organization) }
|
|
||||||
|
|
||||||
describe "when unauthorized" do
|
context "when unauthorized" do
|
||||||
it "fails with no api key" do
|
it "fails with no api key" do
|
||||||
post path, headers: { "content-type" => "application/json" }
|
post api_articles_path, headers: { "content-type" => "application/json" }
|
||||||
expect(response).to have_http_status(:unauthorized)
|
expect(response).to have_http_status(:unauthorized)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails with the wrong api key" do
|
it "fails with the wrong api key" do
|
||||||
post path, headers: { "api-key" => "foobar", "content-type" => "application/json" }
|
post api_articles_path, headers: { "api-key" => "foobar", "content-type" => "application/json" }
|
||||||
expect(response).to have_http_status(:unauthorized)
|
expect(response).to have_http_status(:unauthorized)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails with a failing secure compare" do
|
it "fails with a failing secure compare" do
|
||||||
allow(ActiveSupport::SecurityUtils).
|
allow(ActiveSupport::SecurityUtils).
|
||||||
to receive(:secure_compare).and_return(false)
|
to receive(:secure_compare).and_return(false)
|
||||||
post path, headers: { "api-key" => api_secret.secret, "content-type" => "application/json" }
|
post api_articles_path, headers: { "api-key" => api_secret.secret, "content-type" => "application/json" }
|
||||||
expect(response).to have_http_status(:unauthorized)
|
expect(response).to have_http_status(:unauthorized)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -147,7 +128,7 @@ RSpec.describe "Api::V0::Articles", type: :request do
|
||||||
describe "when authorized" do
|
describe "when authorized" do
|
||||||
def post_article(**params)
|
def post_article(**params)
|
||||||
headers = { "api-key" => api_secret.secret, "content-type" => "application/json" }
|
headers = { "api-key" => api_secret.secret, "content-type" => "application/json" }
|
||||||
post path, params: { article: params }.to_json, headers: headers
|
post api_articles_path, params: { article: params }.to_json, headers: headers
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if no params are given" do
|
it "fails if no params are given" do
|
||||||
|
|
@ -263,6 +244,7 @@ RSpec.describe "Api::V0::Articles", type: :request do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "creates an article on behalf of an organization" do
|
it "creates an article on behalf of an organization" do
|
||||||
|
organization = create(:organization)
|
||||||
create(:organization_membership, user: user, organization: organization)
|
create(:organization_membership, user: user, organization: organization)
|
||||||
expect do
|
expect do
|
||||||
post_article(
|
post_article(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue