require 'spec_helper'

describe ApiSchoolInvitationsController, type: :controller do
  render_views

  let (:owner) {FactoryGirl.create(:user)}
  let (:school) {FactoryGirl.create(:school, user: owner)}
  let (:school_invitation_teacher) {FactoryGirl.create(:school_invitation, school: school, as_teacher: true)}
  let (:school_invitation_student) {FactoryGirl.create(:school_invitation, school: school, as_teacher: false)}

  before(:each) do
    controller.current_user = owner
  end

  describe "index" do
    it "works" do
      get :index, id: school.id, as_teacher: true
      response.should be_success
      JSON.parse(response.body)['total_entries'].should eql 0

      school_invitation_teacher.touch
      get :index, id: school.id, as_teacher: true
      response.should be_success
      JSON.parse(response.body)['total_entries'].should eql 1

      get :index, id: school.id, as_teacher: false
      response.should be_success
      JSON.parse(response.body)['total_entries'].should eql 0
    end

  end

  describe "create" do
    it "works" do
      UserMailer.deliveries.clear
      post :create, id: school.id, first_name: "Seth", last_name: "Call", email: "seth@jamkazam.com", as_teacher: true, :format => 'json'
      UserMailer.deliveries.length.should eql 1
      response.should be_success
      JSON.parse(response.body)['id'].should eql SchoolInvitation.find_by_email("seth@jamkazam.com").id
    end
  end

  describe "resend" do
    it "works" do
      UserMailer.deliveries.clear
      post :resend, id: school.id, invitation_id: school_invitation_teacher.id, :format => 'json'
      UserMailer.deliveries.length.should eql 1
      response.should be_success
    end
  end

  describe "delete" do
    it "works" do
      delete :delete, id: school.id, invitation_id: school_invitation_teacher.id, :format => 'json'
      response.should be_success
    end
  end
end
