chapter 7.2.4
1. we need to create a user, save it to database
2. we need to get the user object by email
3. we need to verify that it has a given password
not surprising, we will start from test again, right, TDD!!!!!
we expect we will have a class method:
User.authenticate(email, submitted_password)
it should return nil if email/password combination is invalid
it should return nil if no user exists with the given email
it should return the user object itself on success.
from this points, we can write the test:
describe "authenticate method" do it "should return nil on email/password mismatch" do wrong_password_user = User.authenticate(@attr[:email], "wrongpasss") end it "should return nil for an email address with no user" do nonexistent_user = User.authenticate("bar@foo.com", @attr[:password]) end it "should return the user on email/password match" do matching_user = User.authenticate(@attr[:email], @attr[:password]) matching_user.should == @user end end
(for a class method, when using inside the class definition, we can omit the class name)
now it is the turn of authenticate method definition:
def self.authenticate(email, submitted_password) user = find_by_email(email) return nil if user.nil? return user if user.has_password?(submitted_password) end
note, since find_by_email class method is called inside the class, the class name can be omitte.
the method will return the value of the last statement.
2. what is self?
a. inside the method definition, self means the object itself.
b. when define a class method, self means the Class itself.
def self.authenticate
end
c. another way to define a class method:
def User.authenticate
end
d. another way to defind a class method
class << self
def authenticate
end
end
the last way of definition is very weird, you don't need to use it by yourself, but you need to know it, in case you come across it when others' codes.
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 788the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8421. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 8961. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8581.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7291. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8821. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6831. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 10871. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 715in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 757in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 634again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 6931. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 718whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7211. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 703There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 642start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 636a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 699in integration test, you can te ...
相关推荐
The reason is you account in window xp did not have "administrator" privacy
在与银联的对接中,调试过程中报错或使用类似登入加密:java.lang.SecurityException: JCE cannot authenticate the provider BC 进行问题解决,里面包含 bcprov-jdk16-143.jar与bcprov-jdk15-135.jar与具体文件存放...
标题中的“jboss7.1.1下报java.lang.SecurityException: JCE cannot authenticate the provider BC”是一个典型的Java安全异常,通常发生在尝试使用不受信任的加密提供者时。在这个问题中,"BC"指的是Bouncy Castle...
vue-authenticate, 简单 Vue.js 身份验证库 [ WARNING ]: 自述文件目前正在重写中,即将发布。身份验证 使用 Vue.js,提供本地登录/注册,以及使用 Github,Facebook,Google和其他OAuth提供商,可以方便地为提供...
Dr.COM Authenticate Client Installer网通宽带插件
Mifare Demo HID USB modem, allow to read, write, authenticate and generate Access Bits, Visual Basic
"Authenticate:认证服务"这个标题暗示了我们将会深入探讨如何利用编程语言如Perl来实现这样的服务。 Perl是一种功能强大的脚本语言,特别适合处理文本、网络和系统管理任务,包括构建安全的认证系统。在描述中只...
标题“authenticate.rar”暗示了这个压缩包可能包含与身份验证(Authenticate)相关的文件,这在IT行业中是一个关键的安全概念。身份验证是网络安全的基础,确保只有合法用户能够访问系统或资源。在这个上下文中,...
The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. doGet...
在标题中提到的问题,“www-authenticate:缺少用于超级0.10.x的HTTP WWW-Authenticate标头解析器”,意味着在特定版本的Hyper库中,对于"WWW-Authenticate"头的处理存在缺失或不兼容的情况。 Hyper是一个用Rust编程...
INTERNAL AUTHENTICATE命令用于经口令ˉ回令的过程来鉴别智能卡。一个8字节的随机数被送到智能卡, 它用DES算法给以加密。所用密钥的编号必须在参数”中给出,它必须指明所用之密钥是位于MF的BF的文件 中,还是在...
"OCB_encrypt_authenticate_memory"这个主题涉及到一种特定的加密模式——OCB(Offset Codebook)模式,它是在块密码模式下的一个高效、安全的选择,尤其适用于内存中的数据加密。OCB模式结合了块加密和消息认证码...
django提供了一个默认的auth系统用于用户的登录和授权,并提供了一定的扩展性,允许开发者自行定义多个验证后台,每个验证后台必须实现authenticate函数,并返回None或者User对象。 默认的后台是django.contrib.auth...
"authenticate-me-project" 是一个可能基于JavaScript开发的身份验证项目,主要关注用户登录和身份验证的安全性和用户体验。在Web开发中,身份验证是至关重要的,它确保只有授权的用户能够访问受保护的资源和服务。...
Haventec身份验证Web SDK 一组JavaScript函数,可促进客户端与Haventec Authenticate的集成。安装npm install @haventec/authenticate-web-sdk 一般用法authenticate-web-sdk不会作为默认导出导出,应该为每个用户...
GoogleEarthEngine的Python3.6运行环境,可以直接激活使用,基于conda。放到miniconda或anaconda的安装目录的envs文件夹下,使用conda activate gee激活当前环境,输入earthengine authenticate进行下一步操作
vue-authenticate 简单的Vue.js认证库
这两个函数分别负责发送命令到卡片和接收卡片的响应,它们通常采用ISO 14443A协议规定的帧格式。 4. **安全认证函数**:M1卡支持多种加密算法,如DES和3DES,用于保护数据的安全性。`authenticate()`函数执行认证...