user_test.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class UserTest < ActiveSupport::TestCase
  4. test 'user' do
  5. tests = [
  6. {
  7. :name => '#1 - simple create',
  8. :create => {
  9. :firstname => 'Firstname',
  10. :lastname => 'Lastname',
  11. :email => 'some@example.com',
  12. :login => 'some@example.com',
  13. :updated_by_id => 1,
  14. :created_by_id => 1,
  15. },
  16. :create_verify => {
  17. :firstname => 'Firstname',
  18. :lastname => 'Lastname',
  19. :image => 'none',
  20. :email => 'some@example.com',
  21. :login => 'some@example.com',
  22. },
  23. },
  24. {
  25. :name => '#2 - simple create - no lastname',
  26. :create => {
  27. :firstname => 'Firstname Lastname',
  28. :lastname => '',
  29. :email => 'some@example.com',
  30. :login => 'some@example.com',
  31. :updated_by_id => 1,
  32. :created_by_id => 1,
  33. },
  34. :create_verify => {
  35. :firstname => 'Firstname',
  36. :lastname => 'Lastname',
  37. :image => 'none',
  38. :email => 'some@example.com',
  39. :login => 'some@example.com',
  40. },
  41. },
  42. {
  43. :name => '#3 - simple create - nil as lastname',
  44. :create => {
  45. :firstname => 'Firstname Lastname',
  46. :lastname => nil,
  47. :email => 'some@example.com',
  48. :login => 'some@example.com',
  49. :updated_by_id => 1,
  50. :created_by_id => 1,
  51. },
  52. :create_verify => {
  53. :firstname => 'Firstname',
  54. :lastname => 'Lastname',
  55. :image => 'none',
  56. :email => 'some@example.com',
  57. :login => 'some@example.com',
  58. },
  59. },
  60. {
  61. :name => '#4 - simple create - no lastname, firstname with ","',
  62. :create => {
  63. :firstname => 'Lastname, Firstname',
  64. :lastname => '',
  65. :email => 'some@example.com',
  66. :login => 'some@example.com',
  67. :updated_by_id => 1,
  68. :created_by_id => 1,
  69. },
  70. :create_verify => {
  71. :firstname => 'Firstname',
  72. :lastname => 'Lastname',
  73. :email => 'some@example.com',
  74. :login => 'some@example.com',
  75. },
  76. },
  77. {
  78. :name => '#5 - simple create - no lastname/firstname',
  79. :create => {
  80. :firstname => '',
  81. :lastname => '',
  82. :email => 'firstname.lastname@example.com',
  83. :login => 'login-1',
  84. :updated_by_id => 1,
  85. :created_by_id => 1,
  86. },
  87. :create_verify => {
  88. :firstname => 'Firstname',
  89. :lastname => 'Lastname',
  90. :email => 'firstname.lastname@example.com',
  91. :login => 'login-1',
  92. },
  93. },
  94. {
  95. :name => '#6 - simple create - no lastname/firstnam',
  96. :create => {
  97. :firstname => '',
  98. :lastname => '',
  99. :email => 'FIRSTNAME.lastname@example.com',
  100. :login => 'login-2',
  101. :updated_by_id => 1,
  102. :created_by_id => 1,
  103. },
  104. :create_verify => {
  105. :firstname => 'Firstname',
  106. :lastname => 'Lastname',
  107. :email => 'firstname.lastname@example.com',
  108. :login => 'login-2',
  109. },
  110. },
  111. {
  112. :name => '#7 - simple create - nill as fristname and lastname',
  113. :create => {
  114. :firstname => nil,
  115. :lastname => nil,
  116. :email => 'FIRSTNAME.lastname@example.com',
  117. :login => 'login-3',
  118. :updated_by_id => 1,
  119. :created_by_id => 1,
  120. },
  121. :create_verify => {
  122. :firstname => 'Firstname',
  123. :lastname => 'Lastname',
  124. :email => 'firstname.lastname@example.com',
  125. :login => 'login-3',
  126. },
  127. },
  128. {
  129. :name => '#8 - update with avatar check',
  130. :create => {
  131. :firstname => 'Bob',
  132. :lastname => 'Smith',
  133. :email => 'bob.smith@example.com',
  134. :login => 'login-4',
  135. :updated_by_id => 1,
  136. :created_by_id => 1,
  137. },
  138. :create_verify => {
  139. :firstname => 'Bob',
  140. :lastname => 'Smith',
  141. :image => 'none',
  142. :image_md5 => '76fdc28c07e4f3d7802b75aacfccdf6a',
  143. :email => 'bob.smith@example.com',
  144. :login => 'login-4',
  145. },
  146. :update => {
  147. :email => 'unit-test1@znuny.com',
  148. },
  149. :update_verify => {
  150. :firstname => 'Bob',
  151. :lastname => 'Smith',
  152. :image => '9f19a507f4574b8455543408a7ef2257',
  153. :image_md5 => '9f19a507f4574b8455543408a7ef2257',
  154. :email => 'unit-test1@znuny.com',
  155. :login => 'login-4',
  156. }
  157. },
  158. {
  159. :name => '#9 - update create with avatar check',
  160. :create => {
  161. :firstname => 'Bob',
  162. :lastname => 'Smith',
  163. :email => 'unit-test2@znuny.com',
  164. :login => 'login-5',
  165. :updated_by_id => 1,
  166. :created_by_id => 1,
  167. },
  168. :create_verify => {
  169. :firstname => 'Bob',
  170. :lastname => 'Smith',
  171. :image => 'fb83638ff260b20555b25173169135dd',
  172. :image_md5 => 'fb83638ff260b20555b25173169135dd',
  173. :email => 'unit-test2@znuny.com',
  174. :login => 'login-5',
  175. },
  176. :update => {
  177. :email => 'unit-test1@znuny.com',
  178. },
  179. :update_verify => {
  180. :firstname => 'Bob',
  181. :lastname => 'Smith',
  182. :image => '9f19a507f4574b8455543408a7ef2257',
  183. :image_md5 => '9f19a507f4574b8455543408a7ef2257',
  184. :email => 'unit-test1@znuny.com',
  185. :login => 'login-5',
  186. }
  187. },
  188. ]
  189. tests.each { |test|
  190. # check if user exists
  191. user = User.where( :login => test[:create][:login] ).first
  192. if user
  193. user.destroy
  194. end
  195. user = User.create( test[:create] )
  196. test[:create_verify].each { |key, value|
  197. next if key == :image_md5
  198. assert_equal( value, user[key], "create check #{ key } in (#{ test[:name] })" )
  199. }
  200. if test[:create_verify][:image_md5]
  201. file = user.get_image
  202. file_md5 = Digest::MD5.hexdigest( file[:content] )
  203. assert_equal( test[:create_verify][:image_md5], file_md5, "create avatar md5 check in (#{ test[:name] })" )
  204. end
  205. if test[:update]
  206. user.update_attributes( test[:update] )
  207. test[:update_verify].each { |key, value|
  208. next if key == :image_md5
  209. assert_equal( value, user[key], "update check #{ key } in (#{ test[:name] })" )
  210. }
  211. if test[:update_verify][:image_md5]
  212. file = user.get_image
  213. file_md5 = Digest::MD5.hexdigest( file[:content] )
  214. assert_equal( test[:update_verify][:image_md5], file_md5, "update avatar md5 check in (#{ test[:name] })" )
  215. end
  216. end
  217. user.destroy
  218. }
  219. end
  220. end