diff options
author | jessib <jessib@leap.se> | 2012-12-07 10:31:12 -0800 |
---|---|---|
committer | jessib <jessib@leap.se> | 2012-12-07 10:31:12 -0800 |
commit | e1a004e2b24c2c235452469abfefa033e24435b2 (patch) | |
tree | a4b1c89a5aa4d5ccb3c17521499c999dedd5842d /users | |
parent | 4793c2b770fcb018303b7ab226b75cc218364327 (diff) | |
parent | 8282b83c798ba4e5c1e26ec8243b82669b3ee6d4 (diff) |
Merge branch 'master' into help_develop
Conflicts:
app/views/layouts/application.html.haml
help/app/controllers/tickets_controller.rb
help/test/functional/tickets_controller_test.rb
users/test/support/stub_record_helper.rb
Diffstat (limited to 'users')
-rw-r--r-- | users/app/assets/javascripts/users.js.coffee | 7 | ||||
-rw-r--r-- | users/app/controllers/users_controller.rb | 8 | ||||
-rw-r--r-- | users/app/helpers/users_helper.rb | 30 | ||||
-rw-r--r-- | users/app/models/user.rb | 1 | ||||
-rw-r--r-- | users/app/views/users/_cancel_account.html.haml | 6 | ||||
-rw-r--r-- | users/app/views/users/_email_field.html.haml | 1 | ||||
-rw-r--r-- | users/app/views/users/_email_forward_field.html.haml | 1 | ||||
-rw-r--r-- | users/app/views/users/_form.html.haml | 14 | ||||
-rw-r--r-- | users/app/views/users/_legend_and_submit.html.haml | 4 | ||||
-rw-r--r-- | users/app/views/users/_login_field.html.haml | 1 | ||||
-rw-r--r-- | users/app/views/users/_password_fields.html.haml | 2 | ||||
-rw-r--r-- | users/app/views/users/_user.html.haml | 3 | ||||
-rw-r--r-- | users/app/views/users/edit.html.haml | 15 | ||||
-rw-r--r-- | users/app/views/users/new.html.haml | 10 | ||||
-rw-r--r-- | users/config/locales/en.yml | 20 | ||||
-rw-r--r-- | users/test/functional/users_controller_test.rb | 55 | ||||
-rw-r--r-- | users/test/support/auth_test_helper.rb | 17 | ||||
-rw-r--r-- | users/test/support/stub_record_helper.rb | 26 |
18 files changed, 180 insertions, 41 deletions
diff --git a/users/app/assets/javascripts/users.js.coffee b/users/app/assets/javascripts/users.js.coffee index 76a6d79..0595292 100644 --- a/users/app/assets/javascripts/users.js.coffee +++ b/users/app/assets/javascripts/users.js.coffee @@ -3,7 +3,7 @@ preventDefault = (event) -> srp.session = new srp.Session() srp.signedUp = -> - srp.login + srp.login() srp.loggedIn = -> window.location = '/' @@ -29,7 +29,6 @@ $(document).ready -> $('#new_user').submit srp.signup $('#new_session').submit preventDefault $('#new_session').submit srp.login - $('.user.form.edit').submit srp.update - $('.user.form.edit').submit preventDefault + $('.user.form.change_password').submit srp.update + $('.user.form.change_password').submit preventDefault $('.user.typeahead').typeahead({source: pollUsers}); - diff --git a/users/app/controllers/users_controller.rb b/users/app/controllers/users_controller.rb index 3407191..4921a4a 100644 --- a/users/app/controllers/users_controller.rb +++ b/users/app/controllers/users_controller.rb @@ -30,13 +30,15 @@ class UsersController < ApplicationController end def update - @user.update_attributes(params[:user]) - respond_with @user + if @user.update_attributes(params[:user]) + flash[:notice] = t(:user_updated_successfully) + end + respond_with @user, :location => edit_user_path(@user) end def destroy @user.destroy - redirect_to users_path + redirect_to admin? ? users_path : login_path end protected diff --git a/users/app/helpers/users_helper.rb b/users/app/helpers/users_helper.rb index 2310a24..b017bca 100644 --- a/users/app/helpers/users_helper.rb +++ b/users/app/helpers/users_helper.rb @@ -1,2 +1,32 @@ module UsersHelper + + def user_form_with(partial, options = {}) + user_form(options) do |f| + options[:f] = f + render :partial => partial, + :layout => 'legend_and_submit', + :locals => options + end + end + + def user_form(options = {}) + simple_form_for @user, + :html => user_form_html_options(options), + :validate => true do |f| + yield f + end + end + + def user_form_html_options(options) + { :class => user_form_html_classes(options).join(" "), + :id => dom_id(@user, options[:legend]) + } + end + + def user_form_html_classes(options) + classes = %W/form-horizontal user form/ + classes << options[:legend] + classes << (@user.new_record? ? 'new' : 'edit') + classes.compact + end end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 325c981..ae271ce 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -2,6 +2,7 @@ class User < CouchRest::Model::Base property :login, String, :accessible => true property :email, String, :accessible => true + property :email_forward, String, :accessible => true property :password_verifier, String, :accessible => true property :password_salt, String, :accessible => true diff --git a/users/app/views/users/_cancel_account.html.haml b/users/app/views/users/_cancel_account.html.haml new file mode 100644 index 0000000..41580b0 --- /dev/null +++ b/users/app/views/users/_cancel_account.html.haml @@ -0,0 +1,6 @@ +%legend + =t :cancel_account + %small You will not be able to login anymore. += link_to user_path(@user), :method => :delete, :class => "btn btn-danger" do + %i.icon-remove.icon-white + Remove my Account diff --git a/users/app/views/users/_email_field.html.haml b/users/app/views/users/_email_field.html.haml new file mode 100644 index 0000000..36bbeca --- /dev/null +++ b/users/app/views/users/_email_field.html.haml @@ -0,0 +1 @@ += f.input :email diff --git a/users/app/views/users/_email_forward_field.html.haml b/users/app/views/users/_email_forward_field.html.haml new file mode 100644 index 0000000..049428f --- /dev/null +++ b/users/app/views/users/_email_forward_field.html.haml @@ -0,0 +1 @@ += f.input :email_forward diff --git a/users/app/views/users/_form.html.haml b/users/app/views/users/_form.html.haml index fc835af..cb51175 100644 --- a/users/app/views/users/_form.html.haml +++ b/users/app/views/users/_form.html.haml @@ -1,9 +1,11 @@ +- only = local_assigns[:only] - html = {:class => 'form-horizontal user form ' + (@user.new_record? ? 'new' : 'edit')} = simple_form_for @user, :validate => true, :format => :json, :html => html do |f| %legend - = @user.new_record? ? t(:signup_message) : t(:edit_settings) - = f.input :login, :input_html => { :id => :srp_username } - = f.input :password, :required => true, :validate => true, :input_html => { :id => :srp_password } - = f.input :password_confirmation, :required => true, :input_html => { :id => :srp_password_confirmation } - = f.button :submit, :class => 'btn-primary' - = link_to t(:cancel), root_url, :class => :btn + = t(only || :signup_message) + = yield + .pull-right + = f.button :submit + - unless only + = link_to t(:cancel), root_url, :class => :btn + .clearfix diff --git a/users/app/views/users/_legend_and_submit.html.haml b/users/app/views/users/_legend_and_submit.html.haml new file mode 100644 index 0000000..6fc0e4a --- /dev/null +++ b/users/app/views/users/_legend_and_submit.html.haml @@ -0,0 +1,4 @@ +%legend= t(legend) +=yield +.pull-right= f.button :submit, :value => t(legend) +.clearfix diff --git a/users/app/views/users/_login_field.html.haml b/users/app/views/users/_login_field.html.haml new file mode 100644 index 0000000..8ab36c3 --- /dev/null +++ b/users/app/views/users/_login_field.html.haml @@ -0,0 +1 @@ += f.input :login, :input_html => { :id => :srp_username } diff --git a/users/app/views/users/_password_fields.html.haml b/users/app/views/users/_password_fields.html.haml new file mode 100644 index 0000000..c2e6a69 --- /dev/null +++ b/users/app/views/users/_password_fields.html.haml @@ -0,0 +1,2 @@ += f.input :password, :required => true, :validate => true, :input_html => { :id => :srp_password } += f.input :password_confirmation, :required => true, :input_html => { :id => :srp_password_confirmation } diff --git a/users/app/views/users/_user.html.haml b/users/app/views/users/_user.html.haml index 5eb7941..7db0041 100644 --- a/users/app/views/users/_user.html.haml +++ b/users/app/views/users/_user.html.haml @@ -2,6 +2,9 @@ %td= user.login %td= time_ago_in_words(user.created_at) + " ago" %td + = link_to edit_user_path(user), :class => "btn btn-mini btn-primary" do + %i.icon-edit.icon-white + Edit = link_to user_path(user), :method => :delete, :class => "btn btn-danger btn-mini" do %i.icon-remove.icon-white Remove diff --git a/users/app/views/users/edit.html.haml b/users/app/views/users/edit.html.haml index 8298443..b33c19b 100644 --- a/users/app/views/users/edit.html.haml +++ b/users/app/views/users/edit.html.haml @@ -1,3 +1,16 @@ .span8.offset2 %h2=t :settings - = render 'form' + %ul.nav.nav-tabs + %li.active + %a{:href => '#account', 'data-toggle' => 'tab'}Account + %li + %a{:href => '#email', 'data-toggle' => 'tab'}Email + + .tab-content + .tab-pane.active#account + = user_form_with 'login_field', :legend => :change_login + = user_form_with 'password_fields', :legend => :change_password + = render 'cancel_account' if @user == current_user + .tab-pane#email + = user_form_with 'email_field', :legend => :set_email_address + = user_form_with 'email_forward_field', :legend => :forward_email diff --git a/users/app/views/users/new.html.haml b/users/app/views/users/new.html.haml index c1c4208..98cccb0 100644 --- a/users/app/views/users/new.html.haml +++ b/users/app/views/users/new.html.haml @@ -1,3 +1,11 @@ .span8.offset2 %h2=t :signup - = render 'form' + = user_form do |f| + %legend= t(:signup_message) + = render :partial => 'login_field', :locals => {:f => f} + = render :partial => 'password_fields', :locals => {:f => f} + .pull-right + = f.button :submit, :class => 'btn-primary' + = link_to t(:cancel), root_url, :class => :btn + .clearfix + diff --git a/users/config/locales/en.yml b/users/config/locales/en.yml index 1260494..fe7e824 100644 --- a/users/config/locales/en.yml +++ b/users/config/locales/en.yml @@ -6,9 +6,29 @@ en: login_message: "Please login with your account." wrong_password: "wrong password" user_not_found: "could not be found" + change_login: "Change Login" + change_password: "Change Password" + cancel_account: "Cancel your account" + set_email_address: "Set email address" + forward_email: "Forward email" + email_aliases: "Email aliases" + user_updated_successfully: "Settings have been updated successfully." + user_created_successfully: "Successfully created your account." activemodel: models: user: one: User other: "%{count} Users" + simple_form: + labels: + user: + email_forward: "Email forward" + hints: + user: + email_forward: "Forward all emails to this address" + email: "Your leap web email address" + placeholders: + user: + email_forward: "my_other_email@domain.net" + diff --git a/users/test/functional/users_controller_test.rb b/users/test/functional/users_controller_test.rb index ab29845..1840a72 100644 --- a/users/test/functional/users_controller_test.rb +++ b/users/test/functional/users_controller_test.rb @@ -5,6 +5,7 @@ class UsersControllerTest < ActionController::TestCase test "should get new" do get :new + assert_equal User, assigns(:user).class assert_response :success end @@ -12,7 +13,9 @@ class UsersControllerTest < ActionController::TestCase test "should create new user" do user = stub_record User User.expects(:create).with(user.params).returns(user) + post :create, :user => user.params, :format => :json + assert_nil session[:user_id] assert_json_response user assert_response :success @@ -24,50 +27,81 @@ class UsersControllerTest < ActionController::TestCase params.stringify_keys! assert !user.valid? User.expects(:create).with(params).returns(user) + post :create, :user => params, :format => :json + assert_json_error user.errors.messages assert_response 422 end test "should get edit view" do - user = stub_record User - User.expects(:find_by_param).with(user.id.to_s).returns(user) + user = find_record User, :email => nil, :email_forward => nil + login user get :edit, :id => user.id + assert_equal user, assigns[:user] end test "should process updated params" do - user = stub_record User + user = find_record User user.expects(:update_attributes).with(user.params).returns(true) - User.expects(:find_by_param).with(user.id.to_s).returns(user) + login user put :update, :user => user.params, :id => user.id, :format => :json + assert_equal user, assigns[:user] + assert_response 204 assert_equal " ", @response.body + end + + test "admin can update user" do + user = find_record User + user.expects(:update_attributes).with(user.params).returns(true) + + login :is_admin? => true + put :update, :user => user.params, :id => user.id, :format => :json + + assert_equal user, assigns[:user] assert_response 204 + assert_equal " ", @response.body end test "admin can destroy user" do - login :is_admin? => true - user = stub_record User + user = find_record User user.expects(:destroy) - User.expects(:find_by_param).with(user.id.to_s).returns(user) + + login :is_admin? => true delete :destroy, :id => user.id + + assert_response :redirect + assert_redirected_to users_path + end + + test "user can cancel account" do + user = find_record User + user.expects(:destroy) + + login user + delete :destroy, :id => @current_user.id + assert_response :redirect - # assert_redirected_to users_path + assert_redirected_to login_path end test "non-admin can't destroy user" do - login user = stub_record User + + login delete :destroy, :id => user.id + assert_access_denied end test "admin can list users" do login :is_admin? => true get :index + assert_response :success assert assigns(:users) end @@ -75,12 +109,14 @@ class UsersControllerTest < ActionController::TestCase test "non-admin can't list users" do login get :index + assert_access_denied end test "admin can autocomplete users" do login :is_admin? => true get :index, :format => :json + assert_response :success assert assigns(:users) end @@ -88,6 +124,7 @@ class UsersControllerTest < ActionController::TestCase test "admin can search users" do login :is_admin? => true get :index, :query => "a" + assert_response :success assert assigns(:users) end diff --git a/users/test/support/auth_test_helper.rb b/users/test/support/auth_test_helper.rb index 99dc141..6a82f24 100644 --- a/users/test/support/auth_test_helper.rb +++ b/users/test/support/auth_test_helper.rb @@ -10,8 +10,8 @@ module AuthTestHelper end end - def login(user_or_method_hash = nil) - @current_user = stub_user(user_or_method_hash) + def login(user_or_method_hash = {}) + @current_user = stub_record(User, user_or_method_hash) unless @current_user.respond_to? :is_admin? @current_user.stubs(:is_admin?).returns(false) end @@ -30,19 +30,6 @@ module AuthTestHelper end end - protected - - # Will create a stub user for logging in from either - # * a hash of methods to stub - # * a user record - # * nil -> create a user record stub - def stub_user(user_or_method_hash) - if user_or_method_hash.is_a?(Hash) - stub_record User, user_or_method_hash - else - user_or_method_hash || stub_record(User) - end - end end class ActionController::TestCase diff --git a/users/test/support/stub_record_helper.rb b/users/test/support/stub_record_helper.rb index ede21cf..2e1a533 100644 --- a/users/test/support/stub_record_helper.rb +++ b/users/test/support/stub_record_helper.rb @@ -1,19 +1,41 @@ module StubRecordHelper + # Will expect find_by_param or find_by_id to be called on klass and + # return the record given. + # If no record is given but a hash or nil will create a stub based on + # that instead and returns the stub. + def find_record(klass, record_or_method_hash = {}) + record = stub_record(klass, record_or_method_hash) + finder = klass.respond_to?(:find_by_param) ? :find_by_param : :find_by_id + klass.expects(finder).with(record.to_param).returns(record) + return record + end + # Create a stub that has the usual functions of a database record. # It won't fail on rendering a form for example. - def stub_record(klass, params = {}, persisted = true) + # + # If the second parameter is a record we return the record itself. + # This way you can build functions that either take a record or a + # method hash to stub from. See find_record for an example. + def stub_record(klass, record_or_method_hash = {}, persisted = true) + if record_or_method_hash && !record_or_method_hash.is_a?(Hash) + return record_or_method_hash + end + stub record_params_for(klass, record_or_method_hash, persisted) + end + + def record_params_for(klass, params = {}, persisted = true) if klass.respond_to?(:valid_attributes_hash) params.reverse_merge!(klass.valid_attributes_hash) end params[:params] = params.stringify_keys params.reverse_merge! :id => "A123", + :to_param => "A123", :class => klass, :to_key => ['123'], :to_json => %Q({"stub":"#{klass.name}"}), :new_record? => !persisted, :persisted? => persisted - stub params end end |