From ed76fe869911a9f386fc516260789f14723e006a Mon Sep 17 00:00:00 2001 From: Seth Call Date: Tue, 13 Nov 2012 23:37:18 -0600 Subject: [PATCH] * moving user_manager out of jam_ruby, but moving alot of logic within user_manager to User model --- lib/jam_ruby.rb | 1 - lib/jam_ruby/models/user.rb | 76 ++++++++++++++++++++++++ lib/jam_ruby/user_manager.rb | 70 ----------------------- spec/jam_ruby/models/user_spec.rb | 24 ++++++++ spec/jam_ruby/user_manager_spec.rb | 92 ------------------------------ 5 files changed, 100 insertions(+), 163 deletions(-) delete mode 100644 lib/jam_ruby/user_manager.rb delete mode 100644 spec/jam_ruby/user_manager_spec.rb diff --git a/lib/jam_ruby.rb b/lib/jam_ruby.rb index dfbb727ff..63c5d0c7c 100644 --- a/lib/jam_ruby.rb +++ b/lib/jam_ruby.rb @@ -13,7 +13,6 @@ require "jam_ruby/errors/state_error" require "jam_ruby/errors/jam_argument_error" require "jam_ruby/mq_router" require "jam_ruby/base_manager" -require "jam_ruby/user_manager" require "jam_ruby/connection_manager" require "jam_ruby/version" require "jam_ruby/environment" diff --git a/lib/jam_ruby/models/user.rb b/lib/jam_ruby/models/user.rb index b824834cc..b4cecfe78 100644 --- a/lib/jam_ruby/models/user.rb +++ b/lib/jam_ruby/models/user.rb @@ -224,6 +224,82 @@ module JamRuby end + # throws ActiveRecord::RecordNotFound if instrument is invalid + # throws an email delivery error if unable to connect out to SMTP + def self.signup(name, email, password, password_confirmation, + city, state, country, instruments, signup_confirm_url) + user = User.new + + UserManager.active_record_transaction do |user_manager| + user.name = name + user.email = email + user.password = password + user.password_confirmation = password_confirmation + user.admin = false + user.email_confirmed = false + user.city = city + user.state = state + user.country = country + unless instruments.nil? + instruments.each do |musician_instrument_param| + instrument = Instrument.find(musician_instrument_param[:instrument_id]) + musician_instrument = MusicianInstrument.new + musician_instrument.user = user + musician_instrument.instrument = instrument + musician_instrument.proficiency_level = musician_instrument_param[:proficiency_level] + musician_instrument.priority = musician_instrument_param[:priority] + musician_instrument.save + user.musician_instruments << musician_instrument + end + end + user.signup_token = SecureRandom.urlsafe_base64 + + user.save + + if user.errors.any? + raise ActiveRecord::Rollback + else + # any errors here should also rollback the transaction; that's OK. If emails aren't going to be delivered, + # it's already a really bad situation; make user signup again + UserMailer.welcome_message(user, signup_confirm_url + "/" + user.signup_token).deliver + end + end + + return user + end + + # throws RecordNotFound if signup token is invalid; i.e., if it's nil, empty string, or not belonging to a user + def self.signup_confirm(signup_token) + if signup_token.nil? || signup_token.empty? + # there are plenty of confirmed users with nil signup_tokens, so we can't look on it + raise ActiveRecord::RecordNotFound + else + UserManager.active_record_transaction do |user_manager| + # throws ActiveRecord::RecordNotFound if invalid + user = User.find_by_signup_token!(signup_token) + user.signup_token = nil + user.email_confirmed = true + user.save + return user + end + end + end + + # if valid credentials are supplied for an 'active' user, returns the user + # if not authenticated, returns nil + def self.authenticate(email, password) + # we only allow users that have confirmed email to authenticate + user = User.where('email_confirmed=true').find_by_email(email) + + if user && user.authenticate(password) + return user + else + return nil + end + + end + + ### Elasticsearch/Tire integration ### # # Define the name based on the environment diff --git a/lib/jam_ruby/user_manager.rb b/lib/jam_ruby/user_manager.rb deleted file mode 100644 index cbcf746f0..000000000 --- a/lib/jam_ruby/user_manager.rb +++ /dev/null @@ -1,70 +0,0 @@ -module JamRuby - class UserManager < BaseManager - - def initialize(options={}) - super(options) - @log = Logging.logger[self] - end - - # throws ActiveRecord::RecordNotFound if instrument is invalid - # throws an email delivery error if unable to connect out to SMTP - def signup(name, email, password, password_confirmation, - city, state, country, instruments, signup_confirm_url) - user = User.new - - UserManager.active_record_transaction do |user_manager| - user.name = name - user.email = email - user.password = password - user.password_confirmation = password_confirmation - user.admin = false - user.email_confirmed = false - user.city = city - user.state = state - user.country = country - unless instruments.nil? - instruments.each do |musician_instrument_param| - instrument = Instrument.find(musician_instrument_param[:instrument_id]) - musician_instrument = MusicianInstrument.new - musician_instrument.user = user - musician_instrument.instrument = instrument - musician_instrument.proficiency_level = musician_instrument_param[:proficiency_level] - musician_instrument.priority = musician_instrument_param[:priority] - musician_instrument.save - user.musician_instruments << musician_instrument - end - end - user.signup_token = SecureRandom.urlsafe_base64 - - user.save - - if user.errors.any? - raise ActiveRecord::Rollback - else - # any errors here should also rollback the transaction; that's OK. If emails aren't going to be delivered, - # it's already a really bad situation; make user signup again - UserMailer.welcome_message(user, signup_confirm_url + "/" + user.signup_token).deliver - end - end - - return user - end - - # throws RecordNotFound if signup token is invalid; i.e., if it's nil, empty string, or not belonging to a user - def signup_confirm(signup_token) - if signup_token.nil? || signup_token.empty? - # there are plenty of confirmed users with nil signup_tokens, so we can't look on it - raise ActiveRecord::RecordNotFound - else - UserManager.active_record_transaction do |user_manager| - # throws ActiveRecord::RecordNotFound if invalid - user = User.find_by_signup_token!(signup_token) - user.signup_token = nil - user.email_confirmed = true - user.save - return user - end - end - end - end -end \ No newline at end of file diff --git a/spec/jam_ruby/models/user_spec.rb b/spec/jam_ruby/models/user_spec.rb index 97b651353..7f0f06f49 100644 --- a/spec/jam_ruby/models/user_spec.rb +++ b/spec/jam_ruby/models/user_spec.rb @@ -145,4 +145,28 @@ describe User do before { @user.save } its(:remember_token) { should_not be_blank } end + + describe "authenticate (class-instance)" do + before { @user.email_confirmed=true; @user.save } + + describe "with valid password" do + it { should == User.authenticate(@user.email, @user.password) } + end + + describe "with invalid password" do + it { User.authenticate(@user.email, "invalid").should be_nil } + end + + describe "with invalid email" do + it { User.authenticate("junk", "invalid").should be_nil } + end + + describe "with nil args" do + it { User.authenticate(nil, nil).should be_nil } + end + + describe "with empty args" do + it { User.authenticate("", "").should be_nil } + end + end end \ No newline at end of file diff --git a/spec/jam_ruby/user_manager_spec.rb b/spec/jam_ruby/user_manager_spec.rb deleted file mode 100644 index 74f8ef3ff..000000000 --- a/spec/jam_ruby/user_manager_spec.rb +++ /dev/null @@ -1,92 +0,0 @@ -require 'spec_helper' - -# these tests avoid the use of ActiveRecord and FactoryGirl to do blackbox, non test-instrumented tests -describe UserManager do - - - before(:each) do - @user_manager = UserManager.new(:conn => @conn) - UserMailer.deliveries.clear - end - - describe "signup" do - it "signup successfully" do - @user = @user_manager.signup("bob", "bob@jamkazam.com", "foobar", "foobar", "Austin", "TX", "USA", nil, "http://localhost:3000/confirm" ) - - @user.errors.any?.should be_false - @user.name.should == "bob" - @user.email.should == "bob@jamkazam.com" - @user.email_confirmed.should be_false - @user.city.should == "Austin" - @user.state.should == "TX" - @user.country.should == "USA" - @user.instruments.length.should == 0 - @user.signup_token.should_not be_nil - - UserMailer.deliveries.length.should == 1 - end - - it "signup successfully with instruments" do - @user = @user_manager.signup("bob", "bob@jamkazam.com", "foobar", "foobar", "Austin", "TX", "USA", - [{ :instrument_id => "electric guitar", :proficiency_level => 3, :priority => 0}], "http://localhost:3000/confirm" ) - - @user.errors.any?.should be_false - @user.instruments.length.should == 1 - musician_instrument = @user.musician_instruments[0] - musician_instrument.instrument.should == Instrument.find("electric guitar") - musician_instrument.proficiency_level.should == 3 - end - - it "duplicate signup failure" do - @user = @user_manager.signup("bob", "bob@jamkazam.com", "foobar", "foobar", "Austin", "TX", "USA", nil, "http://localhost:3000/confirm" ) - UserMailer.deliveries.length.should == 1 - @user.errors.any?.should be_false - - # exactly the same parameters; should dup on email, and send no email - @user = @user_manager.signup("bob", "bob@jamkazam.com", "foobar", "foobar", "Austin", "TX", "USA", nil, "http://localhost:3000/confirm" ) - UserMailer.deliveries.length.should == 1 - @user.errors.any?.should be_true - @user.errors[:email][0].should == "has already been taken" - - # change email so that name appears dupped - @user = @user_manager.signup("bob", "bobbie@jamkazam.com", "foobar", "foobar", "Austin", "TX", "USA", nil, "http://localhost:3000/confirm" ) - UserMailer.deliveries.length.should == 1 - @user.errors.any?.should be_true - @user.errors[:name][0].should == "has already been taken" - end - - it "fail on no username" do - @user = @user_manager.signup("", "bob@jamkazam.com", "foobar", "foobar", "Austin", "TX", "USA", nil, "http://localhost:3000/confirm" ) - UserMailer.deliveries.length.should == 0 - @user.errors.any?.should be_true - @user.errors[:name][0].should == "can't be blank" - end - - it "fail on no username" do - @user = @user_manager.signup("murp", "", "foobar", "foobar", "Austin", "TX", "USA", nil, "http://localhost:3000/confirm" ) - UserMailer.deliveries.length.should == 0 - @user.errors.any?.should be_true - @user.errors[:email][0].should == "can't be blank" - end - end - - describe "signup_confirm" do - it "fail on no username" do - @user = @user_manager.signup("bob", "bob@jamkazam.com", "foobar", "foobar", "Austin", "TX", "USA", nil, "http://localhost:3000/confirm" ) - @user = @user_manager.signup_confirm(@user.signup_token) - @user.email_confirmed.should be_true - end - - it "fail to confirm bogus signup token" do - expect { @user_manager.signup_confirm("murp") }.to raise_error ActiveRecord::RecordNotFound - end - - it "fail to confirm empty signup token" do - expect { @user_manager.signup_confirm("") }.to raise_error ActiveRecord::RecordNotFound - end - - it "fail to confirm nil signup token" do - expect { @user_manager.signup_confirm(nil) }.to raise_error ActiveRecord::RecordNotFound - end - end -end