diff --git a/app/models/poll/voter.rb b/app/models/poll/voter.rb new file mode 100644 index 000000000..92df28443 --- /dev/null +++ b/app/models/poll/voter.rb @@ -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 \ No newline at end of file diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 07791e53c..933b0f572 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -82,6 +82,10 @@ en: attributes: max_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: attributes: tag_list: diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index d5b7f0005..38ceeabdd 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -82,6 +82,10 @@ es: attributes: max_per_day: 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: attributes: tag_list: diff --git a/db/migrate/20160914172016_create_poll_voters.rb b/db/migrate/20160914172016_create_poll_voters.rb new file mode 100644 index 000000000..df5bb4585 --- /dev/null +++ b/db/migrate/20160914172016_create_poll_voters.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index becde4f8a..8a928ca79 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160803154011) do +ActiveRecord::Schema.define(version: 20160914172535) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -270,6 +270,25 @@ ActiveRecord::Schema.define(version: 20160803154011) do add_index "organizations", ["user_id"], name: "index_organizations_on_user_id", using: :btree + create_table "poll_booths", force: :cascade do |t| + t.string "name" + t.integer "poll_id" + end + + create_table "poll_officers", force: :cascade do |t| + t.integer "user_id" + end + + create_table "poll_voters", force: :cascade do |t| + t.integer "booth_id" + t.string "document_number" + t.string "document_type" + end + + create_table "polls", force: :cascade do |t| + t.string "name" + end + create_table "proposal_notifications", force: :cascade do |t| t.string "title" t.text "body" diff --git a/lib/census_api.rb b/lib/census_api.rb index 678a748c6..d80f420e7 100644 --- a/lib/census_api.rb +++ b/lib/census_api.rb @@ -74,7 +74,7 @@ class CensusApi if end_point_available? client.call(:get_habita_datos, message: request(document_type, document_number)).body else - stubbed_response_body + stubbed_response(document_type, document_number) end end @@ -97,8 +97,20 @@ class CensusApi Rails.env.staging? || Rails.env.preproduction? || Rails.env.production? end - def stubbed_response_body - {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"}}}}} + def stubbed_response(document_type, document_number) + 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 def is_dni?(document_type) diff --git a/spec/factories.rb b/spec/factories.rb index cdcfe7381..adda53948 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -259,6 +259,32 @@ FactoryGirl.define do user end + factory :poll do + sequence(:name) { |n| "Poll #{n}" } + end + + factory :poll_officer, class: 'Poll::Officer' do + user + 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 + factory :organization do user responsible_name "Johnny Utah" diff --git a/spec/models/poll/voter_spec.rb b/spec/models/poll/voter_spec.rb new file mode 100644 index 000000000..de4536243 --- /dev/null +++ b/spec/models/poll/voter_spec.rb @@ -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 \ No newline at end of file