omniauth_callbacks_controller_spec.rb
1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
describe Users::OmniauthCallbacksController, type: 'controller' 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 }
)
}
allow(@controller).to receive(: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
expect(request.flash[:error]).to include('already registered')
expect(response).to redirect_to(user_path(@user))
end
it "should link an authorized GitHub account" do
stub_env_for_github_omniauth("new_user")
get :github
expect(request.flash[:success]).to include('Successfully linked')
expect(response).to redirect_to(user_path(@user))
end
end
# See spec/acceptance/sign_in_with_github_spec.rb for 'Signing in with github' integration tests.
end