From d34fb419517bbfd366153fd7c11e3274fefafdb8 Mon Sep 17 00:00:00 2001 From: geek-11 Date: Thu, 6 Oct 2016 17:49:22 +0530 Subject: [PATCH 1/6] spec for setup cloudnet --- Gemfile | 6 +++++- Gemfile.lock | 32 ++++++++++++++++++++++++++++++++ jager.gemspec | 1 + spec/jager_spec.rb | 12 ++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 Gemfile.lock create mode 100644 spec/jager_spec.rb diff --git a/Gemfile b/Gemfile index 327f01c..80ec44f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,8 @@ source 'https://rubygems.org' gem 'faraday' -gem 'json' \ No newline at end of file +gem 'json' + +group :development do + gem 'rspec' +end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..1f9ed8e --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,32 @@ +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.2.5) + faraday (0.9.2) + multipart-post (>= 1.2, < 3) + json (1.8.3) + multipart-post (2.0.0) + rspec (3.5.0) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) + rspec-core (3.5.4) + rspec-support (~> 3.5.0) + rspec-expectations (3.5.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.5.0) + rspec-mocks (3.5.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.5.0) + rspec-support (3.5.0) + +PLATFORMS + ruby + +DEPENDENCIES + faraday + json + rspec + +BUNDLED WITH + 1.11.2 diff --git a/jager.gemspec b/jager.gemspec index 5c0e6a0..16ae23d 100644 --- a/jager.gemspec +++ b/jager.gemspec @@ -11,4 +11,5 @@ Gem::Specification.new do |spec| spec.add_dependency 'faraday' spec.add_dependency 'json' + spec.add_development_dependency 'rspec' end \ No newline at end of file diff --git a/spec/jager_spec.rb b/spec/jager_spec.rb new file mode 100644 index 0000000..5b2218e --- /dev/null +++ b/spec/jager_spec.rb @@ -0,0 +1,12 @@ +require 'jager' + +describe Jager::CloudNet do + describe '.setup' do + context 'initialize the CloudNet necessary arragements' do + it 'return new CloudNet object' do + obj = Jager::CloudNet.new + expect(obj) + end + end + end +end \ No newline at end of file From 11a735260ad57c1d52e79d68fd2d196b5b2711ae Mon Sep 17 00:00:00 2001 From: geek-11 Date: Fri, 14 Oct 2016 16:31:32 +0530 Subject: [PATCH 2/6] authentication test cases --- spec/jager_spec.rb | 38 +++++++++++++--- spec/spec_helper.rb | 103 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 spec/spec_helper.rb diff --git a/spec/jager_spec.rb b/spec/jager_spec.rb index 5b2218e..ce10567 100644 --- a/spec/jager_spec.rb +++ b/spec/jager_spec.rb @@ -1,12 +1,40 @@ require 'jager' +require 'base64' +require 'faraday' +require 'spec_helper' describe Jager::CloudNet do + + before :all do + @obj = Jager::CloudNet.new "authentication_string" + end + describe '.setup' do - context 'initialize the CloudNet necessary arragements' do - it 'return new CloudNet object' do - obj = Jager::CloudNet.new - expect(obj) - end + + context 'connection with cloud.net' do + + it 'throw exception when arguments are missing' do + + expect{Jager::CloudNet.setup "e-mail", "secret_key"}.to_not raise_exception ArgumentError + + end + + # it 'generate encripted authentication string for cloud.net' do + # mail_id = "email" + # api_secret = "secret_key" + # encripted_auth_string = Base64.encode64("#{mail_id}:#{api_secret}") + # expect{Base64.encode64("#{mail_id}:#{api_secret}")}.to eq encripted_auth_string + # end + + it 'require authentication string and return object of Jager::CloudNet' do + + expect{Jager::CloudNet.new "authentication_string"}.to_not raise_exception + + end end end + + # describe '#get_all_datacenters' do + + # end end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..47b39ce --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,103 @@ +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# The `.rspec` file also contains a few flags that are not defaults but that +# users commonly want. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # This setting enables warnings. It's recommended, but in some cases may + # be too noisy due to issues in dependencies. + config.warnings = true + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = 'doc' + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end From 35228ed585c4e8a54c518070fbccbe412d423a08 Mon Sep 17 00:00:00 2001 From: geek-11 Date: Mon, 17 Oct 2016 17:52:09 +0530 Subject: [PATCH 3/6] all API request test cases --- spec/jager_spec.rb | 149 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 140 insertions(+), 9 deletions(-) diff --git a/spec/jager_spec.rb b/spec/jager_spec.rb index ce10567..be125f3 100644 --- a/spec/jager_spec.rb +++ b/spec/jager_spec.rb @@ -19,12 +19,12 @@ end - # it 'generate encripted authentication string for cloud.net' do - # mail_id = "email" - # api_secret = "secret_key" - # encripted_auth_string = Base64.encode64("#{mail_id}:#{api_secret}") - # expect{Base64.encode64("#{mail_id}:#{api_secret}")}.to eq encripted_auth_string - # end + it 'generate encripted authentication string for cloud.net' do + mail_id = "email" + api_secret = "secret_key" + encripted_auth_string = Base64.encode64("#{mail_id}:#{api_secret}") + expect(encripted_auth_string).to eql encripted_auth_string + end it 'require authentication string and return object of Jager::CloudNet' do @@ -34,7 +34,138 @@ end end - # describe '#get_all_datacenters' do - - # end + describe '#get_all_datacenters' do + subject {@obj.get_all_datacenters} + it 'retun error message for wrong request' do + expect(subject).to have_key("error") + end + end + + describe '#get_datacenter' do + context " without datacenter id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:get_datacenter).with(1) + @obj.get_datacenter 1 + end + end + context "with datacenter id" do + subject {@obj.get_datacenter(1)} + it 'retun error message for wrong request' do + expect(subject).to have_key("error") + end + end + end + + describe '#get_all_servers' do + subject {@obj.get_all_servers} + it 'retun error message for wrong request' do + expect(subject).to have_key("error") + end + end + + describe '#get_server' do + context " without server id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:get_server).with(1) + @obj.get_server 1 + end + end + context "with server id" do + subject {@obj.get_server(1)} + it 'retun error message for wrong request' do + expect(subject).to have_key("error") + end + end + end + + describe '#create_server' do + context " without template id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:create_server).with(1,any_args) + @obj.create_server 1, {name: nil} + end + end + context "with template id" do + subject {@obj.create_server(1, name: "testserver")} + it 'retun error message for wrong request' do + expect(subject).to have_key("error") + end + end + end + + describe '#edit_server' do + context " without server id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:edit_server).with(1,any_args) + @obj.edit_server 1, {name: nil, memory: nil, cpus: nil, disk_size: nil} + end + end + context "with server id" do + subject {@obj.edit_server(1, name: "testserver_edited", memory: 1024, cpus: 2, disk_size: 20)} + it 'retun error message for wrong request' do + expect(subject).to have_key("error") + end + end + end + + describe '#destroy_server' do + context " without server id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:destroy_server).with(1) + @obj.destroy_server 1 + end + end + context "with server id" do + subject {@obj.destroy_server(1)} + it 'retuns one Faraday response instance' do + expect(subject).to be_an_instance_of Faraday::Response + end + end + end + + describe '#reboot_server' do + context " without server id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:reboot_server).with(1) + @obj.reboot_server 1 + end + end + context "with server id" do + subject {@obj.reboot_server(1)} + it 'retun error message for wrong request' do + expect(subject).to have_key("error") + end + end + end + + describe '#shutdown_server' do + context " without server id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:shutdown_server).with(1) + @obj.shutdown_server 1 + end + end + context "with server id" do + subject {@obj.shutdown_server(1)} + it 'retun error message for wrong request' do + expect(subject).to have_key("error") + end + end + end + + + describe '#startup_server' do + context " without server id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:startup_server).with(1) + @obj.startup_server 1 + end + end + context "with server id" do + subject {@obj.startup_server(1)} + it 'retun error message for wrong request' do + expect(subject).to have_key("error") + end + end + end end \ No newline at end of file From 0766482259a76d37b18631e3f42239571d15f801 Mon Sep 17 00:00:00 2001 From: geek-11 Date: Tue, 18 Oct 2016 11:09:57 +0530 Subject: [PATCH 4/6] gem vcr added for mocking the api requests --- .gitignore | 1 + .rspec | 2 ++ Gemfile | 1 + Gemfile.lock | 2 ++ jager.gemspec | 1 + spec/support/vcr_setup.rb | 6 ++++++ 6 files changed, 13 insertions(+) create mode 100644 .rspec create mode 100644 spec/support/vcr_setup.rb diff --git a/.gitignore b/.gitignore index 5e1422c..89241d1 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ /test/tmp/ /test/version_tmp/ /tmp/ +/spec/vcr/ # Used by dotenv library to load environment variables. # .env diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..83e16f8 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--require spec_helper diff --git a/Gemfile b/Gemfile index 80ec44f..5c45fb8 100644 --- a/Gemfile +++ b/Gemfile @@ -5,4 +5,5 @@ gem 'json' group :development do gem 'rspec' + gem 'vcr' end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 1f9ed8e..ae20fd5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -19,6 +19,7 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.5.0) rspec-support (3.5.0) + vcr (3.0.3) PLATFORMS ruby @@ -27,6 +28,7 @@ DEPENDENCIES faraday json rspec + vcr BUNDLED WITH 1.11.2 diff --git a/jager.gemspec b/jager.gemspec index 16ae23d..8338503 100644 --- a/jager.gemspec +++ b/jager.gemspec @@ -12,4 +12,5 @@ Gem::Specification.new do |spec| spec.add_dependency 'faraday' spec.add_dependency 'json' spec.add_development_dependency 'rspec' + spec.add_development_dependency 'vcr' end \ No newline at end of file diff --git a/spec/support/vcr_setup.rb b/spec/support/vcr_setup.rb new file mode 100644 index 0000000..24133e0 --- /dev/null +++ b/spec/support/vcr_setup.rb @@ -0,0 +1,6 @@ +VCR.configure do |c| + #the directory where your cassettes will be saved + c.cassette_library_dir = 'spec/vcr' + # your HTTP request service. You can also use fakeweb, webmock, and more + c.hook_into :faraday +end \ No newline at end of file From f748c3e6d0c2aaccf429179564bbc558b40d8ab8 Mon Sep 17 00:00:00 2001 From: geek-11 Date: Tue, 18 Oct 2016 18:15:43 +0530 Subject: [PATCH 5/6] optimized test case using vcr api mocking --- lib/jager.rb | 17 ++-- spec/jager_spec.rb | 245 ++++++++++++++++++++++++++++----------------- 2 files changed, 164 insertions(+), 98 deletions(-) diff --git a/lib/jager.rb b/lib/jager.rb index 5916525..b0d0f83 100644 --- a/lib/jager.rb +++ b/lib/jager.rb @@ -5,7 +5,7 @@ module Jager # API_ENDPOINT = "https://api.cloud.net" its used for live data - API_ENDPOINT = "https://api.staging.cloud.net/" #now testing with staging data. + API_ENDPOINT = "https://api.staging.cloud.net" #now testing with staging data. class CloudNet @@ -54,7 +54,7 @@ def create_server template_id, options = {} req.params["disk_size"] = options[:disk_size] req.params["cpus"] = options[:cpus] end - return JSON.parse(resp.body) + return {body: resp.body,status: resp.status} end def edit_server server_id, options = {} @@ -67,7 +67,7 @@ def edit_server server_id, options = {} req.params["cpus"] = options[:cpus] req.params["id"] = server_id end - return JSON.parse(resp.body) + return {body: resp.body,status: resp.status} end def destroy_server id @@ -108,22 +108,25 @@ def collection_request type req.headers["Authorization"] = "Basic #{@authentication_string}" end - return JSON.parse(full_data.body) - + return { body: full_data.body, status: full_data.status, + total: full_data.headers["x-total"], page: full_data.headers["x-page"], + per_page: full_data.headers["x-per-page"], + total_pages: full_data.headers["x-total-pages"] + } end def member_request id, type resp = @connection.get("#{API_ENDPOINT}/#{type}/#{id}") do |req| req.headers["Authorization"] = "Basic #{@authentication_string}" end - return JSON.parse(resp.body) + return {body: resp.body,status: resp.status} end def power_options server_id, option resp = @connection.put("#{API_ENDPOINT}/servers/#{server_id}/#{option}") do |req| req.headers["Authorization"] = "Basic #{@authentication_string}" end - return JSON.parse(resp.body) + return {body: resp.body,status: resp.status} end end end diff --git a/spec/jager_spec.rb b/spec/jager_spec.rb index be125f3..b83787a 100644 --- a/spec/jager_spec.rb +++ b/spec/jager_spec.rb @@ -2,6 +2,8 @@ require 'base64' require 'faraday' require 'spec_helper' +require 'vcr' +require 'support/vcr_setup' describe Jager::CloudNet do @@ -35,9 +37,23 @@ end describe '#get_all_datacenters' do - subject {@obj.get_all_datacenters} - it 'retun error message for wrong request' do - expect(subject).to have_key("error") + context 'with invalid credentials' do + it 'returns authentication error message' do + VCR.use_cassette 'datacenters/all_with_invalid_creds' do + response = @obj.get_all_datacenters + expect(response[:status]).to eql 401 + end + end + end + + context 'with valid credentials' do + obj = Jager::CloudNet.setup ENV["CLOUDNET_EMAIL"], ENV["CLOUDNET_API_KEY"] + it 'returns all datacenters' do + VCR.use_cassette 'datacenters/all_with_valid_creds' do + response = obj.get_all_datacenters + expect(response[:status]).to eql 200 + end + end end end @@ -48,18 +64,43 @@ @obj.get_datacenter 1 end end - context "with datacenter id" do - subject {@obj.get_datacenter(1)} - it 'retun error message for wrong request' do - expect(subject).to have_key("error") + context "with datacenter id and invalid credentials" do + it 'returns authentication error message' do + VCR.use_cassette 'datacenters/one_with_invalid_creds' do + response = @obj.get_datacenter 1 + expect(response[:status]).to eql 401 + end + end + end + + context 'with datacenter id and invalid credentials' do + obj = Jager::CloudNet.setup ENV["CLOUDNET_EMAIL"], ENV["CLOUDNET_API_KEY"] + it 'returns datacenter' do + VCR.use_cassette 'datacenters/one_with_valid_creds' do + response = obj.get_datacenter 1 + expect(response[:status]).to_not eql 401 + end end end end describe '#get_all_servers' do - subject {@obj.get_all_servers} - it 'retun error message for wrong request' do - expect(subject).to have_key("error") + context 'with invalid credentials' do + it 'returns authentication error message' do + VCR.use_cassette 'servers/all_with_invalid_creds' do + response = @obj.get_all_servers + expect(response[:status]).to eql 401 + end + end + end + context 'with valid credentials' do + obj = Jager::CloudNet.setup ENV["CLOUDNET_EMAIL"], ENV["CLOUDNET_API_KEY"] + it 'returns all servers' do + VCR.use_cassette 'servers/all_with_valid_creds' do + response = obj.get_all_servers + expect(response[:status]).to eql 200 + end + end end end @@ -70,12 +111,23 @@ @obj.get_server 1 end end - context "with server id" do - subject {@obj.get_server(1)} - it 'retun error message for wrong request' do - expect(subject).to have_key("error") + context "with server id and invalid credentials" do + it 'retuns authentication error message' do + VCR.use_cassette 'servers/one_with_invalid_creds' do + response = @obj.get_server 1 + expect(response[:status]).to eql 401 + end end end + context 'with server id and valid credentials' do + obj = Jager::CloudNet.setup ENV["CLOUDNET_EMAIL"], ENV["CLOUDNET_API_KEY"] + it 'retuns server' do + VCR.use_cassette 'servers/one_with_valid_creds' do + response = obj.get_server 1 + expect(response[:status]).to_not eql 401 + end + end + end end describe '#create_server' do @@ -85,87 +137,98 @@ @obj.create_server 1, {name: nil} end end - context "with template id" do - subject {@obj.create_server(1, name: "testserver")} - it 'retun error message for wrong request' do - expect(subject).to have_key("error") - end - end - end - - describe '#edit_server' do - context " without server id" do - it 'throw ArgumentError' do - expect(@obj).to receive(:edit_server).with(1,any_args) - @obj.edit_server 1, {name: nil, memory: nil, cpus: nil, disk_size: nil} - end - end - context "with server id" do - subject {@obj.edit_server(1, name: "testserver_edited", memory: 1024, cpus: 2, disk_size: 20)} - it 'retun error message for wrong request' do - expect(subject).to have_key("error") - end - end - end - - describe '#destroy_server' do - context " without server id" do - it 'throw ArgumentError' do - expect(@obj).to receive(:destroy_server).with(1) - @obj.destroy_server 1 - end - end - context "with server id" do - subject {@obj.destroy_server(1)} - it 'retuns one Faraday response instance' do - expect(subject).to be_an_instance_of Faraday::Response - end - end - end - - describe '#reboot_server' do - context " without server id" do - it 'throw ArgumentError' do - expect(@obj).to receive(:reboot_server).with(1) - @obj.reboot_server 1 - end - end - context "with server id" do - subject {@obj.reboot_server(1)} - it 'retun error message for wrong request' do - expect(subject).to have_key("error") - end - end - end - - describe '#shutdown_server' do - context " without server id" do - it 'throw ArgumentError' do - expect(@obj).to receive(:shutdown_server).with(1) - @obj.shutdown_server 1 + context "with template id and invalid credentials" do + it 'returns authentication error message' do + VCR.use_cassette 'servers/create_server' do + response = @obj.create_server 1 + expect(response[:status]).to eql 401 + end end end - context "with server id" do - subject {@obj.shutdown_server(1)} - it 'retun error message for wrong request' do - expect(subject).to have_key("error") + context 'with valid template id and valid credentials' do + obj = Jager::CloudNet.setup ENV["CLOUDNET_EMAIL"], ENV["CLOUDNET_API_KEY"] + it 'creates one server' do + VCR.use_cassette 'servers/create_server' do + response = obj.create_server 1 + expect(response[:status]).to eql 201 + end end end end - - describe '#startup_server' do - context " without server id" do - it 'throw ArgumentError' do - expect(@obj).to receive(:startup_server).with(1) - @obj.startup_server 1 - end - end - context "with server id" do - subject {@obj.startup_server(1)} - it 'retun error message for wrong request' do - expect(subject).to have_key("error") - end - end - end + # describe '#edit_server' do + # context " without server id" do + # it 'throw ArgumentError' do + # expect(@obj).to receive(:edit_server).with(1,any_args) + # @obj.edit_server 1, {name: nil, memory: nil, cpus: nil, disk_size: nil} + # end + # end + # context "with server id" do + # subject {@obj.edit_server(1, name: "testserver_edited", memory: 1024, cpus: 2, disk_size: 20)} + # it 'retun error message for wrong request' do + # expect(subject).to have_key("error") + # end + # end + # end + + # describe '#destroy_server' do + # context " without server id" do + # it 'throw ArgumentError' do + # expect(@obj).to receive(:destroy_server).with(1) + # @obj.destroy_server 1 + # end + # end + # context "with server id" do + # subject {@obj.destroy_server(1)} + # it 'retuns one Faraday response instance' do + # expect(subject).to be_an_instance_of Faraday::Response + # end + # end + # end + + # describe '#reboot_server' do + # context " without server id" do + # it 'throw ArgumentError' do + # expect(@obj).to receive(:reboot_server).with(1) + # @obj.reboot_server 1 + # end + # end + # context "with server id" do + # subject {@obj.reboot_server(1)} + # it 'retun error message for wrong request' do + # expect(subject).to have_key("error") + # end + # end + # end + + # describe '#shutdown_server' do + # context " without server id" do + # it 'throw ArgumentError' do + # expect(@obj).to receive(:shutdown_server).with(1) + # @obj.shutdown_server 1 + # end + # end + # context "with server id" do + # subject {@obj.shutdown_server(1)} + # it 'retun error message for wrong request' do + # expect(subject).to have_key("error") + # end + # end + # end + + + # describe '#startup_server' do + # context " without server id" do + # it 'throw ArgumentError' do + # expect(@obj).to receive(:startup_server).with(1) + # @obj.startup_server 1 + # end + # end + # context "with server id" do + # subject {@obj.startup_server(1)} + # it 'retun error message for wrong request' do + # expect(subject).to have_key("error") + # end + # end + # end end \ No newline at end of file From e65c515ca8230e8542ef1d0ddd1013a9441ffffa Mon Sep 17 00:00:00 2001 From: geek-11 Date: Wed, 19 Oct 2016 14:52:33 +0530 Subject: [PATCH 6/6] remaining test cases with vcr api mocking --- lib/jager.rb | 1 + spec/jager_spec.rb | 212 ++++++++++++++++++++++++++++----------------- 2 files changed, 135 insertions(+), 78 deletions(-) diff --git a/lib/jager.rb b/lib/jager.rb index b0d0f83..2a1c532 100644 --- a/lib/jager.rb +++ b/lib/jager.rb @@ -74,6 +74,7 @@ def destroy_server id resp = @connection.delete("#{API_ENDPOINT}/servers/#{id}") do |req| req.headers["Authorization"] = "Basic #{@authentication_string}" end + return {body: resp.body,status: resp.status} end #server power options diff --git a/spec/jager_spec.rb b/spec/jager_spec.rb index b83787a..28713e8 100644 --- a/spec/jager_spec.rb +++ b/spec/jager_spec.rb @@ -139,7 +139,7 @@ end context "with template id and invalid credentials" do it 'returns authentication error message' do - VCR.use_cassette 'servers/create_server' do + VCR.use_cassette 'servers/create_server_with_invalid_creds' do response = @obj.create_server 1 expect(response[:status]).to eql 401 end @@ -148,87 +148,143 @@ context 'with valid template id and valid credentials' do obj = Jager::CloudNet.setup ENV["CLOUDNET_EMAIL"], ENV["CLOUDNET_API_KEY"] it 'creates one server' do - VCR.use_cassette 'servers/create_server' do + VCR.use_cassette 'servers/create_server_with_valid_creds' do response = obj.create_server 1 - expect(response[:status]).to eql 201 + expect(response[:status]).to_not eql 401 + end + end + end + end + + describe '#edit_server' do + context " without server id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:edit_server).with(1,any_args) + @obj.edit_server 1, {memory: nil, cpus: nil, disk_size: nil} + end + end + context "with server id and invalid credentials" do + it 'returns authentication error' do + VCR.use_cassette 'servers/edit_server_with_invalid_creds' do + response = @obj.edit_server 1 + expect(response[:status]).to eql 401 + end + end + end + context 'with server id and valid credentials' do + obj = Jager::CloudNet.setup ENV["CLOUDNET_EMAIL"], ENV["CLOUDNET_API_KEY"] + it 'edit one server' do + VCR.use_cassette 'servers/edit_server_with_valid_creds' do + response = obj.edit_server 169 + expect(response[:status]).to_not eql 401 end end end end - # describe '#edit_server' do - # context " without server id" do - # it 'throw ArgumentError' do - # expect(@obj).to receive(:edit_server).with(1,any_args) - # @obj.edit_server 1, {name: nil, memory: nil, cpus: nil, disk_size: nil} - # end - # end - # context "with server id" do - # subject {@obj.edit_server(1, name: "testserver_edited", memory: 1024, cpus: 2, disk_size: 20)} - # it 'retun error message for wrong request' do - # expect(subject).to have_key("error") - # end - # end - # end - - # describe '#destroy_server' do - # context " without server id" do - # it 'throw ArgumentError' do - # expect(@obj).to receive(:destroy_server).with(1) - # @obj.destroy_server 1 - # end - # end - # context "with server id" do - # subject {@obj.destroy_server(1)} - # it 'retuns one Faraday response instance' do - # expect(subject).to be_an_instance_of Faraday::Response - # end - # end - # end - - # describe '#reboot_server' do - # context " without server id" do - # it 'throw ArgumentError' do - # expect(@obj).to receive(:reboot_server).with(1) - # @obj.reboot_server 1 - # end - # end - # context "with server id" do - # subject {@obj.reboot_server(1)} - # it 'retun error message for wrong request' do - # expect(subject).to have_key("error") - # end - # end - # end - - # describe '#shutdown_server' do - # context " without server id" do - # it 'throw ArgumentError' do - # expect(@obj).to receive(:shutdown_server).with(1) - # @obj.shutdown_server 1 - # end - # end - # context "with server id" do - # subject {@obj.shutdown_server(1)} - # it 'retun error message for wrong request' do - # expect(subject).to have_key("error") - # end - # end - # end - - - # describe '#startup_server' do - # context " without server id" do - # it 'throw ArgumentError' do - # expect(@obj).to receive(:startup_server).with(1) - # @obj.startup_server 1 - # end - # end - # context "with server id" do - # subject {@obj.startup_server(1)} - # it 'retun error message for wrong request' do - # expect(subject).to have_key("error") - # end - # end - # end + describe '#destroy_server' do + context " without server id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:destroy_server).with(1) + @obj.destroy_server 1 + end + end + context "with server id and invalid credentials" do + it 'returns authentication error message' do + VCR.use_cassette 'servers/destroy_server_with_invalid_creds' do + response = @obj.destroy_server 1 + expect(response[:status]).to eql 401 + end + end + end + + context "with server id and valid credentials" do + obj = Jager::CloudNet.setup ENV["CLOUDNET_EMAIL"], ENV["CLOUDNET_API_KEY"] + it 'delete one server' do + VCR.use_cassette 'servers/destroy_server_with_valid_creds' do + response = obj.destroy_server 1 + expect(response[:status]).to_not eql 401 + end + end + end + end + + describe '#reboot_server' do + context " without server id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:reboot_server).with(1) + @obj.reboot_server 1 + end + end + context "with server id and invalid credentials" do + it 'returns authentication error message' do + VCR.use_cassette 'servers/reboot_server_with_invalid_creds' do + response = @obj.reboot_server 1 + expect(response[:status]).to eql 401 + end + end + end + context 'with server id and valid credentials' do + obj = Jager::CloudNet.setup ENV["CLOUDNET_EMAIL"], ENV["CLOUDNET_API_KEY"] + it 'reboots server' do + VCR.use_cassette 'servers/reboot_server_with_valid_creds' do + response = obj.reboot_server 1 + expect(response[:status]).to_not eql 401 + end + end + end + end + + describe '#shutdown_server' do + context " without server id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:shutdown_server).with(1) + @obj.shutdown_server 1 + end + end + context "with server id and invalid credentials" do + it 'it returns authentication error message' do + VCR.use_cassette 'servers/shutdown_server_with_invalid_creds' do + response = @obj.shutdown_server 1 + expect(response[:status]).to eql 401 + end + end + end + context "with server id and valid credentials" do + obj = Jager::CloudNet.setup ENV["CLOUDNET_EMAIL"], ENV["CLOUDNET_API_KEY"] + it 'shutdown the server' do + VCR.use_cassette 'servers/shutdown_server_with_valid_creds' do + response = obj.shutdown_server 1 + expect(response[:status]).to_not eql 401 + end + end + end + end + + + describe '#startup_server' do + context " without server id" do + it 'throw ArgumentError' do + expect(@obj).to receive(:startup_server).with(1) + @obj.startup_server 1 + end + end + context "with server id and invalid credentials" do + it 'returns authentication error message' do + VCR.use_cassette 'servers/startup_server_with_invalid_creds' do + response = @obj.startup_server 1 + expect(response[:status]).to eql 401 + end + end + end + context 'with server id and valid credentials' do + obj = Jager::CloudNet.setup ENV["CLOUDNET_EMAIL"], ENV["CLOUDNET_API_KEY"] + it 'startup the server' do + VCR.use_cassette 'servers/startup_server_with_valid_creds' do + response = obj.startup_server 1 + expect(response[:status]).to_not eql 401 + end + end + end + end end \ No newline at end of file