require 'spec_helper'

describe "Invited Users API ", :type => :api do

  include Rack::Test::Methods

  subject { page }

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

  let(:user) { FactoryGirl.create(:user) }

  describe "api" do

    before do
      InvitedUser.delete_all
      UserMailer.deliveries.clear

      login(user.email, user.password, 200, true)
    end

    it "create with no note" do
      post '/api/invited_users.json', {:email => 'tester@jamkazam.com'}.to_json, "CONTENT_TYPE" => 'application/json'
      last_response.status.should eql(201)

      UserMailer.deliveries.length.should == 1

      # now fetch it's data
      location_header = last_response.headers["Location"]
      get location_header

      # parse json and test
      body = JSON.parse(last_response.body)
      body["id"].should_not be_nil
      body["created_at"].should_not be_nil
      body["email"].should == "tester@jamkazam.com"
      body["accepted"].should be_false
      body["note"].should be_nil
    end

    it "create with a note" do
      post '/api/invited_users.json', {:email => 'tester@jamkazam.com', :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
      last_response.status.should eql(201)

      # now fetch it's data
      location_header = last_response.headers["Location"]
      get location_header

      body = JSON.parse(last_response.body)
      body["id"].should_not be_nil
      body["created_at"].should_not be_nil
      body["email"].should == "tester@jamkazam.com"
      body["accepted"].should be_false
      body["note"].should_not be_nil
    end

    it "cant create when not invited" do
      user.can_invite = false
      user.save

      post '/api/invited_users.json', {:email => 'tester@jamkazam.com', :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
      last_response.status.should eql(422)
      body = JSON.parse(last_response.body)
      body["errors"].should_not be_nil
      body["errors"]["sender"].length.should == 1
    end

    it "cant create with no email" do
      post '/api/invited_users.json', {:note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
      last_response.status.should eql(422)
      body = JSON.parse(last_response.body)
      body["errors"].should_not be_nil
      body["errors"]["email"].length.should == 2
    end

    it "cant create with blank email" do
      post '/api/invited_users.json', {:email => "", :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
      last_response.status.should eql(422)
      body = JSON.parse(last_response.body)
      body["errors"].should_not be_nil
      body["errors"]["email"].length.should == 2
    end

    it "cant create with invalid email" do
      post '/api/invited_users.json', {:email => "blurp", :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
      last_response.status.should eql(422)
      body = JSON.parse(last_response.body)
      body["errors"].should_not be_nil
      body["errors"]["email"].length.should == 1
    end

    it "list" do
      post '/api/invited_users.json', {:email => "tester@jamkazam.com", :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
      last_response.status.should eql(201)

      # now fetch it's data
      location_header = last_response.headers["Location"]
      get location_header

      body = JSON.parse(last_response.body)
      id = body["id"]

      get '/api/invited_users.json',  "CONTENT_TYPE" => 'application/json'

      last_response.status.should eql(200)

      body = JSON.parse(last_response.body)
      body.length.should == 1
      body[0]["id"].should == id
    end

    it "show" do
      post '/api/invited_users.json', {:email => "tester@jamkazam.com", :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json'
      last_response.status.should eql(201)

      # now fetch it's data
      location_header = last_response.headers["Location"]
      get location_header

      body = JSON.parse(last_response.body)
      id = body["id"]

      get "/api/invited_users/#{id}.json",  "CONTENT_TYPE" => 'application/json'

      last_response.status.should eql(200)

      body = JSON.parse(last_response.body)
      body["id"].should == id
    end
  end
end
