diff --git a/app/models/identity.rb b/app/models/identity.rb new file mode 100644 index 000000000..e0778dfae --- /dev/null +++ b/app/models/identity.rb @@ -0,0 +1,10 @@ +class Identity < ActiveRecord::Base + belongs_to :user + + validates :provider, presence: true + validates :uid, presence: true, uniqueness: { scope: :provider } + + def self.find_for_oauth(auth) + where(uid: auth.uid, provider: auth.provider).first_or_create + end +end diff --git a/db/migrate/20150824144524_create_identities.rb b/db/migrate/20150824144524_create_identities.rb new file mode 100644 index 000000000..38a5e603a --- /dev/null +++ b/db/migrate/20150824144524_create_identities.rb @@ -0,0 +1,11 @@ +class CreateIdentities < ActiveRecord::Migration + def change + create_table :identities do |t| + t.references :user, index: true, foreign_key: true + t.string :provider + t.string :uid + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 12e457a1c..d21459719 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: 20150824113326) do +ActiveRecord::Schema.define(version: 20150824144524) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -66,8 +66,8 @@ ActiveRecord::Schema.define(version: 20150824113326) do t.integer "author_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.datetime "hidden_at" t.string "visit_id" + t.datetime "hidden_at" t.datetime "flagged_as_inappropiate_at" t.integer "inappropiate_flags_count", default: 0 t.datetime "reviewed_at" @@ -75,6 +75,16 @@ ActiveRecord::Schema.define(version: 20150824113326) do add_index "debates", ["hidden_at"], name: "index_debates_on_hidden_at", using: :btree + create_table "identities", force: :cascade do |t| + t.integer "user_id" + t.string "provider" + t.string "uid" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "identities", ["user_id"], name: "index_identities_on_user_id", using: :btree + create_table "inappropiate_flags", force: :cascade do |t| t.integer "user_id" t.string "flaggable_type" @@ -156,10 +166,10 @@ ActiveRecord::Schema.define(version: 20150824113326) do t.string "unconfirmed_email" t.boolean "email_on_debate_comment", default: false t.boolean "email_on_comment_reply", default: false + t.string "phone_number", limit: 30 t.string "official_position" t.integer "official_level", default: 0 t.datetime "hidden_at" - t.string "phone_number", limit: 30 t.string "username" end @@ -214,6 +224,7 @@ ActiveRecord::Schema.define(version: 20150824113326) do add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope", using: :btree add_foreign_key "administrators", "users" + add_foreign_key "identities", "users" add_foreign_key "inappropiate_flags", "users" add_foreign_key "moderators", "users" add_foreign_key "organizations", "users" diff --git a/spec/factories.rb b/spec/factories.rb index 038a0102e..4fa2a70e3 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,5 +1,4 @@ FactoryGirl.define do - factory :user do username 'Manuela' sequence(:email) { |n| "manuela#{n}@madrid.es" } @@ -7,6 +6,12 @@ FactoryGirl.define do confirmed_at { Time.now } end + factory :identity do + user nil + provider "Twitter" + uid "MyString" + end + factory :debate do sequence(:title) { |n| "Debate #{n} title" } description 'Debate description' diff --git a/spec/models/identity_spec.rb b/spec/models/identity_spec.rb new file mode 100644 index 000000000..82d5c4be3 --- /dev/null +++ b/spec/models/identity_spec.rb @@ -0,0 +1,9 @@ +require 'rails_helper' + +RSpec.describe Identity, type: :model do + let(:identity) { build(:identity) } + + it "should be valid" do + expect(identity).to be_valid + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 5a4bbdf88..e0260285a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -50,6 +50,26 @@ describe User do end end + describe 'OmniAuth' do + describe '#email_provided?' do + it "is false if the email matchs was temporarely assigned by the OmniAuth process" do + subject.email = 'omniauth@participacion-ABCD-twitter.com' + expect(subject.email_provided?).to eq(false) + end + + it "is true if the email is not omniauth-like" do + subject.email = 'manuelacarmena@example.com' + expect(subject.email_provided?).to eq(true) + end + + it "is true if the user's real email is pending to be confirmed" do + subject.email = 'omniauth@participacion-ABCD-twitter.com' + subject.unconfirmed_email = 'manuelacarmena@example.com' + expect(subject.email_provided?).to eq(true) + end + end + end + describe "administrator?" do it "is false when the user is not an admin" do expect(subject.administrator?).to be false