diff --git a/.gitignore b/.gitignore index d87d4be..f294155 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ spec/reports test/tmp test/version_tmp tmp +*.swp +.rspec +vendor/bundle diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..b83d9b7 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--color +--format documentation +--require spec_helper diff --git a/spec/issue_tracker_spec.rb b/spec/issue_tracker_spec.rb new file mode 100644 index 0000000..22dd275 --- /dev/null +++ b/spec/issue_tracker_spec.rb @@ -0,0 +1,156 @@ +describe ErrbitGithubPlugin::IssueTracker do + describe '.label' do + it 'return LABEL' do + expect(described_class.label).to eq described_class::LABEL + end + end + + describe '.note' do + it 'return NOTE' do + expect(described_class.note).to eq described_class::NOTE + end + end + + describe '.fields' do + it 'return FIELDS' do + expect(described_class.fields).to eq described_class::FIELDS + end + end + + describe '.icons' do + + it 'puts create icon onto the icons' do + expect(described_class.icons[:create][0]).to eq 'image/png' + expect( + described_class.icons[:create][1] + ).to eq ErrbitGithubPlugin.read_static_file('github_create.png') + end + + it 'puts goto icon onto the icons' do + expect(described_class.icons[:goto][0]).to eq 'image/png' + expect( + described_class.icons[:goto][1] + ).to eq ErrbitGithubPlugin.read_static_file('github_goto.png') + end + + it 'puts inactive icon onto the icons' do + expect(described_class.icons[:inactive][0]).to eq 'image/png' + expect( + described_class.icons[:inactive][1] + ).to eq ErrbitGithubPlugin.read_static_file('github_inactive.png') + end + end + + let(:tracker) { described_class.new(options) } + + describe '#configured?' do + context 'with errors' do + let(:options) { { invalid_key: '' } } + it 'return false' do + expect(tracker.configured?).to eq false + end + end + context 'without errors' do + let(:options) do + { username: 'foo', password: 'bar', github_repo: 'user/repos' } + end + it 'return true' do + expect(tracker.configured?).to eq true + end + end + end + + describe '#url' do + let(:options) { { github_repo: 'repo' } } + it 'returns issues url' do + expect(tracker.url).to eq 'https://github.com/repo/issues' + end + end + + describe '#errors' do + subject { tracker.errors } + context 'without username' do + let(:options) { { username: '', password: 'bar', github_repo: 'repo' } } + it { is_expected.not_to be_empty } + end + context 'without password' do + let(:options) do + { username: '', password: 'bar', github_repo: 'repo' } + end + it { is_expected.not_to be_empty } + end + context 'without github_repo' do + let(:options) do + { username: 'foo', password: 'bar', github_repo: '' } + end + it { is_expected.not_to be_empty } + end + context 'with completed options' do + let(:options) do + { username: 'foo', password: 'bar', github_repo: 'repo' } + end + it { is_expected.to be_empty } + end + end + + describe '#repo' do + let(:options) { { github_repo: 'baz' } } + it 'returns github repo' do + expect(tracker.repo).to eq 'baz' + end + end + + describe '#create_issue' do + subject { tracker.create_issue('title', 'body', user: user) } + let(:options) do + { username: 'foo', password: 'bar', github_repo: 'user/repos' } + end + let(:fake_github_client) do + double('Fake GitHub Client').tap do |github_client| + github_client.stub(:create_issue).and_return(fake_issue) + end + end + let(:fake_issue) do + double('Fake Issue').tap do |issue| + issue.stub(:html_url).and_return('http://github.com/user/repos/issues/878') + end + end + + context 'signed in with token' do + let(:user) do + { + 'github_login' => 'bob', + 'github_oauth_token' => 'valid_token' + } + end + it 'return issue url' do + Octokit::Client.stub(:new).with( + login: user['github_login'], access_token: user['github_oauth_token'] + ).and_return(fake_github_client) + expect(subject).to eq fake_issue.html_url + end + end + + context 'signed in with password' do + let(:user) { {} } + it 'return issue url' do + (Octokit::Client).stub(:new).with( + login: options['username'], password: options['password'] + ).and_return(fake_github_client) + expect(subject).to eq fake_issue.html_url + end + end + + context 'when unauthentication error' do + let(:user) do + { 'github_login' => 'alice', 'github_oauth_token' => 'invalid_token' } + end + it 'raise AuthenticationError' do + (Octokit::Client).stub(:new).with( + login: user['github_login'], access_token: user['github_oauth_token'] + ).and_raise(Octokit::Unauthorized) + expect { subject }.to raise_error + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..badea54 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,28 @@ +if ENV['COVERAGE'] + require 'simplecov' + if ENV['CI'] + require 'coveralls' + Coveralls.wear! + SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ + SimpleCov::Formatter::HTMLFormatter, + Coveralls::SimpleCov::Formatter + ] + end + + SimpleCov.start +end + +require 'errbit_plugin' +require 'errbit_github_plugin' +require 'active_support/all' + +RSpec.configure do |config| + config.run_all_when_everything_filtered = true + config.filter_run :focus + + # 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' +end