require 'spec_helper'

describe Fixxpert::MarketController do

  let(:fixxpert) { create :fixxpert }
  let(:customer) { create :customer }

  # accept_market_policy
  #----------------------------------------------------------------------

  describe "GET accept_market_policy" do
    context 'without login' do
      it 'should redirect to login' do
        get :accept_market_policy
        response.should redirect_to login_path
      end
    end

    context 'with fixxpert login, not policy accepted yet' do
      context "if user has signature and has accepted compensation policy" do
        let(:fixxpert) { create(:fixxpert, accepted_compensation_policy: true, signature: "signature") }
        it 'should succeed' do
          login_user fixxpert
          get :accept_market_policy
          response.should be_success
        end
      end
      context "if signature is not present and has not accepted compensation policy" do
        it 'should succeed' do
          login_user fixxpert
          get :accept_market_policy
          response.should redirect_to fixxpert_accept_compensation_policy_url
        end
      end
    end

    context 'with fixxpert login, policy already accepted' do
      let(:fixxpert) { create(:fixxpert, accepted_compensation_policy: true, signature: "signature") }
      it 'should redirect to root path' do
        login_user fixxpert
        data = {
          token_id: "FOO",
          refund_token_id: "WADUS",
          recipient_email: "foo@bar.com"
        }

        fixxpert.update_market_policy! data
        get :accept_market_policy
        response.should redirect_to root_path
      end
    end

    context 'with customer login' do
      it 'should redirect to root path' do
        login_user customer
        get :accept_market_policy
        response.should redirect_to root_path
      end
    end
  end


  # accept_market_policy_callback
  #----------------------------------------------------------------------

  describe "GET accept_market_policy_callback" do

    valid_params = {
      "tokenID"=>"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
      "refundTokenID"=>"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
      "signatureMethod"=>"RSA-SHA1",
      "status"=>"SR",
      "recipientEmail"=>"foo@wadus.com",
      "signatureVersion"=>"2",
      "signature"=>"ZpHu9HV8/475LxBLBdnjSahBPNVG8+plwNm4EU7nFih74zu8KWn15/NM2+b6B0KY20+Qh2yHJuGD\nL4EMxacAl39BGKkJur0bfidUq5nNSe/XffnUOBDirFDFXltpWKUEj9iyBT3sHrKe9KrS6o5ptfZ8\n5Gr+b2FyMR0H9NZfsa8=",
      "certificateUrl"=>"https://fps.sandbox.amazonaws.com/certs/090911/PKICert.pem?requestId=bjyk7l7mcvg9c42hh1h10f36t335zyb9k5q111hn3cv2l8opik6",
      "callerReference"=>"00000000-0000-0000-0000-000000000000"
    }

    context 'without login' do
      it 'should redirect to login' do
        get :accept_market_policy_callback
        response.should redirect_to login_path
      end
    end

    context 'with fixxpert login, valid response' do
      it 'should succeed' do
        login_user fixxpert
        get :accept_market_policy_callback, valid_params
        response.should be_success
        # TODO ...
      end
    end

    context 'with fixxpert login, invalid_response' do
      it 'should succeed' do
        login_user fixxpert
        get :accept_market_policy_callback, {}
        response.should be_success
        # TODO ...
      end
    end

    context 'with customer login' do
      it 'should redirect to root path' do
        login_user customer
        get :accept_market_policy_callback
        response.should redirect_to root_path
      end
    end
  end


end
