From 8bf71e818b1daf690e9f296dde88b57edc359ba9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?=
Date: Fri, 11 Sep 2015 14:39:43 +0200
Subject: [PATCH 1/7] adds verification status methods to user
---
lib/verification.rb | 12 ++++++++++++
spec/models/user_spec.rb | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 48 insertions(+)
diff --git a/lib/verification.rb b/lib/verification.rb
index 1e2f7be81..67b0f4422 100644
--- a/lib/verification.rb
+++ b/lib/verification.rb
@@ -1,5 +1,17 @@
module Verification
+ def verification_email_sent?
+ email_verification_token.present?
+ end
+
+ def verification_sms_sent?
+ unconfirmed_phone.present? && sms_confirmation_code.present?
+ end
+
+ def verification_letter_sent?
+ letter_requested_at.present? && letter_verification_code.present?
+ end
+
def residence_verified?
residence_verified_at.present?
end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 801702cae..e8e710dd0 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -304,6 +304,42 @@ describe User do
user = create(:user, verified_at: Time.now, confirmed_phone: "123456789", residence_verified_at: Time.now)
expect(user.unverified?).to eq(false)
end
+
+ it "verification_email_sent? is true only if user has email_verification_token" do
+ user = create(:user, email_verification_token: "xxxxxxx")
+ expect(user.verification_email_sent?).to eq(true)
+
+ user = create(:user, email_verification_token: nil)
+ expect(user.verification_email_sent?).to eq(false)
+ end
+
+ it "verification_sms_sent? is true only if user has unconfirmed_phone and sms_confirmation_code" do
+ user = create(:user, unconfirmed_phone: "666666666", sms_confirmation_code: "666")
+ expect(user.verification_sms_sent?).to eq(true)
+
+ user = create(:user, unconfirmed_phone: nil, sms_confirmation_code: "666")
+ expect(user.verification_sms_sent?).to eq(false)
+
+ user = create(:user, unconfirmed_phone: "666666666", sms_confirmation_code: nil)
+ expect(user.verification_sms_sent?).to eq(false)
+
+ user = create(:user, unconfirmed_phone: nil, sms_confirmation_code: nil)
+ expect(user.verification_sms_sent?).to eq(false)
+ end
+
+ it "verification_letter_sent? is true only if user has letter_requested_at and letter_verification_code" do
+ user = create(:user, letter_requested_at: Time.now, letter_verification_code: "666")
+ expect(user.verification_letter_sent?).to eq(true)
+
+ user = create(:user, letter_requested_at: nil, letter_verification_code: "666")
+ expect(user.verification_letter_sent?).to eq(false)
+
+ user = create(:user, letter_requested_at: Time.now, letter_verification_code: nil)
+ expect(user.verification_letter_sent?).to eq(false)
+
+ user = create(:user, letter_requested_at: nil, letter_verification_code: nil)
+ expect(user.verification_letter_sent?).to eq(false)
+ end
end
describe "cache" do
From bc86d813025621d39e0c4406bc26fb9db0bf1458 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juanjo=20Baza=CC=81n?=
Date: Fri, 11 Sep 2015 14:40:42 +0200
Subject: [PATCH 2/7] adds verification controller
---
app/controllers/verification_controller.rb | 31 ++++++++++++++++++++++
config/locales/verification.en.yml | 5 +++-
config/locales/verification.es.yml | 3 +++
config/routes.rb | 1 +
4 files changed, 39 insertions(+), 1 deletion(-)
create mode 100644 app/controllers/verification_controller.rb
diff --git a/app/controllers/verification_controller.rb b/app/controllers/verification_controller.rb
new file mode 100644
index 000000000..6d1945af4
--- /dev/null
+++ b/app/controllers/verification_controller.rb
@@ -0,0 +1,31 @@
+class VerificationController < ApplicationController
+ before_action :authenticate_user!
+ before_action :verify_lock
+
+ skip_authorization_check
+
+ def show
+ redirect_to next_step_path[:path], notice: next_step_path[:notice]
+ end
+
+ private
+
+ def next_step_path(user = current_user)
+ if user.level_three_verified?
+ { path: account_path, notice: t('verification.redirect_notices.already_verified') }
+ elsif user.verification_letter_sent?
+ { path: edit_letter_path }
+ elsif user.level_two_verified?
+ { path: new_letter_path }
+ elsif user.verification_sms_sent?
+ { path: edit_sms_path }
+ elsif user.verification_email_sent?
+ { path: verified_user_path, notice: t('verification.redirect_notices.email_already_sent') }
+ elsif user.residence_verified?
+ { path: verified_user_path }
+ else
+ { path: new_residence_path }
+ end
+ end
+
+end
\ No newline at end of file
diff --git a/config/locales/verification.en.yml b/config/locales/verification.en.yml
index a333f78d7..267138b5b 100644
--- a/config/locales/verification.en.yml
+++ b/config/locales/verification.en.yml
@@ -93,4 +93,7 @@ en:
phone_title: "Phones"
use_another_phone: "Use another phone"
form:
- submit_button: "Send code"
\ No newline at end of file
+ submit_button: "Send code"
+ redirect_notices:
+ email_already_sent: "We already sent you a confirmation email, if you have not received it you can try resend it here"
+ already_verified: "You are a verified user!"
diff --git a/config/locales/verification.es.yml b/config/locales/verification.es.yml
index 6c86abcfc..fc7bd884b 100644
--- a/config/locales/verification.es.yml
+++ b/config/locales/verification.es.yml
@@ -94,3 +94,6 @@ es:
use_another_phone: "Utilizar otro teléfono"
form:
submit_button: "Enviar código"
+ redirect_notices:
+ email_already_sent: "Ya te enviamos un email con un enlace de confirmación, si no lo encuentras puedes solicitar aquí que te lo reenviemos"
+ already_verified: "Tu cuenta ya está verificada"
diff --git a/config/routes.rb b/config/routes.rb
index aa596fc25..e434d71e8 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -46,6 +46,7 @@ Rails.application.routes.draw do
end
resource :account, controller: "account", only: [:show, :update]
+ resource :verification, controller: "verification", only: [:show]
scope module: :verification do
resource :residence, controller: "residence", only: [:new, :create]
From 66c2813664bf0bf6d1db7f68653cf727f6b67924 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?=
Date: Fri, 11 Sep 2015 14:41:12 +0200
Subject: [PATCH 3/7] adds specs
---
.../verification/verification_path_spec.rb | 77 +++++++++++++++++++
1 file changed, 77 insertions(+)
create mode 100644 spec/features/verification/verification_path_spec.rb
diff --git a/spec/features/verification/verification_path_spec.rb b/spec/features/verification/verification_path_spec.rb
new file mode 100644
index 000000000..dc9f244a1
--- /dev/null
+++ b/spec/features/verification/verification_path_spec.rb
@@ -0,0 +1,77 @@
+require 'rails_helper'
+
+feature 'Verification path' do
+
+ scenario "User is verified" do
+ user = create(:user, verified_at: Time.now)
+
+ login_as(user)
+ visit verification_path
+
+ expect(current_path).to eq account_path
+ expect(page).to have_content 'You are a verified user!'
+ end
+
+ scenario "User requested a letter" do
+ user = create(:user, confirmed_phone: "623456789", residence_verified_at: Time.now,
+ letter_requested_at: Time.now, letter_verification_code: "666")
+
+ login_as(user)
+ visit verification_path
+
+ expect(current_path).to eq edit_letter_path
+ end
+
+ scenario "User is level two verified" do
+ user = create(:user, residence_verified_at: Time.now, confirmed_phone: "666666666")
+
+ login_as(user)
+ visit verification_path
+
+ expect(current_path).to eq new_letter_path
+ end
+
+ scenario "User received a verification sms" do
+ user = create(:user, residence_verified_at: Time.now, unconfirmed_phone: "666666666", sms_confirmation_code: "666")
+
+ login_as(user)
+ visit verification_path
+
+ expect(current_path).to eq edit_sms_path
+ end
+
+ scenario "User received verification email" do
+ user = create(:user, letter_requested_at: Time.now, letter_verification_code: "666")
+
+ login_as(user)
+ visit verification_path
+
+ verification_redirect = current_path
+
+ visit verified_user_path
+
+ expect(current_path).to eq verification_redirect
+ end
+
+ scenario "User has verified residence" do
+ user = create(:user, residence_verified_at: Time.now)
+
+ login_as(user)
+ visit verification_path
+
+ verification_redirect = current_path
+
+ visit verified_user_path
+
+ expect(current_path).to eq verification_redirect
+ end
+
+ scenario "User has not started verification process" do
+ user = create(:user)
+
+ login_as(user)
+ visit verification_path
+
+ expect(current_path).to eq new_residence_path
+ end
+end
\ No newline at end of file
From eabcbb3ef789fb6adf9d2c3252633fdfaa0638c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?=
Date: Fri, 11 Sep 2015 14:41:27 +0200
Subject: [PATCH 4/7] fixes I18n string
---
config/locales/es.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/config/locales/es.yml b/config/locales/es.yml
index 9d968eb59..e10386bac 100644
--- a/config/locales/es.yml
+++ b/config/locales/es.yml
@@ -211,7 +211,7 @@ es:
welcome:
title: Verificación de cuenta
instructions_1_html: "Bienvenido a la página de participación ciudadana"
- instructions_2_html: "Hemos detectado que tu email está confirmada pero no hemos verificado tus datos todavía."
+ instructions_2_html: "Hemos detectado que tu dirección de email está confirmada pero no hemos verificado tus datos todavía."
instructions_3_html: "Sin verificar tus datos el acceso que tienes es limitado. Verificarlos ahora te permitirá, por ejemplo, apoyar propuestas ciudadanas."
verify_account: "Verificar mi cuenta"
go_to_index: "Quiero entrar como un usuario no verificado (acceso limitado)"
From eb276b917ae07b1420e53771571a27ba5fb21ff8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?=
Date: Fri, 11 Sep 2015 14:41:51 +0200
Subject: [PATCH 5/7] changes link in account page to verification path
---
app/views/account/show.html.erb | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/app/views/account/show.html.erb b/app/views/account/show.html.erb
index 413a0e892..d580052e2 100644
--- a/app/views/account/show.html.erb
+++ b/app/views/account/show.html.erb
@@ -10,9 +10,9 @@
<%= t("account.show.verified_account") %>
<% elsif current_user.level_two_verified? %>
- <%= link_to t("account.show.finish_verification"), new_letter_path, class: "button radius small success right" %>
+ <%= link_to t("account.show.finish_verification"), verification_path, class: "button radius small success right" %>
<% else %>
- <%= link_to t("account.show.verify_my_account"), new_residence_path, class: "button radius small success right" %>
+ <%= link_to t("account.show.verify_my_account"), verification_path, class: "button radius small success right" %>
<% end %>
From 129666860699947f89981a1e5050be867c86a142 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?=
Date: Fri, 11 Sep 2015 16:44:00 +0200
Subject: [PATCH 6/7] changes links to verification process
---
app/views/debates/_votes.html.erb | 2 +-
app/views/welcome/welcome.html.erb | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/app/views/debates/_votes.html.erb b/app/views/debates/_votes.html.erb
index c5704b34a..5f38b1e7a 100644
--- a/app/views/debates/_votes.html.erb
+++ b/app/views/debates/_votes.html.erb
@@ -31,7 +31,7 @@
<%= t("votes.anonymous",
- verify_account: link_to(t("votes.verify_account"), new_residence_path )).html_safe %>
+ verify_account: link_to(t("votes.verify_account"), verification_path )).html_safe %>
<% elsif !user_signed_in? %>
diff --git a/app/views/welcome/welcome.html.erb b/app/views/welcome/welcome.html.erb
index fa49f9218..965edb795 100644
--- a/app/views/welcome/welcome.html.erb
+++ b/app/views/welcome/welcome.html.erb
@@ -4,7 +4,7 @@
<%= t("welcome.welcome.instructions_3_html") %>
<%= link_to t("welcome.welcome.verify_account"),
- new_residence_path, class: "button large success radius margin-top expand" %>
+ verification_path, class: "button large success radius margin-top expand" %>
<%= link_to t("welcome.welcome.go_to_index"),
From f517d7f410937e6b1f4c2b16b70ad88fa4f94242 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?=
Date: Fri, 11 Sep 2015 16:44:52 +0200
Subject: [PATCH 7/7] adds before action for verified users
---
app/controllers/application_controller.rb | 4 ++++
app/controllers/verification/email_controller.rb | 1 +
app/controllers/verification/letter_controller.rb | 1 +
.../verification/residence_controller.rb | 1 +
app/controllers/verification/sms_controller.rb | 1 +
.../verification/verified_user_controller.rb | 1 +
.../verification/verification_path_spec.rb | 14 ++++++++++++++
7 files changed, 23 insertions(+)
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index f71ebf723..aa6da3a80 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -96,4 +96,8 @@ class ApplicationController < ActionController::Base
redirect_to new_residence_path, alert: t('verification.residence.alert.unconfirmed_residency')
end
end
+
+ def verify_verified!
+ redirect_to(account_path, notice: t('verification.redirect_notices.already_verified')) if current_user.level_three_verified?
+ end
end
diff --git a/app/controllers/verification/email_controller.rb b/app/controllers/verification/email_controller.rb
index cb7542c80..017269095 100644
--- a/app/controllers/verification/email_controller.rb
+++ b/app/controllers/verification/email_controller.rb
@@ -1,5 +1,6 @@
class Verification::EmailController < ApplicationController
before_action :authenticate_user!
+ before_action :verify_verified!
before_action :set_verified_user, only: :create
skip_authorization_check
diff --git a/app/controllers/verification/letter_controller.rb b/app/controllers/verification/letter_controller.rb
index 15d408a5f..e1e23903b 100644
--- a/app/controllers/verification/letter_controller.rb
+++ b/app/controllers/verification/letter_controller.rb
@@ -2,6 +2,7 @@ class Verification::LetterController < ApplicationController
before_action :authenticate_user!
before_action :verify_resident!
before_action :verify_phone!
+ before_action :verify_verified!
before_action :verify_lock
skip_authorization_check
diff --git a/app/controllers/verification/residence_controller.rb b/app/controllers/verification/residence_controller.rb
index df24454e6..f5a826058 100644
--- a/app/controllers/verification/residence_controller.rb
+++ b/app/controllers/verification/residence_controller.rb
@@ -1,5 +1,6 @@
class Verification::ResidenceController < ApplicationController
before_action :authenticate_user!
+ before_action :verify_verified!
before_action :verify_lock, only: [:new, :create]
skip_authorization_check
diff --git a/app/controllers/verification/sms_controller.rb b/app/controllers/verification/sms_controller.rb
index 87604f726..09e4840a7 100644
--- a/app/controllers/verification/sms_controller.rb
+++ b/app/controllers/verification/sms_controller.rb
@@ -1,6 +1,7 @@
class Verification::SmsController < ApplicationController
before_action :authenticate_user!
before_action :verify_resident!
+ before_action :verify_verified!
before_action :verify_lock, only: [:new, :create]
before_action :set_phone, only: :create
diff --git a/app/controllers/verification/verified_user_controller.rb b/app/controllers/verification/verified_user_controller.rb
index bf2bc65fb..6964a2bd5 100644
--- a/app/controllers/verification/verified_user_controller.rb
+++ b/app/controllers/verification/verified_user_controller.rb
@@ -1,5 +1,6 @@
class Verification::VerifiedUserController < ApplicationController
before_action :authenticate_user!
+ before_action :verify_verified!
skip_authorization_check
def show
diff --git a/spec/features/verification/verification_path_spec.rb b/spec/features/verification/verification_path_spec.rb
index dc9f244a1..2967f5e2f 100644
--- a/spec/features/verification/verification_path_spec.rb
+++ b/spec/features/verification/verification_path_spec.rb
@@ -74,4 +74,18 @@ feature 'Verification path' do
expect(current_path).to eq new_residence_path
end
+
+ scenario "A verified user can not access verification pages" do
+ user = create(:user, verified_at: Time.now)
+
+ login_as(user)
+
+ verification_paths = [new_residence_path, verified_user_path, edit_sms_path, new_letter_path, edit_letter_path]
+ verification_paths.each do |step_path|
+ visit step_path
+
+ expect(current_path).to eq account_path
+ expect(page).to have_content 'You are a verified user!'
+ end
+ end
end
\ No newline at end of file