settings_controller_test.rb 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class SettingsControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. # set accept header
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. # create agent
  8. roles = Role.where(name: %w(Admin Agent))
  9. groups = Group.all
  10. UserInfo.current_user_id = 1
  11. @admin_full = User.create_or_update(
  12. login: 'setting-admin',
  13. firstname: 'Setting',
  14. lastname: 'Admin',
  15. email: 'setting-admin@example.com',
  16. password: 'adminpw',
  17. active: true,
  18. roles: roles,
  19. groups: groups,
  20. )
  21. role_api = Role.create_or_update(
  22. name: 'AdminApi',
  23. note: 'To configure your api.',
  24. preferences: {
  25. not: ['Customer'],
  26. },
  27. default_at_signup: false,
  28. updated_by_id: 1,
  29. created_by_id: 1
  30. )
  31. role_api.permission_grant('admin.api')
  32. @admin_api = User.create_or_update(
  33. login: 'setting-admin-api',
  34. firstname: 'Setting',
  35. lastname: 'Admin Api',
  36. email: 'setting-admin-api@example.com',
  37. password: 'adminpw',
  38. active: true,
  39. roles: [role_api],
  40. groups: groups,
  41. )
  42. # create agent
  43. roles = Role.where(name: 'Agent')
  44. @agent = User.create_or_update(
  45. login: 'setting-agent@example.com',
  46. firstname: 'Setting',
  47. lastname: 'Agent',
  48. email: 'setting-agent@example.com',
  49. password: 'agentpw',
  50. active: true,
  51. roles: roles,
  52. groups: groups,
  53. )
  54. # create customer without org
  55. roles = Role.where(name: 'Customer')
  56. @customer_without_org = User.create_or_update(
  57. login: 'setting-customer1@example.com',
  58. firstname: 'Setting',
  59. lastname: 'Customer1',
  60. email: 'setting-customer1@example.com',
  61. password: 'customer1pw',
  62. active: true,
  63. roles: roles,
  64. )
  65. end
  66. test 'settings index with nobody' do
  67. # index
  68. get '/api/v1/settings', {}, @headers
  69. assert_response(401)
  70. result = JSON.parse(@response.body)
  71. assert_equal(Hash, result.class)
  72. assert_not(result['settings'])
  73. # show
  74. setting = Setting.find_by(name: 'product_name')
  75. get "/api/v1/settings/#{setting.id}", {}, @headers
  76. assert_response(401)
  77. result = JSON.parse(@response.body)
  78. assert_equal('authentication failed', result['error'])
  79. end
  80. test 'settings index with admin' do
  81. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('setting-admin@example.com', 'adminpw')
  82. # index
  83. get '/api/v1/settings', {}, @headers.merge('Authorization' => credentials)
  84. assert_response(200)
  85. result = JSON.parse(@response.body)
  86. assert_equal(Array, result.class)
  87. assert(result)
  88. hit_api = false
  89. hit_product_name = false
  90. result.each { |setting|
  91. if setting['name'] == 'api_token_access'
  92. hit_api = true
  93. end
  94. if setting['name'] == 'product_name'
  95. hit_product_name = true
  96. end
  97. }
  98. assert_equal(true, hit_api)
  99. assert_equal(true, hit_product_name)
  100. # show
  101. setting = Setting.find_by(name: 'product_name')
  102. get "/api/v1/settings/#{setting.id}", {}, @headers.merge('Authorization' => credentials)
  103. assert_response(200)
  104. result = JSON.parse(@response.body)
  105. assert_equal(Hash, result.class)
  106. assert_equal('product_name', result['name'])
  107. setting = Setting.find_by(name: 'api_token_access')
  108. get "/api/v1/settings/#{setting.id}", {}, @headers.merge('Authorization' => credentials)
  109. assert_response(200)
  110. result = JSON.parse(@response.body)
  111. assert_equal(Hash, result.class)
  112. assert_equal('api_token_access', result['name'])
  113. # update
  114. setting = Setting.find_by(name: 'product_name')
  115. params = {
  116. id: setting.id,
  117. name: 'some_new_name',
  118. preferences: {
  119. permission: ['admin.branding', 'admin.some_new_permission'],
  120. some_new_key: true,
  121. }
  122. }
  123. put "/api/v1/settings/#{setting.id}", params.to_json, @headers.merge('Authorization' => credentials)
  124. assert_response(200)
  125. result = JSON.parse(@response.body)
  126. assert_equal(Hash, result.class)
  127. assert_equal('product_name', result['name'])
  128. assert_equal(1, result['preferences']['permission'].length)
  129. assert_equal('admin.branding', result['preferences']['permission'][0])
  130. assert_equal(true, result['preferences']['some_new_key'])
  131. # update
  132. setting = Setting.find_by(name: 'api_token_access')
  133. params = {
  134. id: setting.id,
  135. name: 'some_new_name',
  136. preferences: {
  137. permission: ['admin.branding', 'admin.some_new_permission'],
  138. some_new_key: true,
  139. }
  140. }
  141. put "/api/v1/settings/#{setting.id}", params.to_json, @headers.merge('Authorization' => credentials)
  142. assert_response(200)
  143. result = JSON.parse(@response.body)
  144. assert_equal(Hash, result.class)
  145. assert_equal('api_token_access', result['name'])
  146. assert_equal(1, result['preferences']['permission'].length)
  147. assert_equal('admin.api', result['preferences']['permission'][0])
  148. assert_equal(true, result['preferences']['some_new_key'])
  149. # delete
  150. setting = Setting.find_by(name: 'product_name')
  151. delete "/api/v1/settings/#{setting.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  152. assert_response(401)
  153. result = JSON.parse(@response.body)
  154. assert_equal('Not authorized (feature not possible)', result['error'])
  155. end
  156. test 'settings index with admin-api' do
  157. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('setting-admin-api@example.com', 'adminpw')
  158. # index
  159. get '/api/v1/settings', {}, @headers.merge('Authorization' => credentials)
  160. assert_response(200)
  161. result = JSON.parse(@response.body)
  162. assert_equal(Array, result.class)
  163. assert(result)
  164. hit_api = false
  165. hit_product_name = false
  166. result.each { |setting|
  167. if setting['name'] == 'api_token_access'
  168. hit_api = true
  169. end
  170. if setting['name'] == 'product_name'
  171. hit_product_name = true
  172. end
  173. }
  174. assert_equal(true, hit_api)
  175. assert_equal(false, hit_product_name)
  176. # show
  177. setting = Setting.find_by(name: 'product_name')
  178. get "/api/v1/settings/#{setting.id}", {}, @headers.merge('Authorization' => credentials)
  179. assert_response(401)
  180. result = JSON.parse(@response.body)
  181. assert_equal('Not authorized (required ["admin.branding"])', result['error'])
  182. setting = Setting.find_by(name: 'api_token_access')
  183. get "/api/v1/settings/#{setting.id}", {}, @headers.merge('Authorization' => credentials)
  184. assert_response(200)
  185. result = JSON.parse(@response.body)
  186. assert_equal(Hash, result.class)
  187. assert_equal('api_token_access', result['name'])
  188. # update
  189. setting = Setting.find_by(name: 'product_name')
  190. params = {
  191. id: setting.id,
  192. name: 'some_new_name',
  193. preferences: {
  194. permission: ['admin.branding', 'admin.some_new_permission'],
  195. some_new_key: true,
  196. }
  197. }
  198. put "/api/v1/settings/#{setting.id}", params.to_json, @headers.merge('Authorization' => credentials)
  199. assert_response(401)
  200. result = JSON.parse(@response.body)
  201. assert_equal('Not authorized (required ["admin.branding"])', result['error'])
  202. # update
  203. setting = Setting.find_by(name: 'api_token_access')
  204. params = {
  205. id: setting.id,
  206. name: 'some_new_name',
  207. preferences: {
  208. permission: ['admin.branding', 'admin.some_new_permission'],
  209. some_new_key: true,
  210. }
  211. }
  212. put "/api/v1/settings/#{setting.id}", params.to_json, @headers.merge('Authorization' => credentials)
  213. assert_response(200)
  214. result = JSON.parse(@response.body)
  215. assert_equal(Hash, result.class)
  216. assert_equal('api_token_access', result['name'])
  217. assert_equal(1, result['preferences']['permission'].length)
  218. assert_equal('admin.api', result['preferences']['permission'][0])
  219. assert_equal(true, result['preferences']['some_new_key'])
  220. # delete
  221. setting = Setting.find_by(name: 'product_name')
  222. delete "/api/v1/settings/#{setting.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  223. assert_response(401)
  224. result = JSON.parse(@response.body)
  225. assert_equal('Not authorized (feature not possible)', result['error'])
  226. end
  227. test 'settings index with agent' do
  228. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('setting-agent@example.com', 'agentpw')
  229. # index
  230. get '/api/v1/settings', {}, @headers.merge('Authorization' => credentials)
  231. assert_response(401)
  232. result = JSON.parse(@response.body)
  233. assert_equal(Hash, result.class)
  234. assert_not(result['settings'])
  235. assert_equal('Not authorized (user)!', result['error'])
  236. # show
  237. setting = Setting.find_by(name: 'product_name')
  238. get "/api/v1/settings/#{setting.id}", {}, @headers.merge('Authorization' => credentials)
  239. assert_response(401)
  240. result = JSON.parse(@response.body)
  241. assert_equal('Not authorized (user)!', result['error'])
  242. end
  243. test 'settings index with customer' do
  244. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('setting-customer1@example.com', 'customer1pw')
  245. # index
  246. get '/api/v1/settings', {}, @headers.merge('Authorization' => credentials)
  247. assert_response(401)
  248. result = JSON.parse(@response.body)
  249. assert_equal(Hash, result.class)
  250. assert_not(result['settings'])
  251. assert_equal('Not authorized (user)!', result['error'])
  252. # show
  253. setting = Setting.find_by(name: 'product_name')
  254. get "/api/v1/settings/#{setting.id}", {}, @headers.merge('Authorization' => credentials)
  255. assert_response(401)
  256. result = JSON.parse(@response.body)
  257. assert_equal('Not authorized (user)!', result['error'])
  258. # delete
  259. setting = Setting.find_by(name: 'product_name')
  260. delete "/api/v1/settings/#{setting.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  261. assert_response(401)
  262. result = JSON.parse(@response.body)
  263. assert_equal('Not authorized (user)!', result['error'])
  264. end
  265. end