2018-08-24 14:19:44 +02:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2019-07-02 18:15:03 +02:00
|
|
|
describe Users::ProfilController, type: :controller do
|
2019-07-09 17:08:27 +02:00
|
|
|
include ActiveJob::TestHelper
|
|
|
|
|
2019-07-08 10:40:50 +02:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
2019-07-02 18:15:03 +02:00
|
|
|
describe 'POST #renew_api_token' do
|
|
|
|
let(:administrateur) { create(:administrateur) }
|
2018-08-24 14:19:44 +02:00
|
|
|
|
2019-08-09 10:46:39 +02:00
|
|
|
before { sign_in(administrateur.user) }
|
2018-08-24 14:19:44 +02:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(administrateur).to receive(:renew_api_token)
|
|
|
|
allow(controller).to receive(:current_administrateur) { administrateur }
|
|
|
|
post :renew_api_token
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(administrateur).to have_received(:renew_api_token) }
|
|
|
|
it { expect(response.status).to render_template(:show) }
|
|
|
|
it { expect(flash.notice).to eq('Votre jeton a été regénéré.') }
|
|
|
|
end
|
2019-07-08 10:40:50 +02:00
|
|
|
|
|
|
|
describe 'PATCH #update_email' do
|
|
|
|
context 'when everything is fine' do
|
|
|
|
before do
|
|
|
|
patch :update_email, params: { user: { email: 'loulou@lou.com' } }
|
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(user.unconfirmed_email).to eq('loulou@lou.com') }
|
|
|
|
it { expect(response).to redirect_to(profil_path) }
|
|
|
|
it { expect(flash.notice).to eq(I18n.t('devise.registrations.update_needs_confirmation')) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the mail is already taken' do
|
2019-07-09 17:08:27 +02:00
|
|
|
let(:existing_user) { create(:user) }
|
2019-07-08 10:40:50 +02:00
|
|
|
|
|
|
|
before do
|
2019-07-09 17:08:27 +02:00
|
|
|
perform_enqueued_jobs do
|
|
|
|
patch :update_email, params: { user: { email: existing_user.email } }
|
|
|
|
end
|
2019-07-08 10:40:50 +02:00
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
2019-07-09 17:08:27 +02:00
|
|
|
it { expect(user.unconfirmed_email).to be_nil }
|
|
|
|
it { expect(ActionMailer::Base.deliveries.last.to).to eq([existing_user.email]) }
|
2019-07-08 10:40:50 +02:00
|
|
|
it { expect(response).to redirect_to(profil_path) }
|
|
|
|
it { expect(flash.notice).to eq(I18n.t('devise.registrations.update_needs_confirmation')) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the mail is incorrect' do
|
|
|
|
before do
|
|
|
|
patch :update_email, params: { user: { email: 'incorrect' } }
|
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(response).to redirect_to(profil_path) }
|
|
|
|
it { expect(flash.alert).to eq(['Email invalide']) }
|
|
|
|
end
|
2019-12-09 17:11:12 +01:00
|
|
|
|
|
|
|
context 'when the user has an instructeur role' do
|
|
|
|
let(:instructeur_email) { 'instructeur_email@a.com' }
|
|
|
|
let!(:user) { create(:instructeur, email: instructeur_email).user }
|
|
|
|
|
|
|
|
before do
|
|
|
|
patch :update_email, params: { user: { email: 'loulou@lou.com' } }
|
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(user.unconfirmed_email).to be_nil }
|
|
|
|
it { expect(response).to redirect_to(profil_path) }
|
|
|
|
end
|
2019-07-08 10:40:50 +02:00
|
|
|
end
|
2018-08-24 14:19:44 +02:00
|
|
|
end
|