external_credentials_spec.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. require 'rails_helper'
  2. RSpec.describe 'External Credentials', type: :request do
  3. let(:admin_user) { create(:admin_user) }
  4. context 'without authentication' do
  5. describe '#index' do
  6. it 'returns 401 unauthorized' do
  7. get '/api/v1/external_credentials', as: :json
  8. expect(response).to have_http_status(:unauthorized)
  9. expect(json_response).to include('error' => 'authentication failed')
  10. end
  11. end
  12. describe '#app_verify' do
  13. it 'returns 401 unauthorized' do
  14. post '/api/v1/external_credentials/facebook/app_verify', as: :json
  15. expect(response).to have_http_status(:unauthorized)
  16. expect(json_response).to include('error' => 'authentication failed')
  17. end
  18. end
  19. describe '#link_account' do
  20. it 'returns 401 unauthorized' do
  21. get '/api/v1/external_credentials/facebook/link_account', as: :json
  22. expect(response).to have_http_status(:unauthorized)
  23. expect(json_response).to include('error' => 'authentication failed')
  24. end
  25. end
  26. describe '#callback' do
  27. it 'returns 401 unauthorized' do
  28. get '/api/v1/external_credentials/facebook/callback', as: :json
  29. expect(response).to have_http_status(:unauthorized)
  30. expect(json_response).to include('error' => 'authentication failed')
  31. end
  32. end
  33. end
  34. context 'authenticated as admin' do
  35. before { authenticated_as(admin_user) }
  36. describe '#index' do
  37. it 'responds with an array of ExternalCredential records' do
  38. get '/api/v1/external_credentials', as: :json
  39. expect(response).to have_http_status(:ok)
  40. expect(json_response).to eq([])
  41. end
  42. context 'with expand=true URL parameters' do
  43. it 'responds with an array of ExternalCredential records and their association data' do
  44. get '/api/v1/external_credentials?expand=true', as: :json
  45. expect(response).to have_http_status(:ok)
  46. expect(json_response).to eq([])
  47. end
  48. end
  49. end
  50. context 'for Facebook' do
  51. let(:invalid_credentials) do
  52. { application_id: 123, application_secret: 123 }
  53. end
  54. describe '#app_verify' do
  55. describe 'failure cases' do
  56. context 'when permission for Facebook channel is deactivated' do
  57. before { Permission.find_by(name: 'admin.channel_facebook').update(active: false) }
  58. it 'returns 401 unauthorized with internal (Zammad) error' do
  59. post '/api/v1/external_credentials/facebook/app_verify', as: :json
  60. expect(response).to have_http_status(:unauthorized)
  61. expect(json_response).to include('error' => 'Not authorized (user)!')
  62. end
  63. end
  64. context 'with no credentials' do
  65. it 'returns 200 with internal (Zammad) error' do
  66. post '/api/v1/external_credentials/facebook/app_verify', as: :json
  67. expect(response).to have_http_status(:ok)
  68. expect(json_response).to include('error' => 'No application_id param!')
  69. end
  70. end
  71. context 'with invalid credentials, via request params' do
  72. it 'returns 200 with remote (Facebook auth) error' do
  73. VCR.use_cassette('request/external_credentials/facebook/app_verify_invalid_credentials_with_not_created') do
  74. post '/api/v1/external_credentials/facebook/app_verify', params: invalid_credentials, as: :json
  75. end
  76. expect(response).to have_http_status(:ok)
  77. expect(json_response).to include('error' => 'type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
  78. end
  79. end
  80. context 'with invalid credentials, via ExternalCredential record' do
  81. before { create(:facebook_credential, credentials: invalid_credentials) }
  82. it 'returns 200 with remote (Facebook auth) error' do
  83. VCR.use_cassette('request/external_credentials/facebook/app_verify_invalid_credentials_with_created') do
  84. post '/api/v1/external_credentials/facebook/app_verify', as: :json
  85. end
  86. expect(response).to have_http_status(:ok)
  87. expect(json_response).to include('error' => 'type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
  88. end
  89. end
  90. end
  91. end
  92. describe '#link_account' do
  93. describe 'failure cases' do
  94. context 'with no credentials' do
  95. it 'returns 422 unprocessable entity with internal (Zammad) error' do
  96. get '/api/v1/external_credentials/facebook/link_account', as: :json
  97. expect(response).to have_http_status(:unprocessable_entity)
  98. expect(json_response).to include('error' => 'No facebook app configured!')
  99. end
  100. end
  101. context 'with invalid credentials, via request params' do
  102. it 'returns 422 unprocessable entity with internal (Zammad) error' do
  103. get '/api/v1/external_credentials/facebook/link_account', params: invalid_credentials, as: :json
  104. expect(response).to have_http_status(:unprocessable_entity)
  105. expect(json_response).to include('error' => 'No facebook app configured!')
  106. end
  107. end
  108. context 'with invalid credentials, via ExternalCredential record' do
  109. before { create(:facebook_credential, credentials: invalid_credentials) }
  110. it 'returns 500 with remote (Facebook auth) error' do
  111. VCR.use_cassette('request/external_credentials/facebook/link_account_with_invalid_credential') do
  112. get '/api/v1/external_credentials/facebook/link_account', as: :json
  113. end
  114. expect(response).to have_http_status(:internal_server_error)
  115. expect(json_response).to include('error' => 'type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
  116. end
  117. end
  118. end
  119. end
  120. describe '#callback' do
  121. describe 'failure cases' do
  122. context 'with no credentials' do
  123. it 'returns 422 unprocessable entity with internal (Zammad) error' do
  124. get '/api/v1/external_credentials/facebook/callback', as: :json
  125. expect(response).to have_http_status(:unprocessable_entity)
  126. expect(json_response).to include('error' => 'No facebook app configured!')
  127. end
  128. end
  129. context 'with invalid credentials, via request params' do
  130. it 'returns 422 unprocessable entity with internal (Zammad) error' do
  131. get '/api/v1/external_credentials/facebook/callback', params: invalid_credentials, as: :json
  132. expect(response).to have_http_status(:unprocessable_entity)
  133. expect(json_response).to include('error' => 'No facebook app configured!')
  134. end
  135. end
  136. context 'with invalid credentials, via ExternalCredential record' do
  137. before { create(:facebook_credential, credentials: invalid_credentials) }
  138. it 'returns 500 with remote (Facebook auth) error' do
  139. VCR.use_cassette('request/external_credentials/facebook/callback_invalid_credentials') do
  140. get '/api/v1/external_credentials/facebook/callback', as: :json
  141. end
  142. expect(response).to have_http_status(:internal_server_error)
  143. expect(json_response).to include('error' => 'type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
  144. end
  145. end
  146. end
  147. end
  148. end
  149. context 'for Twitter' do
  150. let(:invalid_credentials) do
  151. { consumer_key: 123, consumer_secret: 123, oauth_token: 123, oauth_token_secret: 123 }
  152. end
  153. describe '#app_verify' do
  154. describe 'failure cases' do
  155. context 'when permission for Twitter channel is deactivated' do
  156. before { Permission.find_by(name: 'admin.channel_twitter').update(active: false) }
  157. it 'returns 401 unauthorized with internal (Zammad) error' do
  158. post '/api/v1/external_credentials/twitter/app_verify', as: :json
  159. expect(response).to have_http_status(:unauthorized)
  160. expect(json_response).to include('error' => 'Not authorized (user)!')
  161. end
  162. end
  163. context 'with no credentials' do
  164. it 'returns 200 with internal (Zammad) error' do
  165. post '/api/v1/external_credentials/twitter/app_verify', as: :json
  166. expect(response).to have_http_status(:ok)
  167. expect(json_response).to include('error' => 'No consumer_key param!')
  168. end
  169. end
  170. context 'with invalid credentials, via request params' do
  171. it 'returns 200 with remote (Twitter auth) error' do
  172. VCR.use_cassette('request/external_credentials/twitter/app_verify_invalid_credentials_with_not_created') do
  173. post '/api/v1/external_credentials/twitter/app_verify', params: invalid_credentials, as: :json
  174. end
  175. expect(response).to have_http_status(:ok)
  176. expect(json_response).to include('error' => '401 Authorization Required')
  177. end
  178. end
  179. context 'with invalid credentials, via existing ExternalCredential record' do
  180. before { create(:twitter_credential, credentials: invalid_credentials) }
  181. it 'returns 200 with remote (Twitter auth) error' do
  182. VCR.use_cassette('request/external_credentials/twitter/app_verify_invalid_credentials_with_created') do
  183. post '/api/v1/external_credentials/twitter/app_verify', as: :json
  184. end
  185. expect(response).to have_http_status(:ok)
  186. expect(json_response).to include('error' => '401 Authorization Required')
  187. end
  188. end
  189. end
  190. end
  191. describe '#link_account' do
  192. describe 'failure cases' do
  193. context 'with no credentials' do
  194. it 'returns 422 unprocessable entity with internal (Zammad) error' do
  195. get '/api/v1/external_credentials/twitter/link_account', as: :json
  196. expect(response).to have_http_status(:unprocessable_entity)
  197. expect(json_response).to include('error' => 'No twitter app configured!')
  198. end
  199. end
  200. context 'with invalid credentials, via request params' do
  201. it 'returns 422 unprocessable entity with internal (Zammad) error' do
  202. get '/api/v1/external_credentials/twitter/link_account', params: invalid_credentials, as: :json
  203. expect(response).to have_http_status(:unprocessable_entity)
  204. expect(json_response).to include('error' => 'No twitter app configured!')
  205. end
  206. end
  207. context 'with invalid credentials, via ExternalCredential record' do
  208. before { create(:twitter_credential, credentials: invalid_credentials) }
  209. it 'returns 500 with remote (Twitter auth) error' do
  210. VCR.use_cassette('request/external_credentials/twitter/link_account_with_invalid_credential') do
  211. get '/api/v1/external_credentials/twitter/link_account', as: :json
  212. end
  213. expect(response).to have_http_status(:internal_server_error)
  214. expect(json_response).to include('error' => '401 Authorization Required')
  215. end
  216. end
  217. end
  218. end
  219. describe '#callback' do
  220. describe 'failure cases' do
  221. context 'with no credentials' do
  222. it 'returns 422 unprocessable entity with internal (Zammad) error' do
  223. get '/api/v1/external_credentials/twitter/callback', as: :json
  224. expect(response).to have_http_status(:unprocessable_entity)
  225. expect(json_response).to include('error' => 'No twitter app configured!')
  226. end
  227. end
  228. context 'with invalid credentials, via request params' do
  229. it 'returns 422 unprocessable entity with internal (Zammad) error' do
  230. get '/api/v1/external_credentials/twitter/callback', params: invalid_credentials, as: :json
  231. expect(response).to have_http_status(:unprocessable_entity)
  232. expect(json_response).to include('error' => 'No twitter app configured!')
  233. end
  234. end
  235. context 'with invalid credentials, via ExternalCredential record' do
  236. before { create(:twitter_credential, credentials: invalid_credentials) }
  237. it 'returns 422 unprocessable entity with internal (Zammad) error' do
  238. get '/api/v1/external_credentials/twitter/callback', as: :json
  239. expect(response).to have_http_status(:unprocessable_entity)
  240. expect(json_response).to include('error' => 'No request_token for session found!')
  241. end
  242. end
  243. end
  244. end
  245. end
  246. end
  247. end