BigW Consortium Gitlab

session_spec.rb 3.68 KB
Newer Older
1 2
require 'spec_helper'

3
describe Mattermost::Session, type: :request do
4 5
  let(:user) { create(:user) }

6 7 8
  let(:gitlab_url) { "http://gitlab.com" }
  let(:mattermost_url) { "http://mattermost.com" }

9
  subject { described_class.new(user) }
10 11 12 13 14 15 16

  # Needed for doorkeeper to function
  it { is_expected.to respond_to(:current_resource_owner) }
  it { is_expected.to respond_to(:request) }
  it { is_expected.to respond_to(:authorization) }
  it { is_expected.to respond_to(:strategy) }

17 18 19 20
  before do
    described_class.base_uri(mattermost_url)
  end

21 22 23
  describe '#with session' do
    let(:location) { 'http://location.tld' }
    let!(:stub) do
24
      WebMock.stub_request(:get, "#{mattermost_url}/api/v3/oauth/gitlab/login").
25 26 27 28 29 30 31 32 33 34 35
        to_return(headers: { 'location' => location }, status: 307)
    end

    context 'without oauth uri' do
      it 'makes a request to the oauth uri' do
        expect { subject.with_session }.to raise_error(Mattermost::NoSessionError)
      end
    end

    context 'with oauth_uri' do
      let!(:doorkeeper) do
36 37 38 39
        Doorkeeper::Application.create(
          name: "GitLab Mattermost",
          redirect_uri: "#{mattermost_url}/signup/gitlab/complete\n#{mattermost_url}/login/gitlab/complete",
          scopes: "")
40 41 42 43 44 45 46 47 48 49 50
      end

      context 'without token_uri' do
        it 'can not create a session' do
          expect do
            subject.with_session
          end.to raise_error(Mattermost::NoSessionError)
        end
      end

      context 'with token_uri' do
51 52 53 54 55 56 57 58 59 60
        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
61 62

        before do
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
          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").
79
            to_return(headers: { Authorization: 'token thisworksnow' }, status: 200)
80 81 82
        end

        it 'can setup a session' do
83 84
          subject.with_session do |session|
          end
85

86
          expect(subject.token).not_to be_nil
87 88 89
        end

        it 'returns the value of the block' do
90 91 92
          result = subject.with_session do |session|
            "value"
          end
93

94
          expect(result).to eq("value")
95 96 97
        end
      end
    end
Z.J. van de Weg committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

    context 'with lease' do
      before do
        allow(subject).to receive(:lease_try_obtain).and_return('aldkfjsldfk')
      end

      it 'tries to obtain a lease' do
        expect(subject).to receive(:lease_try_obtain)
        expect(Gitlab::ExclusiveLease).to receive(:cancel)

        # Cannot setup a session, but we should still cancel the lease
        expect { subject.with_session }.to raise_error(Mattermost::NoSessionError)
      end
    end

    context 'without lease' do
      before do
        allow(subject).to receive(:lease_try_obtain).and_return(nil)
      end

      it 'returns a NoSessionError error' do
        expect { subject.with_session }.to raise_error(Mattermost::NoSessionError)
      end
    end
122
  end
123
end