diff --git a/app/controllers/api/v1/accounts/articles_controller.rb b/app/controllers/api/v1/accounts/articles_controller.rb index da2be2312..8a6fd61f8 100644 --- a/app/controllers/api/v1/accounts/articles_controller.rb +++ b/app/controllers/api/v1/accounts/articles_controller.rb @@ -22,9 +22,10 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController def edit; end def create - @article = @portal.articles.create!(article_params) + params_with_defaults = article_params + params_with_defaults[:status] ||= :draft + @article = @portal.articles.create!(params_with_defaults) @article.associate_root_article(article_params[:associated_article_id]) - @article.draft! render json: { error: @article.errors.messages }, status: :unprocessable_entity and return unless @article.valid? end diff --git a/spec/controllers/api/v1/accounts/articles_controller_spec.rb b/spec/controllers/api/v1/accounts/articles_controller_spec.rb index 42fd062dc..e74be76e2 100644 --- a/spec/controllers/api/v1/accounts/articles_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/articles_controller_spec.rb @@ -36,7 +36,7 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do expect(response).to have_http_status(:success) json_response = response.parsed_body expect(json_response['payload']['title']).to eql('MyTitle') - expect(json_response['payload']['status']).to eql('draft') + expect(json_response['payload']['status']).to eql('published') expect(json_response['payload']['position']).to be(3) end @@ -59,10 +59,30 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do expect(response).to have_http_status(:success) json_response = response.parsed_body expect(json_response['payload']['title']).to eql('MyTitle') - expect(json_response['payload']['status']).to eql('draft') + expect(json_response['payload']['status']).to eql('published') expect(json_response['payload']['position']).to be(3) end + it 'creates article as draft when status is not provided' do + article_params = { + article: { + category_id: category.id, + description: 'test description', + title: 'DraftTitle', + slug: 'draft-title', + content: 'This is my draft content.', + author_id: agent.id + } + } + post "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles", + params: article_params, + headers: admin.create_new_auth_token + expect(response).to have_http_status(:success) + json_response = response.parsed_body + expect(json_response['payload']['title']).to eql('DraftTitle') + expect(json_response['payload']['status']).to eql('draft') + end + it 'associate to the root article' do root_article = create(:article, category: category, slug: 'root-article', portal: portal, account_id: account.id, author_id: agent.id, associated_article_id: nil)