<commit_before># encoding: utf-8

require 'test_helper'

describe Api::StoriesController do
  let(:story) { create(:story) }

  before(:all) { Story.delete_all }

  describe 'editing' do

    let(:user) { create(:user) }
    let(:account) { user.approved_accounts.first }

    before { @controller.current_user = user }

    it 'can create a new story' do
      story_hash = { title: 'create story', set_account_uri: "/api/v1/accounts/#{account.id}" }
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :create, story_hash.to_json, api_version: 'v1', format: 'json'
      assert_response :success
    end

    it 'can create a new story with url parameters' do
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :create,
           { title: 'story' }.to_json,
           api_version: 'v1',
           account_id: account.id
      assert_response :success
      id = JSON.parse(response.body)['id']
      Story.find(id).account_id.must_equal account.id
    end

    it 'rejects new stories with an invalid account' do
      new_account = create(:account)
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :create,
           { title: 'story' }.to_json,
           api_version: 'v1',
           account_id: new_account.id
      response.status.must_equal 401
    end

    it 'can update a story' do
      story = create(:story, title: 'not this', account: account)
      @request.env['CONTENT_TYPE'] = 'application/json'
      put :update,
          { title: 'this' }.to_json,
          api_version: 'v1',
          format: 'json',
          id: story.id
      assert_response :success
      Story.find(story.id).title.must_equal('this')
    end

    it 'can publish a story' do
      story = create(:story, title: 'not this', account: account, published_at: nil)
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :publish, api_version: 'v1', format: 'json', id: story.id
      assert_response :success
      Story.find(story.id).published_at.wont_be_nil
    end

    it 'can unpublish a story' do
      story = create(:story, title: 'not this', account: account, published_at: Time.now)
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :unpublish, api_version: 'v1', format: 'json', id: story.id
      assert_response :success
      Story.find(story.id).published_at.must_be_nil
    end

    it 'can delete a story' do
      story = create(:story, account: account)
      delete :destroy, api_version: 'v1', format: 'json', id: story.id
      response.status.must_equal 204
      Story.where(id: story.id).must_be :empty?
    end
  end<commit_msg>shorter lines<commit_after># encoding: utf-8

require 'test_helper'

describe Api::StoriesController do
  let(:story) { create(:story) }

  before(:all) { Story.delete_all }

  describe 'editing' do

    let(:user) { create(:user) }
    let(:account) { user.approved_accounts.first }

    before { @controller.current_user = user }

    it 'can create a new story' do
      story_hash = { title: 'create story', set_account_uri: "/api/v1/accounts/#{account.id}" }
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :create, story_hash.to_json, api_version: 'v1', format: 'json'
      assert_response :success
    end

    it 'can create a new story with url parameters' do
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :create,
           { title: 'story' }.to_json,
           api_version: 'v1',
           account_id: account.id
      assert_response :success
      id = JSON.parse(response.body)['id']
      Story.find(id).account_id.must_equal account.id
    end

    it 'rejects new stories with an invalid account' do
      new_account = create(:account)
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :create,
           { title: 'story' }.to_json,
           api_version: 'v1',
           account_id: new_account.id
      response.status.must_equal 401
    end

    it 'can update a story' do
      story = create(:story, title: 'not this', account: account)
      @request.env['CONTENT_TYPE'] = 'application/json'
      put :update,
          { title: 'this' }.to_json,
          api_version: 'v1',
          format: 'json',
          id: story.id
      assert_response :success
      Story.find(story.id).title.must_equal('this')
    end

    it 'can publish a story' do
      story = create(:story,
        title: 'not this',
        account: account,
        published_at: nil
      )
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :publish, api_version: 'v1', format: 'json', id: story.id
      assert_response :success
      Story.find(story.id).published_at.wont_be_nil
    end

    it 'can unpublish a story' do
      story = create(:story,
        title: 'not this',
        account: account,
        published_at: Time.now
      )
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :unpublish, api_version: 'v1', format: 'json', id: story.id
      assert_response :success
      Story.find(story.id).published_at.must_be_nil
    end

    it 'can delete a story' do
      story = create(:story, account: account)
      delete :destroy, api_version: 'v1', format: 'json', id: story.id
      response.status.must_equal 204
      Story.where(id: story.id).must_be :empty?
    end
  end
{'fraction_non_alphanumeric': 0.12514092446448705, 'fraction_numerical': 0.005636978579481398, 'mean_word_length': 2.706824512534819, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '363652', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1676, 'n_words': 426}