require 'spec_helper'
require 'jam_ruby/recurly_client'

describe ApiRecurlyController, :type=>:controller do
  render_views

  before(:each) do
    @user = FactoryGirl.create(:user) 
    #@jamtrack = FactoryGirl.create(:jam_track) 
    @billing_info = {}
    @billing_info[:first_name]          = @user.first_name
    @billing_info[:last_name]           = @user.last_name
    @billing_info[:address1]            = 'Test Address 1'
    @billing_info[:address2]            = 'Test Address 2'
    @billing_info[:city]                = @user.city
    @billing_info[:state]               = @user.state
    @billing_info[:country]             = @user.country
    @billing_info[:zip]                 = '12345'
    @billing_info[:number]              = '4111-1111-1111-1111'
    @billing_info[:month]               = '08'
    @billing_info[:year]                = '2017'
    @billing_info[:verification_value]  = '111'
    @billing_info[:vat_number]          = ''    
    controller.current_user = @user
  end

  after(:each) do
    if (@user.recurly_code)
      @account = Recurly::Account.find(@user.recurly_code)
      if @account.present?
        @account.destroy    
      end
    end
  end

  it "should send correct error" do
    @billing_info[:number]='121'
    post :create_account, {:format => 'json', :billing_info=>@billing_info, reuse_card_this_time: false, reuse_card_next_time: false}
    response.status.should == 404
    body = JSON.parse(response.body)
    body['errors'].should have(1).items
    body['errors']['number'].should_not be_nil    
  end

  it "should create account" do
    post :create_account, {:format => 'json',billing_info: @billing_info,  reuse_card_this_time: false, reuse_card_next_time: false}
    response.should be_success
    body = JSON.parse(response.body)
    response.should be_success
    body['billing_info']['first_name'].should eq(@user.first_name)
  end

  it "should retrieve account with no billing info" do
    post :create_account, {:format => 'json', reuse_card_this_time: false, reuse_card_next_time: false}
    response.should be_success

    get :get_account
    body = JSON.parse(response.body)
    response.should be_success      
    body['billing_info'].should be_nil
  end

  it "should update account" do
    post :create_account, {:format => 'json', billing_info: @billing_info, reuse_card_this_time: false, reuse_card_next_time: false}
    response.should be_success
    body = JSON.parse(response.body)
    body['billing_info']['first_name'].should eq("Person")

    controller.current_user = @user
    @billing_info[:first_name] = "Thing"
    put :update_account, {:format => 'json', billing_info: @billing_info}
    body = JSON.parse(response.body)    
    body['billing_info']['first_name'].should eq("Thing")

    get :get_account, { :format => 'json'}
    response.should be_success      
    body = JSON.parse(response.body)
    body['billing_info']['first_name'].should eq("Thing")
  end

  # Note: We don't have any  subscriptions yet:
  it "should get subscription" do 
    pending "We don't have any subscriptions yet -- uncomment in routes"
    get :get_subscription, { :format => 'json'}
    response.should be_success      
    body = JSON.parse(response.body)        
  end

  it "should update billing info" do 
    # $enable_tracing = false
    # $trace_out = open('trace.txt', 'w')

    # set_trace_func proc { |event, file, line, id, binding, classname|
    #   if $enable_tracing && event == 'call' && !file.start_with?("/Users/tangledpath/.ddrvm/")
    #     $trace_out.puts "#{file}:#{line} #{classname}##{id}"
    #   end
    # }

    # $enable_tracing = true

    post :create_account, {:format => 'json', billing_info: @billing_info, reuse_card_this_time: false, reuse_card_next_time: false}
    response.should be_success    
    body = JSON.parse(response.body)    
    body['billing_info']['first_name'].should eq("Person")
    @billing_info[:state] = "NE"    
    
    put :update_billing_info, {format: 'json', billing_info: @billing_info}
    
    response.should be_success
    body = JSON.parse(response.body)        
    
    get :billing_info
    response.should be_success    
    body = JSON.parse(response.body)    
  end

end # spec
