creates Manager
This commit is contained in:
committed by
Juanjo Bazán
parent
39119b9d6b
commit
b921c67676
12
app/models/manager.rb
Normal file
12
app/models/manager.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
class Manager < ActiveRecord::Base
|
||||||
|
validates :username, presence: true, uniqueness: true
|
||||||
|
validates :password_digest, presence: true
|
||||||
|
|
||||||
|
has_secure_password
|
||||||
|
|
||||||
|
def self.valid_auth?(username = nil, password = nil)
|
||||||
|
return false unless username.present? && password.present?
|
||||||
|
Manager.find_by(username: username).try(:authenticate, password).present?
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
12
db/migrate/20150930082311_create_managers.rb
Normal file
12
db/migrate/20150930082311_create_managers.rb
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
class CreateManagers < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
create_table :managers do |t|
|
||||||
|
t.string :username, null: false
|
||||||
|
t.string :password_digest, null: false
|
||||||
|
t.timestamp :last_login_at
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index :managers, [:username]
|
||||||
|
end
|
||||||
|
end
|
||||||
12
db/schema.rb
12
db/schema.rb
@@ -11,7 +11,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20150926115929) do
|
ActiveRecord::Schema.define(version: 20150930082311) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
@@ -168,6 +168,16 @@ ActiveRecord::Schema.define(version: 20150926115929) do
|
|||||||
|
|
||||||
add_index "locks", ["user_id"], name: "index_locks_on_user_id", using: :btree
|
add_index "locks", ["user_id"], name: "index_locks_on_user_id", using: :btree
|
||||||
|
|
||||||
|
create_table "managers", force: :cascade do |t|
|
||||||
|
t.string "username", null: false
|
||||||
|
t.string "password_digest", null: false
|
||||||
|
t.datetime "last_login_at"
|
||||||
|
t.datetime "created_at"
|
||||||
|
t.datetime "updated_at"
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index "managers", ["username"], name: "index_managers_on_username", using: :btree
|
||||||
|
|
||||||
create_table "moderators", force: :cascade do |t|
|
create_table "moderators", force: :cascade do |t|
|
||||||
t.integer "user_id"
|
t.integer "user_id"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -218,6 +218,11 @@ FactoryGirl.define do
|
|||||||
user
|
user
|
||||||
end
|
end
|
||||||
|
|
||||||
|
factory :manager do
|
||||||
|
sequence(:username) { |n| "manager#{n}" }
|
||||||
|
password 'supersecret'
|
||||||
|
end
|
||||||
|
|
||||||
factory :organization do
|
factory :organization do
|
||||||
user
|
user
|
||||||
responsible_name "Johnny Utah"
|
responsible_name "Johnny Utah"
|
||||||
|
|||||||
46
spec/models/manager_spec.rb
Normal file
46
spec/models/manager_spec.rb
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe Manager do
|
||||||
|
|
||||||
|
describe "valid?" do
|
||||||
|
|
||||||
|
let(:manager) { create(:manager) }
|
||||||
|
|
||||||
|
it "is false when username is blank" do
|
||||||
|
manager.username = nil
|
||||||
|
expect(manager).to_not be_valid
|
||||||
|
end
|
||||||
|
it "is false when password is blank" do
|
||||||
|
manager.password_digest = nil
|
||||||
|
expect(manager).to_not be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is true if username and password present" do
|
||||||
|
expect(manager).to be_valid
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "self.valid_auth?" do
|
||||||
|
before(:all) { create(:manager, username: "Silvia" ,password: "supersecret") }
|
||||||
|
|
||||||
|
it "is false when username is blank" do
|
||||||
|
expect(Manager.valid_auth?(nil, "supersecret")).to be false
|
||||||
|
end
|
||||||
|
it "is false when password is blank" do
|
||||||
|
expect(Manager.valid_auth?("Silvia", nil)).to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is false if manager unexistent" do
|
||||||
|
expect(Manager.valid_auth?("Manager", "supersecret")).to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is false if wrong password unexistent" do
|
||||||
|
expect(Manager.valid_auth?("Silvia", "wrong")).to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is true if right username/password combination" do
|
||||||
|
expect(Manager.valid_auth?("Silvia", "supersecret")).to be true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user