2014-02-24 22:30:49 -05:00
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
describe DiscourseSingleSignOn do
|
|
|
|
before do
|
|
|
|
@sso_url = "http://somesite.com/discourse_sso"
|
|
|
|
@sso_secret = "shjkfdhsfkjh"
|
|
|
|
|
2014-11-26 01:25:54 -05:00
|
|
|
SiteSetting.enable_sso = true
|
|
|
|
SiteSetting.sso_url = @sso_url
|
|
|
|
SiteSetting.sso_secret = @sso_secret
|
2014-02-24 22:30:49 -05:00
|
|
|
end
|
|
|
|
|
2014-03-19 17:14:09 -04:00
|
|
|
def make_sso
|
2014-02-24 22:30:49 -05:00
|
|
|
sso = SingleSignOn.new
|
|
|
|
sso.sso_url = "http://meta.discorse.org/topics/111"
|
|
|
|
sso.sso_secret = "supersecret"
|
|
|
|
sso.nonce = "testing"
|
|
|
|
sso.email = "some@email.com"
|
|
|
|
sso.username = "sam"
|
|
|
|
sso.name = "sam saffron"
|
|
|
|
sso.external_id = "100"
|
2014-04-21 23:52:13 -04:00
|
|
|
sso.custom_fields["a"] = "Aa"
|
|
|
|
sso.custom_fields["b.b"] = "B.b"
|
2014-03-19 17:14:09 -04:00
|
|
|
sso
|
|
|
|
end
|
2014-02-24 22:30:49 -05:00
|
|
|
|
2014-03-19 17:14:09 -04:00
|
|
|
def test_parsed(parsed, sso)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(parsed.nonce).to eq sso.nonce
|
|
|
|
expect(parsed.email).to eq sso.email
|
|
|
|
expect(parsed.username).to eq sso.username
|
|
|
|
expect(parsed.name).to eq sso.name
|
|
|
|
expect(parsed.external_id).to eq sso.external_id
|
|
|
|
expect(parsed.custom_fields["a"]).to eq "Aa"
|
|
|
|
expect(parsed.custom_fields["b.b"]).to eq "B.b"
|
2014-03-19 17:14:09 -04:00
|
|
|
end
|
|
|
|
|
2014-12-28 21:30:54 -05:00
|
|
|
it "can do round trip parsing correctly" do
|
|
|
|
sso = SingleSignOn.new
|
|
|
|
sso.sso_secret = "test"
|
|
|
|
sso.name = "sam saffron"
|
|
|
|
sso.username = "sam"
|
|
|
|
sso.email = "sam@sam.com"
|
|
|
|
|
|
|
|
sso = SingleSignOn.parse(sso.payload, "test")
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(sso.name).to eq "sam saffron"
|
|
|
|
expect(sso.username).to eq "sam"
|
|
|
|
expect(sso.email).to eq "sam@sam.com"
|
2014-12-28 21:30:54 -05:00
|
|
|
end
|
|
|
|
|
2015-02-23 15:58:45 -05:00
|
|
|
let(:ip_address) { "127.0.0.1" }
|
|
|
|
|
2014-06-02 03:32:39 -04:00
|
|
|
it "can lookup or create user when name is blank" do
|
|
|
|
# so we can create system messages
|
|
|
|
Fabricate(:admin)
|
|
|
|
sso = DiscourseSingleSignOn.new
|
|
|
|
sso.username = "test"
|
|
|
|
sso.name = ""
|
|
|
|
sso.email = "test@test.com"
|
|
|
|
sso.external_id = "A"
|
2015-02-23 15:58:45 -05:00
|
|
|
user = sso.lookup_or_create_user(ip_address)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user).to_not be_nil
|
2014-06-02 03:32:39 -04:00
|
|
|
end
|
|
|
|
|
2014-03-19 17:14:09 -04:00
|
|
|
it "can fill in data on way back" do
|
|
|
|
sso = make_sso
|
|
|
|
|
|
|
|
url, payload = sso.to_url.split("?")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(url).to eq sso.sso_url
|
2014-03-19 17:14:09 -04:00
|
|
|
parsed = SingleSignOn.parse(payload, "supersecret")
|
|
|
|
|
|
|
|
test_parsed(parsed, sso)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles sso_url with query params" do
|
|
|
|
sso = make_sso
|
|
|
|
sso.sso_url = "http://tcdev7.wpengine.com/?action=showlogin"
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(sso.to_url.split('?').size).to eq 2
|
2014-03-19 17:14:09 -04:00
|
|
|
|
|
|
|
url, payload = sso.to_url.split("?")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(url).to eq "http://tcdev7.wpengine.com/"
|
2014-03-19 17:14:09 -04:00
|
|
|
parsed = SingleSignOn.parse(payload, "supersecret")
|
2014-02-24 22:30:49 -05:00
|
|
|
|
2014-03-19 17:14:09 -04:00
|
|
|
test_parsed(parsed, sso)
|
2014-02-24 22:30:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "validates nonce" do
|
|
|
|
_ , payload = DiscourseSingleSignOn.generate_url.split("?")
|
|
|
|
|
|
|
|
sso = DiscourseSingleSignOn.parse(payload)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(sso.nonce_valid?).to eq true
|
2014-02-24 22:30:49 -05:00
|
|
|
|
|
|
|
sso.expire_nonce!
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(sso.nonce_valid?).to eq false
|
2014-02-24 22:30:49 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it "generates a correct sso url" do
|
|
|
|
|
|
|
|
url, payload = DiscourseSingleSignOn.generate_url.split("?")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(url).to eq @sso_url
|
2014-02-24 22:30:49 -05:00
|
|
|
|
|
|
|
sso = DiscourseSingleSignOn.parse(payload)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(sso.nonce).to_not be_nil
|
2014-02-24 22:30:49 -05:00
|
|
|
end
|
2015-01-28 10:47:59 -05:00
|
|
|
|
|
|
|
context 'when sso_overrides_avatar is enabled' do
|
2015-01-28 11:52:07 -05:00
|
|
|
let!(:sso_record) { Fabricate(:single_sign_on_record, external_avatar_url: "http://example.com/an_image.png") }
|
|
|
|
let!(:sso) {
|
|
|
|
sso = DiscourseSingleSignOn.new
|
|
|
|
sso.username = "test"
|
|
|
|
sso.name = "test"
|
|
|
|
sso.email = sso_record.user.email
|
|
|
|
sso.external_id = sso_record.external_id
|
|
|
|
sso
|
|
|
|
}
|
|
|
|
let(:logo) { file_from_fixtures("logo.png") }
|
2015-01-28 10:47:59 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.sso_overrides_avatar = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deal with no avatar url passed for an existing user with an avatar" do
|
|
|
|
# Deliberately not setting avatar_url.
|
|
|
|
|
2015-02-23 15:58:45 -05:00
|
|
|
user = sso.lookup_or_create_user(ip_address)
|
2015-01-28 10:47:59 -05:00
|
|
|
expect(user).to_not be_nil
|
|
|
|
end
|
2015-01-28 11:52:07 -05:00
|
|
|
|
|
|
|
it "deal with no avatar_force_update passed as a boolean" do
|
|
|
|
FileHelper.stubs(:download).returns(logo)
|
|
|
|
|
|
|
|
sso.avatar_url = "http://example.com/a_different_image.png"
|
|
|
|
sso.avatar_force_update = true
|
|
|
|
|
2015-02-23 15:58:45 -05:00
|
|
|
user = sso.lookup_or_create_user(ip_address)
|
2015-01-28 11:52:07 -05:00
|
|
|
expect(user).to_not be_nil
|
|
|
|
end
|
2015-01-28 10:47:59 -05:00
|
|
|
end
|
2014-02-24 22:30:49 -05:00
|
|
|
end
|