/testscripts/SlingRuby/kerns/kern-2035.rb

https://github.com/sgithens/nakamura · Ruby · 279 lines · 174 code · 43 blank · 62 comment · 0 complexity · 74fed0a7d0698d5cd5fffdfba49f471b MD5 · raw file

  1. #!/usr/bin/env ruby
  2. require 'nakamura/test'
  3. require 'nakamura/file'
  4. require 'nakamura/users'
  5. include SlingUsers
  6. class TC_Kern2035Test < Test::Unit::TestCase
  7. include SlingTest
  8. # tests posting "basic" profile to the profile root element
  9. # this test posts to ~user/public/authprofile.profile.json then checks the same for
  10. # the information that was posted
  11. def test_post_basic_to_authprofile
  12. # create test users
  13. u = create_test_user(2035)
  14. @s.switch_user(u)
  15. profile_root = "~#{u.name}/public/authprofile"
  16. # update using the profile update servlet
  17. data = {}
  18. data[':operation'] = 'import'
  19. data[':contentType'] = 'json'
  20. data[':content'] = '{"basic": {"elements": {"preferredName": {"value": "great tester"}}}}'
  21. res = @s.execute_post(@s.url_for("#{profile_root}.profile.json"), data)
  22. assert_equal('200', res.code, "Should be able to post successfully to the user profile: #{res}\n#{res.body}")
  23. # check that the new details are on the profile feed
  24. res = @s.execute_get(@s.url_for("#{profile_root}.profile.json"))
  25. assert_equal('200', res.code, "Should be able to retrieve profile: #{res}\n#{res.body}")
  26. profile = JSON.parse(res.body)
  27. assert_equal('great tester', profile['basic']['elements']['preferredName']['value'])
  28. # check that the new 'basic' details are not stored in the profile content
  29. res = @s.execute_get(@s.url_for("#{profile_root}.3.json"))
  30. assert_equal('200', res.code, 'Should be able to retrieve profile data')
  31. profile = JSON.parse(res.body)
  32. assert_nil(profile['basic']['elements']['preferredName'])
  33. end
  34. # tests posting "basic" profile to a "basic" subnode of the profile root element
  35. # this test posts to ~user/public/authprofile/basic.profile.json then checks
  36. # ~user/public/authprofile.profile.json and ~user/public/authprofile/basic.json
  37. # for the information that was posted.
  38. def test_post_basic_to_subnode
  39. # create test users
  40. u = create_test_user(2035)
  41. @s.switch_user(u)
  42. profile_root = "~#{u.name}/public/authprofile"
  43. # update using the profile update servlet
  44. data = {}
  45. data[':operation'] = 'import'
  46. data[':contentType'] = 'json'
  47. data[':content'] = '{"elements": {"preferredName": {"value": "great tester"}}}'
  48. data[':removeTree'] = true
  49. res = @s.execute_post(@s.url_for("#{profile_root}/basic.profile.json"), data)
  50. assert_equal('200', res.code, "Should be able to post successfully to the user profile: #{res}\n#{res.body}")
  51. # check that the new details are on the profile feed
  52. res = @s.execute_get(@s.url_for("#{profile_root}.profile.json"))
  53. assert_equal('200', res.code, "Should be able to retrieve profile: #{res}\n#{res.body}")
  54. profile = JSON.parse(res.body)
  55. assert_equal('great tester', profile['basic']['elements']['preferredName']['value'])
  56. # check that the new 'basic' details are not stored in the profile content
  57. res = @s.execute_get(@s.url_for("#{profile_root}/basic.2.json"))
  58. assert_equal('200', res.code, 'Should be able to retrieve profile data')
  59. profile = JSON.parse(res.body)
  60. assert_nil(profile['elements']['preferredName'])
  61. end
  62. # tests posting "basic" profile to a "basic" subnode not on the profile root element
  63. # since this "basic" section is not on the root, it should not be copied to the authorizable
  64. def test_add_basic_not_at_root
  65. # create test users
  66. u = create_test_user(2035)
  67. @s.switch_user(u)
  68. profile_root = "~#{u.name}/public/authprofile"
  69. # update using the profile update servlet
  70. data = {}
  71. data[':operation'] = 'import'
  72. data[':contentType'] = 'json'
  73. data[':content'] = '{"elements": {"preferredName": {"value": "great tester"}}}'
  74. data[':removeTree'] = true
  75. res = @s.execute_post(@s.url_for("#{profile_root}/random/basic.profile.json"), data)
  76. assert_equal('200', res.code, "Should be able to post successfully to the user profile: #{res}\n#{res.body}")
  77. # check that the new details are not in the root "basic" section
  78. res = @s.execute_get(@s.url_for("#{profile_root}.profile.json"))
  79. assert_equal('200', res.code, "Should be able to retrieve profile: #{res}\n#{res.body}")
  80. profile = JSON.parse(res.body)
  81. assert_nil(profile['basic']['elements']['preferredName'])
  82. # check that the new 'basic' details are stored in the subnode location
  83. res = @s.execute_get(@s.url_for("#{profile_root}/random/basic.2.json"))
  84. assert_equal('200', res.code, 'Should be able to retrieve profile data')
  85. profile = JSON.parse(res.body)
  86. assert_equal('great tester', profile['elements']['preferredName']['value'])
  87. # check that the new 'basic' details are available in the profile feed
  88. res = @s.execute_get(@s.url_for("#{profile_root}.profile.json"))
  89. assert_equal('200', res.code, 'Should be able to retrieve profile data')
  90. profile = JSON.parse(res.body)
  91. assert_equal('great tester', profile['random']['basic']['elements']['preferredName']['value'])
  92. end
  93. # tests posting "aboutme" profile to the profile root element
  94. # this test posts to ~user/public/authprofile.profile.json then checks
  95. # ~user/public/authprofile.profile.json and ~user/public/authprofile/aboutme.json
  96. # for the information that was posted.
  97. def test_post_aboutme_to_authprofile
  98. # create test users
  99. u = create_test_user(2035)
  100. @s.switch_user(u)
  101. profile_root = "~#{u.name}/public/authprofile"
  102. # update using the profile update servlet
  103. data = {}
  104. data[':operation'] = 'import'
  105. data[':contentType'] = 'json'
  106. data[':content'] = '{"aboutme": {"elements":{"aboutme":{"value":"test1"},"academicinterests":{"value":"test2"},"personalinterests":{"value":"test3"},"hobbies":{"value":"test4"}}}}'
  107. res = @s.execute_post(@s.url_for("#{profile_root}.profile.json"), data)
  108. assert_equal('200', res.code, "Should be able to post successfully to the user profile: #{res}\n#{res.body}")
  109. # check that the new details are on the profile feed
  110. res = @s.execute_get(@s.url_for("#{profile_root}.profile.json"))
  111. assert_equal('200', res.code, "Should be able to retrieve profile: #{res}\n#{res.body}")
  112. profile = JSON.parse(res.body)
  113. assert_equal('test1', profile['aboutme']['elements']['aboutme']['value'])
  114. assert_equal('test2', profile['aboutme']['elements']['academicinterests']['value'])
  115. assert_equal('test3', profile['aboutme']['elements']['personalinterests']['value'])
  116. assert_equal('test4', profile['aboutme']['elements']['hobbies']['value'])
  117. # check that the new 'basic' details are not stored in the profile content
  118. res = @s.execute_get(@s.url_for("#{profile_root}.3.json"))
  119. assert_equal('200', res.code, 'Should be able to retrieve profile data')
  120. profile = JSON.parse(res.body)
  121. assert_equal('test1', profile['aboutme']['elements']['aboutme']['value'])
  122. assert_equal('test2', profile['aboutme']['elements']['academicinterests']['value'])
  123. assert_equal('test3', profile['aboutme']['elements']['personalinterests']['value'])
  124. assert_equal('test4', profile['aboutme']['elements']['hobbies']['value'])
  125. end
  126. # tests posting "aboutme" profile to a "aboutme" subnode of the profile root element
  127. # this test posts to ~user/public/authprofile/aboutme.profile.json then checks
  128. # ~user/public/authprofile.profile.json and ~user/public/authprofile/aboutme.json
  129. # for the information that was posted.
  130. def test_post_aboutme_to_subnode
  131. # create test users
  132. u = create_test_user(2035)
  133. @s.switch_user(u)
  134. profile_root = "~#{u.name}/public/authprofile"
  135. # update using the profile update servlet
  136. data = {}
  137. data[':operation'] = 'import'
  138. data[':contentType'] = 'json'
  139. data[':content'] = '{"elements":{"aboutme":{"value":"test1"},"academicinterests":{"value":"test2"},"personalinterests":{"value":"test3"},"hobbies":{"value":"test4"}}}'
  140. data[':removeTree'] = true
  141. res = @s.execute_post(@s.url_for("#{profile_root}/aboutme.profile.json"), data)
  142. assert_equal('200', res.code, "Should be able to post successfully to the user profile: #{res}\n#{res.body}")
  143. # check that the new details are on the profile feed
  144. res = @s.execute_get(@s.url_for("#{profile_root}.profile.json"))
  145. assert_equal('200', res.code, "Should be able to retrieve profile: #{res}\n#{res.body}")
  146. profile = JSON.parse(res.body)
  147. assert_equal('test1', profile['aboutme']['elements']['aboutme']['value'])
  148. assert_equal('test2', profile['aboutme']['elements']['academicinterests']['value'])
  149. assert_equal('test3', profile['aboutme']['elements']['personalinterests']['value'])
  150. assert_equal('test4', profile['aboutme']['elements']['hobbies']['value'])
  151. # check that the new 'basic' details are not stored in the profile content
  152. res = @s.execute_get(@s.url_for("#{profile_root}/aboutme.2.json"))
  153. assert_equal('200', res.code, 'Should be able to retrieve profile data')
  154. profile = JSON.parse(res.body)
  155. assert_equal('test1', profile['elements']['aboutme']['value'])
  156. assert_equal('test2', profile['elements']['academicinterests']['value'])
  157. assert_equal('test3', profile['elements']['personalinterests']['value'])
  158. assert_equal('test4', profile['elements']['hobbies']['value'])
  159. end
  160. # tests adding,updating,replacing a list of publications
  161. # this test posts to ~user/public/authprofile/publications.profile.json then checks
  162. # ~user/public/authprofile.profile.json and ~user/public/authprofile/publications.json
  163. # for the information that was posted. the test then replaces an entry in the list
  164. # and replaces the entire list
  165. def test_update_publications
  166. # create test users
  167. u = create_test_user(2035)
  168. @s.switch_user(u)
  169. profile_root = "~#{u.name}/public/authprofile"
  170. #
  171. # add publications using the profile update servlet to the publication subnode
  172. #
  173. data = {}
  174. data[':operation'] = 'import'
  175. data[':contentType'] = 'json'
  176. data[':removeTree'] = true
  177. elements = []
  178. (1..3).each do |i|
  179. elements << "'#{i}':{'id':{'display':false,'value':'#{i}'},'maintitle':{'label':'__MSG__PROFILE_PUBLICATIONS_MAIN_TITLE__','required':true,'display':true,'example':'__MSG__PROFILE_PUBLICATIONS_MAIN_TITLE_EXAMPLE__','value':'test#{i}'},'mainauthor':{'label':'__MSG__PROFILE_PUBLICATIONS_MAIN_AUTHOR__','required':true,'display':true,'value':'test#{i}'},'coauthor':{'label':'__MSG__PROFILE_PUBLICATIONS_CO_AUTHOR__','required':false,'display':true,'example':'__MSG__PROFILE_PUBLICATIONS_CO_AUTHOR_EXAMPLE__','value':''},'publisher':{'label':'__MSG__PROFILE_PUBLICATIONS_PUBLISHER__','required':true,'display':true,'value':'test#{i}'},'placeofpublication':{'label':'__MSG__PROFILE_PUBLICATIONS_PLACE_OF_PUBLICATION__','required':true,'display':true,'value':'test#{i}'},'volumetitle':{'label':'__MSG__PROFILE_PUBLICATIONS_VOLUME_TITLE__','required':false,'display':true,'value':''},'volumeinformation':{'label':'__MSG__PROFILE_PUBLICATIONS_VOLUME_INFORMATION__','required':false,'display':true,'example':'__MSG__PROFILE_PUBLICATIONS_VOLUME_INFORMATION_EXAMPLE__','value':''},'year':{'label':'__MSG__PROFILE_PUBLICATIONS_YEAR__','required':true,'display':true,'value':'test#{i}'},'number':{'label':'__MSG__PROFILE_PUBLICATIONS_NUMBER__','required':false,'display':true,'value':''},'series title':{'label':'__MSG__PROFILE_PUBLICATIONS_SERIES_TITLE__','required':false,'display':true,'value':''},'url':{'label':'__MSG__PROFILE_PUBLICATIONS_URL__','required':false,'display':true,'validation':'appendhttp url','value':''},'order':0}"
  180. end
  181. data[':content'] = "{'elements':{#{elements.join(',')}}}"
  182. res = @s.execute_post(@s.url_for("#{profile_root}/publications.profile.json"), data)
  183. assert_equal('200', res.code, "Should be able to post successfully to the user profile: #{res}\n#{res.body}")
  184. # check that the new details are on the profile feed
  185. res = @s.execute_get(@s.url_for("#{profile_root}.profile.json"))
  186. assert_equal('200', res.code, "Should be able to retrieve profile: #{res}\n#{res.body}")
  187. profile = JSON.parse(res.body)
  188. # number of entries + 1 for the _path element
  189. assert_equal(4, profile['publications']['elements'].length)
  190. assert_not_nil(profile['publications']['elements']['1'])
  191. assert_not_nil(profile['publications']['elements']['2'])
  192. assert_not_nil(profile['publications']['elements']['3'])
  193. #
  194. # replace an entry in the list
  195. #
  196. i = 4
  197. elements[1] = "'#{i}':{'id':{'display':false,'value':'#{i}'},'maintitle':{'label':'__MSG__PROFILE_PUBLICATIONS_MAIN_TITLE__','required':true,'display':true,'example':'__MSG__PROFILE_PUBLICATIONS_MAIN_TITLE_EXAMPLE__','value':'test#{i}'},'mainauthor':{'label':'__MSG__PROFILE_PUBLICATIONS_MAIN_AUTHOR__','required':true,'display':true,'value':'test#{i}'},'coauthor':{'label':'__MSG__PROFILE_PUBLICATIONS_CO_AUTHOR__','required':false,'display':true,'example':'__MSG__PROFILE_PUBLICATIONS_CO_AUTHOR_EXAMPLE__','value':''},'publisher':{'label':'__MSG__PROFILE_PUBLICATIONS_PUBLISHER__','required':true,'display':true,'value':'test#{i}'},'placeofpublication':{'label':'__MSG__PROFILE_PUBLICATIONS_PLACE_OF_PUBLICATION__','required':true,'display':true,'value':'test#{i}'},'volumetitle':{'label':'__MSG__PROFILE_PUBLICATIONS_VOLUME_TITLE__','required':false,'display':true,'value':''},'volumeinformation':{'label':'__MSG__PROFILE_PUBLICATIONS_VOLUME_INFORMATION__','required':false,'display':true,'example':'__MSG__PROFILE_PUBLICATIONS_VOLUME_INFORMATION_EXAMPLE__','value':''},'year':{'label':'__MSG__PROFILE_PUBLICATIONS_YEAR__','required':true,'display':true,'value':'test#{i}'},'number':{'label':'__MSG__PROFILE_PUBLICATIONS_NUMBER__','required':false,'display':true,'value':''},'series title':{'label':'__MSG__PROFILE_PUBLICATIONS_SERIES_TITLE__','required':false,'display':true,'value':''},'url':{'label':'__MSG__PROFILE_PUBLICATIONS_URL__','required':false,'display':true,'validation':'appendhttp url','value':''},'order':0}"
  198. data[':content'] = "{'elements':{#{elements.join(',')}}}"
  199. res = @s.execute_post(@s.url_for("#{profile_root}/publications.profile.json"), data)
  200. assert_equal('200', res.code, "Should be able to post successfully to the user profile: #{res}\n#{res.body}")
  201. # check that the new details are on the profile feed
  202. res = @s.execute_get(@s.url_for("#{profile_root}.profile.json"))
  203. assert_equal('200', res.code, "Should be able to retrieve profile: #{res}\n#{res.body}")
  204. profile = JSON.parse(res.body)
  205. # number of entries + 1 for the _path element
  206. assert_equal(4, profile['publications']['elements'].length)
  207. assert_not_nil(profile['publications']['elements']['1'])
  208. assert_nil(profile['publications']['elements']['2'])
  209. assert_not_nil(profile['publications']['elements']['3'])
  210. assert_not_nil(profile['publications']['elements']['4'])
  211. #
  212. # replace the publications with completely different entries
  213. #
  214. elements = []
  215. (5..6).each do |i|
  216. elements << "'#{i}':{'id':{'display':false,'value':'#{i}'},'maintitle':{'label':'__MSG__PROFILE_PUBLICATIONS_MAIN_TITLE__','required':true,'display':true,'example':'__MSG__PROFILE_PUBLICATIONS_MAIN_TITLE_EXAMPLE__','value':'test#{i}'},'mainauthor':{'label':'__MSG__PROFILE_PUBLICATIONS_MAIN_AUTHOR__','required':true,'display':true,'value':'test#{i}'},'coauthor':{'label':'__MSG__PROFILE_PUBLICATIONS_CO_AUTHOR__','required':false,'display':true,'example':'__MSG__PROFILE_PUBLICATIONS_CO_AUTHOR_EXAMPLE__','value':''},'publisher':{'label':'__MSG__PROFILE_PUBLICATIONS_PUBLISHER__','required':true,'display':true,'value':'test#{i}'},'placeofpublication':{'label':'__MSG__PROFILE_PUBLICATIONS_PLACE_OF_PUBLICATION__','required':true,'display':true,'value':'test#{i}'},'volumetitle':{'label':'__MSG__PROFILE_PUBLICATIONS_VOLUME_TITLE__','required':false,'display':true,'value':''},'volumeinformation':{'label':'__MSG__PROFILE_PUBLICATIONS_VOLUME_INFORMATION__','required':false,'display':true,'example':'__MSG__PROFILE_PUBLICATIONS_VOLUME_INFORMATION_EXAMPLE__','value':''},'year':{'label':'__MSG__PROFILE_PUBLICATIONS_YEAR__','required':true,'display':true,'value':'test#{i}'},'number':{'label':'__MSG__PROFILE_PUBLICATIONS_NUMBER__','required':false,'display':true,'value':''},'series title':{'label':'__MSG__PROFILE_PUBLICATIONS_SERIES_TITLE__','required':false,'display':true,'value':''},'url':{'label':'__MSG__PROFILE_PUBLICATIONS_URL__','required':false,'display':true,'validation':'appendhttp url','value':''},'order':0}"
  217. end
  218. data[':content'] = "{'elements':{#{elements.join(',')}}}"
  219. res = @s.execute_post(@s.url_for("#{profile_root}/publications.profile.json"), data)
  220. assert_equal('200', res.code, "Should be able to post successfully to the user profile: #{res}\n#{res.body}")
  221. # check that the details have been updated
  222. res = @s.execute_get(@s.url_for("#{profile_root}.profile.json"))
  223. assert_equal('200', res.code, "Should be able to retrieve profile: #{res}\n#{res.body}")
  224. profile = JSON.parse(res.body)
  225. # number of entries + 1 for the _path element
  226. assert_equal(3, profile['publications']['elements'].length)
  227. # verify the old entries are gone
  228. assert_nil(profile['publications']['elements']['1'])
  229. assert_nil(profile['publications']['elements']['2'])
  230. assert_nil(profile['publications']['elements']['3'])
  231. assert_nil(profile['publications']['elements']['4'])
  232. # check for the new entries
  233. assert_not_nil('2', profile['publications']['elements']['5'])
  234. assert_not_nil('3', profile['publications']['elements']['6'])
  235. end
  236. end