rest_test.rb 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # encoding: utf-8
  2. require 'test_helper'
  3. require 'faraday'
  4. class RestTest < ActiveSupport::TestCase
  5. test 'users and orgs' do
  6. if !ENV['BROWSER_URL']
  7. puts "NOTICE: Do not execute rest tests, no BROWSER_URL=http://some_host:port is defined! e. g. export BROWSER_URL=http://localhost:3000"
  8. return
  9. end
  10. # create agent
  11. roles = Role.where( :name => ['Admin', 'Agent'] )
  12. groups = Group.all
  13. UserInfo.current_user_id = 1
  14. admin = User.create_or_update(
  15. :login => 'rest-admin',
  16. :firstname => 'Rest',
  17. :lastname => 'Agent',
  18. :email => 'rest-admin@example.com',
  19. :password => 'adminpw',
  20. :active => true,
  21. :roles => roles,
  22. :groups => groups,
  23. )
  24. # create agent
  25. roles = Role.where( :name => 'Agent' )
  26. agent = User.create_or_update(
  27. :login => 'rest-agent@example.com',
  28. :firstname => 'Rest',
  29. :lastname => 'Agent',
  30. :email => 'rest-agent@example.com',
  31. :password => 'agentpw',
  32. :active => true,
  33. :roles => roles,
  34. :groups => groups,
  35. )
  36. # create customer without org
  37. roles = Role.where( :name => 'Customer' )
  38. customer_without_org = User.create_or_update(
  39. :login => 'rest-customer1@example.com',
  40. :firstname => 'Rest',
  41. :lastname => 'Customer1',
  42. :email => 'rest-customer1@example.com',
  43. :password => 'customer1pw',
  44. :active => true,
  45. :roles => roles,
  46. )
  47. # create orgs
  48. organization = Organization.create_or_update(
  49. :name => 'Rest Org',
  50. )
  51. organization2 = Organization.create_or_update(
  52. :name => 'Rest Org #2',
  53. )
  54. organization3 = Organization.create_or_update(
  55. :name => 'Rest Org #3',
  56. )
  57. # create customer with org
  58. customer_with_org = User.create_or_update(
  59. :login => 'rest-customer2@example.com',
  60. :firstname => 'Rest',
  61. :lastname => 'Customer2',
  62. :email => 'rest-customer2@example.com',
  63. :password => 'customer2pw',
  64. :active => true,
  65. :roles => roles,
  66. :organization_id => organization.id,
  67. )
  68. # not existing user
  69. request = get( 'not_existing@example.com', 'adminpw', '/api/v1/users')
  70. assert_equal( request[:response].status, 401 )
  71. assert_equal( request[:data].class, Hash)
  72. assert_equal( request[:data]['error'], 'authentication failed' )
  73. # username auth, wrong pw
  74. request = get( 'rest-admin', 'not_existing', '/api/v1/users' )
  75. assert_equal( request[:response].status, 401 )
  76. assert_equal( request[:data]['error'], 'authentication failed' )
  77. # email auth, wrong pw
  78. request = get( 'rest-admin@example.com', 'not_existing', '/api/v1/users' )
  79. assert_equal( request[:response].status, 401 )
  80. assert_equal( request[:data]['error'], 'authentication failed' )
  81. # username auth
  82. request = get( 'rest-admin', 'adminpw', '/api/v1/users' )
  83. assert_equal( request[:response].status, 200 )
  84. # email auth
  85. request = get( 'rest-admin@example.com', 'adminpw', '/api/v1/users' )
  86. assert_equal( request[:response].status, 200 )
  87. # /users
  88. # index
  89. request = get( 'rest-agent@example.com', 'agentpw', '/api/v1/users')
  90. assert_equal( request[:response].status, 200 )
  91. assert_equal( request[:data].class, Array)
  92. assert( request[:data].length >= 3 )
  93. # show/:id
  94. request = get( 'rest-agent@example.com', 'agentpw', '/api/v1/users/' + agent.id.to_s )
  95. assert_equal( request[:response].status, 200 )
  96. assert_equal( request[:data].class, Hash)
  97. assert_equal( request[:data]['email'], 'rest-agent@example.com')
  98. request = get( 'rest-agent@example.com', 'agentpw', '/api/v1/users/' + customer_without_org.id.to_s )
  99. assert_equal( request[:response].status, 200 )
  100. assert_equal( request[:data].class, Hash)
  101. assert_equal( request[:data]['email'], 'rest-customer1@example.com')
  102. # index
  103. request = get( 'rest-customer1@example.com', 'customer1pw', '/api/v1/users')
  104. assert_equal( request[:response].status, 200 )
  105. assert_equal( request[:data].class, Array)
  106. assert_equal( request[:data].length, 1 )
  107. # show/:id
  108. request = get( 'rest-customer1@example.com', 'customer1pw', '/api/v1/users/' + customer_without_org.id.to_s )
  109. assert_equal( request[:response].status, 200 )
  110. assert_equal( request[:data].class, Hash)
  111. assert_equal( request[:data]['email'], 'rest-customer1@example.com')
  112. request = get( 'rest-customer1@example.com', 'customer1pw', '/api/v1/users/' + customer_with_org.id.to_s )
  113. assert_equal( request[:response].status, 401 )
  114. assert_equal( request[:data].class, Hash)
  115. assert_equal( request[:data]['email'], nil)
  116. # index
  117. request = get( 'rest-customer2@example.com', 'customer2pw', '/api/v1/users')
  118. assert_equal( request[:response].status, 200 )
  119. assert_equal( request[:data].class, Array)
  120. assert_equal( request[:data].length, 1 )
  121. # show/:id
  122. request = get( 'rest-customer2@example.com', 'customer2pw', '/api/v1/users/' + customer_with_org.id.to_s )
  123. assert_equal( request[:response].status, 200 )
  124. assert_equal( request[:data].class, Hash)
  125. assert_equal( request[:data]['email'], 'rest-customer2@example.com')
  126. request = get( 'rest-customer2@example.com', 'customer2pw', '/api/v1/users/' + customer_without_org.id.to_s )
  127. assert_equal( request[:response].status, 401 )
  128. assert_equal( request[:data].class, Hash)
  129. assert_equal( request[:data]['email'], nil)
  130. # /organizations
  131. # index
  132. request = get( 'rest-agent@example.com', 'agentpw', '/api/v1/organizations')
  133. assert_equal( request[:response].status, 200 )
  134. assert_equal( request[:data].class, Array)
  135. assert( request[:data].length >= 3 )
  136. # show/:id
  137. request = get( 'rest-agent@example.com', 'agentpw', '/api/v1/organizations/' + organization.id.to_s )
  138. assert_equal( request[:response].status, 200 )
  139. assert_equal( request[:data].class, Hash)
  140. assert_equal( request[:data]['name'], 'Rest Org')
  141. request = get( 'rest-agent@example.com', 'agentpw', '/api/v1/organizations/' + organization2.id.to_s )
  142. assert_equal( request[:response].status, 200 )
  143. assert_equal( request[:data].class, Hash)
  144. assert_equal( request[:data]['name'], 'Rest Org #2')
  145. # index
  146. request = get( 'rest-customer1@example.com', 'customer1pw', '/api/v1/organizations')
  147. assert_equal( request[:response].status, 200 )
  148. assert_equal( request[:data].class, Array)
  149. assert_equal( request[:data].length, 0 )
  150. # show/:id
  151. request = get( 'rest-customer1@example.com', 'customer1pw', '/api/v1/organizations/' + organization.id.to_s )
  152. assert_equal( request[:response].status, 200 )
  153. assert_equal( request[:data].class, Hash)
  154. assert_equal( request[:data]['name'], nil)
  155. request = get( 'rest-customer1@example.com', 'customer1pw', '/api/v1/organizations/' + organization2.id.to_s )
  156. assert_equal( request[:response].status, 200 )
  157. assert_equal( request[:data].class, Hash)
  158. assert_equal( request[:data]['name'], nil)
  159. # index
  160. request = get( 'rest-customer2@example.com', 'customer2pw', '/api/v1/organizations')
  161. assert_equal( request[:response].status, 200 )
  162. assert_equal( request[:data].class, Array)
  163. assert_equal( request[:data].length, 1 )
  164. # show/:id
  165. request = get( 'rest-customer2@example.com', 'customer2pw', '/api/v1/organizations/' + organization.id.to_s )
  166. assert_equal( request[:response].status, 200 )
  167. assert_equal( request[:data].class, Hash)
  168. assert_equal( request[:data]['name'], 'Rest Org')
  169. request = get( 'rest-customer2@example.com', 'customer2pw', '/api/v1/organizations/' + organization2.id.to_s )
  170. assert_equal( request[:response].status, 401 )
  171. assert_equal( request[:data].class, Hash)
  172. assert_equal( request[:data]['name'], nil)
  173. # packages
  174. request = get( 'rest-admin@example.com', 'adminpw', '/api/v1/packages' )
  175. assert_equal( request[:response].status, 200 )
  176. assert_equal( request[:data].class, Hash)
  177. assert( request[:data]['packages'] )
  178. request = get( 'rest-agent@example.com', 'agentpw', '/api/v1/packages' )
  179. assert_equal( request[:response].status, 401 )
  180. assert_equal( request[:data].class, Hash)
  181. assert( !request[:data]['name'] )
  182. request = get( 'rest-customer1@example.com', 'customer1pw', '/api/v1/packages' )
  183. assert_equal( request[:response].status, 401 )
  184. assert_equal( request[:data].class, Hash)
  185. assert( !request[:data]['name'] )
  186. # settings
  187. request = get( 'rest-admin@example.com', 'adminpw', '/api/v1/settings' )
  188. assert_equal( request[:response].status, 200 )
  189. assert_equal( request[:data].class, Array)
  190. assert( request[:data][0] )
  191. request = get( 'rest-agent@example.com', 'agentpw', '/api/v1/settings' )
  192. assert_equal( request[:response].status, 401 )
  193. assert_equal( request[:data].class, Hash)
  194. assert( !request[:data]['name'] )
  195. request = get( 'rest-customer1@example.com', 'customer1pw', '/api/v1/settings' )
  196. assert_equal( request[:response].status, 401 )
  197. assert_equal( request[:data].class, Hash)
  198. assert( !request[:data]['name'] )
  199. end
  200. def get(user, pw, url)
  201. conn = Faraday.new( :url => ENV['BROWSER_URL'] )
  202. conn.basic_auth( user, pw )
  203. response = conn.get url
  204. # puts 'URL: ' + url
  205. # puts response.body.to_s
  206. data = JSON.parse( response.body )
  207. return { :data => data, :response => response }
  208. end
  209. end