Skip to content

Commit

Permalink
Merge pull request bitbucket-rest-api#48 from dmgarland/master
Browse files Browse the repository at this point in the history
RSpec additions
  • Loading branch information
dmgarland committed Mar 14, 2016
2 parents 38ceb2c + a1c50d3 commit 8373f38
Show file tree
Hide file tree
Showing 15 changed files with 523 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ log
*.sqlite3
features/settings.yml
*.gem

.tags
TAGS
.tags_sorted_by_file
coverage
6 changes: 0 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
source "http://rubygems.org"

gemspec

group :development, :test do
gem 'rb-fsevent', :require => false if RUBY_PLATFORM =~ /darwin/i
gem 'growl', :require => false if RUBY_PLATFORM =~ /darwin/i
gem 'growl_notify', :require => false if RUBY_PLATFORM =~ /darwin/i
end
6 changes: 2 additions & 4 deletions bitbucket_rest_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ Gem::Specification.new do |gem|
gem.add_dependency 'simple_oauth'

gem.add_development_dependency 'rspec', '>= 0'
gem.add_development_dependency 'cucumber', '>= 0'
gem.add_development_dependency 'webmock', '~> 1.8.0'
gem.add_development_dependency 'vcr', '~> 2.2.0'
gem.add_development_dependency 'simplecov', '~> 0.6.1'
gem.add_development_dependency 'guard'
gem.add_development_dependency 'guard-rspec'
gem.add_development_dependency 'guard-cucumber'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'bundler'
gem.add_development_dependency 'pry'
gem.add_development_dependency 'mocha'
end
1 change: 1 addition & 0 deletions lib/bitbucket_rest_api/core_ext/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def except(*keys) # :nodoc:
self.dup.except!(*keys)
end unless method_defined?(:except)

# TODO except! should moodify self not a copy
def except!(*items) # :nodoc:
copy = self.dup
copy.reject! { |item| items.include? item }
Expand Down
1 change: 1 addition & 0 deletions lib/bitbucket_rest_api/core_ext/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def except(*items) # :nodoc:

def except!(*keys) # :nodoc:
copy = self.dup
# FIXME delete! is not a hash instance method
keys.each { |key| copy.delete!(key) }
copy
end unless method_defined?(:except!)
Expand Down
30 changes: 30 additions & 0 deletions spec/bitbucket_rest_api/api_factory_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# encoding: utf-8

require 'spec_helper'

describe BitBucket::ApiFactory do

subject(:factory) { described_class }

context '#new' do
it 'throws error if klass type is ommitted' do
expect { factory.new nil }.to raise_error(ArgumentError)
end

it 'instantiates a new object' do
expect(factory.new('Issues')).to be_a BitBucket::Issues
end
end

context '#create_instance' do
it 'creates class instance' do
expect(factory.create_instance('User', {})).to be_kind_of(BitBucket::User)
end
end

context '#convert_to_constant' do
it 'knows how to convert nested classes' do
expect(factory.convert_to_constant('Repos::Changesets')).to eql(BitBucket::Repos::Changesets)
end
end
end # BitBucket::ApiFactory
86 changes: 86 additions & 0 deletions spec/bitbucket_rest_api/api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require 'spec_helper'

describe BitBucket::API do
let(:setup_options) { { user: 'test_user' } }
let(:bitbucket_api) { described_class.new(setup_options) }
after do
[:user, :login, :password].each do |key|
bitbucket_api.send "clear_#{key}".to_sym
end
end

describe '#new' do
it 'passes options to bitbucket' do
described_class.new(setup_options)

expect(bitbucket_api.user).to eq(setup_options[:user])
end

context 'valid options' do
it 'sets valid options' do
setup_options = {
login: 'johnwick',
password: 'password'
}
bitbucket_api = described_class.new(setup_options)

expect(bitbucket_api.login).to eq('johnwick')
expect(bitbucket_api.password).to eq('password')
end

it 'ignores invalid options' do
setup_options = {
invalid_option: 'invalid option'
}

bitbucket_api = described_class.new(setup_options)

expect{ bitbucket_api.invalid_option }.to raise_error(NoMethodError)
end
end
end

describe '#method_missing' do
let(:setup_options) { { user: 'test_user' } }

it 'responds to attribute query' do
expect(bitbucket_api.user?).to eq(true)
end

it 'clears the attributes' do
bitbucket_api.clear_user

expect(bitbucket_api.user).to be_nil
end
end

describe '#_update_user_repo_params' do
it 'sets the username and repo_name' do
bitbucket_api._update_user_repo_params('test_user1', 'test_repo')

expect(bitbucket_api.user).to eq('test_user1')
expect(bitbucket_api.repo).to eq('test_repo')
end
end

describe '#_merge_user_into_params!' do
let(:params) { {} }

it 'takes a hash and merges user into it' do
bitbucket_api._merge_user_into_params!(params)

expect(params).to include('user')
end
end

describe '#_merge_user_repo_into_params!' do
let(:params) { {} }

it 'takes a hash and merges user into it' do
new_params = bitbucket_api._merge_user_repo_into_params!(params)

expect(new_params).to include('user')
expect(new_params).to include('repo')
end
end
end
59 changes: 59 additions & 0 deletions spec/bitbucket_rest_api/authorization_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'

describe BitBucket::Authorization do
let(:oauth_api) { BitBucket::API.new(oauth_token: 'example_oauth_token') }
let(:basic_auth_api) { BitBucket::API.new(basic_auth: 'example_login:example_password') }
let(:login_and_password_api) do
BitBucket::API.new(
login: 'example_login',
password: 'example_password',
basic_auth: nil
)
end

describe '#authenticated?' do
context 'context: oauth authentication' do
it 'should return true of oauth is used' do
expect(oauth_api.authenticated?).to eq(true)
end
end

context 'context: basic authentication' do
it 'should return true if basic authentication is used' do
expect(basic_auth_api.authenticated?).to eq(true)
end
end
end

describe '#basic_authed?' do
context 'context: with basic_auth' do
it 'should return true if basic_auth is set' do
expect(basic_auth_api.basic_authed?).to eq(true)
end
end

context 'context: with login and password' do
it 'should return true if a login and password are set' do
expect(login_and_password_api.basic_authed?).to eq(true)
end
end
end

describe '#authentication' do
context 'context: with basic_auth' do
it 'should return a hash containing the value for :basic_auth' do
expectation = { basic_auth: 'example_login:example_password' }

expect(basic_auth_api.authentication).to eq(expectation)
end
end

context 'context: with login and password' do
it 'should return a hash containing values for :login and :password' do
expectation = { login: 'example_login', password: 'example_password' }

expect(login_and_password_api.authentication).to eq(expectation)
end
end
end
end
30 changes: 30 additions & 0 deletions spec/bitbucket_rest_api/core_ext/array_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'
require 'bitbucket_rest_api/core_ext/array'

describe Array do
let(:array) { [:a, :b, :c, :d, { key: :value }] }
describe '#except' do
it 'removes the keys' do
new_array = array.except(:a, :b)

expect(new_array).to_not include(:a)
expect(new_array).to_not include(:b)
end
end

describe '#except!' do
xit 'removes the keys from the self' do
array = [:a, :b, :c, :d]
array.except!(:a, :b)

expect(array).to_not include(:a)
expect(array).to_not include(:b)
end
end

describe '#extract_options!' do
it 'selects a hash from the arguments list' do
expect(array.extract_options!).to eq({ key: :value })
end
end
end
67 changes: 67 additions & 0 deletions spec/bitbucket_rest_api/core_ext/hash_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# encoding: utf-8

require 'spec_helper'

describe Hash do

before do
BitBucket.new
@hash = { :a => 1, :b => 2, :c => 'e'}
@serialized = "a=1&b=2&c=e"
@nested_hash = { 'a' => { 'b' => {'c' => 1 } } }
@symbols = { :a => { :b => { :c => 1 } } }
end

context '#except!' do
# TODO fix this test after fixing except!
xit 'should respond to except!' do
@nested_hash.should respond_to :except!
copy = @nested_hash.dup
copy.except!('b', 'a')
copy.should be_empty
end
end

context '#except' do
it 'should respond to except' do
@nested_hash.should respond_to :except
end

# TODO fix this test after fixing except!
xit 'should remove key from the hash' do
@nested_hash.except('a').should be_empty
end
end

context '#symbolize_keys' do
it 'should respond to symbolize_keys' do
@nested_hash.should respond_to :symbolize_keys
end
end

context '#symbolize_keys!' do
it 'should respond to symbolize_keys!' do
@nested_hash.should respond_to :symbolize_keys!
end

it 'should convert nested keys to symbols' do
@nested_hash.symbolize_keys!.should == @symbols
end
end

context '#serialize' do
it 'should respond to serialize' do
@nested_hash.should respond_to :serialize
end

it 'should serialize hash' do
@hash.serialize.should == @serialized
end
end

context '#deep_key?' do
it 'should find key inside nested hash' do
@nested_hash.has_deep_key?('c').should be_truthy
end
end
end # Hash
30 changes: 30 additions & 0 deletions spec/bitbucket_rest_api/normalizer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# encoding: utf-8

require 'spec_helper'
require 'bitbucket_rest_api/core_ext/hash'

describe BitBucket::Normalizer, '#normalize!' do
let(:hash) { { 'a' => { :b => { 'c' => 1 }, 'd' => ['a', { :e => 2 }] } } }

let(:klass) do
Class.new do
include BitBucket::Normalizer
end
end

subject(:instance) { klass.new }

context '#normalize!' do
it 'converts hash keys to string' do
['a', 'b', 'c'].each do |key|
expect(subject.normalize!(hash).has_deep_key?(key)).to eq(true)
end
end

it 'should stringify all the keys inside nested hash' do
actual = subject.normalize! hash
expected = { 'a' => { 'b'=> { 'c' => 1 }, 'd' => ['a', { 'e'=> 2 }] } }
actual.should be_eql expected
end
end
end # BitBucket::Normalizer
31 changes: 31 additions & 0 deletions spec/bitbucket_rest_api/parameter_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'
require 'bitbucket_rest_api/core_ext/hash'

describe BitBucket::ParameterFilter, '#filter!' do
let(:hash) { { :a => { :b => { :c => 1 } } } }

let(:klass) {
Class.new do
include BitBucket::ParameterFilter
end
}

subject(:instance) { klass.new }

it 'removes unwanted keys from hash' do
instance.filter!([:a], hash)
expect(hash.has_deep_key?(:a)).to eq(true)
expect(hash.has_deep_key?(:b)).to eq(false)
expect(hash.has_deep_key?(:c)).to eq(false)
end

it 'recursively filters inputs tree' do
instance.filter!([:a, :b], hash)
expect(hash.has_deep_key?(:c)).to eq(false)
end

it 'filters inputs tree only on top level' do
instance.filter!([:a, :b], hash, :recursive => false)
expect(hash.has_deep_key?(:c)).to eq(true)
end
end
Loading

0 comments on commit 8373f38

Please sign in to comment.