external_credential_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. require 'rails_helper'
  2. RSpec.describe 'ExternalCredentials', type: :request do
  3. let(:admin_user) do
  4. create(:admin_user)
  5. end
  6. describe 'request handling' do
  7. it 'does external_credential index with nobody' do
  8. get '/api/v1/external_credentials', as: :json
  9. expect(response).to have_http_status(401)
  10. expect(json_response).to be_a_kind_of(Hash)
  11. expect(json_response['error']).to eq('authentication failed')
  12. end
  13. it 'does external_credential app_verify with nobody' do
  14. post '/api/v1/external_credentials/facebook/app_verify', as: :json
  15. expect(response).to have_http_status(401)
  16. expect(json_response).to be_a_kind_of(Hash)
  17. expect(json_response['error']).to eq('authentication failed')
  18. end
  19. it 'does link_account app_verify with nobody' do
  20. get '/api/v1/external_credentials/facebook/link_account', as: :json
  21. expect(response).to have_http_status(401)
  22. expect(json_response).to be_a_kind_of(Hash)
  23. expect(json_response['error']).to eq('authentication failed')
  24. end
  25. it 'does external_credential callback with nobody' do
  26. get '/api/v1/external_credentials/facebook/callback', as: :json
  27. expect(response).to have_http_status(401)
  28. expect(json_response).to be_a_kind_of(Hash)
  29. expect(json_response['error']).to eq('authentication failed')
  30. end
  31. it 'does external_credential index with admin' do
  32. authenticated_as(admin_user)
  33. get '/api/v1/external_credentials', as: :json
  34. expect(response).to have_http_status(200)
  35. expect(json_response).to be_a_kind_of(Array)
  36. expect(json_response).to be_truthy
  37. expect(json_response.count).to eq(0)
  38. get '/api/v1/external_credentials?expand=true', as: :json
  39. expect(response).to have_http_status(200)
  40. expect(json_response).to be_a_kind_of(Array)
  41. expect(json_response).to be_truthy
  42. expect(json_response.count).to eq(0)
  43. end
  44. it 'does external_credential app_verify with admin - facebook' do
  45. authenticated_as(admin_user)
  46. post '/api/v1/external_credentials/facebook/app_verify', as: :json
  47. expect(response).to have_http_status(200)
  48. expect(json_response).to be_a_kind_of(Hash)
  49. expect(json_response['error']).to eq('No application_id param!')
  50. VCR.use_cassette('request/external_credentials/facebook/app_verify_invalid_credentials_with_not_created') do
  51. post '/api/v1/external_credentials/facebook/app_verify', params: { application_id: 123, application_secret: 123 }, as: :json
  52. end
  53. expect(response).to have_http_status(200)
  54. expect(json_response).to be_a_kind_of(Hash)
  55. expect(json_response['error']).to eq('type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
  56. create(:external_credential, { name: 'facebook', credentials: { application_id: 123, application_secret: 123 } })
  57. VCR.use_cassette('request/external_credentials/facebook/app_verify_invalid_credentials_with_created') do
  58. post '/api/v1/external_credentials/facebook/app_verify', as: :json
  59. end
  60. expect(response).to have_http_status(200)
  61. expect(json_response).to be_a_kind_of(Hash)
  62. expect(json_response['error']).to eq('type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
  63. end
  64. it 'does external_credential app_verify with admin - twitter' do
  65. authenticated_as(admin_user)
  66. post '/api/v1/external_credentials/twitter/app_verify', as: :json
  67. expect(response).to have_http_status(200)
  68. expect(json_response).to be_a_kind_of(Hash)
  69. expect(json_response['error']).to eq('No consumer_key param!')
  70. VCR.use_cassette('request/external_credentials/twitter/app_verify_invalid_credentials_with_not_created') do
  71. post '/api/v1/external_credentials/twitter/app_verify', params: { consumer_key: 123, consumer_secret: 123, oauth_token: 123, oauth_token_secret: 123 }, as: :json
  72. end
  73. expect(response).to have_http_status(200)
  74. expect(json_response).to be_a_kind_of(Hash)
  75. expect(json_response['error']).to eq('401 Authorization Required')
  76. create(:external_credential, { name: 'twitter', credentials: { consumer_key: 123, consumer_secret: 123, oauth_token: 123, oauth_token_secret: 123 } })
  77. VCR.use_cassette('request/external_credentials/twitter/app_verify_invalid_credentials_with_created') do
  78. post '/api/v1/external_credentials/twitter/app_verify', as: :json
  79. end
  80. expect(response).to have_http_status(200)
  81. expect(json_response).to be_a_kind_of(Hash)
  82. expect(json_response['error']).to eq('401 Authorization Required')
  83. end
  84. it 'does link_account app_verify with admin - facebook' do
  85. authenticated_as(admin_user)
  86. get '/api/v1/external_credentials/facebook/link_account', as: :json
  87. expect(response).to have_http_status(422)
  88. expect(json_response).to be_a_kind_of(Hash)
  89. expect(json_response['error']).to eq('No facebook app configured!')
  90. get '/api/v1/external_credentials/facebook/link_account', params: { application_id: 123, application_secret: 123 }, as: :json
  91. expect(response).to have_http_status(422)
  92. expect(json_response).to be_a_kind_of(Hash)
  93. expect(json_response['error']).to eq('No facebook app configured!')
  94. create(:external_credential, { name: 'facebook', credentials: { application_id: 123, application_secret: 123 } })
  95. VCR.use_cassette('request/external_credentials/facebook/link_account_with_invalid_credential') do
  96. get '/api/v1/external_credentials/facebook/link_account', as: :json
  97. end
  98. expect(response).to have_http_status(500)
  99. expect(json_response).to be_a_kind_of(Hash)
  100. expect(json_response['error']).to eq('type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
  101. end
  102. it 'does link_account app_verify with admin - twitter' do
  103. authenticated_as(admin_user)
  104. get '/api/v1/external_credentials/twitter/link_account', as: :json
  105. expect(response).to have_http_status(422)
  106. expect(json_response).to be_a_kind_of(Hash)
  107. expect(json_response['error']).to eq('No twitter app configured!')
  108. get '/api/v1/external_credentials/twitter/link_account', params: { consumer_key: 123, consumer_secret: 123, oauth_token: 123, oauth_token_secret: 123 }, as: :json
  109. expect(response).to have_http_status(422)
  110. expect(json_response).to be_a_kind_of(Hash)
  111. expect(json_response['error']).to eq('No twitter app configured!')
  112. create(:external_credential, { name: 'twitter', credentials: { consumer_key: 123, consumer_secret: 123, oauth_token: 123, oauth_token_secret: 123 } })
  113. VCR.use_cassette('request/external_credentials/twitter/link_account_with_invalid_credential') do
  114. get '/api/v1/external_credentials/twitter/link_account', as: :json
  115. end
  116. expect(response).to have_http_status(500)
  117. expect(json_response).to be_a_kind_of(Hash)
  118. expect(json_response['error']).to eq('401 Authorization Required')
  119. end
  120. it 'does external_credential callback with admin - facebook' do
  121. authenticated_as(admin_user)
  122. get '/api/v1/external_credentials/facebook/callback', as: :json
  123. expect(response).to have_http_status(422)
  124. expect(json_response).to be_a_kind_of(Hash)
  125. expect(json_response['error']).to eq('No facebook app configured!')
  126. get '/api/v1/external_credentials/facebook/callback', params: { application_id: 123, application_secret: 123 }, as: :json
  127. expect(response).to have_http_status(422)
  128. expect(json_response).to be_a_kind_of(Hash)
  129. expect(json_response['error']).to eq('No facebook app configured!')
  130. create(:external_credential, { name: 'facebook', credentials: { application_id: 123, application_secret: 123 } })
  131. VCR.use_cassette('request/external_credentials/facebook/callback_invalid_credentials') do
  132. get '/api/v1/external_credentials/facebook/callback', as: :json
  133. end
  134. expect(response).to have_http_status(500)
  135. expect(json_response).to be_a_kind_of(Hash)
  136. expect(json_response['error']).to eq('type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
  137. end
  138. it 'does external_credential callback with admin - twitter' do
  139. authenticated_as(admin_user)
  140. get '/api/v1/external_credentials/twitter/callback', as: :json
  141. expect(response).to have_http_status(422)
  142. expect(json_response).to be_a_kind_of(Hash)
  143. expect(json_response['error']).to eq('No twitter app configured!')
  144. get '/api/v1/external_credentials/twitter/callback', params: { consumer_key: 123, consumer_secret: 123 }, as: :json
  145. expect(response).to have_http_status(422)
  146. expect(json_response).to be_a_kind_of(Hash)
  147. expect(json_response['error']).to eq('No twitter app configured!')
  148. create(:external_credential, { name: 'twitter', credentials: { consumer_key: 123, consumer_secret: 123 } })
  149. get '/api/v1/external_credentials/twitter/callback', as: :json
  150. expect(response).to have_http_status(422)
  151. expect(json_response).to be_a_kind_of(Hash)
  152. expect(json_response['error']).to eq('No request_token for session found!')
  153. #request.session[:oauth_token] = 'some_token'
  154. #get '/api/v1/external_credentials/twitter/callback', as: :json
  155. #expect(response).to have_http_status(422)
  156. #expect(json_response).to be_a_kind_of(Hash)
  157. #expect(json_response['error']).to eq('Invalid oauth_token given!')
  158. end
  159. it 'does external_credential app_verify with admin and different permissions' do
  160. authenticated_as(admin_user)
  161. create(:external_credential, { name: 'twitter', credentials: { consumer_key: 123, consumer_secret: 123 } })
  162. VCR.use_cassette('request/external_credentials/twitter/app_verify_twitter') do
  163. post '/api/v1/external_credentials/twitter/app_verify', as: :json
  164. end
  165. expect(response).to have_http_status(200)
  166. expect(json_response).to be_a_kind_of(Hash)
  167. expect(json_response['error']).to eq('401 Authorization Required')
  168. permission = Permission.find_by(name: 'admin.channel_twitter')
  169. permission.active = false
  170. permission.save!
  171. post '/api/v1/external_credentials/twitter/app_verify', as: :json
  172. expect(response).to have_http_status(401)
  173. expect(json_response).to be_a_kind_of(Hash)
  174. expect(json_response['error']).to eq('Not authorized (user)!')
  175. create(:external_credential, { name: 'facebook', credentials: { application_id: 123, application_secret: 123 } })
  176. VCR.use_cassette('request/external_credentials/facebook/app_verify_facebook') do
  177. post '/api/v1/external_credentials/facebook/app_verify', as: :json
  178. end
  179. expect(response).to have_http_status(200)
  180. expect(json_response).to be_a_kind_of(Hash)
  181. expect(json_response['error']).to eq('type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
  182. permission = Permission.find_by(name: 'admin.channel_facebook')
  183. permission.active = false
  184. permission.save!
  185. post '/api/v1/external_credentials/facebook/app_verify', as: :json
  186. expect(response).to have_http_status(401)
  187. expect(json_response).to be_a_kind_of(Hash)
  188. expect(json_response['error']).to eq('Not authorized (user)!')
  189. end
  190. end
  191. end