测试使用带有Cucumber的Devise令牌可验证模块的JSON API

时间:2011-08-26 00:23:09

标签: ruby-on-rails-3 json rest cucumber acceptance-testing

我正在为RESTfull JSON服务编写验收测试。我希望能够针对生产服务器运行测试。此API由iphone客户端使用。对于身份验证,JSON服务使用Devise身份验证令牌模块。

简而言之,这是协议:

iphone:POST / api / v1 / tokens with params email =user@serivce.com& pass = secretpass server:返回200和以下JSON {“token”:“UYUKJHBKHJJHSAD”} iphone:GET / api / v1 / messages?auth_token = UYUKJHBKHJJHSAD

一切都很好。

用Cucumber测试这个的最佳方法是什么?

我正在使用来自https://github.com/jayzes/cucumber-api-steps的api_steps并且我一起攻击了一些内容,以便每次GET请求都会传递auth_token,但这有点像黑客攻击。

我所做的是创建以下步骤:

我使用密码“bingobingo”作为用户“admin@myservice.com”进行身份验证

在其中我设置了一个全局变量auth_token然后我追加到所有GET请求。难看!

我问你Cucumber / Rails /测试专家!这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分:1)

我发现RSpec比黄瓜更容易使用。

# spec/api/messages_spec.rb
describe "messages", :type => :api do
  it "retrieves messages" do
    get "/api/v1/messages", :token => user.authentication_token
    @messages = user.messages # this is the expected result
    last_response.body.should eql @messages.to_json
    last_response.status.should eql 200
  end
end

# spec/support/api/helper.rb, so we have access to get, post, put, delete methods
module ApiHelper
  include Rack::Test::Methods
  def app
    Rails.application
  end
end
RSpec.configure do |c|
  c.include ApiHelper, :type => :api
end