<commit_before>require 'spec_helper'

describe Travis::Addons::GithubStatus::Task do
  include Travis::Testing::Stubs

  let(:subject)    { Travis::Addons::GithubStatus::Task }
  let(:url)        { '/repos/svenfuchs/minimal/statuses/62aae5f70ceee39123ef' }
  let(:target_url) { 'https://travis-ci.org/svenfuchs/minimal/builds/1' }
  let(:payload) { Marshal.load(Marshal.dump(TASK_PAYLOAD)) }
  let(:io)         { StringIO.new }

  before do
    Travis.logger = Logger.new(io)
  end

  def run
    subject.new(payload, tokens: { '<PASSWORD>' => '<PASSWORD>', '<PASSWORD>' => '<PASSWORD>' }).run
  end

  it 'posts status info for a created build' do
    payload["build"]["state"] = 'created'
    GH.expects(:post).with(url, state: 'pending', description: 'The Travis CI build is in progress', target_url: target_url, context: 'continuous-integration/pr/travis-ci').returns({})
    run
  end

  it 'posts status info for a passed build' do
    payload["build"]["state"] = 'passed'
    GH.expects(:post).with(url, state: 'success', description: 'The Travis CI build passed', target_url: target_url, context: 'continuous-integration/pr/travis-ci').returns({})
    run
  end

  it 'posts status info for a failed build' do
    payload["build"]["state"] = 'failed'
    GH.expects(:post).with(url, state: 'failure', description: 'The Travis CI build failed', target_url: target_url, context: 'continuous-integration/pr/travis-ci').returns({})
    run
  end

  it 'posts status info for a errored build' do
    payload['build']["state"] = 'errored'
    GH.expects(:post).with(url, state: 'error', description: 'The Travis CI build could not complete due to an error', target_url: target_url, context: 'continuous-integration/pr/travis-ci').returns({})
    run
  end

  it 'posts status info for a canceled build' do
    payload["build"]["state"] = 'canceled'
    GH.expects(:post).with(url, state: 'error', description: 'The Travis CI build could not complete due to an error', target_url: target_url, context: 'continuous-integration/pr/travis-ci').returns({})
    run
  end

  it 'authenticates using the token passed into the task' do
    GH.expects(:with).with { |options| options[:token] == '1<PASSWORD>' }.returns({})
    run
  end

  it 'authenticates using the next token if the first token failed' do
    GH.expects(:with).with { |options| options[:token] == '1<PASSWORD>' }.raises(GH::Error.new(nil, nil, response_status: 401))
    GH.expects(:with).with { |options| options[:token] == '6<PASSWORD>' }.returns({})
    run
  end

  it 'accepts a single token using the legacy payload' do
    GH.expects(:with).with { |options| options[:token] == '1<PASSWORD>' }.returns({})
    subject.new(payload, token: '1<PASSWORD>').run
  end

  describe 'logging' do
    it 'warns about a failed request' do
      GH.stubs(:post).raises(GH::Error.new(nil))
      expect {
        run
      }.to raise_error
      io.string.should include('error=not_updated')
      io.string.should include('message=GH request failed')
    end

    it "doesn't raise an error with bad credentials" do<commit_msg>actually, they are push payloads<commit_after>require 'spec_helper'

describe Travis::Addons::GithubStatus::Task do
  include Travis::Testing::Stubs

  let(:subject)    { Travis::Addons::GithubStatus::Task }
  let(:url)        { '/repos/svenfuchs/minimal/statuses/62aae5f70ceee39123ef' }
  let(:target_url) { 'https://travis-ci.org/svenfuchs/minimal/builds/1' }
  let(:payload)    { Marshal.load(Marshal.dump(TASK_PAYLOAD)) }
  let(:io)         { StringIO.new }

  before do
    Travis.logger = Logger.new(io)
  end

  def run
    subject.new(payload, tokens: { '<PASSWORD>' => '<PASSWORD>', '<PASSWORD>' => '6<PASSWORD>' }).run
  end

  it 'posts status info for a created build' do
    payload["build"]["state"] = 'created'
    GH.expects(:post).with(url, state: 'pending', description: 'The Travis CI build is in progress', target_url: target_url, context: 'continuous-integration/push/travis-ci').returns({})
    run
  end

  it 'posts status info for a passed build' do
    payload["build"]["state"] = 'passed'
    GH.expects(:post).with(url, state: 'success', description: 'The Travis CI build passed', target_url: target_url, context: 'continuous-integration/push/travis-ci').returns({})
    run
  end

  it 'posts status info for a failed build' do
    payload["build"]["state"] = 'failed'
    GH.expects(:post).with(url, state: 'failure', description: 'The Travis CI build failed', target_url: target_url, context: 'continuous-integration/push/travis-ci').returns({})
    run
  end

  it 'posts status info for a errored build' do
    payload['build']["state"] = 'errored'
    GH.expects(:post).with(url, state: 'error', description: 'The Travis CI build could not complete due to an error', target_url: target_url, context: 'continuous-integration/push/travis-ci').returns({})
    run
  end

  it 'posts status info for a canceled build' do
    payload["build"]["state"] = 'canceled'
    GH.expects(:post).with(url, state: 'error', description: 'The Travis CI build could not complete due to an error', target_url: target_url, context: 'continuous-integration/push/travis-ci').returns({})
    run
  end

  it 'authenticates using the token passed into the task' do
    GH.expects(:with).with { |options| options[:token] == '<PASSWORD>' }.returns({})
    run
  end

  it 'authenticates using the next token if the first token failed' do
    GH.expects(:with).with { |options| options[:token] == '12345' }.raises(GH::Error.new(nil, nil, response_status: 401))
    GH.expects(:with).with { |options| options[:token] == '67890' }.returns({})
    run
  end

  it 'accepts a single token using the legacy payload' do
    GH.expects(:with).with { |options| options[:token] == '12345' }.returns({})
    subject.new(payload, token: '12345').run
  end

  describe 'logging' do
    it 'warns about a failed request' do
      GH.stubs(:post).raises(GH::Error.new(nil))
      expect {
        run
      }.to raise_error
      io.string.should include('error=not_updated')
      io.string.should include('message=GH request failed')
    end

    it "doesn't raise an error with bad credentials" do
{'fraction_non_alphanumeric': 0.15101974749109745, 'fraction_numerical': 0.008740692780835222, 'mean_word_length': 4.056464811783961, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, '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': '657435', 'n_tokens_mistral': 2086, 'n_tokens_neox': 1959, 'n_words': 543}