Commit 364b273
Changed files (2)
app
models
spec
models
app/models/user.rb
@@ -11,6 +11,10 @@ class User < ActiveRecord::Base
Digest::MD5::hexdigest(email.downcase)
end
+ def to_param
+ username
+ end
+
def self.authenticate(username,password)
if user = User.where("email = :email OR username = :username", username: username, email: username).first
user.authenticate(password)
spec/models/user_spec.rb
@@ -88,33 +88,32 @@ describe User do
expect(User::USERNAME_REGEX).to match("username1")
end
end
-
+
describe "#authenticate" do
context "when credentials are correct" do
-
it "returns true" do
user = create(:user, password: "password", password_confirmation: "password")
expect(User.authenticate(user.email, "password")).to eql(user)
end
-
end
-
+
context "when the email is not registered" do
-
it "returns nil" do
expect(User.authenticate("sofake@noteven.com", "password")).to be_nil
end
-
end
-
+
context "when the username is not registered" do
-
it "returns nil" do
expect(User.authenticate("sofake", "password")).to be_nil
end
-
end
-
end
-
+
+ describe "#to_param" do
+ it "returns the username as the uniq identifier" do
+ user = build(:user)
+ expect(user.to_param).to eql(user.username)
+ end
+ end
end