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/config.feature | 39 +++++++++ features/step_definitions/.gitkeep | 0 features/step_definitions/api_steps.rb | 132 ++++++++++++++++++++++++++++++ features/step_definitions/config_steps.rb | 6 ++ features/support/env.rb | 58 +++++++++++++ features/support/hooks.rb | 6 ++ 6 files changed, 241 insertions(+) create mode 100644 features/config.feature 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 create mode 100644 features/support/env.rb create mode 100644 features/support/hooks.rb (limited to 'features') diff --git a/features/config.feature b/features/config.feature new file mode 100644 index 0000000..2d237f2 --- /dev/null +++ b/features/config.feature @@ -0,0 +1,39 @@ +Feature: Download Provider Configuration + + The LEAP Provider exposes parts of its configuration through the API. + + This can be used to find out about services offered. The big picture can be retrieved from `/provider.json`. More detailed settings of the services are available after authentication. You can get a list of the available settings from `/1/configs.json`. + + Background: + Given I set headers: + | Accept | application/json | + | Content-Type | application/json | + + @tempfile + Scenario: Fetch provider config + Given the provider config is: + """ + {"config": "me"} + """ + When I send a GET request to "/provider.json" + Then the response status should be "200" + And the response should be: + """ + {"config": "me"} + """ + + Scenario: Missing provider config + When I send a GET request to "/provider.json" + Then the response status should be "404" + And the response should be: + """ + {"error": "not found"} + """ + + Scenario: Authentication required for list of configs + When I send a GET request to "/1/configs" + Then the response status should be "401" + And the response should be: + """ + {"error": "Please log in to perform that action."} + """ 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 diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000..d3067db --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1,58 @@ +# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril. +# It is recommended to regenerate this file in the future when you upgrade to a +# newer version of cucumber-rails. Consider adding your own code to a new file +# instead of editing this one. Cucumber will automatically load all features/**/*.rb +# files. + +require 'cucumber/rails' + +# Capybara defaults to CSS3 selectors rather than XPath. +# If you'd prefer to use XPath, just uncomment this line and adjust any +# selectors in your step definitions to use the XPath syntax. +# Capybara.default_selector = :xpath + +# By default, any exception happening in your Rails application will bubble up +# to Cucumber so that your scenario will fail. This is a different from how +# your application behaves in the production environment, where an error page will +# be rendered instead. +# +# Sometimes we want to override this default behaviour and allow Rails to rescue +# exceptions and display an error page (just like when the app is running in production). +# Typical scenarios where you want to do this is when you test your error pages. +# There are two ways to allow Rails to rescue exceptions: +# +# 1) Tag your scenario (or feature) with @allow-rescue +# +# 2) Set the value below to true. Beware that doing this globally is not +# recommended as it will mask a lot of errors for you! +# +ActionController::Base.allow_rescue = false + +# Remove/comment out the lines below if your app doesn't have a database. +# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead. +begin + #DatabaseCleaner.strategy = :truncation +rescue NameError + raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it." +end + +# You may also want to configure DatabaseCleaner to use different strategies for certain features and scenarios. +# See the DatabaseCleaner documentation for details. Example: +# +# Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do +# # { :except => [:widgets] } may not do what you expect here +# # as Cucumber::Rails::Database.javascript_strategy overrides +# # this setting. +# DatabaseCleaner.strategy = :truncation +# end +# +# Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do +# DatabaseCleaner.strategy = :transaction +# end +# + +# Possible values are :truncation and :transaction +# The :transaction strategy is faster, but might give you threading problems. +# See https://github.com/cucumber/cucumber-rails/blob/master/features/choose_javascript_database_strategy.feature +Cucumber::Rails::Database.javascript_strategy = :truncation + diff --git a/features/support/hooks.rb b/features/support/hooks.rb new file mode 100644 index 0000000..360f231 --- /dev/null +++ b/features/support/hooks.rb @@ -0,0 +1,6 @@ +After('@tempfile') do + if @tempfile + @tempfile.close + @tempfile.unlink + end +end -- cgit v1.2.3