Untitled

mail@pastecode.io avatar
unknown
ruby
2 years ago
2.1 kB
4
Indexable
require "test_helper"

class ArticlesControllerTest < ActionDispatch::IntegrationTest
  def setup
    @organization = create(:organization)
    @author = create(:user, organization: @organization)
    @category = create(:category, organization: @organization)
    @article = create(:article, author: @author)
  end

  def test_should_list_all_articles_of_organization
    get articles_path, as: :json
    assert_response :success
    response_json = response.parsed_body
    assert_equal @organization.articles.count, response_json["articles"].length
  end

  def test_should_create_valid_article
    post articles_path,
      params: {
        article: { title: "Lorem Ipsum", body: "Lorel", category_id: @category.id, author_id: @author.id },
        status: "draft"
      }, as: :json
    assert_response :success
    response_json = response.parsed_body
    assert_equal t("successfully_created", entity: "Article"), response_json["notice"]
  end

  def test_shouldnt_create_article_without_title
    post articles_path,
      params: {
        article: { title: "", body: "Lorel", category_id: @category.id, author_id: @author.id },
        status: "draft"
      }, as: :json
    assert_response :unprocessable_entity
    response_json = response.parsed_body
    assert_equal "Title can't be blank", response_json["error"]
  end

  def test_should_destroy_article
    assert_difference "Article.count", -1 do
      delete article_path(@article.id), as: :json
    end
    assert_response :ok
  end

  def test_user_can_change_status_of_article
    article_params = { article: { status: "published" } }

    put article_path(@article.id), params: article_params, as: :json
    assert_response :success
    @article.reload
    assert @article.published?
  end

  def test_not_found_error_rendered_for_invalid_article_id
    invalid_id = "df90"
    get article_path(invalid_id), as: :json
    assert_response :not_found
  end

  def test_show_article
    get article_path(@article.id), as: :json
    assert_response :ok
    response_json = response.parsed_body
    assert_equal @article.body, response_json["article"]["body"]
  end
end