diff --git a/spec/factories.rb b/spec/factories.rb index ded2e20a0..ff66b10cf 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -12,6 +12,33 @@ FactoryGirl.define do uid "MyString" end + factory :residence do + document_number '12345678Z' + document_type 1 + date_of_birth Date.new(1980, 12, 31) + postal_code "28013" + end + + factory :sms do + phone "699999999" + end + + factory :letter do + user + address + end + + factory :address do + street_type "Calle" + street "Alcalá" + number "1" + end + + factory :verified_user do + document_number '12345678Z' + document_type 'dni' + end + factory :debate do sequence(:title) { |n| "Debate #{n} title" } description 'Debate description' diff --git a/spec/features/verification/email_spec.rb b/spec/features/verification/email_spec.rb new file mode 100644 index 000000000..c886e4ff6 --- /dev/null +++ b/spec/features/verification/email_spec.rb @@ -0,0 +1,65 @@ +require 'rails_helper' + +feature 'Verify email' do + + scenario 'Verify' do + user = create(:user, + residence_verified_at: Time.now, + document_number: '12345678Z', + document_type: 'dni') + + verified_user = create(:verified_user, + document_number: '12345678Z', + document_type: 'dni', + email: 'rock@example.com') + + login_as(user) + + visit verified_user_path + + within("#verified_user_#{verified_user.id}_email") do + expect(page).to have_content 'rock@example.com' + click_button "Send" + end + + expect(page).to have_content 'We have send you a confirmation email to your email account: rock@example.com' + + sent_token = /.*email_verification_token=(.*)".*/.match(ActionMailer::Base.deliveries.last.body.to_s)[1] + visit email_path(email_verification_token: sent_token) + + expect(page).to have_content "You are now a verified user" + + expect(page).to_not have_link "Verify my account" + expect(page).to have_content "You are a level 3 user" + end + + scenario "Errors on token verification" do + user = create(:user, residence_verified_at: Time.now) + + login_as(user) + visit email_path(email_verification_token: "1234") + + expect(page).to have_content "Incorrect verification code" + end + + scenario "Errors on sending confirmation email" do + user = create(:user, + residence_verified_at: Time.now, + document_number: '12345678Z', + document_type: 'dni') + + verified_user = create(:verified_user, + document_number: '12345678Z', + document_type: 'dni', + email: 'rock@example.com') + + login_as(user) + + visit verified_user_path + + verified_user.destroy + click_button "Send" + + expect(page).to have_content "There was a problem sending you an email to your account" + end +end \ No newline at end of file diff --git a/spec/features/verification/letter_spec.rb b/spec/features/verification/letter_spec.rb new file mode 100644 index 000000000..4816bd9b1 --- /dev/null +++ b/spec/features/verification/letter_spec.rb @@ -0,0 +1,60 @@ +require 'rails_helper' + +feature 'Verify Letter' do + + scenario 'Send letter level 2 verified with phone' do + user = create(:user, residence_verified_at: Time.now, confirmed_phone: "611111111") + + login_as(user) + visit new_letter_path + + click_button "Send me a letter" + + expect(page).to have_content "You will receive a letter to your home address" + end + + scenario "Error accessing address from UserApi" do + user = create(:user, residence_verified_at: Time.now, confirmed_phone: "611111111") + + login_as(user) + visit new_letter_path + + allow_any_instance_of(UserApi).to receive(:address).and_return(nil) + + click_button "Send me a letter" + + expect(page).to have_content "We could not verify your address with the Census please try again later" + end + + scenario 'Send letter level 2 user verified with email' do + user = create(:user, residence_verified_at: Time.now, confirmed_phone: "611111111") + + login_as(user) + visit new_letter_path + + click_button "Send me a letter" + + expect(page).to have_content "You will receive a letter to your home address" + end + + scenario "Deny access unless verified residence" do + user = create(:user) + + login_as(user) + visit new_letter_path + + expect(page).to have_content 'You have not yet confirmed your residence' + expect(URI.parse(current_url).path).to eq(new_residence_path) + end + + scenario "Deny access unless verified phone/email" do + user = create(:user, residence_verified_at: Time.now) + + login_as(user) + visit new_letter_path + + expect(page).to have_content 'You have not yet confirmed your personal data' + expect(URI.parse(current_url).path).to eq(new_sms_path) + end + +end \ No newline at end of file diff --git a/spec/features/verification/level_three_verification_spec.rb b/spec/features/verification/level_three_verification_spec.rb new file mode 100644 index 000000000..617065379 --- /dev/null +++ b/spec/features/verification/level_three_verification_spec.rb @@ -0,0 +1,113 @@ +require 'rails_helper' + +feature 'Level three verification' do + scenario 'Verification with residency and verified sms' do + user = create(:user) + + verified_user = create(:verified_user, + document_number: '12345678Z', + document_type: '1', + phone: '611111111') + + login_as(user) + + visit account_path + click_link 'Verify my account' + + select 'Spanish ID', from: 'residence_document_type' + fill_in 'residence_document_number', with: "12345678Z" + select_date '31-December-1980', from: 'residence_date_of_birth' + fill_in 'residence_postal_code', with: '28013' + + click_button 'Verify' + + expect(page).to have_content 'Residence verified' + + within("#verified_user_#{verified_user.id}_phone") do + click_button "Send" + end + + expect(page).to have_content 'Security code confirmation' + + user = user.reload + fill_in 'sms_confirmation_code', with: user.sms_confirmation_code + click_button 'Send' + + expect(page).to have_content 'Correct code' + + expect(page).to have_content "You are now a verified user" + + expect(page).to_not have_link "Verify my account" + expect(page).to have_content "You are a level 3 user" + end + + scenario 'Verification with residency and verified email' do + user = create(:user) + + verified_user = create(:verified_user, + document_number: '12345678Z', + document_type: '1', + email: 'rock@example.com') + + login_as(user) + + visit account_path + click_link 'Verify my account' + + select 'Spanish ID', from: 'residence_document_type' + fill_in 'residence_document_number', with: "12345678Z" + select_date '31-December-1980', from: 'residence_date_of_birth' + fill_in 'residence_postal_code', with: '28013' + + click_button 'Verify' + + expect(page).to have_content 'Residence verified' + + within("#verified_user_#{verified_user.id}_email") do + click_button "Send" + end + + expect(page).to have_content 'We have send you a confirmation email to your email account: rock@example.com' + + sent_token = /.*email_verification_token=(.*)".*/.match(ActionMailer::Base.deliveries.last.body.to_s)[1] + visit email_path(email_verification_token: sent_token) + + expect(page).to have_content "You are now a verified user" + + expect(page).to_not have_link "Verify my account" + expect(page).to have_content "You are a level 3 user" + end + + scenario 'Verification with residency and sms and letter' do + + user = create(:user) + login_as(user) + + visit account_path + click_link 'Verify my account' + + select 'Spanish ID', from: 'residence_document_type' + fill_in 'residence_document_number', with: "12345678Z" + select_date '31-December-1980', from: 'residence_date_of_birth' + fill_in 'residence_postal_code', with: '28013' + + click_button 'Verify' + + expect(page).to have_content 'Residence verified' + + fill_in 'sms_phone', with: "611111111" + click_button 'Send' + + expect(page).to have_content 'Security code confirmation' + + user = user.reload + fill_in 'sms_confirmation_code', with: user.sms_confirmation_code + click_button 'Send' + + expect(page).to have_content 'Correct code' + + click_button "Send me a letter" + + expect(page).to have_content "You will receive a letter to your home address" + end +end \ No newline at end of file diff --git a/spec/features/verification/level_two_verification_spec.rb b/spec/features/verification/level_two_verification_spec.rb new file mode 100644 index 000000000..8358d33fe --- /dev/null +++ b/spec/features/verification/level_two_verification_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' + +feature 'Level two verification' do + + scenario 'Verification with residency and sms' do + user = create(:user) + login_as(user) + + visit account_path + click_link 'Verify my account' + + select 'Spanish ID', from: 'residence_document_type' + fill_in 'residence_document_number', with: "12345678Z" + select_date '31-December-1980', from: 'residence_date_of_birth' + fill_in 'residence_postal_code', with: '28013' + + click_button 'Verify' + + expect(page).to have_content 'Residence verified' + + fill_in 'sms_phone', with: "611111111" + click_button 'Send' + + expect(page).to have_content 'Security code confirmation' + + user = user.reload + fill_in 'sms_confirmation_code', with: user.sms_confirmation_code + click_button 'Send' + + expect(page).to have_content 'Correct code' + end + +end \ No newline at end of file diff --git a/spec/features/verification/residence_spec.rb b/spec/features/verification/residence_spec.rb new file mode 100644 index 000000000..95f25ed2b --- /dev/null +++ b/spec/features/verification/residence_spec.rb @@ -0,0 +1,80 @@ +require 'rails_helper' + +feature 'Residence' do + + scenario 'Verify resident in Madrid' do + user = create(:user) + login_as(user) + + visit account_path + click_link 'Verify my account' + + fill_in 'residence_document_number', with: "12345678Z" + select 'Spanish ID', from: 'residence_document_type' + select_date '31-December-1980', from: 'residence_date_of_birth' + fill_in 'residence_postal_code', with: '28013' + + click_button 'Verify' + + expect(page).to have_content 'Residence verified' + end + + scenario 'Error on verify' do + user = create(:user) + login_as(user) + + visit account_path + click_link 'Verify my account' + + click_button 'Verify' + + expect(page).to have_content /\d errors? prevented your residence verification/ + end + + scenario 'Error on Madrid census' do + user = create(:user) + login_as(user) + + visit account_path + click_link 'Verify my account' + + fill_in 'residence_document_number', with: "12345678Z" + select 'Spanish ID', from: 'residence_document_type' + select '1997', from: 'residence_date_of_birth_1i' + select 'January', from: 'residence_date_of_birth_2i' + select '1', from: 'residence_date_of_birth_3i' + fill_in 'residence_postal_code', with: '28013' + + click_button 'Verify' + + expect(page).to have_content 'The census of the city of Madrid could not verify your information' + end + + scenario '5 tries allowed' do + user = create(:user) + login_as(user) + + visit account_path + click_link 'Verify my account' + + 5.times do + fill_in 'residence_document_number', with: "12345678Z" + select 'Spanish ID', from: 'residence_document_type' + select '1997', from: 'residence_date_of_birth_1i' + select 'January', from: 'residence_date_of_birth_2i' + select '1', from: 'residence_date_of_birth_3i' + fill_in 'residence_postal_code', with: '28013' + + click_button 'Verify' + expect(page).to have_content 'The census of the city of Madrid could not verify your information' + end + + click_button 'Verify' + expect(page).to have_content 'You have reached the maximum number of Census verification tries' + expect(URI.parse(current_url).path).to eq(account_path) + + visit new_residence_path + expect(page).to have_content 'You have reached the maximum number of Census verification tries' + expect(URI.parse(current_url).path).to eq(account_path) + end +end \ No newline at end of file diff --git a/spec/features/verification/sms_spec.rb b/spec/features/verification/sms_spec.rb new file mode 100644 index 000000000..e445b3a27 --- /dev/null +++ b/spec/features/verification/sms_spec.rb @@ -0,0 +1,80 @@ +require 'rails_helper' + +feature 'SMS Verification' do + + scenario 'Verify' do + user = create(:user, residence_verified_at: Time.now) + login_as(user) + + visit new_sms_path + + fill_in 'sms_phone', with: "611111111" + click_button 'Send' + + expect(page).to have_content 'Security code confirmation' + + user = user.reload + fill_in 'sms_confirmation_code', with: user.sms_confirmation_code + click_button 'Send' + + expect(page).to have_content 'Correct code' + end + + scenario 'Errors on phone number' do + user = create(:user, residence_verified_at: Time.now) + login_as(user) + + visit new_sms_path + + click_button 'Send' + + expect(page).to have_content error_message + end + + scenario 'Errors on verification code' do + user = create(:user, residence_verified_at: Time.now) + login_as(user) + + visit new_sms_path + + fill_in 'sms_phone', with: "611111111" + click_button 'Send' + + expect(page).to have_content 'Security code confirmation' + + click_button 'Send' + + expect(page).to have_content 'Incorrect confirmation code' + end + + scenario 'Deny access unless residency verified' do + user = create(:user) + login_as(user) + + visit new_sms_path + + expect(page).to have_content 'You have not yet confirmed your residence' + expect(URI.parse(current_url).path).to eq(new_residence_path) + end + + scenario '3 tries allowed' do + user = create(:user, residence_verified_at: Time.now) + login_as(user) + + visit new_sms_path + + 3.times do + fill_in 'sms_phone', with: "611111111" + click_button 'Send' + click_link 'Click here to send the confirmation code again' + end + + expect(page).to have_content 'You have reached the maximum number of sms verification tries' + expect(URI.parse(current_url).path).to eq(account_path) + + visit new_sms_path + expect(page).to have_content 'You have reached the maximum number of sms verification tries' + expect(URI.parse(current_url).path).to eq(account_path) + end + +end \ No newline at end of file diff --git a/spec/features/verification/verified_user_spec.rb b/spec/features/verification/verified_user_spec.rb new file mode 100644 index 000000000..ce28ce80f --- /dev/null +++ b/spec/features/verification/verified_user_spec.rb @@ -0,0 +1,132 @@ +require 'rails_helper' + +feature 'Verified users' do + + scenario "Verified emails" do + user = create(:user, + residence_verified_at: Time.now, + document_number: '12345678Z', + document_type: '2') + + create(:verified_user, + document_number: '12345678Z', + document_type: '2', + email: 'rock@example.com') + + create(:verified_user, + document_number: '12345678Z', + document_type: '2', + email: 'roll@example.com') + + create(:verified_user, + document_number: '99999999R', + document_type: '2', + email: 'another@example.com') + + login_as(user) + visit verified_user_path + + expect(page).to have_content 'rock@example.com' + expect(page).to have_content 'roll@example.com' + end + + scenario "Verified phones" do + user = create(:user, + residence_verified_at: Time.now, + document_number: '12345678Z', + document_type: '2') + + create(:verified_user, + document_number: '12345678Z', + document_type: '2', + phone: '611111111') + + create(:verified_user, + document_number: '12345678Z', + document_type: '2', + email: '622222222') + + create(:verified_user, + document_number: '99999999R', + document_type: '2', + email: '633333333') + + login_as(user) + visit verified_user_path + + expect(page).to have_content '611111111' + expect(page).to have_content '622222222' + end + + scenario "Select a verified email" do + user = create(:user, + residence_verified_at: Time.now, + document_number: '12345678Z', + document_type: '2') + + verified_user = create(:verified_user, + document_number: '12345678Z', + document_type: '2', + email: 'rock@example.com') + + login_as(user) + visit verified_user_path + + within("#verified_user_#{verified_user.id}_email") do + click_button "Send" + end + + expect(page).to have_content 'We have send you a confirmation email to your email account: rock@example.com' + expect(URI.parse(current_url).path).to eq(account_path) + end + + scenario "Select a verified phone" do + user = create(:user, + residence_verified_at: Time.now, + document_number: '12345678Z', + document_type: '2') + + verified_user = create(:verified_user, + document_number: '12345678Z', + document_type: '2', + phone: '611111111') + + login_as(user) + visit verified_user_path + + within("#verified_user_#{verified_user.id}_phone") do + click_button "Send" + end + + expect(page).to have_content 'Enter the confirmation code' + end + + scenario "Continue without selecting any verified information" do + user = create(:user, + residence_verified_at: Time.now, + document_number: '12345678Z', + document_type: '2') + + create(:verified_user, + document_number: '12345678Z', + document_type: '2', + phone: '611111111') + + login_as(user) + visit verified_user_path + + click_link "Use another phone" + + expect(URI.parse(current_url).path).to eq(new_sms_path) + end + + scenario "No verified information" do + user = create(:user, residence_verified_at: Time.now) + + login_as(user) + visit verified_user_path + + expect(URI.parse(current_url).path).to eq(new_sms_path) + end + +end \ No newline at end of file diff --git a/spec/models/letter_spec.rb b/spec/models/letter_spec.rb new file mode 100644 index 000000000..e5e871d7c --- /dev/null +++ b/spec/models/letter_spec.rb @@ -0,0 +1,59 @@ +require 'rails_helper' + +describe 'Letter' do + + let(:user) { create(:user) } + + describe "validations" do + + let(:letter) { build(:letter) } + + it "should be valid" do + expect(letter).to be_valid + end + + it "should not be valid without a user" do + letter.user = nil + expect(letter).to_not be_valid + end + + it "should not be valid without an address" do + letter.address = {} + expect(letter).to_not be_valid + end + + end + + describe "save" do + + before(:each) do + letter = Letter.new(user: user) + letter.save + user.reload + end + + it "should update letter_requested" do + expect(user.letter_requested_at).to be + end + + it "should update address" do + expect(user.address).to have_attributes({ + postal_code: "28013", + street: "ALCALÁ", + street_type: "CALLE", + number: "1", + number_type: "NUM", + letter: "B", + portal: "1", + stairway: "4", + floor: "PB", + door: "DR", + km: "0", + neighbourhood: "JUSTICIA", + district: "CENTRO" + }) + end + + end + +end \ No newline at end of file diff --git a/spec/models/residence_spec.rb b/spec/models/residence_spec.rb new file mode 100644 index 000000000..66eb34951 --- /dev/null +++ b/spec/models/residence_spec.rb @@ -0,0 +1,52 @@ +require 'rails_helper' + +describe Residence do + + let(:residence) { build(:residence) } + + describe "validations" do + + it "should be valid" do + expect(residence).to be_valid + end + + describe "dates" do + it "should be valid with a valid date of birth" do + residence = Residence.new({"date_of_birth(3i)"=>"1", "date_of_birth(2i)"=>"1", "date_of_birth(1i)"=>"1980"}) + expect(residence.errors[:date_of_birth].size).to eq(0) + end + + it "should not be valid without a date of birth" do + residence = Residence.new({"date_of_birth(3i)"=>"", "date_of_birth(2i)"=>"", "date_of_birth(1i)"=>""}) + residence.valid? + expect(residence.errors[:date_of_birth]).to include("can't be blank") + end + end + + it "should validate uniquness of document_number" do + user = create(:user) + residence.user = user + residence.save + + residence2 = build(:residence) + + residence.valid? + expect(residence.errors[:document_number]).to include("Already in use") + end + end + + describe "save" do + + it "should store document number and type" do + user = create(:user) + residence.user = user + residence.save + + user.reload + expect(user.document_number).to eq('12345678Z') + expect(user.document_type).to eq("1") + end + + end + +end \ No newline at end of file diff --git a/spec/models/sms_spec.rb b/spec/models/sms_spec.rb new file mode 100644 index 000000000..bdcd2d739 --- /dev/null +++ b/spec/models/sms_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +describe Sms do + it "should be valid" do + sms = build(:sms) + expect(sms).to be_valid + end + + it "should validate uniqness of phone" do + user = create(:user, confirmed_phone: "699999999") + sms = Sms.new(phone: "699999999") + expect(sms).to_not be_valid + end + +end \ No newline at end of file diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb index 12d6affd3..591e0cebd 100644 --- a/spec/support/common_actions.rb +++ b/spec/support/common_actions.rb @@ -80,4 +80,12 @@ module CommonActions def expect_to_be_signed_in expect(find('.top-bar')).to have_content 'My account' end + + def select_date(values, selector) + selector = selector[:from] + day, month, year = values.split("-") + select day, from: "#{selector}_3i" + select month, from: "#{selector}_2i" + select year, from: "#{selector}_1i" + end end