<commit_before>require_relative './spec_helper'
require_relative '../src/app/rss2slack_handler'
require 'logger'

class MockOutgoingMessage
  def self.normal_body(other)
    base = {
      'token' => 'TEST_TOKEN', # valid
      'team_id' => 'TEST',
      'team_domain' => 'test.domain', # valid
      'channel_id' => 'channel_id', # valid
      'channel_name' => 'test-rss2slack',
      'timestamp' => 'timestamp_hoge',
      'user_id' => 'user_id_hoge',
      'user_name' => 'me',
      'text' => 'rss2slack_register',
      'trigger_word' => '<PASSWORD>'
    }
    base.merge(other)
  end
end

describe R2S::Handler do
  before(:each) do
    ENV['env'] = 'test'
    @conf = R2S::Conf.new
    @logger = Logger.new(STDOUT)
    @db = Object.new
    @feed_model = R2S::FeedModel.new(@logger, @db)
  end

  after(:each) do
    # NOOP
  end

  it 'valid request' do
    headers = {}
    invalid_token = { 'token' => 'INVALID' }
    body = MockOutgoingMessage::normal_body(invalid_token)

    allow(@feed_model).to receive(:find_by_url).and_return([])
    allow(@feed_model).to receive(:save).and_return(true)

    handler = R2S::Handler.new(@logger, @conf, @feed_model)
    res = handler.handle_slack_feed(headers, body)
  end

  # TODO:
  it 'normal test' do
    result = []
    allow(@feed_model).to receive(:find_by_url).and_return(result)
    allow(@feed_model).to receive(:save).and_return(true)
  end
end<commit_msg>update handler spec for invalid request<commit_after>require_relative './spec_helper'
require_relative '../src/app/rss2slack_handler'
require 'logger'

class MockOutgoingMessage
  def self.body(other)
    base = {
      :token => 'TEST_TOKEN', # valid
      :team_id => 'TEST',
      :team_domain => 'test.domain', # valid
      :channel_id => 'channel_id', # valid
      :channel_name => 'test-rss2slack',
      :timestamp => 'timestamp_hoge',
      :user_id => 'user_id_hoge',
      :user_name => 'me',
      :text => 'rss2slack_register',
      :trigger_word => 'rss2slack_register, hoge, http://hoge.com'
    }
    base.merge(other)
  end
end

describe R2S::Handler do
  before(:each) do
    ENV['env'] = 'test'
    @conf = R2S::Conf.new
    @logger = Logger.new(STDOUT)
    @db = Object.new
    @feed_model = R2S::FeedModel.new(@logger, @db)
  end

  after(:each) do
    @feed_model = nil
  end

  describe 'invalid request' do
    it 'invalid token' do
      headers = {}
      invalid_token = { :token => 'INVALID' }
      body = MockOutgoingMessage::body(invalid_token)

      allow(@feed_model).to receive(:find_by_url).and_return([])
      allow(@feed_model).to receive(:save).and_return(true)

      handler = R2S::Handler.new(@logger, @conf, @feed_model)
      res = handler.handle_slack_feed(headers, body)
      
      expect(res.code).to eq 400
      expect(res.headers).to eq nil
      expect(res.body).to eq ''
    end

    it 'invalid team domain' do
      headers = {}
      invalid_team_domain = { :team_domain => 'INVALID' }
      body = MockOutgoingMessage::body(invalid_team_domain)

      allow(@feed_model).to receive(:find_by_url).and_return([])
      allow(@feed_model).to receive(:save).and_return(true)

      handler = R2S::Handler.new(@logger, @conf, @feed_model)
      res = handler.handle_slack_feed(headers, body)

      expect(res.code).to eq 400
      expect(res.headers).to eq nil
      expect(res.body).to eq ''
    end

    it 'invalid channel id' do
      headers = {}
      invalid_channel_id = { :channel_id => 'INVALID' }
      body = MockOutgoingMessage::body(invalid_channel_id)

      allow(@feed_model).to receive(:find_by_url).and_return([])
      allow(@feed_model).to receive(:save).and_return(true)

      handler = R2S::Handler.new(@logger, @conf, @feed_model)
      res = handler.handle_slack_feed(headers, body)

      expect(res.code).to eq 400
      expect(res.headers).to eq nil
      expect(res.body).to eq ''
    end
  end

  # TODO:
  it 'normal test' do
  end
end
{'fraction_non_alphanumeric': 0.1458650431253171, 'fraction_numerical': 0.006595636732623034, 'mean_word_length': 3.2126068376068377, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 28, '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': '6621587', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1344, 'n_words': 261}