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 327f01c..5c45fb8 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,9 @@ source 'https://rubygems.org' gem 'faraday' -gem 'json' \ No newline at end of file +gem 'json' + +group :development do + gem 'rspec' + gem 'vcr' +end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..ae20fd5 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,34 @@ +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) + vcr (3.0.3) + +PLATFORMS + ruby + +DEPENDENCIES + faraday + json + rspec + vcr + +BUNDLED WITH + 1.11.2 diff --git a/jager.gemspec b/jager.gemspec index 5c0e6a0..8338503 100644 --- a/jager.gemspec +++ b/jager.gemspec @@ -11,4 +11,6 @@ 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/lib/jager.rb b/lib/jager.rb index 5916525..2a1c532 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,13 +67,14 @@ 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 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 @@ -108,22 +109,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 new file mode 100644 index 0000000..28713e8 --- /dev/null +++ b/spec/jager_spec.rb @@ -0,0 +1,290 @@ +require 'jager' +require 'base64' +require 'faraday' +require 'spec_helper' +require 'vcr' +require 'support/vcr_setup' + +describe Jager::CloudNet do + + before :all do + @obj = Jager::CloudNet.new "authentication_string" + end + + describe '.setup' do + + 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(encripted_auth_string).to eql 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 + 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 + + 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 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 + 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 + + 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 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 + 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 and invalid credentials" do + it 'returns authentication error message' do + VCR.use_cassette 'servers/create_server_with_invalid_creds' do + response = @obj.create_server 1 + expect(response[:status]).to eql 401 + end + end + end + 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_with_valid_creds' do + response = obj.create_server 1 + 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 '#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 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 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