diff options
author | Zeger-Jan van de Weg <zegerjan@gitlab.com> | 2016-12-18 15:59:09 +0300 |
---|---|---|
committer | Zeger-Jan van de Weg <zegerjan@gitlab.com> | 2016-12-18 15:59:09 +0300 |
commit | a9d9895dc7b4c024471f49a61166a6bb402eab9e (patch) | |
tree | a36edcc9449123a9fd4112dc18c5c18216ae07a1 /spec/lib/mattermost/session_spec.rb | |
parent | b23f32a735459f1c76ac8232f9de06942b1d2f8a (diff) | |
parent | 546fa165ff728bc2d25ed9b55b95dd1d48139d4a (diff) |
Merge branch 'master' into 'zj-mattermost-slash-config'
# Conflicts:
# config/gitlab.yml.example
# lib/mattermost/session.rb
# spec/lib/mattermost/session_spec.rb
Diffstat (limited to 'spec/lib/mattermost/session_spec.rb')
-rw-r--r-- | spec/lib/mattermost/session_spec.rb | 67 |
1 files changed, 49 insertions, 18 deletions
diff --git a/spec/lib/mattermost/session_spec.rb b/spec/lib/mattermost/session_spec.rb index a93bab877da..752ac796b1c 100644 --- a/spec/lib/mattermost/session_spec.rb +++ b/spec/lib/mattermost/session_spec.rb @@ -1,9 +1,12 @@ require 'spec_helper' -describe Mattermost::Session do +describe Mattermost::Session, type: :request do let(:user) { create(:user) } - subject { described_class.new('http://localhost:8065', user) } + let(:gitlab_url) { "http://gitlab.com" } + let(:mattermost_url) { "http://mattermost.com" } + + subject { described_class.new(user) } # Needed for doorkeeper to function it { is_expected.to respond_to(:current_resource_owner) } @@ -11,10 +14,14 @@ describe Mattermost::Session do it { is_expected.to respond_to(:authorization) } it { is_expected.to respond_to(:strategy) } + before do + described_class.base_uri(mattermost_url) + end + describe '#with session' do let(:location) { 'http://location.tld' } let!(:stub) do - WebMock.stub_request(:get, 'http://localhost:8065/api/v3/oauth/gitlab/login'). + WebMock.stub_request(:get, "#{mattermost_url}/api/v3/oauth/gitlab/login"). to_return(headers: { 'location' => location }, status: 307) end @@ -26,9 +33,10 @@ describe Mattermost::Session do context 'with oauth_uri' do let!(:doorkeeper) do - Doorkeeper::Application.create(name: "GitLab Mattermost", - redirect_uri: "http://localhost:8065/signup/gitlab/complete\nhttp://localhost:8065/login/gitlab/complete", - scopes: "") + Doorkeeper::Application.create( + name: "GitLab Mattermost", + redirect_uri: "#{mattermost_url}/signup/gitlab/complete\n#{mattermost_url}/login/gitlab/complete", + scopes: "") end context 'without token_uri' do @@ -40,29 +48,52 @@ describe Mattermost::Session do end context 'with token_uri' do - let(:state) { "eyJhY3Rpb24iOiJsb2dpbiIsImhhc2giOiIkMmEkMTAkVC9wYVlEaTdIUS8vcWdKRmdOOUllZUptaUNJWUlvNVNtNEcwU2NBMXFqelNOVmVPZ1cxWUsifQ%3D%3D" } - let(:location) { "http://locahost:8065/oauth/authorize?response_type=code&client_id=#{doorkeeper.uid}&redirect_uri=http%3A%2F%2Flocalhost:8065%2Fsignup%2Fgitlab%2Fcomplete&state=#{state}" } + let(:state) { "state" } + let(:params) do + { response_type: "code", + client_id: doorkeeper.uid, + redirect_uri: "#{mattermost_url}/signup/gitlab/complete", + state: state } + end + let(:location) do + "#{gitlab_url}/oauth/authorize?#{URI.encode_www_form(params)}" + end before do - WebMock.stub_request(:get, /http:\/\/localhost:8065\/signup\/gitlab\/complete*/). - to_return(headers: { 'token' => 'thisworksnow' }, status: 202) + WebMock.stub_request(:get, "#{mattermost_url}/signup/gitlab/complete"). + with(query: hash_including({ 'state' => state })). + to_return do |request| + post "/oauth/token", + client_id: doorkeeper.uid, + client_secret: doorkeeper.secret, + redirect_uri: params[:redirect_uri], + grant_type: 'authorization_code', + code: request.uri.query_values['code'] + + if response.status == 200 + { headers: { 'token' => 'thisworksnow' }, status: 202 } + end + end + + WebMock.stub_request(:post, "#{mattermost_url}/api/v3/users/logout"). + to_return(headers: { Authorization: 'token thisworksnow' }, status: 200) end it 'can setup a session' do - expect(subject).to receive(:destroy) + subject.with_session do |session| + end - subject.with_session { 1 + 1 } + expect(subject.token).not_to be_nil end it 'returns the value of the block' do - WebMock.stub_request(:post, "http://localhost:8065/api/v3/users/logout"). - to_return(headers: { 'token' => 'thisworksnow' }, status: 200) - - value = subject.with_session { 1 + 1 } + result = subject.with_session do |session| + "value" + end - expect(value).to be(2) + expect(result).to eq("value") end end end end -end +end
\ No newline at end of file |