validates voter in census

This commit is contained in:
rgarcia
2016-09-19 13:31:18 +02:00
committed by kikito
parent 7f0d233bce
commit 44df91346a
8 changed files with 134 additions and 3 deletions

15
app/models/poll/voter.rb Normal file
View File

@@ -0,0 +1,15 @@
class Poll
class Voter < ActiveRecord::Base
belongs_to :booth
validate :in_census
def in_census
errors.add(:document_number, :not_in_census) unless census_api_response.valid?
end
def census_api_response
CensusApi.new.call(document_type, document_number)
end
end
end

View File

@@ -83,6 +83,10 @@ en:
attributes: attributes:
max_per_day: max_per_day:
invalid: "You have reached the maximum number of private messages per day" invalid: "You have reached the maximum number of private messages per day"
poll/voter:
attributes:
document_number:
not_in_census: "Document not in census"
proposal: proposal:
attributes: attributes:
tag_list: tag_list:

View File

@@ -83,6 +83,10 @@ es:
attributes: attributes:
max_per_day: max_per_day:
invalid: "Has llegado al número máximo de mensajes privados por día" invalid: "Has llegado al número máximo de mensajes privados por día"
poll/voter:
attributes:
document_number:
not_in_census: "Este documento no aparece en el censo"
proposal: proposal:
attributes: attributes:
tag_list: tag_list:

View File

@@ -0,0 +1,9 @@
class CreatePollVoters < ActiveRecord::Migration
def change
create_table :poll_voters do |t|
t.integer :booth_id
t.string :document_number
t.string :document_type
end
end
end

View File

@@ -270,10 +270,27 @@ ActiveRecord::Schema.define(version: 20161102133838) do
add_index "organizations", ["user_id"], name: "index_organizations_on_user_id", using: :btree add_index "organizations", ["user_id"], name: "index_organizations_on_user_id", using: :btree
<<<<<<< HEAD
=======
create_table "poll_booths", force: :cascade do |t|
t.string "name"
t.integer "poll_id"
end
>>>>>>> validates voter in census
create_table "poll_officers", force: :cascade do |t| create_table "poll_officers", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
end end
<<<<<<< HEAD
=======
create_table "poll_voters", force: :cascade do |t|
t.integer "booth_id"
t.string "document_number"
t.string "document_type"
end
>>>>>>> validates voter in census
create_table "polls", force: :cascade do |t| create_table "polls", force: :cascade do |t|
t.string "name" t.string "name"
end end

View File

@@ -74,7 +74,7 @@ class CensusApi
if end_point_available? if end_point_available?
client.call(:get_habita_datos, message: request(document_type, document_number)).body client.call(:get_habita_datos, message: request(document_type, document_number)).body
else else
stubbed_response_body stubbed_response(document_type, document_number)
end end
end end
@@ -97,8 +97,20 @@ class CensusApi
Rails.env.staging? || Rails.env.preproduction? || Rails.env.production? Rails.env.staging? || Rails.env.preproduction? || Rails.env.production?
end end
def stubbed_response_body def stubbed_response(document_type, document_number)
{get_habita_datos_response: {get_habita_datos_return: {hay_errores: false, datos_habitante: { item: {fecha_nacimiento_string: "31-12-1980", identificador_documento: "12345678Z", descripcion_sexo: "Varón" }}, datos_vivienda: {item: {codigo_postal: "28013", codigo_distrito: "01"}}}}} if document_number == "12345678Z" && document_type == "1"
stubbed_valid_response
else
stubbed_invalid_response
end
end
def stubbed_valid_response
{get_habita_datos_response: {get_habita_datos_return: {datos_habitante: { item: {fecha_nacimiento_string: "31-12-1980", identificador_documento: "12345678Z", descripcion_sexo: "Varón" }}, datos_vivienda: {item: {codigo_postal: "28013", codigo_distrito: "01"}}}}}
end
def stubbed_invalid_response
{get_habita_datos_response: {get_habita_datos_return: {datos_habitante: {}, datos_vivienda: {}}}}
end end
def is_dni?(document_type) def is_dni?(document_type)

View File

@@ -263,10 +263,33 @@ FactoryGirl.define do
user user
end end
factory :poll do
sequence(:name) { |n| "Poll #{n}" }
end
factory :poll_officer, class: 'Poll::Officer' do factory :poll_officer, class: 'Poll::Officer' do
user user
end end
factory :poll_booth, class: 'Poll::Booth' do
sequence(:name) { |n| "Booth #{n}" }
poll
end
factory :poll_voter, class: 'Poll::Voter' do
association :booth, factory: :budget_booth
trait :valid_document do
document_type "1"
document_number "12345678Z"
end
trait :invalid_document do
document_type "1"
document_number "99999999A"
end
end
>>>>>>> validates voter in census
factory :organization do factory :organization do
user user
responsible_name "Johnny Utah" responsible_name "Johnny Utah"

View File

@@ -0,0 +1,47 @@
require 'rails_helper'
describe :voter do
let(:poll) { create(:poll) }
let(:booth) { create(:poll_booth, poll: poll) }
describe "validations" do
it "should be valid if in census and has not voted" do
voter = build(:poll_voter, :valid_document, booth: booth)
expect(voter).to be_valid
end
it "should not be valid if the user is not in the census" do
voter = build(:poll_voter, :invalid_document, booth: booth)
expect(voter).to_not be_valid
expect(voter.errors.messages[:document_number]).to eq(["Document not in census"])
end
it "should not be valid if the user has already voted in the same booth" do
voter1 = create(:poll_voter, :valid_document, booth: booth)
voter2 = build(:poll_voter, :valid_document, booth: booth)
expect(voter2).to_not be_valid
expect(voter2.errors.messages[:document_number]).to eq(["José García has already voted"])
end
it "should not be valid if the user has already voted in another booth" do
booth1 = create(:poll_booth, poll: poll)
booth2 = create(:poll_booth, poll: poll)
voter1 = create(:poll_voter, :valid_document, booth: booth1)
voter2 = build(:poll_voter, :valid_document, booth: booth2)
expect(voter2).to_not be_valid
expect(voter2.errors.messages[:document_number]).to eq(["José García has already voted"])
end
xit "should not be valid if the user has voted via web" do
pending "Implementation for voting via web"
end
end
end