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

发布于 2024-12-01 20:58:09 字数 978 浏览 1 评论 0原文

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

简而言之,协议如下:

iphone: POST /api/v1/tokens with params [电子邮件受保护]&pass=secretpass 服务器:返回 200 和以下 JSON {"token":"UYUKJHBKHJJHSAD"} iphone: GET /api/v1/messages?auth_token=UYUKJHBKHJJHSAD

一切都很好。

使用 Cucumber 进行测试的最佳方法是什么?

我正在使用 https://github.com/jayzes/cucumber-api-steps 和我一起破解了一些东西,以便 auth_token 随每个 GET 请求一起传递,但这有点破解。

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

我以用户身份进行身份验证“[email protected 密码“bingobingo”

]”,其中我设置了 然后我将一个全局变量 auth_token 附加到所有 GET 请求中。丑陋的!

我向你们 Cucumber/Rails/Test 大师请教!这样做的最佳方法是什么?

I am writing acceptance tests for a RESTfull JSON service. I want to be able to run the tests against production server. This API is used by the iphone client. For authentication the JSON service uses the Devise authentication token module.

Here is the protocol in a nutshell:

iphone: POST /api/v1/tokens with params [email protected]&pass=secretpass
server: return 200 and the following JSON {"token":"UYUKJHBKHJJHSAD"}
iphone: GET /api/v1/messages?auth_token=UYUKJHBKHJJHSAD

Everything works great.

What is the best way to test this with Cucumber?

I am using the api_steps from https://github.com/jayzes/cucumber-api-steps and I hacked something together so that the auth_token is passed with every GET request however it is a bit of an hack.

What I did is to create the following step:

And I authenticate as the user "[email protected]" with the password "bingobingo"

in it I set a global variable auth_token that I then append to all the GET requests. UGLY!

I am asking to you Cucumber/Rails/Test gurus! What is the best way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃→可爱长大的 2024-12-08 20:58:09

我发现 RSpec 比 Cucumber 更容易使用。

# 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

I've found RSpec easier to use than Cucumber.

# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文