diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 6b79b180bc2e1bf69783d6bc83d1958bd90e0b47..ec9bd7f1beb768554e5df915d0784f9717d7d57d 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -7,13 +7,14 @@ class RegistrationsController < Devise::RegistrationsController begin user = User.instantiate!(params[:user]) rescue MongoMapper::DocumentNotValid => e - user = nil flash[:error] = e.message + redirect_to new_user_registration_path end - if user + if user.save flash[:notice] = I18n.t 'registrations.create.success' sign_in_and_redirect(:user, user) else + flash[:error] = user.errors.full_messages.join(', ') redirect_to new_user_registration_path end end diff --git a/app/models/user.rb b/app/models/user.rb index ec7107fc3c0d37f5868166407a80c9201b3ee237..ca9a0d8423609979266aab8b08bddf9ae10c1c7b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -24,6 +24,7 @@ class User devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + key :username, :unique => true key :serialized_private_key, String @@ -36,6 +37,8 @@ class User key :visible_post_ids, Array key :visible_person_ids, Array + validates_presence_of :username + one :person, :class_name => 'Person', :foreign_key => :owner_id many :inviters, :in => :inviter_ids, :class_name => 'User' diff --git a/spec/controllers/registrations_controller_spec.rb b/spec/controllers/registrations_controller_spec.rb index 828e6a4e4394471137d8e58dfbac669a5077e88c..da7643fc9cabc433b8f9f1967b045c59e3c610f5 100644 --- a/spec/controllers/registrations_controller_spec.rb +++ b/spec/controllers/registrations_controller_spec.rb @@ -35,5 +35,22 @@ describe RegistrationsController do response.should redirect_to root_path end end + context "with invalid parameters" do + before do + @valid_params["user"].delete("username") + @invalid_params = @valid_params + end + it "does not create a user" do + lambda { get :create, @invalid_params }.should_not change(User, :count) + end + it "sets the flash error" do + get :create, @invalid_params + flash[:error].should_not be_blank + end + it "goes back to the form" do + get :create, @invalid_params + response.should redirect_to new_user_registration_path + end + end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index cf7c4c51a2e305fadd1fcc1404fe115287557d0c..4f8b836b2a27f010eecf57e707f1eda01f5a6bbd 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -13,6 +13,10 @@ describe User do let(:aspect3) { user3.aspect(:name => 'stuff') } describe "validations" do + it "requires a username" do + user = Factory.build(:user, :username => nil) + user.should_not be_valid + end it "downcases the username" do user = Factory.build(:user, :username => "ALLUPPERCASE") user.valid?