From eb0a01eaa1997c20a18455ad23ef93fc7e5f0cfd Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 27 Jul 2014 13:40:42 +0200 Subject: [PATCH] Added unit tests to test user avatars. --- .../javascripts/app/models/user.js.coffee | 2 +- app/controllers/users_controller.rb | 30 ++--- app/models/user.rb | 50 +++++++- test/unit/user_test.rb | 114 ++++++++++++++++-- 4 files changed, 162 insertions(+), 34 deletions(-) diff --git a/app/assets/javascripts/app/models/user.js.coffee b/app/assets/javascripts/app/models/user.js.coffee index fbe8c056a..b7fc0ada9 100644 --- a/app/assets/javascripts/app/models/user.js.coffee +++ b/app/assets/javascripts/app/models/user.js.coffee @@ -18,7 +18,7 @@ class App.User extends App.Model { name: 'street', display: 'Street', tag: 'input', type: 'text', limit: 100, null: true, class: 'span4', signup: false, quick: true, info: true }, { name: 'zip', display: 'Zip', tag: 'input', type: 'text', limit: 100, null: true, class: 'span4', signup: false, quick: true, info: true }, { name: 'city', display: 'City', tag: 'input', type: 'text', limit: 100, null: true, class: 'span4', signup: false, quick: true, info: true }, - { name: 'password', display: 'Password', tag: 'input', type: 'password', limit: 50, null: true, autocomplete: 'off', class: 'span4', signup: true, quick: false, }, + { name: 'password', display: 'Password', tag: 'input', type: 'password', limit: 50, null: true, autocomplete: 'off', class: 'span4', signup: true, quick: false }, { name: 'note', display: 'Note', tag: 'textarea', note: 'Notes are visible to agents only, never to customers.', limit: 250, null: true, class: 'span4', quick: true, info: true }, { name: 'role_ids', display: 'Roles', tag: 'checkbox', multiple: true, null: false, relation: 'Role', class: 'span4' }, { name: 'group_ids', display: 'Groups', tag: 'checkbox', multiple: true, null: true, relation: 'Group', class: 'span4', invite_agent: true }, diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index b57ba7125..90cf6653e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -590,31 +590,19 @@ curl http://localhost/api/v1/users/image/8d6cca1c6bdc226cf2ba131e264ca2c7 -v -u response.headers["Cache-Control"] = "cache, store, max-age=31536000, must-revalidate" response.headers["Pragma"] = "cache" - # serve user image user = User.where( :image => params[:hash] ).first if user - # find file - list = Store.list( :object => 'User::Image', :o_id => user.id ) - if list && list[0] - file = Store.find( list[0] ) - send_data( - file.content, - :filename => file.filename, - :type => file.preferences['Content-Type'] || file.preferences['Mime-Type'], - :disposition => 'inline' - ) - return - end + image = user.get_image + send_data( + image[:content], + :filename => image[:filename], + :type => image[:content_type], + :disposition => 'inline' + ) + return end - # serve defalt image - image = 'R0lGODdhMAAwAOMAAMzMzJaWlr6+vqqqqqOjo8XFxbe3t7GxsZycnAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAMAAwAAAEcxDISau9OOvNu/9gKI5kaZ5oqq5s675wLM90bd94ru98TwuAA+KQAQqJK8EAgBAgMEqmkzUgBIeSwWGZtR5XhSqAULACCoGCJGwlm1MGQrq9RqgB8fm4ZTUgDBIEcRR9fz6HiImKi4yNjo+QkZKTlJWWkBEAOw==' - send_data( - Base64.decode64(image), - :filename => 'image.gif', - :type => 'image/gif', - :disposition => 'inline', - ) + render :json => {}, :status => 404 end end diff --git a/app/models/user.rb b/app/models/user.rb index b077f802b..6c512fe70 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -487,6 +487,48 @@ returns self.save end +=begin + +get image of user + + user = User.find(123) + result = user.get_image + +returns + + result = { + :filename => 'some filename', + :content_type => 'image/png', + :content => bin_string, + } + +=end + + def get_image + + # find file + list = Store.list( :object => 'User::Image', :o_id => self.id ) + puts list.inspect + if list && list[0] + file = Store.find( list[0] ) + result = { + :content => file.content, + :filename => file.filename, + :content_type => file.preferences['Content-Type'] || file.preferences['Mime-Type'], + } + return result + end + + # serve defalt image + image = 'R0lGODdhMAAwAOMAAMzMzJaWlr6+vqqqqqOjo8XFxbe3t7GxsZycnAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAMAAwAAAEcxDISau9OOvNu/9gKI5kaZ5oqq5s675wLM90bd94ru98TwuAA+KQAQqJK8EAgBAgMEqmkzUgBIeSwWGZtR5XhSqAULACCoGCJGwlm1MGQrq9RqgB8fm4ZTUgDBIEcRR9fz6HiImKi4yNjo+QkZKTlJWWkBEAOw==' + result = { + :content => Base64.decode64(image), + :filename => 'image.gif', + :content_type => 'image/gif', + } + return result + end + private def check_name @@ -552,6 +594,7 @@ returns if self.email hash = Digest::MD5.hexdigest(self.email) self.image_source = "http://www.gravatar.com/avatar/#{hash}?s=48&d=404" + #puts "#{self.email}: http://www.gravatar.com/avatar/#{hash}?s=48&d=404" end end end @@ -566,20 +609,20 @@ returns if !response.success? self.update_column( :image, 'none' ) self.cache_delete - #puts "WARNING: Can't fetch '#{self.image_source}', http code: #{response.code.to_s}" + #puts "WARNING: Can't fetch '#{self.image_source}' (maybe no avatar available), http code: #{response.code.to_s}" #raise "Can't fetch '#{self.image_source}', http code: #{response.code.to_s}" return end + #puts "NOTICE: Fetch '#{self.image_source}', http code: #{response.code.to_s}" # store image local hash = Digest::MD5.hexdigest( response.body ) # check if image has changed return if self.image == hash - + #puts "NOTICE: update image in store" # save new image self.update_column( :image, hash ) - self.cache_delete Store.remove( :object => 'User::Image', :o_id => self.id ) Store.add( :object => 'User::Image', @@ -591,6 +634,7 @@ returns }, :created_by_id => self.updated_by_id, ) + self.cache_delete end def check_password diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index ce9491e65..a923dbbf9 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -1,10 +1,11 @@ # encoding: utf-8 require 'test_helper' - + class UserTest < ActiveSupport::TestCase test 'user' do tests = [ { + :name => '#1 - simple create', :create => { :firstname => 'Firstname', :lastname => 'Lastname', @@ -16,11 +17,13 @@ class UserTest < ActiveSupport::TestCase :create_verify => { :firstname => 'Firstname', :lastname => 'Lastname', + :image => 'none', :email => 'some@example.com', :login => 'some@example.com', }, }, { + :name => '#2 - simple create - no lastname', :create => { :firstname => 'Firstname Lastname', :lastname => '', @@ -32,11 +35,13 @@ class UserTest < ActiveSupport::TestCase :create_verify => { :firstname => 'Firstname', :lastname => 'Lastname', + :image => 'none', :email => 'some@example.com', :login => 'some@example.com', }, }, { + :name => '#3 - simple create - nil as lastname', :create => { :firstname => 'Firstname Lastname', :lastname => nil, @@ -48,11 +53,13 @@ class UserTest < ActiveSupport::TestCase :create_verify => { :firstname => 'Firstname', :lastname => 'Lastname', + :image => 'none', :email => 'some@example.com', :login => 'some@example.com', }, }, { + :name => '#4 - simple create - no lastname, firstname with ","', :create => { :firstname => 'Lastname, Firstname', :lastname => '', @@ -69,11 +76,12 @@ class UserTest < ActiveSupport::TestCase }, }, { + :name => '#5 - simple create - no lastname/firstname', :create => { :firstname => '', :lastname => '', :email => 'firstname.lastname@example.com', - :login => 'login', + :login => 'login-1', :updated_by_id => 1, :created_by_id => 1, }, @@ -81,15 +89,16 @@ class UserTest < ActiveSupport::TestCase :firstname => 'Firstname', :lastname => 'Lastname', :email => 'firstname.lastname@example.com', - :login => 'login', + :login => 'login-1', }, }, { + :name => '#6 - simple create - no lastname/firstnam', :create => { :firstname => '', :lastname => '', :email => 'FIRSTNAME.lastname@example.com', - :login => 'login', + :login => 'login-2', :updated_by_id => 1, :created_by_id => 1, }, @@ -97,15 +106,16 @@ class UserTest < ActiveSupport::TestCase :firstname => 'Firstname', :lastname => 'Lastname', :email => 'firstname.lastname@example.com', - :login => 'login', + :login => 'login-2', }, }, { + :name => '#7 - simple create - nill as fristname and lastname', :create => { :firstname => nil, :lastname => nil, :email => 'FIRSTNAME.lastname@example.com', - :login => 'login', + :login => 'login-3', :updated_by_id => 1, :created_by_id => 1, }, @@ -113,21 +123,107 @@ class UserTest < ActiveSupport::TestCase :firstname => 'Firstname', :lastname => 'Lastname', :email => 'firstname.lastname@example.com', - :login => 'login', + :login => 'login-3', }, }, + { + :name => '#8 - update with avatar check', + :create => { + :firstname => 'Bob', + :lastname => 'Smith', + :email => 'bob.smith@example.com', + :login => 'login-4', + :updated_by_id => 1, + :created_by_id => 1, + }, + :create_verify => { + :firstname => 'Bob', + :lastname => 'Smith', + :image => 'none', + :image_md5 => '76fdc28c07e4f3d7802b75aacfccdf6a', + :email => 'bob.smith@example.com', + :login => 'login-4', + }, + :update => { + :email => 'unit-test1@znuny.com', + }, + :update_verify => { + :firstname => 'Bob', + :lastname => 'Smith', + :image => '9f19a507f4574b8455543408a7ef2257', + :image_md5 => '9f19a507f4574b8455543408a7ef2257', + :email => 'unit-test1@znuny.com', + :login => 'login-4', + } + }, + { + :name => '#9 - update create with avatar check', + :create => { + :firstname => 'Bob', + :lastname => 'Smith', + :email => 'unit-test2@znuny.com', + :login => 'login-5', + :updated_by_id => 1, + :created_by_id => 1, + }, + :create_verify => { + :firstname => 'Bob', + :lastname => 'Smith', + :image => 'fb83638ff260b20555b25173169135dd', + :image_md5 => 'fb83638ff260b20555b25173169135dd', + :email => 'unit-test2@znuny.com', + :login => 'login-5', + }, + :update => { + :email => 'unit-test1@znuny.com', + }, + :update_verify => { + :firstname => 'Bob', + :lastname => 'Smith', + :image => '9f19a507f4574b8455543408a7ef2257', + :image_md5 => '9f19a507f4574b8455543408a7ef2257', + :email => 'unit-test1@znuny.com', + :login => 'login-5', + } + }, ] tests.each { |test| + # check if user exists + user = User.where( :login => test[:create][:login] ).first + if user + user.destroy + end + user = User.create( test[:create] ) test[:create_verify].each { |key, value| - assert_equal( user[key], value ) + next if key == :image_md5 + assert_equal( value, user[key], "create check #{ key } in (#{ test[:name] })" ) } + if test[:create_verify][:image_md5] + file = user.get_image + file_md5 = Digest::MD5.hexdigest( file[:content] ) + assert_equal( test[:create_verify][:image_md5], file_md5, "create avatar md5 check in (#{ test[:name] })" ) + end + if test[:update] + user.update_attributes( test[:update] ) + + test[:update_verify].each { |key, value| + next if key == :image_md5 + assert_equal( value, user[key], "update check #{ key } in (#{ test[:name] })" ) + } + + if test[:update_verify][:image_md5] + file = user.get_image + file_md5 = Digest::MD5.hexdigest( file[:content] ) + assert_equal( test[:update_verify][:image_md5], file_md5, "update avatar md5 check in (#{ test[:name] })" ) + end + end user.destroy - } + } end end