<commit_before>require 'rails_helper'

describe "/api/v1/farms", type: :request do
  let(:url) { "/api/v1" }
  let(:another_user) { create(:user, name: "Another User") }

  before do
    @farm1 = create(:farm, name: "farm 1")
    @farm2 = create(:farm, name: "farm 2")
    @orphan_farm = create(:orphan_farm, name: "Orphan farm")
  end

  def expected_index_response_for(farm, authorized)
    response =
    { "id" => farm.id,
      "name" => farm.name,
      "city" => farm.city,
      "address" => farm.address,
      "latitude" => farm.latitude.to_s,
      "longitude" => farm.longitude.to_s,
      "accepts_new_members" => farm.accepts_new_members,
      "is_established" => farm.is_established,
      "description" => farm.description,
      "updated_at" => farm.updated_at.as_json,
      "vegetable_products" => farm.vegetable_products.as_json,
      "animal_products" => farm.animal_products.as_json,
      "beverages" => farm.beverages.as_json,
      "related_places_count"=> farm.related_places_count,
      "type" => farm.type
    }
    methods = authorized ? [:name, :email, :phone] : [:name]
    response = response.merge({
      "ownerships" => farm.ownerships.map{|o| {ownership: o.as_json(except: [:id, :place_id], methods: methods)}}.as_json
    })
    response
  end

  def expected_show_response_for(farm, authorized)
    response = expected_index_response_for(farm, authorized)
    response = response.merge(
      { "places" => farm.places,
        "additional_product_information" => farm.additional_product_information,
        "contact_function" => farm.contact_function,
        "url" => farm.url,
        "acts_ecological" => farm.acts_ecological,
        "economical_behavior" => farm.economical_behavior,
        "participation" => farm.participation,
        "founded_at_year" => farm.founded_at_year,
        "founded_at_month" => farm.founded_at_month,
        "maximum_members" => farm.maximum_members,
        "image" => {"description" => farm.image.description,
                    "url" => nil,
                    "thumbnail_url" => nil}
    })
    methods = authorized ? [:name, :email, :phone] : [:name]
    response = response.merge({
      "ownerships" => farm.ownerships.map{|o| {ownership: o.as_json(except: [:id, :place_id], methods: methods)}}.as_json
    })
    response
  end

  shared_examples_for "a non-existing farm" do
    it "returns an error" do
      non_existing_id = "99999"
      get "#{url}/farms/#{non_existing_id}"
      expect_record_not_found_failure(last_response, "Farm", non_existing_id)
    end
  end

  shared_examples_for "a readable farm" do
    it "returns a farm" do
      get "#{url}/farms/#{@farm1.id}"
<commit_msg>Fix rspec api tests after upgrade.<commit_after>require 'rails_helper'

describe "/api/v1/farms", type: :request do
  let(:url) { "/api/v1" }
  let(:another_user) { create(:user, name: "Another User") }

  before do
    @farm1 = create(:farm, name: "farm 1")
    @farm2 = create(:farm, name: "farm 2")
    @orphan_farm = create(:orphan_farm, name: "Orphan farm")
  end

  def expected_index_response_for(farm, authorized)
    response =
    { "id" => farm.id,
      "name" => farm.name,
      "city" => farm.city,
      "address" => farm.address,
      "latitude" => farm.latitude.to_s.to_f,
      "longitude" => farm.longitude.to_s.to_f,
      "accepts_new_members" => farm.accepts_new_members,
      "is_established" => farm.is_established,
      "description" => farm.description,
      "updated_at" => farm.updated_at.as_json,
      "vegetable_products" => farm.vegetable_products.as_json,
      "animal_products" => farm.animal_products.as_json,
      "beverages" => farm.beverages.as_json,
      "related_places_count"=> farm.related_places_count,
      "type" => farm.type
    }
    methods = authorized ? [:name, :email, :phone] : [:name]
    response = response.merge({
      "ownerships" => farm.ownerships.map { |o|
        {"ownership" => {
          "user_id" => o.user_id,
          "name" => o.name,
          "contact_by_phone" => o.contact_by_phone,
          "contact_by_email" => o.contact_by_email,
          "email" => o.email,
          "phone" => o.phone
        }
        }
      }
    })
    response
  end

  def expected_show_response_for(farm, authorized)
    response = expected_index_response_for(farm, authorized)
    response = response.merge(
      { "places" => farm.places,
        "additional_product_information" => farm.additional_product_information,
        "contact_function" => farm.contact_function,
        "url" => farm.url,
        "acts_ecological" => farm.acts_ecological,
        "economical_behavior" => farm.economical_behavior,
        "participation" => farm.participation,
        "founded_at_year" => farm.founded_at_year,
        "founded_at_month" => farm.founded_at_month,
        "maximum_members" => farm.maximum_members,
        "image" => {"description" => farm.image.description,
                    "url" => nil,
                    "thumbnail_url" => nil}
    })
    methods = authorized ? [:name, :email, :phone] : [:name]
    response = response.merge({
        "ownerships" => farm.ownerships.map { |o|
          {"ownership" => {
              "user_id" => o.user_id,
              "name" => o.name,
              "contact_by_phone" => o.contact_by_phone,
              "contact_by_email" => o.contact_by_email,
              "email" => o.email,
              "phone" => o.phone
          }
          }
        }
    })
    response
  end

  shared_examples_for "a non-existing farm" do
    it "returns an error" do
      non_existing_id = "99999"
      get "#{url}/farms/#{non_existing_id}"
      expect_record_not_found_failure(last_response, "Farm", non_existing_id)
    end
  end

  shared_examples_for "a readable farm" do
    it "returns a farm" do
      get "#{url}/farms/#{@farm1.id}"

{'fraction_non_alphanumeric': 0.14173362968028722, 'fraction_numerical': 0.0041032655154727305, 'mean_word_length': 2.811074918566775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 79, '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': '326171', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1948, 'n_words': 357}