diff --git a/web/app/controllers/api_invited_users_controller.rb b/web/app/controllers/api_invited_users_controller.rb index 3618dce56..87210a778 100644 --- a/web/app/controllers/api_invited_users_controller.rb +++ b/web/app/controllers/api_invited_users_controller.rb @@ -31,8 +31,16 @@ iu.save iu end + else + iu = InvitedUser.new + iu.sender = current_user + iu.save + @invited_users = [iu] + end + if err = @invited_users.detect {|iu| iu.errors.any? } + response.status = :unprocessable_entity + respond_with err end - respond_with @invited_users, :responder => ApiResponder, :location => invitations_url(@invited_users) end end diff --git a/web/spec/requests/invited_users_api_spec.rb b/web/spec/requests/invited_users_api_spec.rb index 199a406b7..8097453e2 100644 --- a/web/spec/requests/invited_users_api_spec.rb +++ b/web/spec/requests/invited_users_api_spec.rb @@ -25,17 +25,15 @@ describe "Invited Users API ", :type => :api do 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) + 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 - # now fetch it's data - location_header = last_response.headers["Location"] - get location_header - # parse json and test - body = JSON.parse(last_response.body) + 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" @@ -44,14 +42,12 @@ describe "Invited Users API ", :type => :api do 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) + post '/api/invited_users.json', {:emails => ['tester@jamkazam.com'], :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json' + last_response.status.should eql(200) - # now fetch it's data - location_header = last_response.headers["Location"] - get location_header - - body = JSON.parse(last_response.body) + 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" @@ -63,7 +59,7 @@ describe "Invited Users API ", :type => :api 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' + 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 @@ -79,7 +75,7 @@ describe "Invited Users API ", :type => :api do end it "cant create with blank email" do - post '/api/invited_users.json', {:email => "", :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json' + 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 @@ -87,7 +83,7 @@ describe "Invited Users API ", :type => :api do end it "cant create with invalid email" do - post '/api/invited_users.json', {:email => "blurp", :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json' + 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 @@ -95,14 +91,12 @@ describe "Invited Users API ", :type => :api do 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) + post '/api/invited_users.json', {:emails => ["tester@jamkazam.com"], :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json' + last_response.status.should eql(200) - # now fetch it's data - location_header = last_response.headers["Location"] - get location_header - - body = JSON.parse(last_response.body) + 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' @@ -115,14 +109,12 @@ describe "Invited Users API ", :type => :api do 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) + post '/api/invited_users.json', {:emails => ["tester@jamkazam.com"], :note => "please join"}.to_json, "CONTENT_TYPE" => 'application/json' + last_response.status.should eql(200) - # now fetch it's data - location_header = last_response.headers["Location"] - get location_header - - body = JSON.parse(last_response.body) + 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'