<commit_before>require "test_helper"

# Controller tests for news articles
class ArticleControllerTest < FunctionalTestCase
  # ----------------------------
  #  Create article
  # ----------------------------
  def test_create_article_get
    # Prove user cannot see article form unless in admin mode
    user = users(:rolf)
    login(user.login)
    get(:create_article)
    assert_flash_text(:permission_denied.l)

    make_admin
    get(:create_article)
    assert_form_action(action: "create_article")
  end

  def test_create_article_post
    user   = users(:rolf)
    author = user.name
    name   = "<NAME>"
    body   = "The body of a new test article."
    params = {
      article: {
        name:    name,
        author:  author,
        body:    body
      }
    }
    old_count = Article.count

    # Prove only authorized users can create articles
    login(user.login)
    post(:create_article, params)
    assert_flash_text(:permission_denied.l)
    assert_equal(old_count, Article.count)

    # Prove article is created
    make_admin
    post(:create_article, params)
    assert_equal(old_count + 1, Article.count)
    article = Article.last
    assert_equal(body,   article.body)
    assert_equal(name,   article.name)
    assert_redirected_to(action: :show_article, id: article.id)
  end

  def test_index
    get(:index)
    assert_template(:index)
  end

  def test_show_article
    get(:show_article, id: articles(:premier_article).id)
    assert_template(:show_article)
  end

=begin
    def test_edit_article
    # Prove only authorized users can create articles
    article = articles(:premier_article)<commit_msg>Improve ArticleController create_article authorization tests<commit_after>require "test_helper"

# Controller tests for news articles
class ArticleControllerTest < FunctionalTestCase
  def test_create_article_get
    # Prove unathorized user cannot see create_article form
    user = users(:rolf)
    login(user.login)
    get(:create_article)
    assert_flash_text(:permission_denied.l)
    assert_redirected_to(action: :index)

    # Prove authorized user can go to create_article form
    make_admin
    get(:create_article)
    assert_form_action(action: "create_article")
  end

  def test_create_article_post
    user   = users(:rolf)
    author = user.name
    name   = "<NAME>"
    body   = "The body of a new test article."
    params = {
      article: {
        name:    name,
        author:  author,
        body:    body
      }
    }
    old_count = Article.count

    # Prove unauthorized user cannot create article
    login(user.login)
    post(:create_article, params)
    assert_flash_text(:permission_denied.l)
    assert_equal(old_count, Article.count)
    assert_redirected_to(action: :index)

    # Prove authorized user can create article
    make_admin
    post(:create_article, params)
    assert_equal(old_count + 1, Article.count)
    article = Article.last
    assert_equal(body, article.body)
    assert_equal(name, article.name)
    assert_redirected_to(action: :show_article, id: article.id)
  end

  def test_index
    get(:index)
    assert_template(:index)
  end

  def test_show_article
    get(:show_article, id: articles(:premier_article).id)
    assert_template(:show_article)
  end

=begin
    def test_edit_article
    # Prove only authorized users can create articles
    article = articles(:premier_article)
{'fraction_non_alphanumeric': 0.09372236958443855, 'fraction_numerical': 0.0005894488653109342, 'mean_word_length': 3.108958837772397, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4032154', 'n_tokens_mistral': 1031, 'n_tokens_neox': 1006, 'n_words': 250}