Skip to content

Commit a1c50d3

Browse files
committed
Adding rspec tests
1 parent 8d37d63 commit a1c50d3

File tree

14 files changed

+505
-86
lines changed

14 files changed

+505
-86
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ log
1111
features/settings.yml
1212
*.gem
1313
.tags
14+
TAGS
1415
.tags_sorted_by_file
1516
coverage

bitbucket_rest_api.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ Gem::Specification.new do |gem|
2727
gem.add_development_dependency 'simplecov', '~> 0.6.1'
2828
gem.add_development_dependency 'rake'
2929
gem.add_development_dependency 'bundler'
30+
gem.add_development_dependency 'pry'
31+
gem.add_development_dependency 'mocha'
3032
end

lib/bitbucket_rest_api/core_ext/array.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ def except(*keys) # :nodoc:
44
self.dup.except!(*keys)
55
end unless method_defined?(:except)
66

7+
# TODO except! should moodify self not a copy
78
def except!(*items) # :nodoc:
89
copy = self.dup
910
copy.reject! { |item| items.include? item }

lib/bitbucket_rest_api/core_ext/hash.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def except(*items) # :nodoc:
66

77
def except!(*keys) # :nodoc:
88
copy = self.dup
9+
# FIXME delete! is not a hash instance method
910
keys.each { |key| copy.delete!(key) }
1011
copy
1112
end unless method_defined?(:except!)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe BitBucket::ApiFactory do
6+
7+
subject(:factory) { described_class }
8+
9+
context '#new' do
10+
it 'throws error if klass type is ommitted' do
11+
expect { factory.new nil }.to raise_error(ArgumentError)
12+
end
13+
14+
it 'instantiates a new object' do
15+
expect(factory.new('Issues')).to be_a BitBucket::Issues
16+
end
17+
end
18+
19+
context '#create_instance' do
20+
it 'creates class instance' do
21+
expect(factory.create_instance('User', {})).to be_kind_of(BitBucket::User)
22+
end
23+
end
24+
25+
context '#convert_to_constant' do
26+
it 'knows how to convert nested classes' do
27+
expect(factory.convert_to_constant('Repos::Changesets')).to eql(BitBucket::Repos::Changesets)
28+
end
29+
end
30+
end # BitBucket::ApiFactory
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
require 'spec_helper'
2+
3+
describe BitBucket::API do
4+
let(:setup_options) { { user: 'test_user' } }
5+
let(:bitbucket_api) { described_class.new(setup_options) }
6+
after do
7+
[:user, :login, :password].each do |key|
8+
bitbucket_api.send "clear_#{key}".to_sym
9+
end
10+
end
11+
12+
describe '#new' do
13+
it 'passes options to bitbucket' do
14+
described_class.new(setup_options)
15+
16+
expect(bitbucket_api.user).to eq(setup_options[:user])
17+
end
18+
19+
context 'valid options' do
20+
it 'sets valid options' do
21+
setup_options = {
22+
login: 'johnwick',
23+
password: 'password'
24+
}
25+
bitbucket_api = described_class.new(setup_options)
26+
27+
expect(bitbucket_api.login).to eq('johnwick')
28+
expect(bitbucket_api.password).to eq('password')
29+
end
30+
31+
it 'ignores invalid options' do
32+
setup_options = {
33+
invalid_option: 'invalid option'
34+
}
35+
36+
bitbucket_api = described_class.new(setup_options)
37+
38+
expect{ bitbucket_api.invalid_option }.to raise_error(NoMethodError)
39+
end
40+
end
41+
end
42+
43+
describe '#method_missing' do
44+
let(:setup_options) { { user: 'test_user' } }
45+
46+
it 'responds to attribute query' do
47+
expect(bitbucket_api.user?).to eq(true)
48+
end
49+
50+
it 'clears the attributes' do
51+
bitbucket_api.clear_user
52+
53+
expect(bitbucket_api.user).to be_nil
54+
end
55+
end
56+
57+
describe '#_update_user_repo_params' do
58+
it 'sets the username and repo_name' do
59+
bitbucket_api._update_user_repo_params('test_user1', 'test_repo')
60+
61+
expect(bitbucket_api.user).to eq('test_user1')
62+
expect(bitbucket_api.repo).to eq('test_repo')
63+
end
64+
end
65+
66+
describe '#_merge_user_into_params!' do
67+
let(:params) { {} }
68+
69+
it 'takes a hash and merges user into it' do
70+
bitbucket_api._merge_user_into_params!(params)
71+
72+
expect(params).to include('user')
73+
end
74+
end
75+
76+
describe '#_merge_user_repo_into_params!' do
77+
let(:params) { {} }
78+
79+
it 'takes a hash and merges user into it' do
80+
new_params = bitbucket_api._merge_user_repo_into_params!(params)
81+
82+
expect(new_params).to include('user')
83+
expect(new_params).to include('repo')
84+
end
85+
end
86+
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'spec_helper'
2+
3+
describe BitBucket::Authorization do
4+
let(:oauth_api) { BitBucket::API.new(oauth_token: 'example_oauth_token') }
5+
let(:basic_auth_api) { BitBucket::API.new(basic_auth: 'example_login:example_password') }
6+
let(:login_and_password_api) do
7+
BitBucket::API.new(
8+
login: 'example_login',
9+
password: 'example_password',
10+
basic_auth: nil
11+
)
12+
end
13+
14+
describe '#authenticated?' do
15+
context 'context: oauth authentication' do
16+
it 'should return true of oauth is used' do
17+
expect(oauth_api.authenticated?).to eq(true)
18+
end
19+
end
20+
21+
context 'context: basic authentication' do
22+
it 'should return true if basic authentication is used' do
23+
expect(basic_auth_api.authenticated?).to eq(true)
24+
end
25+
end
26+
end
27+
28+
describe '#basic_authed?' do
29+
context 'context: with basic_auth' do
30+
it 'should return true if basic_auth is set' do
31+
expect(basic_auth_api.basic_authed?).to eq(true)
32+
end
33+
end
34+
35+
context 'context: with login and password' do
36+
it 'should return true if a login and password are set' do
37+
expect(login_and_password_api.basic_authed?).to eq(true)
38+
end
39+
end
40+
end
41+
42+
describe '#authentication' do
43+
context 'context: with basic_auth' do
44+
it 'should return a hash containing the value for :basic_auth' do
45+
expectation = { basic_auth: 'example_login:example_password' }
46+
47+
expect(basic_auth_api.authentication).to eq(expectation)
48+
end
49+
end
50+
51+
context 'context: with login and password' do
52+
it 'should return a hash containing values for :login and :password' do
53+
expectation = { login: 'example_login', password: 'example_password' }
54+
55+
expect(login_and_password_api.authentication).to eq(expectation)
56+
end
57+
end
58+
end
59+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'spec_helper'
2+
require 'bitbucket_rest_api/core_ext/array'
3+
4+
describe Array do
5+
let(:array) { [:a, :b, :c, :d, { key: :value }] }
6+
describe '#except' do
7+
it 'removes the keys' do
8+
new_array = array.except(:a, :b)
9+
10+
expect(new_array).to_not include(:a)
11+
expect(new_array).to_not include(:b)
12+
end
13+
end
14+
15+
describe '#except!' do
16+
xit 'removes the keys from the self' do
17+
array = [:a, :b, :c, :d]
18+
array.except!(:a, :b)
19+
20+
expect(array).to_not include(:a)
21+
expect(array).to_not include(:b)
22+
end
23+
end
24+
25+
describe '#extract_options!' do
26+
it 'selects a hash from the arguments list' do
27+
expect(array.extract_options!).to eq({ key: :value })
28+
end
29+
end
30+
end
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Hash do
6+
7+
before do
8+
BitBucket.new
9+
@hash = { :a => 1, :b => 2, :c => 'e'}
10+
@serialized = "a=1&b=2&c=e"
11+
@nested_hash = { 'a' => { 'b' => {'c' => 1 } } }
12+
@symbols = { :a => { :b => { :c => 1 } } }
13+
end
14+
15+
context '#except!' do
16+
# TODO fix this test after fixing except!
17+
xit 'should respond to except!' do
18+
@nested_hash.should respond_to :except!
19+
copy = @nested_hash.dup
20+
copy.except!('b', 'a')
21+
copy.should be_empty
22+
end
23+
end
24+
25+
context '#except' do
26+
it 'should respond to except' do
27+
@nested_hash.should respond_to :except
28+
end
29+
30+
# TODO fix this test after fixing except!
31+
xit 'should remove key from the hash' do
32+
@nested_hash.except('a').should be_empty
33+
end
34+
end
35+
36+
context '#symbolize_keys' do
37+
it 'should respond to symbolize_keys' do
38+
@nested_hash.should respond_to :symbolize_keys
39+
end
40+
end
41+
42+
context '#symbolize_keys!' do
43+
it 'should respond to symbolize_keys!' do
44+
@nested_hash.should respond_to :symbolize_keys!
45+
end
46+
47+
it 'should convert nested keys to symbols' do
48+
@nested_hash.symbolize_keys!.should == @symbols
49+
end
50+
end
51+
52+
context '#serialize' do
53+
it 'should respond to serialize' do
54+
@nested_hash.should respond_to :serialize
55+
end
56+
57+
it 'should serialize hash' do
58+
@hash.serialize.should == @serialized
59+
end
60+
end
61+
62+
context '#deep_key?' do
63+
it 'should find key inside nested hash' do
64+
@nested_hash.has_deep_key?('c').should be_truthy
65+
end
66+
end
67+
end # Hash
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
require 'bitbucket_rest_api/core_ext/hash'
5+
6+
describe BitBucket::Normalizer, '#normalize!' do
7+
let(:hash) { { 'a' => { :b => { 'c' => 1 }, 'd' => ['a', { :e => 2 }] } } }
8+
9+
let(:klass) do
10+
Class.new do
11+
include BitBucket::Normalizer
12+
end
13+
end
14+
15+
subject(:instance) { klass.new }
16+
17+
context '#normalize!' do
18+
it 'converts hash keys to string' do
19+
['a', 'b', 'c'].each do |key|
20+
expect(subject.normalize!(hash).has_deep_key?(key)).to eq(true)
21+
end
22+
end
23+
24+
it 'should stringify all the keys inside nested hash' do
25+
actual = subject.normalize! hash
26+
expected = { 'a' => { 'b'=> { 'c' => 1 }, 'd' => ['a', { 'e'=> 2 }] } }
27+
actual.should be_eql expected
28+
end
29+
end
30+
end # BitBucket::Normalizer

0 commit comments

Comments
 (0)