user_organization_controller_test.rb 15 KB

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