<commit_before>require 'test_helper'

describe Api::PodcastsController do
  let(:account_id) { 123 }
  let(:podcast) { create(:podcast, prx_account_uri: "/api/v1/accounts/#{account_id}") }
  let(:podcast_redirect) { create(:podcast, prx_account_uri: "/api/v1/accounts/#{account_id}", published_at: nil) }
  let(:token) { StubToken.new(account_id, ['member']) }
  let(:bad_token) { StubToken.new(account_id + 100, ['member']) }

  let(:podcast_hash) do
    {
      path: 'testcast',
      prxUri: '/api/v1/series/123',
      prxAccountUri: "/api/v1/accounts/#{account_id}",
      itunesCategories: [{ name: 'Arts', subcategories: ['Design', 'Fashion & Beauty'] }]
    }
  end

  describe 'with a valid token' do
    before do
      class << @controller; attr_accessor :prx_auth_token; end
      @controller.prx_auth_token = token
      @request.env['CONTENT_TYPE'] = 'application/json'
    end

    it 'should redirect for authorized request of unpublished resource' do
      podcast_redirect.id.wont_be_nil
      get(:show, { api_version: 'v1', format: 'json', id: podcast_redirect.id } )
      assert_response :redirect
    end

    it 'can create a new podcast' do
      @controller.stub(:publish, true) do
        post :create, podcast_hash.to_json, api_version: 'v1', format: 'json'
      end
      assert_response :success
      id = JSON.parse(response.body)['id']
      new_podcast = Podcast.find(id)
      new_podcast.itunes_categories.first.name.must_equal 'Arts'
    end

    it 'can update a podcast' do
      pua = podcast.updated_at
      podcast.itunes_categories.size.must_be :>, 0
      update_hash = { itunesCategories: [] }

      @controller.stub(:publish, true) do
        put :update, update_hash.to_json, id: podcast.id, api_version: 'v1', format: 'json'
      end
      assert_response :success

      podcast.reload.updated_at.must_be :>, pua
      podcast.itunes_categories.size.must_equal 0
    end

    it 'rejects update for unauthorizd token' do
      @controller.prx_auth_token = bad_token
      pua = podcast.updated_at
      podcast.itunes_categories.size.must_be :>, 0
      update_hash = { itunesCategories: [] }

      @controller.stub(:publish, true) do
        put :update, update_hash.to_json, id: podcast.id, api_version: 'v1', format: 'json'
      end
      assert_response 401
    end

    it 'rejects create without valid token' do
      @controller.prx_auth_token = nil
      post :create, podcast_hash.to_json, api_version: 'v1', format: 'json'
      assert_response 401
    end
  end

  it 'should show' do
    get(:show, { api_version: 'v1', format: 'json', id: podcast.id } )
    assert_response :success<commit_msg>test for errors, and writing to read only properties<commit_after>require 'test_helper'

describe Api::PodcastsController do
  let(:account_id) { 123 }
  let(:podcast) { create(:podcast, prx_account_uri: "/api/v1/accounts/#{account_id}") }
  let(:podcast_redirect) { create(:podcast, prx_account_uri: "/api/v1/accounts/#{account_id}", published_at: nil) }
  let(:token) { StubToken.new(account_id, ['member']) }
  let(:bad_token) { StubToken.new(account_id + 100, ['member']) }

  let(:podcast_hash) do
    {
      path: 'testcast',
      prxUri: '/api/v1/series/123',
      prxAccountUri: "/api/v1/accounts/#{account_id}",
      itunesCategories: [{ name: 'Arts', subcategories: ['Design', 'Fashion & Beauty'] }]
    }
  end

  describe 'with a valid token' do
    before do
      class << @controller; attr_accessor :prx_auth_token; end
      @controller.prx_auth_token = token
      @request.env['CONTENT_TYPE'] = 'application/json'
    end

    it 'should redirect for authorized request of unpublished resource' do
      podcast_redirect.id.wont_be_nil
      get(:show, { api_version: 'v1', format: 'json', id: podcast_redirect.id } )
      assert_response :redirect
    end

    it 'can create a new podcast' do
      @controller.stub(:publish, true) do
        post :create, podcast_hash.to_json, api_version: 'v1', format: 'json'
      end
      assert_response :success
      id = JSON.parse(response.body)['id']
      new_podcast = Podcast.find(id)
      new_podcast.itunes_categories.first.name.must_equal 'Arts'
    end

    it 'can update a podcast' do
      pua = podcast.updated_at
      podcast.itunes_categories.size.must_be :>, 0
      update_hash = { itunesCategories: [] }

      @controller.stub(:publish, true) do
        put :update, update_hash.to_json, id: podcast.id, api_version: 'v1', format: 'json'
      end
      assert_response :success

      podcast.reload.updated_at.must_be :>, pua
      podcast.itunes_categories.size.must_equal 0
    end

    it 'returns errors for any errors' do
      create(:podcast, prx_account_uri: "/api/v1/accounts/#{account_id}", path: 'dupe')
      pua = podcast.updated_at
      podcast.itunes_categories.size.must_be :>, 0
      update_hash = { path: 'dupe' }

      @controller.stub(:publish, true) do
        put :update, update_hash.to_json, id: podcast.id, api_version: 'v1', format: 'json'
      end
      assert_response :error
    end

    it 'ignores updating readonly attribute' do
      pua = podcast.updated_at
      podcast.itunes_categories.size.must_be :>, 0
      update_hash = { published_url: 'this is read only' }

      @controller.stub(:publish, true) do
        put :update, update_hash.to_json, id: podcast.id, api_version: 'v1', format: 'json'
      end
      assert_response :success
      JSON.parse(response.body)['published_url'].wont_equal 'this is read only'
    end

    it 'rejects update for unauthorizd token' do
      @controller.prx_auth_token = bad_token
      pua = podcast.updated_at
      podcast.itunes_categories.size.must_be :>, 0
      update_hash = { itunesCategories: [] }

      @controller.stub(:publish, true) do
        put :update, update_hash.to_json, id: podcast.id, api_version: 'v1', format: 'json'
      end
      assert_response 401
    end

    it 'rejects create without valid token' do
      @controller.prx_auth_token = nil
      post :create, podcast_hash.to_json, api_version: 'v1', format: 'json'
      assert_response 401
    end
  end

  it 'should show' do
    get(:show, { api_version: 'v1', format: 'json', id: podcast.id } )
    assert_response :success
{'fraction_non_alphanumeric': 0.12094064949608063, 'fraction_numerical': 0.009758438649816029, 'mean_word_length': 3.2243243243243245, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '7582652', 'n_tokens_mistral': 2113, 'n_tokens_neox': 2060, 'n_words': 483}