module Symphonia RSpec.describe AccountsController do include ActiveJob::TestHelper routes { Symphonia::Engine.routes } let(:regular_user) { FactoryBot.create(:user) } context 'logged', logged: true do context '#update' do it 'valid' do put :update, params: { user: { login: Faker::Internet.user_name } } expect(response).to redirect_to account_path end it 'invalid' do put :update, params: { user: { login: '' } } expect(response).to have_http_status :success end end end context '#resend_activation' do let(:email) { Faker::Internet.email } subject { get :resend_activation, params: { email: email } } it 'non exist user' do is_expected.to redirect_to :root expect(flash[:error]).not_to be :blank end it 'active user' do _user = FactoryBot.create(:user, email: email, status: 'active') is_expected.to redirect_to :root expect(flash[:error]).not_to be :blank end it 'pending user' do user = FactoryBot.create(:user, email: email, status: 'pending') token = user.perishable_token.dup expect { get :resend_activation, params: { email: email } }.to have_enqueued_job.on_queue('mailers') expect(response).to have_http_status :redirect expect(user.reload.perishable_token).not_to eq token expect(flash[:error]).to be_nil expect(flash[:notice]).not_to be :blank end end context '#activation' do let(:token) { SecureRandom.hex(3) } subject { get :activation, params: { activation_code: token } } it 'valid token' do user = FactoryBot.create(:user, status: 'pending') user.update_columns(perishable_token: token) is_expected.to redirect_to :login expect(user.reload.status).to eq 'active' end it 'invalid token' do is_expected.to redirect_to :root expect(flash[:error]).not_to be :blank end end context '#lost_password' do it 'with mail' do user = FactoryBot.create(:user, status: 'active') expect { post :lost_password, params: { email: user.email } }.to have_enqueued_job.on_queue('mailers') expect(flash[:error]).to be_nil end it 'with inactive user' do user = FactoryBot.create(:user, status: 'pending') expect { post :lost_password, params: { email: user.email } }.not_to have_enqueued_job.on_queue('mailers') expect(flash[:error]).not_to be :blank end end context "#reset_password" do subject { FactoryBot.create :user } it "reset" do put :reset_password, params: { id: subject.perishable_token, password: "secret-pass-1" } expect(response).to redirect_to /user\/current/ expect { subject.reload }.to change(subject, :crypted_password) end end end end