external_credential_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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' 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 facebook app configured!')
  50. create(:external_credential, name: 'facebook')
  51. post '/api/v1/external_credentials/facebook/app_verify', as: :json
  52. expect(response).to have_http_status(200)
  53. expect(json_response).to be_a_kind_of(Hash)
  54. 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]')
  55. end
  56. it 'does link_account app_verify with admin' do
  57. authenticated_as(admin_user)
  58. get '/api/v1/external_credentials/facebook/link_account', as: :json
  59. expect(response).to have_http_status(422)
  60. expect(json_response).to be_a_kind_of(Hash)
  61. expect(json_response['error']).to eq('No facebook app configured!')
  62. create(:external_credential, name: 'facebook')
  63. get '/api/v1/external_credentials/facebook/link_account', as: :json
  64. expect(response).to have_http_status(500)
  65. expect(json_response).to be_a_kind_of(Hash)
  66. 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]')
  67. end
  68. it 'does external_credential callback with admin' do
  69. authenticated_as(admin_user)
  70. get '/api/v1/external_credentials/facebook/callback', as: :json
  71. expect(response).to have_http_status(500)
  72. expect(json_response).to be_a_kind_of(Hash)
  73. expect(json_response['error']).to eq('No such account')
  74. create(:external_credential, name: 'facebook')
  75. get '/api/v1/external_credentials/facebook/callback', as: :json
  76. expect(response).to have_http_status(500)
  77. expect(json_response).to be_a_kind_of(Hash)
  78. 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]')
  79. end
  80. it 'does external_credential app_verify with admin and different permissions' do
  81. authenticated_as(admin_user)
  82. create(:external_credential, name: 'twitter')
  83. post '/api/v1/external_credentials/twitter/app_verify', as: :json
  84. expect(response).to have_http_status(200)
  85. expect(json_response).to be_a_kind_of(Hash)
  86. expect(json_response['error']).to eq('400 Bad Request')
  87. permission = Permission.find_by(name: 'admin.channel_twitter')
  88. permission.active = false
  89. permission.save!
  90. post '/api/v1/external_credentials/twitter/app_verify', as: :json
  91. expect(response).to have_http_status(401)
  92. expect(json_response).to be_a_kind_of(Hash)
  93. expect(json_response['error']).to eq('Not authorized (user)!')
  94. create(:external_credential, name: 'facebook')
  95. post '/api/v1/external_credentials/facebook/app_verify', as: :json
  96. expect(response).to have_http_status(200)
  97. expect(json_response).to be_a_kind_of(Hash)
  98. 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]')
  99. permission = Permission.find_by(name: 'admin.channel_facebook')
  100. permission.active = false
  101. permission.save!
  102. post '/api/v1/external_credentials/facebook/app_verify', as: :json
  103. expect(response).to have_http_status(401)
  104. expect(json_response).to be_a_kind_of(Hash)
  105. expect(json_response['error']).to eq('Not authorized (user)!')
  106. end
  107. end
  108. end