<commit_before>require 'rails_helper'

describe 'PATCH /locations/:location_id/services/:id' do
  before(:each) do
    create_service
    @token = ENV['ADMIN_APP_TOKEN']
    @attrs = { name: 'New Service', description: 'Hot Meals' }
  end

  it 'returns 200 when validations pass' do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      @attrs,
      'HTTP_X_API_TOKEN' => @token
    )
    expect(response).to have_http_status(200)
  end

  it 'returns the updated service when validations pass' do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      @attrs,
      'HTTP_X_API_TOKEN' => @token
    )
    expect(json['name']).to eq 'New Service'
  end

  it "updates the location's service" do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      @attrs,
      'HTTP_X_API_TOKEN' => @token
    )
    get api_endpoint(path: "/locations/#{@location.id}")
    expect(json['services'].first['description']).to eq 'Hot Meals'
  end

  it "doesn't add a new service" do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      @attrs,
      'HTTP_X_API_TOKEN' => @token
    )
    expect(Service.count).to eq(1)
  end

  it 'requires a valid service id' do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/123"),
      @attrs,
      'HTTP_X_API_TOKEN' => @token
    )
    expect(response.status).to eq(404)
    expect(json['message']).
      to eq('The requested resource could not be found.')
  end

  it 'returns 422 when attribute is invalid' do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      @attrs.merge!(keywords: 'food'),
      'HTTP_X_API_TOKEN' => @token
    )
    expect(response.status).to eq(422)
    expect(json['message']).to eq('Validation failed for resource.')
    expect(json['error']).
      to eq('Attribute was supposed to be an Array, but was a String: "food".')
  end

  it "doesn't allow updating a service without a valid token" do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      @attrs,
      'HTTP_X_API_TOKEN' => 'invalid_token'
    )
    expect(response.status).to eq(401)
  end

  it 'updates search index when service changes' do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      { description: 'fresh tunes for the soul' },
      'HTTP_X_API_TOKEN' => @token
    )
    get api_endpoint(path: '/search?keyword=yoga')
    expect(headers['X-Total-Count']).to eq '0'
  end
end<commit_msg>Update service validation spec<commit_after>require 'rails_helper'

describe 'PATCH /locations/:location_id/services/:id' do
  before(:each) do
    create_service
    @token = ENV['ADMIN_APP_TOKEN']
    @attrs = { name: 'New Service', description: 'Hot Meals' }
  end

  it 'returns 200 when validations pass' do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      @attrs,
      'HTTP_X_API_TOKEN' => @token
    )
    expect(response).to have_http_status(200)
  end

  it 'returns the updated service when validations pass' do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      @attrs,
      'HTTP_X_API_TOKEN' => @token
    )
    expect(json['name']).to eq 'New Service'
  end

  it "updates the location's service" do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      @attrs,
      'HTTP_X_API_TOKEN' => @token
    )
    get api_endpoint(path: "/locations/#{@location.id}")
    expect(json['services'].first['description']).to eq 'Hot Meals'
  end

  it "doesn't add a new service" do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      @attrs,
      'HTTP_X_API_TOKEN' => @token
    )
    expect(Service.count).to eq(1)
  end

  it 'requires a valid service id' do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/123"),
      @attrs,
      'HTTP_X_API_TOKEN' => @token
    )
    expect(response.status).to eq(404)
    expect(json['message']).
      to eq('The requested resource could not be found.')
  end

  it 'returns 422 when attribute is invalid' do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      @attrs.merge!(service_areas: ['Belmont, CA']),
      'HTTP_X_API_TOKEN' => @token
    )
    expect(response.status).to eq(422)
    expect(json['message']).to eq('Validation failed for resource.')
    expect(json['errors'].first['service_areas'].first).
      to include('At least one service area is improperly formatted')
  end

  it "doesn't allow updating a service without a valid token" do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      @attrs,
      'HTTP_X_API_TOKEN' => 'invalid_<PASSWORD>'
    )
    expect(response.status).to eq(401)
  end

  it 'updates search index when service changes' do
    patch(
      api_endpoint(path: "/locations/#{@location.id}/services/#{@service.id}"),
      { description: 'fresh tunes for the soul' },
      'HTTP_X_API_TOKEN' => @token
    )
    get api_endpoint(path: '/search?keyword=yoga')
    expect(headers['X-Total-Count']).to eq '0'
  end
end
{'fraction_non_alphanumeric': 0.15565509518477044, 'fraction_numerical': 0.008585293019783502, 'mean_word_length': 3.2667197452229297, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 20, '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': '6250364', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1769, 'n_words': 399}