From 303ec07901af3798efc873cbe050aa5cb4ba7655 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 8 Jul 2014 11:00:40 +0200 Subject: use cucumber; initial ConfigsController --- features/step_definitions/.gitkeep | 0 features/step_definitions/api_steps.rb | 132 ++++++++++++++++++++++++++++++ features/step_definitions/config_steps.rb | 6 ++ 3 files changed, 138 insertions(+) create mode 100644 features/step_definitions/.gitkeep create mode 100644 features/step_definitions/api_steps.rb create mode 100644 features/step_definitions/config_steps.rb (limited to 'features/step_definitions') diff --git a/features/step_definitions/.gitkeep b/features/step_definitions/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/features/step_definitions/api_steps.rb b/features/step_definitions/api_steps.rb new file mode 100644 index 0000000..0e52f7a --- /dev/null +++ b/features/step_definitions/api_steps.rb @@ -0,0 +1,132 @@ +require 'jsonpath' + +if defined?(Rack) + + # Monkey patch Rack::MockResponse to work properly with response debugging + class Rack::MockResponse + def to_str + body + end + end + + World(Rack::Test::Methods) + +end + +Given /^I set headers:$/ do |headers| + headers.rows_hash.each {|k,v| header k, v } +end + +Given /^I send and accept (XML|JSON)$/ do |type| + header 'Accept', "application/#{type.downcase}" + header 'Content-Type', "application/#{type.downcase}" +end + +Given /^I send and accept HTML$/ do + header 'Accept', "text/html" + header 'Content-Type', "application/x-www-form-urlencoded" +end + +When /^I authenticate as the user "([^"]*)" with the password "([^"]*)"$/ do |user, pass| + authorize user, pass +end + +When /^I digest\-authenticate as the user "(.*?)" with the password "(.*?)"$/ do |user, pass| + digest_authorize user, pass +end + +When /^I send a (GET|POST|PUT|DELETE) request (?:for|to) "([^"]*)"(?: with the following:)?$/ do |*args| + request_type = args.shift + path = args.shift + input = args.shift + + request_opts = {method: request_type.downcase.to_sym} + + unless input.nil? + if input.class == Cucumber::Ast::Table + request_opts[:params] = input.rows_hash + else + request_opts[:input] = input + end + end + + request path, request_opts +end + +Then /^show me the (unparsed)?\s?response$/ do |unparsed| + if unparsed == 'unparsed' + puts last_response.body + elsif last_response.headers['Content-Type'] =~ /json/ + json_response = JSON.parse(last_response.body) + puts JSON.pretty_generate(json_response) + else + puts last_response.headers + puts last_response.body + end +end + +Then /^the response status should be "([^"]*)"$/ do |status| + if self.respond_to? :should + last_response.status.should == status.to_i + else + assert_equal status.to_i, last_response.status + end +end + +Then /^the response should (not)?\s?have "([^"]*)"$/ do |negative, json_path| + json = JSON.parse(last_response.body) + results = JsonPath.new(json_path).on(json).to_a.map(&:to_s) + if self.respond_to?(:should) + if negative.present? + results.should be_empty + else + results.should_not be_empty + end + else + if negative.present? + assert results.empty? + else + assert !results.empty? + end + end +end + + +Then /^the response should (not)?\s?have "([^"]*)" with the text "([^"]*)"$/ do |negative, json_path, text| + json = JSON.parse(last_response.body) + results = JsonPath.new(json_path).on(json).to_a.map(&:to_s) + if self.respond_to?(:should) + if negative.present? + results.should_not include(text) + else + results.should include(text) + end + else + if negative.present? + assert !results.include?(text) + else + assert results.include?(text) + end + end +end + +Then /^the response should be:$/ do |json| + expected = JSON.parse(json) + actual = JSON.parse(last_response.body) + + if self.respond_to?(:should) + actual.should == expected + else + assert_equal expected, actual + end +end + +Then /^the response should have "([^"]*)" with a length of (\d+)$/ do |json_path, length| + json = JSON.parse(last_response.body) + results = JsonPath.new(json_path).on(json) + if self.respond_to?(:should) + results.length.should == length.to_i + else + assert_equal length.to_i, results.length + end +end diff --git a/features/step_definitions/config_steps.rb b/features/step_definitions/config_steps.rb new file mode 100644 index 0000000..50ae829 --- /dev/null +++ b/features/step_definitions/config_steps.rb @@ -0,0 +1,6 @@ +Given /the provider config is:$/ do |config| + @tempfile = Tempfile.new('provider.json') + @tempfile.write config + @tempfile.close + StaticConfigController::PROVIDER_JSON = @tempfile.path +end -- cgit v1.2.3 From f1a8cefb810bef263d3a96edffbec511dbe15291 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 8 Jul 2014 12:48:33 +0200 Subject: send static list of configs for now Also added authentication steps to cucumber --- features/step_definitions/api_steps.rb | 5 ++++- features/step_definitions/auth_steps.rb | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 features/step_definitions/auth_steps.rb (limited to 'features/step_definitions') diff --git a/features/step_definitions/api_steps.rb b/features/step_definitions/api_steps.rb index 0e52f7a..3a24d68 100644 --- a/features/step_definitions/api_steps.rb +++ b/features/step_definitions/api_steps.rb @@ -14,7 +14,10 @@ if defined?(Rack) end Given /^I set headers:$/ do |headers| - headers.rows_hash.each {|k,v| header k, v } + headers.rows_hash.each do |key,value| + value.sub!('MY_AUTH_TOKEN', @my_auth_token.to_s) if @my_auth_token + header key, value + end end Given /^I send and accept (XML|JSON)$/ do |type| diff --git a/features/step_definitions/auth_steps.rb b/features/step_definitions/auth_steps.rb new file mode 100644 index 0000000..00d9004 --- /dev/null +++ b/features/step_definitions/auth_steps.rb @@ -0,0 +1,6 @@ + +Given /^I authenticated$/ do + @user = FactoryGirl.create(:user) + @my_auth_token = Token.create user_id: @user.id +end + -- cgit v1.2.3 From e86cccb4b89540f3bd403110d051b2723be781b9 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 17 Jul 2014 11:55:31 +0200 Subject: cuke: drop jsonpath, use simple keys instead Also fixed the test for login_required --- features/step_definitions/api_steps.rb | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'features/step_definitions') diff --git a/features/step_definitions/api_steps.rb b/features/step_definitions/api_steps.rb index 3a24d68..a4f369c 100644 --- a/features/step_definitions/api_steps.rb +++ b/features/step_definitions/api_steps.rb @@ -1,5 +1,3 @@ -require 'jsonpath' - if defined?(Rack) # Monkey patch Rack::MockResponse to work properly with response debugging @@ -76,39 +74,37 @@ Then /^the response status should be "([^"]*)"$/ do |status| end end -Then /^the response should (not)?\s?have "([^"]*)"$/ do |negative, json_path| +Then /^the response should (not)?\s?have "([^"]*)"$/ do |negative, key| json = JSON.parse(last_response.body) - results = JsonPath.new(json_path).on(json).to_a.map(&:to_s) if self.respond_to?(:should) if negative.present? - results.should be_empty + json[key].should be_blank else - results.should_not be_empty + json[key].should be_present end else if negative.present? - assert results.empty? + assert json[key].blank? else - assert !results.empty? + assert json[key].present? end end end -Then /^the response should (not)?\s?have "([^"]*)" with the text "([^"]*)"$/ do |negative, json_path, text| +Then /^the response should (not)?\s?have "([^"]*)" with(?: the text)? "([^"]*)"$/ do |negative, key, text| json = JSON.parse(last_response.body) - results = JsonPath.new(json_path).on(json).to_a.map(&:to_s) if self.respond_to?(:should) if negative.present? - results.should_not include(text) + json[key].should_not == text else - results.should include(text) + results.should == text end else if negative.present? - assert !results.include?(text) + assert ! json[key] == text else - assert results.include?(text) + assert_equal text, json[key] end end end -- cgit v1.2.3