require 'spec_helper'

describe UsersController, :type => :api do
  #render_views

  let(:user) { FactoryGirl.create(:user) }
  let(:utm_valid_url) { '/?utm_source=abcya&utm_medium=browsers&utm_campaign=dog' }
  let(:utm_valid_cookie) { {"utm_source" => "abcya", "utm_medium" => "browser", "utm_campaign" => "dog", "referrer" => nil} }

  before(:each) {
    #controller.current_user = nil
    #login(authenticated_user.email, authenticated_user.password, 200, true)
  }

  def login(user)
    # login as fan
    post '/api/auth_session.json', { :email => user.email, :password => user.password }.to_json, "CONTENT_TYPE" => 'application/json'
    last_response.status.should == 200
    JSON.parse(last_response.body).should == { "success" => true }
  end

  it "unsubscribe" do
    user.subscribe_email.should eql true
    get '/unsubscribe/' + user.unsubscribe_token

    last_response.body.should eql ('You have been unsubscribed.')
    user.reload
    user.subscribe_email.should eql false
  end

  describe "track_origin" do
    describe "logged out" do

      it "should not distrib origin if already set and no utm_source" do

        weird_origin = {"something" => "nothing"}
        cookie_jar[:origin] = weird_origin.to_json
        request.cookies[:origin] = weird_origin.to_json

        get :home
        response.cookies["origin"].should be_nil
      end

      it "should overwrite origin if already set and URL has utm_source" do
        weird_origin = {"utm_source" => "nothing"}
        request.cookies[:origin] = weird_origin
        get :home, utm_valid_cookie
        JSON.parse(response.cookies["origin"]).should eql utm_valid_cookie
      end

      it "should set origin to organic if referrer set" do
        controller.request.stub referer: 'http://somewhere.com/ha'
        get :home
        JSON.parse(response.cookies["origin"]).should eql ({"utm_source" => "organic", "utm_medium" => "organic", "utm_campaign" => "somewhere.com", "referrer" => 'http://somewhere.com/ha'})
      end

      it "should ignore referrer if origin already set" do
        weird_origin = {"utm_source" => "nothing"}
        request.cookies[:origin] = weird_origin
        get :home, utm_valid_cookie
        JSON.parse(response.cookies["origin"]).should eql utm_valid_cookie

        controller.request.stub referer: 'http://somewhere.com/ha'
        get :home
        response.cookies["origin"].should be_nil
      end

      it "should not set origin with no referrer info" do
        get :home

        response.cookies["origin"].should be_nil
      end

      it "should set origin with referrer info" do
        get :home, utm_valid_cookie
        JSON.parse(response.cookies["origin"]).should eql utm_valid_cookie
      end
    end

    describe "logged in" do

      it "should not set origin with no referrer info" do
        login(user)
        get :home
        response.cookies["origin"].should be_nil
      end

      it "should not set origin with referrer info" do
        login(user)
        get :home, utm_valid_cookie
        response.cookies["origin"].should be_nil
      end
    end
  end

end
