diff --git a/lib/jam_ruby/app/mailers/user_mailer.rb b/lib/jam_ruby/app/mailers/user_mailer.rb index 517336c37..c231827c6 100644 --- a/lib/jam_ruby/app/mailers/user_mailer.rb +++ b/lib/jam_ruby/app/mailers/user_mailer.rb @@ -37,5 +37,14 @@ module JamRuby format.html end end + + def password_changed(user) + @user = user + sendgrid_unique_args :type => "password_changed" + mail(:to => user.email, :subject => "Jamkazam Password Changed") do |format| + format.text + format.html + end + end + end end -end \ No newline at end of file diff --git a/lib/jam_ruby/app/views/jam_ruby/user_mailer/password_changed.html.erb b/lib/jam_ruby/app/views/jam_ruby/user_mailer/password_changed.html.erb new file mode 100644 index 000000000..f9a712566 --- /dev/null +++ b/lib/jam_ruby/app/views/jam_ruby/user_mailer/password_changed.html.erb @@ -0,0 +1,5 @@ + + +You Just Changed Your Password! <%= @user.email %> + + diff --git a/lib/jam_ruby/app/views/jam_ruby/user_mailer/password_changed.text.erb b/lib/jam_ruby/app/views/jam_ruby/user_mailer/password_changed.text.erb new file mode 100644 index 000000000..125193f24 --- /dev/null +++ b/lib/jam_ruby/app/views/jam_ruby/user_mailer/password_changed.text.erb @@ -0,0 +1 @@ +You Just Changed Your Password! <%= @user.email %> diff --git a/lib/jam_ruby/constants/validation_messages.rb b/lib/jam_ruby/constants/validation_messages.rb index 1fccad957..08dfa17bf 100644 --- a/lib/jam_ruby/constants/validation_messages.rb +++ b/lib/jam_ruby/constants/validation_messages.rb @@ -25,4 +25,7 @@ module ValidationMessages INSTRUMENT_LIMIT_EXCEEDED = "No more than 5 instruments are allowed." INSTRUMENT_MINIMUM_NOT_MET = "At least 1 instrument is required." + # user + OLD_PASSWORD_DOESNT_MATCH = "Your old password is incorrect." + end \ No newline at end of file diff --git a/lib/jam_ruby/models/music_session.rb b/lib/jam_ruby/models/music_session.rb index 82502627f..f876f5c3a 100644 --- a/lib/jam_ruby/models/music_session.rb +++ b/lib/jam_ruby/models/music_session.rb @@ -4,7 +4,7 @@ module JamRuby self.primary_key = 'id' attr_accessor :legal_terms - attr_accessible :creator, :musician_access, :approval_required, :fan_chat, :fan_access + attr_accessible :creator, :description, :musician_access, :approval_required, :fan_chat, :fan_access belongs_to :creator, :inverse_of => :music_sessions, :class_name => "JamRuby::User", :foreign_key => "user_id" diff --git a/lib/jam_ruby/models/user.rb b/lib/jam_ruby/models/user.rb index 8421873b9..fbdddad34 100644 --- a/lib/jam_ruby/models/user.rb +++ b/lib/jam_ruby/models/user.rb @@ -164,6 +164,18 @@ module JamRuby return id end + def set_password(old_password, new_password, new_password_confirmation) + raise JamRuby::JamArgumentError unless authenticate old_password + # FIXME: Should verify that the new password meets certain quality criteria. Really, maybe that should be a + # verification step. + self.password = new_password + self.password_confirmation = new_password_confirmation + save + + UserMailer.password_changed(self).deliver + end + + # helper method for creating / updating a User def self.save(id, updater_id, first_name, last_name, email, password, password_confirmation, musician, gender, birth_date, internet_service_provider, city, state, country, instruments, photo_url) diff --git a/spec/jam_ruby/models/music_session_spec.rb b/spec/jam_ruby/models/music_session_spec.rb index 21ce78f98..a865124a8 100644 --- a/spec/jam_ruby/models/music_session_spec.rb +++ b/spec/jam_ruby/models/music_session_spec.rb @@ -223,6 +223,21 @@ describe MusicSession do music_sessions.length.should == 1 end + it "updates the fields of a music session properly" do + genre1 = FactoryGirl.create(:genre) + genre2 = FactoryGirl.create(:genre) + genre3 = FactoryGirl.create(:genre) + genre4 = FactoryGirl.create(:genre) + creator = FactoryGirl.create(:user) + session = FactoryGirl.create(:music_session, :creator => creator, :description => "Session", :genres => [genre3,genre4]) + session.update_attributes({:description => "Session2", :genre => [genre1, genre2]}) + session.genres = [genre1, genre2] + session.reload + session.description.should == "Session2" + session.genres.length.should == 2 + session.genres[0].id.should == genre1.id + end + =begin # mslemmer: # I'm going to clean this up into smaller tasks. diff --git a/spec/jam_ruby/models/user_spec.rb b/spec/jam_ruby/models/user_spec.rb index f3d88acd7..72b6bd9eb 100644 --- a/spec/jam_ruby/models/user_spec.rb +++ b/spec/jam_ruby/models/user_spec.rb @@ -125,6 +125,34 @@ describe User do it { should be_invalid } end + describe "set_password" do + before do + @user.confirm_email! + end + + it "setting a new password should work" do + @user.set_password("foobar", "newpassword", "newpassword") + User.authenticate(@user.email, "newpassword").should_not be_nil + end + + it "setting a new password should fail if old one doesnt match" do + expect { @user.set_password("wrongold", "newpassword", "newpassword") }.to raise_error + end + + it "setting a new password should fail if new ones dont match" do + @user.set_password("foobar", "newpassword", "newpassword2") + User.authenticate(@user.email, "newpassword").should be_nil + end + + it "setting a new password should fail if new one doesnt validate" do + @user.set_password("foobar", "a", "a") + User.authenticate(@user.email, "newpassword").should be_nil + end + + end + + + describe "return value of authenticate method" do before { @user.save } let(:found_user) { User.find_by_email(@user.email) }