incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 03/10] Fixed Cucumber features to reflect changes in 0.9.0 release
Date Wed, 29 Sep 2010 14:18:21 GMT
From: Michal Fojtik <mfojtik@redhat.com>

---
 tests/mock/api.feature                          |   11 +++-
 tests/mock/instances.feature                    |    6 +-
 tests/mock/respond_to.feature                   |   71 +++++++++++++++++++++++
 tests/mock/step_definitions/api_steps.rb        |   21 ++++---
 tests/mock/step_definitions/instances_steps.rb  |   20 +++---
 tests/mock/step_definitions/respond_to_steps.rb |   33 +++++++++++
 tests/mock/support/env.rb                       |   18 ++++--
 7 files changed, 148 insertions(+), 32 deletions(-)
 create mode 100644 tests/mock/respond_to.feature
 create mode 100644 tests/mock/step_definitions/respond_to_steps.rb

diff --git a/tests/mock/api.feature b/tests/mock/api.feature
index ec05551..d8cecbe 100644
--- a/tests/mock/api.feature
+++ b/tests/mock/api.feature
@@ -26,7 +26,14 @@ Feature: Accessing API entry points
     Given URI /api exists
     And authentification is not required for this URI
     When client access this URI
-    Then client should get list of valid entry points
+    Then client should get list of valid entry points:
+    | realms     |
+    | instances  |
+    | images     |
+    | instance_states |
+    | hardware_profiles  |
+    | storage_snapshots  |
+    | storage_volumes    |
     And each link should have 'rel' attribute with valid name
     And each link should have 'href' attribute with valid URL
     When client follow this attribute
@@ -37,5 +44,5 @@ Feature: Accessing API entry points
     And authentification is not required for this URI
     When client access this URI
     Then client should get list of features inside 'instances':
-    | hardware_profiles |
     | user_name |
+    | hardware_profiles |
diff --git a/tests/mock/instances.feature b/tests/mock/instances.feature
index 6931b5a..969cb80 100644
--- a/tests/mock/instances.feature
+++ b/tests/mock/instances.feature
@@ -154,10 +154,10 @@ Feature: Managing instances
   Scenario: Destroying created instance
     Given URI /api/instances exists
     And authentification is required for this URI
-    When client want to 'stop' created instance
+    When client want to 'stop' first instance
     And client follow link in actions
-    Then client should get created instance
+    Then client should get this instance
     And this instance should be in 'STOPPED' state
-    When client want to 'destroy' created instance
+    When client want to 'destroy' first instance
     And client follow link in actions
     And this instance should be destroyed
diff --git a/tests/mock/respond_to.feature b/tests/mock/respond_to.feature
new file mode 100644
index 0000000..1445b6b
--- /dev/null
+++ b/tests/mock/respond_to.feature
@@ -0,0 +1,71 @@
+Feature: Returning valid responses with various queries
+
+  Scenario: User will get XML version if there is no Accept or format parameter
+    Given URI /api exists
+    And authentification is not required for this URI
+    And client perform an HTTP request for this URI
+    Then client should get valid XML response
+
+  Scenario: User wants to get XML version of API using Accept header
+    Given URI /api exists
+    And authentification is not required for this URI
+    When client use Accept header:
+    | application/xml;q=0.9 |
+    And client perform an HTTP request for this URI
+    Then client should get valid XML response
+
+  Scenario: User wants to get HTML version of API using Accept header
+  Given URI /api exists
+    And authentification is not required for this URI
+    When client use Accept header:
+    | application/xhtml+html;q=0.9 |
+    And client perform an HTTP request for this URI
+    Then client should get valid HTML response
+
+  Scenario: User wants to get JSON version of API
+  Given URI /api exists
+    And authentification is not required for this URI
+    When client use Accept header:
+    | application/json;q=0.9 |
+    And client perform an HTTP request for this URI
+    Then client should get valid JSON response
+
+  Scenario: User wants to get XML version of API with format parameter
+    Given URI /api exists
+    And authentification is not required for this URI
+    When client use Accept header:
+    | application/xhtml+html;q=0.9 |
+    And client accept this URI with parameters:
+    | format | xml |
+    And client perform an HTTP request for this URI
+    Then client should get valid XML response
+
+  Scenario: User wants to get JSON version of API with format parameter
+    Given URI /api exists
+    And authentification is not required for this URI
+    When client use Accept header:
+    | application/xhtml+html;q=0.9 |
+    And client accept this URI with parameters:
+    | format | json |
+    And client perform an HTTP request for this URI
+    Then client should get valid JSON response
+
+  Scenario: User set Accept to json but force format to XML using format parameter 
+    Given URI /api exists
+    And authentification is not required for this URI
+    When client use Accept header:
+    | application/json;q=0.9 |
+    And client accept this URI with parameters:
+    | format | xml |
+    And client perform an HTTP request for this URI
+    Then client should get valid XML response
+
+  # Extensions are ignored, so this doesn't affect content-negotiation
+  Scenario: User wants to get XML version of API with format parameter and set extension
+    Given URI /api exists
+    And authentification is not required for this URI
+    When client wants to get URI '/api.xml'
+    When client use Accept header:
+    | application/xhtml+html;q=0.9 |
+    And client perform an HTTP request for this URI
+    Then client should get valid HTML response
diff --git a/tests/mock/step_definitions/api_steps.rb b/tests/mock/step_definitions/api_steps.rb
index 9a30c3d..0af6f98 100644
--- a/tests/mock/step_definitions/api_steps.rb
+++ b/tests/mock/step_definitions/api_steps.rb
@@ -6,8 +6,17 @@ Given /^URI ([\w\/\-_]+) exists$/ do |uri|
 end
 
 Given /^URI ([\w\/\-_]+) exists in (.+) format$/ do |uri, format|
-  @uri = "#{uri}.#{format.downcase}"
-  get @uri, {}
+  @no_header = true
+  case format.downcase
+    when 'xml':
+      header 'Accept', 'application/xml;q=9'
+    when 'json'
+      header 'Accept', 'application/json;q=9'
+    when 'html'
+      header 'Accept', 'application/xml+xhtml;q=9'
+  end
+  @uri = uri
+  get uri, {}
   last_response.status.should_not == 404
   last_response.status.should_not == 500
 end
@@ -49,14 +58,6 @@ Then /^this URI should be available in (.+) format$/ do |formats|
   @no_header = false
 end
 
-Then /^client should get list of valid entry points$/ do
-  links = []
-  output_xml.xpath('/api/link').each do |entry_point|
-    links << entry_point['rel']
-  end
-  @entry_points.should == links.sort
-end
-
 Then /^each (\w+) should have '(.+)' attribute with valid (.+)$/ do |el, attr, t|
   case el
     when 'link':
diff --git a/tests/mock/step_definitions/instances_steps.rb b/tests/mock/step_definitions/instances_steps.rb
index 19c01dd..c18bfaa 100644
--- a/tests/mock/step_definitions/instances_steps.rb
+++ b/tests/mock/step_definitions/instances_steps.rb
@@ -31,15 +31,14 @@ When /^client want to '(\w+)' first instance$/ do |action|
 end
 
 When /^client follow link in actions$/ do
-  unless @instance_url
-    l = output_xml.xpath("/instances/instance[1]/actions/link[@rel = '#{@action}']").first
+
+  @instance ||= output_xml.xpath("/instance").first
+  l = @instance.xpath('actions/link[@rel="'+@action+'"]').first
+
+  if @action.eql?('destroy')
+    delete l[:href], { :id => @instance.xpath('@id').first.text }
   else
-    l = @instance.xpath('actions/link[@rel="'+@action+'"]').first
-  end
-  unless @action=='destroy'
     post l[:href], { :id => @instance.xpath('@id').first.text }
-  else
-    delete l[:href], { :id => @instance.xpath('@id').first.text }
   end
   last_response.status.should_not == 500
 end
@@ -100,9 +99,10 @@ Then /^client should get created instance$/ do
 end
 
 When /^this instance should be destroyed$/ do
-  # TODO: Fix this bug in mock driver ?
-  #get @instance_url, {}
-  #last_response.status.should == 404
+  puts @instance[:href].to_s
+  get @instance[:href].to_s, {}
+  last_response.status.should == 404
+  output_xml.xpath('/error').first[:status].should == '404'
 end
 
 Then /^client should get HTML form$/ do
diff --git a/tests/mock/step_definitions/respond_to_steps.rb b/tests/mock/step_definitions/respond_to_steps.rb
new file mode 100644
index 0000000..a354a80
--- /dev/null
+++ b/tests/mock/step_definitions/respond_to_steps.rb
@@ -0,0 +1,33 @@
+When /^client use (\w+) header:$/ do |name, table|
+  accept_header = table.raw.flatten.sort
+  @no_header = true
+  header name, accept_header.first.strip
+end
+
+When /^client perform an HTTP request for this URI$/ do
+  @params ||= {}
+  get @uri, @params
+end
+
+Then /^client should get valid (HTML|JSON|XML|PNG) response$/ do |format|
+  format = format.strip.downcase
+  if format.eql?('html')
+    last_response.content_type.should =~ /text\/html/
+  elsif format.eql?('xml')
+    last_response.content_type.should =~ /application\/xml/
+    Nokogiri::XML(last_response.body).xpath('/api').size.should_not == 0
+  elsif format.eql?('json')
+    last_response.content_type.should =~ /application\/json/
+    JSON::parse(last_response.body).class.to_s.should == 'Hash'
+  end
+end
+
+When /^client accept this URI with parameters:$/ do |table|
+  p = table.raw.flatten.sort
+  @params ||= {}
+  @params[p.first.to_sym] = p.last
+end
+
+When /^client wants to get URI '(.*)'$/ do |uri|
+  @uri = uri
+end
diff --git a/tests/mock/support/env.rb b/tests/mock/support/env.rb
index 68b6a56..bda4b90 100644
--- a/tests/mock/support/env.rb
+++ b/tests/mock/support/env.rb
@@ -1,10 +1,9 @@
 SERVER_DIR = File::expand_path(File::join(File::dirname(__FILE__), "../../../server"))
 Dir.chdir(SERVER_DIR)
-
-require 'sinatra'
-require 'rack/test'
+require 'rubygems'
 require 'nokogiri'
 require '../server/server'
+require 'rack/test'
 
 Sinatra::Application.set :environment, :test
 Sinatra::Application.set :root, SERVER_DIR
@@ -14,12 +13,18 @@ CONFIG = {
   :password => 'mockpassword'
 }
 
+ENV['RACK_ENV']     = 'test'
+
 World do
 
+  include Rack::Test::Methods
+
   def app
     @app = Rack::Builder.new do
-      set :logging, true
-      set :raise_errors, true
+      set :environment => :test
+      set :loggining => true
+      set :raise_errors => true
+      set :show_exceptions => true
       run Sinatra::Application
     end
   end
@@ -30,10 +35,9 @@ World do
 
   Before do
     unless @no_header
-      header 'Accept', 'application/xml'
+      header 'Accept', 'application/xml;q=9'
     end
   end
 
-  include Rack::Test::Methods
 end
 
-- 
1.7.2.3


Mime
View raw message