debian-mirror-gitlab/spec/lib/mattermost/session_spec.rb

126 lines
3.8 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Mattermost::Session, type: :request do
2018-11-08 19:23:39 +05:30
include ExclusiveLeaseHelpers
2019-06-05 12:25:43 +05:30
include StubRequests
2018-11-08 19:23:39 +05:30
2017-08-17 22:00:37 +05:30
let(:user) { create(: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) }
it { is_expected.to respond_to(:request) }
it { is_expected.to respond_to(:authorization) }
it { is_expected.to respond_to(:strategy) }
before do
2018-03-26 14:24:53 +05:30
subject.base_uri = mattermost_url
2017-08-17 22:00:37 +05:30
end
describe '#with session' do
let(:location) { 'http://location.tld' }
2017-09-10 17:25:29 +05:30
let(:cookie_header) {'MMOAUTH=taskik8az7rq8k6rkpuas7htia; Path=/;'}
2017-08-17 22:00:37 +05:30
let!(:stub) do
2019-06-05 12:25:43 +05:30
stub_full_request("#{mattermost_url}/oauth/gitlab/login")
2018-11-08 19:23:39 +05:30
.to_return(headers: { 'location' => location, 'Set-Cookie' => cookie_header }, status: 302)
2017-08-17 22:00:37 +05:30
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
Doorkeeper::Application.create(
2017-09-10 17:25:29 +05:30
name: 'GitLab Mattermost',
2017-08-17 22:00:37 +05:30
redirect_uri: "#{mattermost_url}/signup/gitlab/complete\n#{mattermost_url}/login/gitlab/complete",
2017-09-10 17:25:29 +05:30
scopes: '')
2017-08-17 22:00:37 +05:30
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
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
2019-06-05 12:25:43 +05:30
stub_full_request("#{mattermost_url}/signup/gitlab/complete")
2017-09-10 17:25:29 +05:30
.with(query: hash_including({ 'state' => state }))
.to_return do |request|
2017-08-17 22:00:37 +05:30
post "/oauth/token",
2019-02-15 15:39:39 +05:30
params: {
client_id: doorkeeper.uid,
client_secret: doorkeeper.secret,
redirect_uri: params[:redirect_uri],
grant_type: 'authorization_code',
code: request.uri.query_values['code']
}
2017-08-17 22:00:37 +05:30
if response.status == 200
{ headers: { 'token' => 'thisworksnow' }, status: 202 }
end
end
2019-06-05 12:25:43 +05:30
stub_full_request("#{mattermost_url}/api/v4/users/logout", method: :post)
2017-09-10 17:25:29 +05:30
.to_return(headers: { Authorization: 'token thisworksnow' }, status: 200)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
it 'can set up a session' do
2017-08-17 22:00:37 +05:30
subject.with_session do |session|
end
expect(subject.token).not_to be_nil
end
it 'returns the value of the block' do
result = subject.with_session do |session|
"value"
end
expect(result).to eq("value")
end
end
end
2018-11-08 19:23:39 +05:30
context 'exclusive lease' do
let(:lease_key) { 'mattermost:session' }
2017-08-17 22:00:37 +05:30
it 'tries to obtain a lease' do
2018-11-08 19:23:39 +05:30
expect_to_obtain_exclusive_lease(lease_key, 'uuid')
expect_to_cancel_exclusive_lease(lease_key, 'uuid')
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
# Cannot set up a session, but we should still cancel the lease
2017-08-17 22:00:37 +05:30
expect { subject.with_session }.to raise_error(Mattermost::NoSessionError)
end
2018-11-08 19:23:39 +05:30
it 'returns a NoSessionError error without lease' do
stub_exclusive_lease_taken(lease_key)
2017-08-17 22:00:37 +05:30
expect { subject.with_session }.to raise_error(Mattermost::NoSessionError)
end
end
end
end