Rails 关于需要先登录的 API,求助。。。。

jjzxcc · 2013年05月13日 · 最后由 mimosa 回复于 2013年05月13日 · 2563 次阅读

在用 Grape 写 api,想请问下对于那些需要先登录才能有返回的 api,要怎么写或者说是要怎么测试了呢。 比如 api 文件里有

resource :organization do
   get 'current' do
      Organization.find_by_id(current_user[:organization_id])
   end
end

helper 方法

def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
end

我测这个 current 方法的时候要怎么先登录呢?

# 当前用户
  def current_user
    @current_user ||= User.authenticate!(headers['X-Auth-Token'])
  end

  # 认证
  def authenticate!
    error!('401 Unauthorized', 401) unless current_user
  end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号