confirmations_spec.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Confirmations' do
  4. let(:confirmed_at) { nil }
  5. let(:user) { Fabricate(:user, confirmed_at: confirmed_at) }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. let(:scopes) { 'read:accounts write:accounts' }
  8. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  9. describe 'POST /api/v1/emails/confirmations' do
  10. subject do
  11. post '/api/v1/emails/confirmations', headers: headers, params: params
  12. end
  13. let(:params) { {} }
  14. it_behaves_like 'forbidden for wrong scope', 'read read:accounts'
  15. context 'with an oauth token' do
  16. context 'when user was created by a different application' do
  17. let(:user) { Fabricate(:user, confirmed_at: confirmed_at, created_by_application: Fabricate(:application)) }
  18. it 'returns http forbidden' do
  19. subject
  20. expect(response).to have_http_status(403)
  21. expect(response.content_type)
  22. .to start_with('application/json')
  23. end
  24. end
  25. context 'when user was created by the same application' do
  26. before do
  27. user.update(created_by_application: token.application)
  28. end
  29. context 'when the account is already confirmed' do
  30. let(:confirmed_at) { Time.now.utc }
  31. it 'returns http forbidden' do
  32. subject
  33. expect(response).to have_http_status(403)
  34. expect(response.content_type)
  35. .to start_with('application/json')
  36. end
  37. context 'when user changed e-mail and has not confirmed it' do
  38. before do
  39. user.update(email: 'foo@bar.com')
  40. end
  41. it 'returns http success' do
  42. subject
  43. expect(response).to have_http_status(200)
  44. expect(response.content_type)
  45. .to start_with('application/json')
  46. end
  47. end
  48. end
  49. context 'when the account is unconfirmed' do
  50. it 'returns http success' do
  51. subject
  52. expect(response).to have_http_status(200)
  53. expect(response.content_type)
  54. .to start_with('application/json')
  55. end
  56. end
  57. context 'with email param' do
  58. let(:params) { { email: 'foo@bar.com' } }
  59. it "updates the user's e-mail address", :aggregate_failures do
  60. subject
  61. expect(response).to have_http_status(200)
  62. expect(response.content_type)
  63. .to start_with('application/json')
  64. expect(user.reload.unconfirmed_email).to eq('foo@bar.com')
  65. end
  66. end
  67. context 'with invalid email param' do
  68. let(:params) { { email: 'invalid' } }
  69. it 'returns http unprocessable entity' do
  70. subject
  71. expect(response).to have_http_status(422)
  72. expect(response.content_type)
  73. .to start_with('application/json')
  74. end
  75. end
  76. end
  77. end
  78. context 'without an oauth token' do
  79. let(:headers) { {} }
  80. it 'returns http unauthorized' do
  81. subject
  82. expect(response).to have_http_status(401)
  83. expect(response.content_type)
  84. .to start_with('application/json')
  85. end
  86. end
  87. end
  88. describe 'GET /api/v1/emails/check_confirmation' do
  89. subject do
  90. get '/api/v1/emails/check_confirmation', headers: headers
  91. end
  92. it_behaves_like 'forbidden for wrong scope', 'write'
  93. context 'with an oauth token' do
  94. context 'when the account is not confirmed' do
  95. it 'returns the confirmation status successfully', :aggregate_failures do
  96. subject
  97. expect(response).to have_http_status(200)
  98. expect(response.content_type)
  99. .to start_with('application/json')
  100. expect(response.parsed_body).to be false
  101. end
  102. end
  103. context 'when the account is confirmed' do
  104. let(:confirmed_at) { Time.now.utc }
  105. it 'returns the confirmation status successfully', :aggregate_failures do
  106. subject
  107. expect(response).to have_http_status(200)
  108. expect(response.content_type)
  109. .to start_with('application/json')
  110. expect(response.parsed_body).to be true
  111. end
  112. end
  113. end
  114. context 'with an authentication cookie' do
  115. let(:headers) { {} }
  116. before do
  117. sign_in user, scope: :user
  118. end
  119. context 'when the account is not confirmed' do
  120. it 'returns the confirmation status successfully', :aggregate_failures do
  121. subject
  122. expect(response).to have_http_status(200)
  123. expect(response.content_type)
  124. .to start_with('application/json')
  125. expect(response.parsed_body).to be false
  126. end
  127. end
  128. context 'when the account is confirmed' do
  129. let(:confirmed_at) { Time.now.utc }
  130. it 'returns the confirmation status successfully', :aggregate_failures do
  131. subject
  132. expect(response).to have_http_status(200)
  133. expect(response.content_type)
  134. .to start_with('application/json')
  135. expect(response.parsed_body).to be true
  136. end
  137. end
  138. end
  139. context 'without an oauth token and an authentication cookie' do
  140. let(:headers) { {} }
  141. it 'returns http unauthorized' do
  142. subject
  143. expect(response).to have_http_status(401)
  144. expect(response.content_type)
  145. .to start_with('application/json')
  146. end
  147. end
  148. end
  149. end