2023-02-22 00:55:31 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-03-12 03:14:25 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-07-17 14:22:33 +00:00
|
|
|
RSpec.describe 'Confirmations' do
|
2022-03-12 03:14:25 +00:00
|
|
|
let(:confirmed_at) { nil }
|
|
|
|
let(:user) { Fabricate(:user, confirmed_at: confirmed_at) }
|
2023-07-17 14:22:33 +00:00
|
|
|
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
|
|
|
|
let(:scopes) { 'read:accounts write:accounts' }
|
|
|
|
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
|
|
|
|
|
|
|
describe 'POST /api/v1/emails/confirmations' do
|
|
|
|
subject do
|
|
|
|
post '/api/v1/emails/confirmations', headers: headers, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden for wrong scope', 'read read:accounts'
|
2022-03-12 03:14:25 +00:00
|
|
|
|
|
|
|
context 'with an oauth token' do
|
2023-07-17 14:22:33 +00:00
|
|
|
context 'when user was created by a different application' do
|
|
|
|
let(:user) { Fabricate(:user, confirmed_at: confirmed_at, created_by_application: Fabricate(:application)) }
|
2022-03-12 03:14:25 +00:00
|
|
|
|
|
|
|
it 'returns http forbidden' do
|
2023-07-17 14:22:33 +00:00
|
|
|
subject
|
|
|
|
|
2023-02-20 02:16:40 +00:00
|
|
|
expect(response).to have_http_status(403)
|
2022-03-12 03:14:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-17 14:22:33 +00:00
|
|
|
context 'when user was created by the same application' do
|
2022-03-12 03:14:25 +00:00
|
|
|
before do
|
|
|
|
user.update(created_by_application: token.application)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account is already confirmed' do
|
|
|
|
let(:confirmed_at) { Time.now.utc }
|
|
|
|
|
|
|
|
it 'returns http forbidden' do
|
2023-07-17 14:22:33 +00:00
|
|
|
subject
|
|
|
|
|
2023-02-20 02:16:40 +00:00
|
|
|
expect(response).to have_http_status(403)
|
2022-03-12 03:14:25 +00:00
|
|
|
end
|
|
|
|
|
2023-07-17 14:22:33 +00:00
|
|
|
context 'when user changed e-mail and has not confirmed it' do
|
2022-03-12 03:14:25 +00:00
|
|
|
before do
|
|
|
|
user.update(email: 'foo@bar.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns http success' do
|
2023-07-17 14:22:33 +00:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2022-03-12 03:14:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account is unconfirmed' do
|
|
|
|
it 'returns http success' do
|
2023-07-17 14:22:33 +00:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with email param' do
|
|
|
|
let(:params) { { email: 'foo@bar.com' } }
|
|
|
|
|
|
|
|
it "updates the user's e-mail address", :aggregate_failures do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
expect(user.reload.unconfirmed_email).to eq('foo@bar.com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid email param' do
|
|
|
|
let(:params) { { email: 'invalid' } }
|
|
|
|
|
|
|
|
it 'returns http unprocessable entity' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(422)
|
2022-03-12 03:14:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without an oauth token' do
|
2023-07-17 14:22:33 +00:00
|
|
|
let(:headers) { {} }
|
|
|
|
|
2022-03-12 03:14:25 +00:00
|
|
|
it 'returns http unauthorized' do
|
2023-07-17 14:22:33 +00:00
|
|
|
subject
|
|
|
|
|
2023-02-20 02:16:40 +00:00
|
|
|
expect(response).to have_http_status(401)
|
2022-03-12 03:14:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-05-16 16:03:52 +00:00
|
|
|
|
2023-07-17 14:22:33 +00:00
|
|
|
describe 'GET /api/v1/emails/check_confirmation' do
|
|
|
|
subject do
|
|
|
|
get '/api/v1/emails/check_confirmation', headers: headers
|
|
|
|
end
|
2023-05-16 16:03:52 +00:00
|
|
|
|
2023-07-17 14:22:33 +00:00
|
|
|
it_behaves_like 'forbidden for wrong scope', 'write'
|
2023-05-16 16:03:52 +00:00
|
|
|
|
2023-07-17 14:22:33 +00:00
|
|
|
context 'with an oauth token' do
|
2023-05-16 16:03:52 +00:00
|
|
|
context 'when the account is not confirmed' do
|
2023-07-17 14:22:33 +00:00
|
|
|
it 'returns the confirmation status successfully', :aggregate_failures do
|
|
|
|
subject
|
2023-05-16 16:03:52 +00:00
|
|
|
|
2023-07-17 14:22:33 +00:00
|
|
|
expect(response).to have_http_status(200)
|
2023-05-16 16:03:52 +00:00
|
|
|
expect(body_as_json).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account is confirmed' do
|
|
|
|
let(:confirmed_at) { Time.now.utc }
|
|
|
|
|
2023-07-17 14:22:33 +00:00
|
|
|
it 'returns the confirmation status successfully', :aggregate_failures do
|
|
|
|
subject
|
2023-05-16 16:03:52 +00:00
|
|
|
|
2023-07-17 14:22:33 +00:00
|
|
|
expect(response).to have_http_status(200)
|
2023-05-16 16:03:52 +00:00
|
|
|
expect(body_as_json).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an authentication cookie' do
|
2023-07-17 14:22:33 +00:00
|
|
|
let(:headers) { {} }
|
|
|
|
|
2023-05-16 16:03:52 +00:00
|
|
|
before do
|
|
|
|
sign_in user, scope: :user
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account is not confirmed' do
|
2023-07-17 14:22:33 +00:00
|
|
|
it 'returns the confirmation status successfully', :aggregate_failures do
|
|
|
|
subject
|
2023-05-16 16:03:52 +00:00
|
|
|
|
2023-07-17 14:22:33 +00:00
|
|
|
expect(response).to have_http_status(200)
|
2023-05-16 16:03:52 +00:00
|
|
|
expect(body_as_json).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account is confirmed' do
|
|
|
|
let(:confirmed_at) { Time.now.utc }
|
|
|
|
|
2023-07-17 14:22:33 +00:00
|
|
|
it 'returns the confirmation status successfully', :aggregate_failures do
|
|
|
|
subject
|
2023-05-16 16:03:52 +00:00
|
|
|
|
2023-07-17 14:22:33 +00:00
|
|
|
expect(response).to have_http_status(200)
|
2023-05-16 16:03:52 +00:00
|
|
|
expect(body_as_json).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-07-01 22:05:44 +00:00
|
|
|
|
|
|
|
context 'without an oauth token and an authentication cookie' do
|
2023-07-17 14:22:33 +00:00
|
|
|
let(:headers) { {} }
|
|
|
|
|
2023-07-01 22:05:44 +00:00
|
|
|
it 'returns http unauthorized' do
|
2023-07-17 14:22:33 +00:00
|
|
|
subject
|
2023-07-01 22:05:44 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(401)
|
|
|
|
end
|
|
|
|
end
|
2023-05-16 16:03:52 +00:00
|
|
|
end
|
2022-03-12 03:14:25 +00:00
|
|
|
end
|