<commit_before>require 'spec_helper'
require 'faker'

include Rack::Test::Methods
include Mail::Matchers

describe 'Alerts', :type => :feature do
  include_context 'test data'

  before(:each) do
    Mail::TestMailer.deliveries.clear
    set_environment_variable('GASTRO_RESET_TOKEN', gastro_reset_token)
    set_environment_variable('MORPH_API_KEY', morph_api_key)
  end

  it 'should allow a user to subscribe' do
    subscribed_user
  end

  describe 'validate' do
    it 'has an email' do
      within_25km && within_150km

      # Create it at /search
      visit "/search?lat=#{origin.lat}&lng=#{origin.lng}&address=foobar"
      click_on 'Create alert'
      expect(page.status_code).to be 400
      expect(page.body).to match(/there was a problem creating your alert/i)
      expect(page.body).to match(/we need a valid email address/i)

      GotGastro::Workers::EmailWorker.drain
      expect(Mail::TestMailer.deliveries.size).to be 0

      # Try to create, but with invalid input
      fill_in 'alert[email]', :with => 'aoesntoasnetaoesntoaesnatoesnaote'
      click_on 'Create alert'
      expect(page.status_code).to be 400
      expect(page.body).to match(/there was a problem creating your alert/i)
      expect(page.body).to match(/we need a valid email address/i)

      GotGastro::Workers::EmailWorker.drain
      expect(Mail::TestMailer.deliveries.size).to be 0

      # Create with valid input
      fill_in 'alert[email]', :with => '<EMAIL>'
      click_on 'Create alert'
      expect(page.status_code).to be 200
      expect(page.body).to match(/now check your email/i)

      GotGastro::Workers::EmailWorker.drain
      expect(Mail::TestMailer.deliveries.size).to be 1
    end

    it 'has an address' do
      within_25km && within_150km

      # Create it at /search
      # Muck with the hidden fields
      visit "/search?lat=#{origin.lat}&lng=#{origin.lng}&address="
      click_on 'Create alert'
      expect(page.status_code).to be 400
      expect(page.body).to match(/there was a problem creating your alert/i)
      expect(page.body).to match(/we need an address to create an alert/i)

      GotGastro::Workers::EmailWorker.drain
      expect(Mail::TestMailer.deliveries.size).to be 0

      # There's really no way to recover from this. If you've tried to create
      # an alert by messing with the form parameters, you're gonna have a bad
      # time.
    end

    it 'has a location' do
      within_25km && within_150km

      # Create it at /search
      # Muck with the hidden fields
      visit "/search?lat=&lng=&address="
      click_on 'Create alert'
      expect(page.status_code).to be 400
      expect(page.body).to match(/there was a problem creating your alert/i)
      expect(page.body).to match(/we need a location to create an alert/i)

      GotGastro::Workers::EmailWorker.drain
      expect(Mail::TestMailer.deliveries.size).to be 0

      # There's really no way to recover from this. If you've tried to create
      # an alert by messing with the form parameters, you're gonna have a bad
      # time.
    end

    it 'has a distance' do
      within_25km && within_150km

      # Create it at /search
      # Muck with the hidden fields
      visit "/search?lat=&lng=&address="
      find(:xpath, "//input[@name='alert[distance]']").set ''
      click_on 'Create alert'
      expect(page.status_code).to be 400
      expect(page.body).to match(/there was a problem creating your alert/i)
      expect(page.body).to match(/we need a distance to create an alert/i)

      GotGastro::Workers::EmailWorker.drain
      expect(Mail::TestMailer.deliveries.size).to be 0

      # There's really no way to recover from this. If you've tried to create
      # an alert by messing with the form parameters, you're gonna have a bad
      # time.
    end
  end

  it 'should deny unknown confirmations' do
    visit '/alert/12345/confirm'
    expect(page.status_code).to be 404
  end

  it 'should send notifications when new offences are added' do
    stub_request(:get,
      %r{https://api\.morph\.io/auxesis/gotgastro_scraper/data\.json\?key=.*&query=select%20\*%20from%20'businesses'}
      ).to_return(:status => 200, :body => business_json)

    stub_request(:get,<commit_msg>Fix matcher to find invisible elements<commit_after>require 'spec_helper'
require 'faker'

include Rack::Test::Methods
include Mail::Matchers

describe 'Alerts', :type => :feature do
  include_context 'test data'

  before(:each) do
    Mail::TestMailer.deliveries.clear
    set_environment_variable('GASTRO_RESET_TOKEN', gastro_reset_token)
    set_environment_variable('MORPH_API_KEY', morph_api_key)
  end

  it 'should allow a user to subscribe' do
    subscribed_user
  end

  describe 'validate' do
    it 'has an email' do
      within_25km && within_150km

      # Create it at /search
      visit "/search?lat=#{origin.lat}&lng=#{origin.lng}&address=foobar"
      click_on 'Create alert'
      expect(page.status_code).to be 400
      expect(page.body).to match(/there was a problem creating your alert/i)
      expect(page.body).to match(/we need a valid email address/i)

      GotGastro::Workers::EmailWorker.drain
      expect(Mail::TestMailer.deliveries.size).to be 0

      # Try to create, but with invalid input
      fill_in 'alert[email]', :with => 'aoesntoasnetaoesntoaesnatoesnaote'
      click_on 'Create alert'
      expect(page.status_code).to be 400
      expect(page.body).to match(/there was a problem creating your alert/i)
      expect(page.body).to match(/we need a valid email address/i)

      GotGastro::Workers::EmailWorker.drain
      expect(Mail::TestMailer.deliveries.size).to be 0

      # Create with valid input
      fill_in 'alert[email]', :with => '<EMAIL>'
      click_on 'Create alert'
      expect(page.status_code).to be 200
      expect(page.body).to match(/now check your email/i)

      GotGastro::Workers::EmailWorker.drain
      expect(Mail::TestMailer.deliveries.size).to be 1
    end

    it 'has an address' do
      within_25km && within_150km

      # Create it at /search
      # Muck with the hidden fields
      visit "/search?lat=#{origin.lat}&lng=#{origin.lng}&address="
      click_on 'Create alert'
      expect(page.status_code).to be 400
      expect(page.body).to match(/there was a problem creating your alert/i)
      expect(page.body).to match(/we need an address to create an alert/i)

      GotGastro::Workers::EmailWorker.drain
      expect(Mail::TestMailer.deliveries.size).to be 0

      # There's really no way to recover from this. If you've tried to create
      # an alert by messing with the form parameters, you're gonna have a bad
      # time.
    end

    it 'has a location' do
      within_25km && within_150km

      # Create it at /search
      # Muck with the hidden fields
      visit "/search?lat=&lng=&address="
      click_on 'Create alert'
      expect(page.status_code).to be 400
      expect(page.body).to match(/there was a problem creating your alert/i)
      expect(page.body).to match(/we need a location to create an alert/i)

      GotGastro::Workers::EmailWorker.drain
      expect(Mail::TestMailer.deliveries.size).to be 0

      # There's really no way to recover from this. If you've tried to create
      # an alert by messing with the form parameters, you're gonna have a bad
      # time.
    end

    it 'has a distance' do
      within_25km && within_150km

      # Create it at /search
      # Muck with the hidden fields
      visit "/search?lat=&lng=&address="
      find(:xpath, "//input[@name='alert[distance]']", visible: false).set ''
      click_on 'Create alert'
      expect(page.status_code).to be 400
      expect(page.body).to match(/there was a problem creating your alert/i)
      expect(page.body).to match(/we need a distance to create an alert/i)

      GotGastro::Workers::EmailWorker.drain
      expect(Mail::TestMailer.deliveries.size).to be 0

      # There's really no way to recover from this. If you've tried to create
      # an alert by messing with the form parameters, you're gonna have a bad
      # time.
    end
  end

  it 'should deny unknown confirmations' do
    visit '/alert/12345/confirm'
    expect(page.status_code).to be 404
  end

  it 'should send notifications when new offences are added' do
    stub_request(:get,
      %r{https://api\.morph\.io/auxesis/gotgastro_scraper/data\.json\?key=.*&query=select%20\*%20from%20'businesses'}
      ).to_return(:status => 200, :body => business_json)

    stub_request(:get,
{'fraction_non_alphanumeric': 0.10563463121985649, 'fraction_numerical': 0.014351252793788965, 'mean_word_length': 3.0640535372848947, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '6844949', 'n_tokens_mistral': 2789, 'n_tokens_neox': 2604, 'n_words': 784}