require 'spec_helper' describe "Invited Users API ", :type => :api do 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', {:emails => ['tester@jamkazam.com']}.to_json, "CONTENT_TYPE" => 'application/json' last_response.status.should eql(200) UserMailer.deliveries.length.should == 1 # parse json and test bodies = JSON.parse(last_response.body) expect(bodies.size).to eq(1) body = bodies[0] 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', {:emails => ['tester@jamkazam.com'], :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json' last_response.status.should eql(200) bodies = JSON.parse(last_response.body) expect(bodies.length).to eq(1) body = bodies[0] 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', {:emails => ['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 == 1 end it "cant create with blank email" do post '/api/invited_users.json', {:emails => [""], :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 "cant create with invalid email" do post '/api/invited_users.json', {:emails => ["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', {:emails => ["tester@jamkazam.com"], :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json' last_response.status.should eql(200) bodies = JSON.parse(last_response.body) expect(bodies.length).to eq(1) body = bodies[0] 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', {:emails => ["tester@jamkazam.com"], :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json' last_response.status.should eql(200) bodies = JSON.parse(last_response.body) expect(bodies.length).to eq(1) body = bodies[0] 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