diff --git a/README.md b/README.md index 31a4751..5516522 100644 --- a/README.md +++ b/README.md @@ -258,10 +258,10 @@ Issue Trackers * Project id is the identifier of your project, i.e. **awesomeapp** for project at https://mingle.example.com/projects/awesomeapp * Card properties are comma separated key value pairs. You must specify a 'card_type', but anything else is optional. i.e. card_type = Defect, status = Open, priority = Essential -**Github Issues Integration** +**GitHub Issues Integration** * For 'Account/Repository', the account will either be a username or organization. i.e. **errbit/errbit** -* You will also need to provide your username and password for your Github account. +* You will also need to provide your username and password for your GitHub account. * (We'd really appreciate it if you wanted to help us implement OAuth instead!) @@ -271,23 +271,23 @@ What if Errbit has an error? Errbit will log it's own errors to an internal app named **Self.Errbit**. The **Self.Errbit** app will be automatically created whenever the first error happens. -If your Errbit instance has logged an error, we would appreciate a bug report on Github Issues. +If your Errbit instance has logged an error, we would appreciate a bug report on GitHub Issues. You can post this manually at [https://github.com/errbit/errbit/issues](https://github.com/errbit/errbit/issues), -or you can set up the Github Issues tracker for your **Self.Errbit** app: +or you can set up the GitHub Issues tracker for your **Self.Errbit** app: 1. Go to the **Self.Errbit** app's edit page. If that app does not exist yet, go to the apps page and click **Add a new App** to create it. (You can also create it by running `rake airbrake:test`.) - 2. In the **Issue Tracker** section, click **Github Issues**. + 2. In the **Issue Tracker** section, click **GitHub Issues**. 3. Fill in the **Account/Repository** field with **errbit/errbit**. 4. Fill in the **Username** field with your github username. - 5. If you are logged in on [Github](https://github.com), you can find your **API Token** on this page: [https://github.com/account/admin](https://github.com/account/admin). + 5. If you are logged in on [GitHub](https://github.com), you can find your **API Token** on this page: [https://github.com/account/admin](https://github.com/account/admin). 6. Save the settings by clicking **Update App** (or **Add App**) - 7. You can now easily post bug reports to Github Issues by clicking the **Create Issue** button on a **Self.Errbit** error. + 7. You can now easily post bug reports to GitHub Issues by clicking the **Create Issue** button on a **Self.Errbit** error. TODO diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index f41fdd3..7b629e8 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -1,32 +1,32 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController def github - github_login = request.env["omniauth.auth"].extra.raw_info.login - github_token = request.env["omniauth.auth"].credentials.token + github_login = env["omniauth.auth"].extra.raw_info.login + github_token = env["omniauth.auth"].credentials.token github_user = User.where(:github_login => github_login).first # If user is already signed in, link github details to their account if current_user # ... unless a user is already registered with same github login if github_user && github_user != current_user - flash[:error] = "User already registered with Github login '#{github_login}'" - redirect_to user_path(current_user) + flash[:error] = "User already registered with GitHub login '#{github_login}'" else # Add github details to current user current_user.update_attributes( :github_login => github_login, :github_oauth_token => github_token ) - flash[:success] = "Successfully linked Github account!" - redirect_to user_path(current_user) + flash[:success] = "Successfully linked GitHub account!" end - + # User must have clicked 'link account' from their user page, so redirect there. + redirect_to user_path(current_user) elsif github_user # Store OAuth token - @user.update_attribute :github_oauth_token, request.env["omniauth.auth"].credentials.token + github_user.update_attribute :github_oauth_token, github_token - flash[:success] = I18n.t "devise.omniauth_callbacks.success", :kind => "Github" - sign_in_and_redirect @user, :event => :authentication + flash[:success] = I18n.t "devise.omniauth_callbacks.success", :kind => "GitHub" + sign_in_and_redirect github_user, :event => :authentication else + flash[:error] = "There are no authorized users with GitHub login '#{github_login}'. Please ask an administrator to register your user account." redirect_to new_user_session_path end end diff --git a/app/models/issue_trackers/github_issues_tracker.rb b/app/models/issue_trackers/github_issues_tracker.rb index b54d70f..24d02a1 100644 --- a/app/models/issue_trackers/github_issues_tracker.rb +++ b/app/models/issue_trackers/github_issues_tracker.rb @@ -6,7 +6,7 @@ class IssueTrackers::GithubIssuesTracker < IssueTracker :placeholder => "errbit/errbit from https://github.com/errbit/errbit" }], [:username, { - :placeholder => "Your username on Github" + :placeholder => "Your username on GitHub" }], [:password, { :placeholder => "Password for your account" @@ -15,7 +15,7 @@ class IssueTrackers::GithubIssuesTracker < IssueTracker def check_params if Fields.detect {|f| self[f[0]].blank? } - errors.add :base, 'You must specify your Github repository, username and password' + errors.add :base, 'You must specify your GitHub repository, username and password' end end @@ -25,7 +25,7 @@ class IssueTrackers::GithubIssuesTracker < IssueTracker issue = client.create_issue(project_id, issue_title(problem), body_template.result(binding).unpack('C*').pack('U*'), options = {}) problem.update_attribute :issue_link, issue.html_url rescue Octokit::Unauthorized - raise IssueTrackers::AuthenticationError, "Could not authenticate with Github. Please check your username and password." + raise IssueTrackers::AuthenticationError, "Could not authenticate with GitHub. Please check your username and password." end end diff --git a/app/views/users/show.html.haml b/app/views/users/show.html.haml index 3c3eef6..b7a3d05 100644 --- a/app/views/users/show.html.haml +++ b/app/views/users/show.html.haml @@ -6,7 +6,6 @@ = link_to 'destroy', user_path(@user), :method => :delete, :confirm => 'Seriously?', :class => 'button' - %table.single_user %tr %th Email diff --git a/config/config.example.yml b/config/config.example.yml index 870b54e..3edbfaf 100644 --- a/config/config.example.yml +++ b/config/config.example.yml @@ -49,8 +49,8 @@ deployment: user: deploy deploy_to: /var/www/apps/errbit -# Github OAuth configuration -# If you want to allow authentication via Github, you will need to register +# GitHub OAuth configuration +# If you want to allow authentication via GitHub, you will need to register # your app at: https://github.com/settings/applications # If you hosted Errbit at errbit.example.com, you would fill in: # diff --git a/spec/acceptance/acceptance_helper.rb b/spec/acceptance/acceptance_helper.rb index 65f5be0..bbd8ba3 100644 --- a/spec/acceptance/acceptance_helper.rb +++ b/spec/acceptance/acceptance_helper.rb @@ -3,16 +3,17 @@ require 'capybara/rspec' OmniAuth.config.test_mode = true -RSpec.configure do |config| - config.before(:each) do - OmniAuth.config.mock_auth[:github] = Hashie::Mash.new( - 'provider' => 'github', - 'uid' => '1763', - 'extra' => { - 'raw_info' => { - 'login' => 'nashby' - } +def mock_auth(user = "test_user", token = "abcdef") + OmniAuth.config.mock_auth[:github] = Hashie::Mash.new( + 'provider' => 'github', + 'uid' => '1763', + 'extra' => { + 'raw_info' => { + 'login' => user } - ) - end + }, + 'credentials' => { + 'token' => token + } + ) end diff --git a/spec/acceptance/login_spec.rb b/spec/acceptance/login_spec.rb deleted file mode 100644 index 10787a4..0000000 --- a/spec/acceptance/login_spec.rb +++ /dev/null @@ -1,14 +0,0 @@ -require 'acceptance/acceptance_helper' - -feature 'Log in' do - background do - Errbit::Config.stub(:github_authentication) { true } - Fabricate(:user, :github_login => 'nashby') - end - - scenario 'log in via GitHub' do - visit '/' - click_link 'Sign in with GitHub' - page.should have_content 'Successfully authorized from Github account' - end -end diff --git a/spec/acceptance/sign_in_with_github_spec.rb b/spec/acceptance/sign_in_with_github_spec.rb new file mode 100644 index 0000000..1ea54a6 --- /dev/null +++ b/spec/acceptance/sign_in_with_github_spec.rb @@ -0,0 +1,24 @@ +require 'acceptance/acceptance_helper' + +feature 'Sign in with GitHub' do + background do + Errbit::Config.stub(:github_authentication) { true } + Fabricate(:user, :github_login => 'nashby') + end + + scenario 'log in via GitHub with recognized user' do + mock_auth('nashby') + + visit '/' + click_link 'Sign in with GitHub' + page.should have_content 'Successfully authorized from GitHub account' + end + + scenario 'reject unrecognized user if authenticating via GitHub' do + mock_auth('unknown_user') + + visit '/' + click_link 'Sign in with GitHub' + page.should have_content 'There are no authorized users with GitHub login' + end +end diff --git a/spec/controllers/users/omniauth_callbacks_controller_spec.rb b/spec/controllers/users/omniauth_callbacks_controller_spec.rb new file mode 100644 index 0000000..20c7d8d --- /dev/null +++ b/spec/controllers/users/omniauth_callbacks_controller_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe Users::OmniauthCallbacksController do + + def stub_env_for_github_omniauth(login, token = nil) + # This a Devise specific thing for functional tests. See https://github.com/plataformatec/devise/issues/closed#issue/608 + request.env["devise.mapping"] = Devise.mappings[:user] + env = { + "omniauth.auth" => Hashie::Mash.new( + :provider => 'github', + :extra => { :raw_info => { :login => login }}, + :credentials => { :token => token } + ) + } + @controller.stub!(:env).and_return(env) + end + + context 'Linking a GitHub account to a signed in user' do + before do + sign_in @user = Fabricate(:user) + end + + it "should show an error if another user already has that github login" do + Fabricate(:user, :github_login => "existing_user") + stub_env_for_github_omniauth("existing_user") + get :github + + request.flash[:error].should include('already registered') + response.should redirect_to(user_path(@user)) + end + + it "should link an authorized GitHub account" do + stub_env_for_github_omniauth("new_user") + get :github + + request.flash[:success].should include('Successfully linked') + response.should redirect_to(user_path(@user)) + end + end + + # See spec/acceptance/sign_in_with_github_spec.rb for 'Signing in with github' integration tests. +end diff --git a/spec/models/issue_trackers/github_issues_tracker_spec.rb b/spec/models/issue_trackers/github_issues_tracker_spec.rb index fcd38f7..28a73c4 100644 --- a/spec/models/issue_trackers/github_issues_tracker_spec.rb +++ b/spec/models/issue_trackers/github_issues_tracker_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe IssueTrackers::GithubIssuesTracker do - it "should create an issue on Github Issues with problem params, and set issue link for problem" do + it "should create an issue on GitHub Issues with problem params, and set issue link for problem" do notice = Fabricate :notice tracker = Fabricate :github_issues_tracker, :app => notice.app problem = notice.problem diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4bbd6c7..a6104c5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,3 +27,5 @@ RSpec.configure do |config| end config.include WebMock::API end + +OmniAuth.config.test_mode = true diff --git a/spec/views/users/show.html.haml_spec.rb b/spec/views/users/show.html.haml_spec.rb index 18e748a..d2e5cd4 100644 --- a/spec/views/users/show.html.haml_spec.rb +++ b/spec/views/users/show.html.haml_spec.rb @@ -5,11 +5,12 @@ describe 'users/show.html.haml' do user = stub_model(User, :created_at => Time.now) end - context 'with github auth' do - before do - Errbit::Config.stub(:github_authentication) { true } - end + before do + Errbit::Config.stub(:github_authentication) { true } + controller.stub(:current_user) { Fabricate(:user) } + end + context 'with GitHub authentication' do it 'shows github login' do user.github_login = 'test_user' assign :user, user @@ -25,4 +26,35 @@ describe 'users/show.html.haml' do rendered.should_not match(/GitHub/) end end + + context "Linking GitHub account" do + context 'viewing another user page' do + it "doesn't show and github linking buttons if user is not current user" do + assign :user, user + render + view.content_for(:action_bar).should_not include('Link GitHub account') + view.content_for(:action_bar).should_not include('Unlink GitHub account') + end + end + + context 'viewing own user page' do + before do + @user = Fabricate(:user) + controller.stub!(:current_user).and_return(@user) + assign :user, @user + end + + it 'shows link github button when no login or token' do + render + view.content_for(:action_bar).should include('Link GitHub account') + end + + it 'shows unlink github button when login and token' do + @user.github_login = 'test_user' + @user.github_oauth_token = 'abcdef' + render + view.content_for(:action_bar).should include('Unlink GitHub account') + end + end + end end -- libgit2 0.21.2