require 'spec_helper'
require 'jam_ruby/recurly_client'

describe ApiRecurlyWebHookController, :type=>:request do
  
  let(:success_xml) {
'<?xml version="1.0" encoding="UTF-8"?>
<successful_payment_notification>
  <account>
    <account_code>56d5b2c6-2a4b-46e4-a984-ec1fbe83a50d</account_code>
    <username nil="true"></username>
    <email>seth@jamkazam.com</email>
    <first_name>Seth</first_name>
    <last_name>Call</last_name>
    <company_name nil="true"></company_name>
  </account>
  <transaction>
    <id>2de4448533db12d6d92b4c4b4e90a4f1</id>
    <invoice_id>2de44484fa4528b504555f43ac8bf42f</invoice_id>
    <invoice_number_prefix></invoice_number_prefix>
    <invoice_number type="integer">1037</invoice_number>
    <subscription_id>2de44484b460d95863799a431383b165</subscription_id>
    <action>purchase</action>
    <date type="datetime">2015-04-01T14:53:44Z</date>
    <amount_in_cents type="integer">216</amount_in_cents>
    <status>success</status>
    <message>Successful test transaction</message>
    <reference>6249355</reference>
    <source>subscription</source>
    <cvv_result code=""></cvv_result>
    <avs_result code="D">Street address and postal code match.</avs_result>
    <avs_result_street nil="true"></avs_result_street>
    <avs_result_postal nil="true"></avs_result_postal>
    <test type="boolean">true</test>
    <voidable type="boolean">true</voidable>
    <refundable type="boolean">true</refundable>
  </transaction>
</successful_payment_notification>'
  }

  let(:no_user_xml) {
    '<?xml version="1.0" encoding="UTF-8"?>
<successful_payment_notification>
  <account>
    <account_code>HUHUHUHUHUHUHUHU</account_code>
    <username nil="true"></username>
    <email>seth@jamkazam.com</email>
    <first_name>Seth</first_name>
    <last_name>Call</last_name>
    <company_name nil="true"></company_name>
  </account>
  <transaction>
    <id>2de4448533db12d6d92b4c4b4e90a4f1</id>
    <invoice_id>2de44484fa4528b504555f43ac8bf42f</invoice_id>
    <invoice_number_prefix></invoice_number_prefix>
    <invoice_number type="integer">1037</invoice_number>
    <subscription_id>2de44484b460d95863799a431383b165</subscription_id>
    <action>purchase</action>
    <date type="datetime">2015-04-01T14:53:44Z</date>
    <amount_in_cents type="integer">216</amount_in_cents>
    <status>success</status>
    <message>Successful test transaction</message>
    <reference>6249355</reference>
    <source>subscription</source>
    <cvv_result code=""></cvv_result>
    <avs_result code="D">Street address and postal code match.</avs_result>
    <avs_result_street nil="true"></avs_result_street>
    <avs_result_postal nil="true"></avs_result_postal>
    <test type="boolean">true</test>
    <voidable type="boolean">true</voidable>
    <refundable type="boolean">true</refundable>
  </transaction>
</successful_payment_notification>'
  }

  before(:all) do
    User.delete_all
    @user = FactoryGirl.create(:user, id: '56d5b2c6-2a4b-46e4-a984-ec1fbe83a50d')
  end

  let (:authorization) { "Basic " + Base64::encode64(Rails.application.config.recurly_webhook_user + ":" + Rails.application.config.recurly_webhook_pass ) }

  it "no auth" do
    post '/api/recurly/webhook', success_xml, { 'CONTENT_TYPE' => 'application/xml', 'ACCEPT' => 'application/xml' }
    response.status.should eq(401)
  end

  it "succeeds" do
    post '/api/recurly/webhook', success_xml, { 'Content-Type' => 'application/xml', 'HTTP_AUTHORIZATION' => authorization }
    response.status.should eq(200)
  end

  it "returns 422 on error" do
    post '/api/recurly/webhook', no_user_xml, { 'Content-Type' => 'application/xml', 'HTTP_AUTHORIZATION' => authorization }
    response.status.should eq(422)
  end

  it "returns 200 for unknown hook event" do
    post '/api/recurly/webhook', '<meh />', { 'Content-Type' => 'application/xml', 'HTTP_AUTHORIZATION' => authorization }
    response.status.should eq(200)
  end
end
