user_organization_controller_test.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class UserOrganizationControllerTest < 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 = User.create_or_update(
  12. login: 'rest-admin',
  13. firstname: 'Rest',
  14. lastname: 'Agent',
  15. email: 'rest-admin@example.com',
  16. password: 'adminpw',
  17. active: true,
  18. roles: roles,
  19. groups: groups,
  20. )
  21. # create agent
  22. roles = Role.where( name: 'Agent' )
  23. @agent = User.create_or_update(
  24. login: 'rest-agent@example.com',
  25. firstname: 'Rest',
  26. lastname: 'Agent',
  27. email: 'rest-agent@example.com',
  28. password: 'agentpw',
  29. active: true,
  30. roles: roles,
  31. groups: groups,
  32. )
  33. # create customer without org
  34. roles = Role.where( name: 'Customer' )
  35. @customer_without_org = User.create_or_update(
  36. login: 'rest-customer1@example.com',
  37. firstname: 'Rest',
  38. lastname: 'Customer1',
  39. email: 'rest-customer1@example.com',
  40. password: 'customer1pw',
  41. active: true,
  42. roles: roles,
  43. )
  44. # create orgs
  45. @organization = Organization.create_or_update(
  46. name: 'Rest Org',
  47. )
  48. @organization2 = Organization.create_or_update(
  49. name: 'Rest Org #2',
  50. )
  51. @organization3 = Organization.create_or_update(
  52. name: 'Rest Org #3',
  53. )
  54. # create customer with org
  55. @customer_with_org = User.create_or_update(
  56. login: 'rest-customer2@example.com',
  57. firstname: 'Rest',
  58. lastname: 'Customer2',
  59. email: 'rest-customer2@example.com',
  60. password: 'customer2pw',
  61. active: true,
  62. roles: roles,
  63. organization_id: @organization.id,
  64. )
  65. end
  66. test 'user create tests - no user' do
  67. # create user with disabled feature
  68. Setting.set('user_create_account', false)
  69. post '/api/v1/users', {}, @headers
  70. assert_response(422)
  71. result = JSON.parse(@response.body)
  72. assert(result['error_human'])
  73. assert_equal('Feature not enabled!', result['error_human'])
  74. # already existing user with enabled feature
  75. Setting.set('user_create_account', true)
  76. params = { email: 'rest-customer1@example.com' }
  77. post '/api/v1/users', params.to_json, @headers
  78. assert_response(422)
  79. result = JSON.parse(@response.body)
  80. assert(result['error_human'])
  81. assert_equal('User already exists!', result['error_human'])
  82. # create user with enabled feature
  83. params = { firstname: 'Me First', lastname: 'Me Last', email: 'new_here@example.com' }
  84. post '/api/v1/users', params.to_json, @headers
  85. assert_response(201)
  86. result = JSON.parse(@response.body)
  87. assert(result)
  88. assert_equal('Me First', result['firstname'])
  89. assert_equal('Me Last', result['lastname'])
  90. assert_equal('new_here@example.com', result['login'])
  91. assert_equal('new_here@example.com', result['email'])
  92. # create user with admin role
  93. role = Role.lookup(name: 'Admin')
  94. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin@example.com', role_ids: [ role.id ] }
  95. post '/api/v1/users', params.to_json, @headers
  96. assert_response(201)
  97. result = JSON.parse(@response.body)
  98. assert(result)
  99. user = User.find(result['id'])
  100. assert_not(user.role?('Admin'))
  101. assert_not(user.role?('Agent'))
  102. assert(user.role?('Customer'))
  103. # create user with agent role
  104. role = Role.lookup(name: 'Agent')
  105. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent@example.com', role_ids: [ role.id ] }
  106. post '/api/v1/users', params.to_json, @headers
  107. assert_response(201)
  108. result = JSON.parse(@response.body)
  109. assert(result)
  110. user = User.find(result['id'])
  111. assert_not(user.role?('Admin'))
  112. assert_not(user.role?('Agent'))
  113. assert(user.role?('Customer'))
  114. # no user
  115. get '/api/v1/users', {}, @headers
  116. assert_response(401)
  117. result = JSON.parse(@response.body)
  118. assert_equal('authentication failed', result['error'])
  119. end
  120. test 'auth tests - not existing user' do
  121. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('not_existing@example.com', 'adminpw')
  122. get '/api/v1/users', {}, @headers.merge('Authorization' => credentials)
  123. assert_response(401)
  124. result = JSON.parse(@response.body)
  125. assert_equal('authentication failed', result['error'])
  126. end
  127. test 'auth tests - username auth, wrong pw' do
  128. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin', 'not_existing')
  129. get '/api/v1/users', {}, @headers.merge('Authorization' => credentials)
  130. assert_response(401)
  131. result = JSON.parse(@response.body)
  132. assert_equal('authentication failed', result['error'])
  133. end
  134. test 'auth tests - email auth, wrong pw' do
  135. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'not_existing')
  136. get '/api/v1/users', {}, @headers.merge('Authorization' => credentials)
  137. assert_response(401)
  138. result = JSON.parse(@response.body)
  139. assert_equal('authentication failed', result['error'])
  140. end
  141. test 'auth tests - username auth' do
  142. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin', 'adminpw')
  143. get '/api/v1/users', {}, @headers.merge('Authorization' => credentials)
  144. assert_response(200)
  145. result = JSON.parse(@response.body)
  146. assert(result)
  147. end
  148. test 'auth tests - email auth' do
  149. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  150. get '/api/v1/users', {}, @headers.merge('Authorization' => credentials)
  151. assert_response(200)
  152. result = JSON.parse(@response.body)
  153. assert(result)
  154. end
  155. test 'user index and create with admin' do
  156. # email auth
  157. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  158. # index
  159. get '/api/v1/users', {}, @headers.merge('Authorization' => credentials)
  160. assert_response(200)
  161. result = JSON.parse(@response.body)
  162. assert(result)
  163. # index
  164. get '/api/v1/users', {}, @headers.merge('Authorization' => credentials)
  165. assert_response(200)
  166. result = JSON.parse(@response.body)
  167. assert(result)
  168. assert_equal(result.class, Array)
  169. assert(result.length >= 3)
  170. # show/:id
  171. get "/api/v1/users/#{@agent.id}", {}, @headers.merge('Authorization' => credentials)
  172. assert_response(200)
  173. result = JSON.parse(@response.body)
  174. assert(result)
  175. assert_equal(result.class, Hash)
  176. assert_equal(result['email'], 'rest-agent@example.com')
  177. get "/api/v1/users/#{@customer_without_org.id}", {}, 'Authorization' => credentials
  178. assert_response(200)
  179. result = JSON.parse(@response.body)
  180. assert(result)
  181. assert_equal(result.class, Hash)
  182. assert_equal(result['email'], 'rest-customer1@example.com')
  183. # create user with admin role
  184. role = Role.lookup(name: 'Admin')
  185. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_admin@example.com', role_ids: [ role.id ] }
  186. post '/api/v1/users', params.to_json, @headers
  187. assert_response(201)
  188. result = JSON.parse(@response.body)
  189. assert(result)
  190. user = User.find(result['id'])
  191. assert(user.role?('Admin'))
  192. assert_not(user.role?('Agent'))
  193. assert_not(user.role?('Customer'))
  194. # create user with agent role
  195. role = Role.lookup(name: 'Agent')
  196. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_admin@example.com', role_ids: [ role.id ] }
  197. post '/api/v1/users', params.to_json, @headers
  198. assert_response(201)
  199. result = JSON.parse(@response.body)
  200. assert(result)
  201. user = User.find(result['id'])
  202. assert_not(user.role?('Admin'))
  203. assert(user.role?('Agent'))
  204. assert_not(user.role?('Customer'))
  205. end
  206. test 'user index and create with agent' do
  207. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-agent@example.com', 'agentpw')
  208. # index
  209. get '/api/v1/users', {}, @headers.merge('Authorization' => credentials)
  210. assert_response(200)
  211. result = JSON.parse(@response.body)
  212. assert(result)
  213. # index
  214. get '/api/v1/users', {}, @headers.merge('Authorization' => credentials)
  215. assert_response(200)
  216. result = JSON.parse(@response.body)
  217. assert(result)
  218. assert_equal(result.class, Array)
  219. assert(result.length >= 3)
  220. # create user with admin role
  221. role = Role.lookup(name: 'Admin')
  222. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_agent@example.com', role_ids: [ role.id ] }
  223. post '/api/v1/users', params.to_json, @headers
  224. assert_response(401)
  225. result = JSON.parse(@response.body)
  226. assert(result)
  227. # create user with agent role
  228. role = Role.lookup(name: 'Agent')
  229. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_agent@example.com', role_ids: [ role.id ] }
  230. post '/api/v1/users', params.to_json, @headers
  231. assert_response(401)
  232. result = JSON.parse(@response.body)
  233. assert(result)
  234. # create user with customer role
  235. role = Role.lookup(name: 'Customer')
  236. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_agent@example.com', role_ids: [ role.id ] }
  237. post '/api/v1/users', params.to_json, @headers
  238. assert_response(201)
  239. result = JSON.parse(@response.body)
  240. assert(result)
  241. user = User.find(result['id'])
  242. assert_not(user.role?('Admin'))
  243. assert_not(user.role?('Agent'))
  244. assert(user.role?('Customer'))
  245. end
  246. test 'user index and create with customer1' do
  247. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer1@example.com', 'customer1pw')
  248. # index
  249. get '/api/v1/users', {}, @headers.merge('Authorization' => credentials)
  250. assert_response(200)
  251. result = JSON.parse(@response.body)
  252. assert_equal(result.class, Array)
  253. assert_equal(result.length, 1)
  254. # show/:id
  255. get "/api/v1/users/#{@customer_without_org.id}", {}, @headers.merge('Authorization' => credentials)
  256. assert_response(200)
  257. result = JSON.parse(@response.body)
  258. assert_equal(result.class, Hash)
  259. assert_equal(result['email'], 'rest-customer1@example.com')
  260. get "/api/v1/users/#{@customer_with_org.id}", {}, @headers.merge('Authorization' => credentials)
  261. assert_response(401)
  262. result = JSON.parse(@response.body)
  263. assert_equal(result.class, Hash)
  264. assert(result.empty?)
  265. # create user with admin role
  266. role = Role.lookup(name: 'Admin')
  267. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_customer1@example.com', role_ids: [ role.id ] }
  268. post '/api/v1/users', params.to_json, @headers
  269. assert_response(401)
  270. # create user with agent role
  271. role = Role.lookup(name: 'Agent')
  272. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_customer1@example.com', role_ids: [ role.id ] }
  273. post '/api/v1/users', params.to_json, @headers
  274. assert_response(401)
  275. end
  276. test 'user index with customer2' do
  277. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer2@example.com', 'customer2pw')
  278. # index
  279. get '/api/v1/users', {}, @headers.merge('Authorization' => credentials)
  280. assert_response(200)
  281. result = JSON.parse(@response.body)
  282. assert_equal(result.class, Array)
  283. assert_equal(result.length, 1)
  284. # show/:id
  285. get "/api/v1/users/#{@customer_with_org.id}", {}, @headers.merge('Authorization' => credentials)
  286. assert_response(200)
  287. result = JSON.parse(@response.body)
  288. assert_equal(result.class, Hash)
  289. assert_equal(result['email'], 'rest-customer2@example.com')
  290. get "/api/v1/users/#{@customer_without_org.id}", {}, @headers.merge('Authorization' => credentials)
  291. assert_response(401)
  292. #puts @response.body
  293. result = JSON.parse(@response.body)
  294. assert_equal(result.class, Hash)
  295. assert(result.empty?)
  296. end
  297. test 'organization index with agent' do
  298. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-agent@example.com', 'agentpw')
  299. # index
  300. get '/api/v1/organizations', {}, @headers.merge('Authorization' => credentials)
  301. assert_response(200)
  302. result = JSON.parse(@response.body)
  303. assert_equal(result.class, Array)
  304. assert(result.length >= 3)
  305. # show/:id
  306. get "/api/v1/organizations/#{@organization.id}", {}, @headers.merge('Authorization' => credentials)
  307. assert_response(200)
  308. result = JSON.parse(@response.body)
  309. assert_equal( result.class, Hash)
  310. assert_equal( result['name'], 'Rest Org')
  311. get "/api/v1/organizations/#{@organization2.id}", {}, @headers.merge('Authorization' => credentials)
  312. assert_response(200)
  313. result = JSON.parse(@response.body)
  314. assert_equal( result.class, Hash)
  315. assert_equal( result['name'], 'Rest Org #2')
  316. end
  317. test 'organization index with customer1' do
  318. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer1@example.com', 'customer1pw')
  319. # index
  320. get '/api/v1/organizations', {}, @headers.merge('Authorization' => credentials)
  321. assert_response(200)
  322. result = JSON.parse(@response.body)
  323. assert_equal(result.class, Array)
  324. assert_equal(result.length, 0)
  325. # show/:id
  326. get "/api/v1/organizations/#{@organization.id}", {}, @headers.merge('Authorization' => credentials)
  327. assert_response(200)
  328. result = JSON.parse(@response.body)
  329. assert_equal( result.class, Hash)
  330. assert_equal( result['name'], nil)
  331. get "/api/v1/organizations/#{@organization2.id}", {}, @headers.merge('Authorization' => credentials)
  332. assert_response(200)
  333. result = JSON.parse(@response.body)
  334. assert_equal( result.class, Hash)
  335. assert_equal( result['name'], nil)
  336. end
  337. test 'organization index with customer2' do
  338. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer2@example.com', 'customer2pw')
  339. # index
  340. get '/api/v1/organizations', {}, @headers.merge('Authorization' => credentials)
  341. assert_response(200)
  342. result = JSON.parse(@response.body)
  343. assert_equal(result.class, Array)
  344. assert_equal(result.length, 1)
  345. # show/:id
  346. get "/api/v1/organizations/#{@organization.id}", {}, @headers.merge('Authorization' => credentials)
  347. assert_response(200)
  348. result = JSON.parse(@response.body)
  349. assert_equal( result.class, Hash)
  350. assert_equal( result['name'], 'Rest Org')
  351. get "/api/v1/organizations/#{@organization2.id}", {}, @headers.merge('Authorization' => credentials)
  352. assert_response(401)
  353. result = JSON.parse(@response.body)
  354. assert_equal( result.class, Hash)
  355. assert_equal( result['name'], nil)
  356. end
  357. end