From d70c5796a2989b7b43f7e287899fb1394ae28280 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 19 Jul 2013 16:02:02 +0200 Subject: separate signup and settings service objects for user --- users/test/functional/v1/users_controller_test.rb | 8 ++++++-- users/test/integration/api/account_flow_test.rb | 13 +++++-------- users/test/unit/identity_test.rb | 20 ++++++++++---------- users/test/unit/user_test.rb | 2 +- 4 files changed, 22 insertions(+), 21 deletions(-) (limited to 'users/test') diff --git a/users/test/functional/v1/users_controller_test.rb b/users/test/functional/v1/users_controller_test.rb index 0d44e50..a330bf3 100644 --- a/users/test/functional/v1/users_controller_test.rb +++ b/users/test/functional/v1/users_controller_test.rb @@ -5,7 +5,9 @@ class V1::UsersControllerTest < ActionController::TestCase test "user can change settings" do user = find_record :user changed_attribs = record_attributes_for :user_with_settings - user.expects(:update_attributes).with(changed_attribs) + account_settings = stub + account_settings.expects(:update).with(changed_attribs) + AccountSettings.expects(:new).with(user).returns(account_settings) login user put :update, :user => changed_attribs, :id => user.id, :format => :json @@ -18,7 +20,9 @@ class V1::UsersControllerTest < ActionController::TestCase test "admin can update user" do user = find_record :user changed_attribs = record_attributes_for :user_with_settings - user.expects(:update_attributes).with(changed_attribs) + account_settings = stub + account_settings.expects(:update).with(changed_attribs) + AccountSettings.expects(:new).with(user).returns(account_settings) login :is_admin? => true put :update, :user => changed_attribs, :id => user.id, :format => :json diff --git a/users/test/integration/api/account_flow_test.rb b/users/test/integration/api/account_flow_test.rb index 93b6507..c09dcb6 100644 --- a/users/test/integration/api/account_flow_test.rb +++ b/users/test/integration/api/account_flow_test.rb @@ -84,20 +84,17 @@ class AccountFlowTest < RackTest identity.destroy end put "http://api.lvh.me:3000/1/users/" + @user.id + '.json', :user => {:public_key => test_public_key, :login => new_login}, :format => :json - @user.reload assert last_response.successful? - assert_equal test_public_key, @user.public_key - assert_equal new_login, @user.login + assert_equal test_public_key, Identity.for(@user).keys[:pgp] + # does not change login if no password_verifier is present + assert_equal original_login, @user.login # eventually probably want to remove most of this into a non-integration functional test # should not overwrite public key: put "http://api.lvh.me:3000/1/users/" + @user.id + '.json', :user => {:blee => :blah}, :format => :json - @user.reload - assert_equal test_public_key, @user.public_key + assert_equal test_public_key, Identity.for(@user).keys[:pgp] # should overwrite public key: put "http://api.lvh.me:3000/1/users/" + @user.id + '.json', :user => {:public_key => nil}, :format => :json - # TODO: not sure why i need this, but when public key is removed, the DB is updated but @user.reload doesn't seem to actually reload. - @user = User.find(@user.id) # @user.reload - assert_nil @user.public_key + assert_nil Identity.for(@user).keys[:pgp] end end diff --git a/users/test/unit/identity_test.rb b/users/test/unit/identity_test.rb index 40b1296..bf24f02 100644 --- a/users/test/unit/identity_test.rb +++ b/users/test/unit/identity_test.rb @@ -11,28 +11,28 @@ class IdentityTest < ActiveSupport::TestCase end test "initial identity for a user" do - id = @user.identity + id = Identity.for(@user) assert_equal @user.email_address, id.address assert_equal @user.email_address, id.destination assert_equal @user, id.user end test "add alias" do - id = @user.build_identity address: alias_name + id = Identity.for @user, address: alias_name assert_equal LocalEmail.new(alias_name), id.address assert_equal @user.email_address, id.destination assert_equal @user, id.user end test "add forward" do - id = @user.build_identity destination: forward_address + id = Identity.for @user, destination: forward_address assert_equal @user.email_address, id.address assert_equal Email.new(forward_address), id.destination assert_equal @user, id.user end test "forward alias" do - id = @user.build_identity address: alias_name, destination: forward_address + id = Identity.for @user, address: alias_name, destination: forward_address assert_equal LocalEmail.new(alias_name), id.address assert_equal Email.new(forward_address), id.destination assert_equal @user, id.user @@ -40,29 +40,29 @@ class IdentityTest < ActiveSupport::TestCase end test "prevents duplicates" do - id = @user.create_identity address: alias_name, destination: forward_address - dup = @user.build_identity address: alias_name, destination: forward_address + id = Identity.create_for @user, address: alias_name, destination: forward_address + dup = Identity.build_for @user, address: alias_name, destination: forward_address assert !dup.valid? assert_equal ["This alias already exists"], dup.errors[:base] end test "validates availability" do other_user = FactoryGirl.create(:user) - id = @user.create_identity address: alias_name, destination: forward_address - taken = other_user.build_identity address: alias_name + id = Identity.create_for @user, address: alias_name, destination: forward_address + taken = Identity.build_for other_user, address: alias_name assert !taken.valid? assert_equal ["This email has already been taken"], taken.errors[:base] other_user.destroy end test "setting and getting pgp key" do - id = @user.identity + id = Identity.for(@user) id.set_key(:pgp, pgp_key_string) assert_equal pgp_key_string, id.keys[:pgp] end test "querying pgp key via couch" do - id = @user.identity + id = Identity.for(@user) id.set_key(:pgp, pgp_key_string) id.save view = Identity.pgp_key_by_email.key(id.address) diff --git a/users/test/unit/user_test.rb b/users/test/unit/user_test.rb index f303c8d..477c727 100644 --- a/users/test/unit/user_test.rb +++ b/users/test/unit/user_test.rb @@ -58,7 +58,7 @@ class UserTest < ActiveSupport::TestCase test "login needs to be unique amongst aliases" do other_user = FactoryGirl.create :user - other_user.create_identity address: @user.login + Identity.create_for other_user, address: @user.login assert !@user.valid? other_user.destroy end -- cgit v1.2.3