Remove hub username integration

This commit is contained in:
Neil Lalonde
2014-07-16 12:25:24 -04:00
parent 01a68f8cc7
commit 939e8505a9
19 changed files with 77 additions and 676 deletions

View File

@@ -2,71 +2,6 @@ require 'spec_helper'
require_dependency 'discourse_hub'
describe DiscourseHub do
describe '#username_available?' do
it 'should return true when username is available and no suggestion' do
RestClient.stubs(:get).returns( {success: 'OK', available: true}.to_json )
DiscourseHub.username_available?('MacGyver').should == [true, nil]
end
it 'should return false and a suggestion when username is not available' do
RestClient.stubs(:get).returns( {success: 'OK', available: false, suggestion: 'MacGyver1'}.to_json )
available, suggestion = DiscourseHub.username_available?('MacGyver')
available.should be_false
suggestion.should_not be_nil
end
# How to handle connect errors? timeout? 401? 403? 429?
end
describe '#username_match?' do
it 'should return true when it is a match and no suggestion' do
RestClient.stubs(:get).returns( {success: 'OK', match: true, available: false}.to_json )
DiscourseHub.username_match?('MacGyver', 'macg@example.com').should == [true, false, nil]
end
it 'should return false and a suggestion when it is not a match and the username is not available' do
RestClient.stubs(:get).returns( {success: 'OK', match: false, available: false, suggestion: 'MacGyver1'}.to_json )
match, available, suggestion = DiscourseHub.username_match?('MacGyver', 'macg@example.com')
match.should be_false
available.should be_false
suggestion.should_not be_nil
end
it 'should return false and no suggestion when it is not a match and the username is available' do
RestClient.stubs(:get).returns( {success: 'OK', match: false, available: true}.to_json )
match, available, suggestion = DiscourseHub.username_match?('MacGyver', 'macg@example.com')
match.should be_false
available.should be_true
suggestion.should be_nil
end
end
describe '#register_username' do
it 'should return true when registration succeeds' do
RestClient.stubs(:post).returns( {success: 'OK'}.to_json )
DiscourseHub.register_username('MacGyver', 'macg@example.com').should be_true
end
it 'should return raise an exception when registration fails' do
RestClient.stubs(:post).returns( {failed: -200}.to_json )
expect {
DiscourseHub.register_username('MacGyver', 'macg@example.com')
}.to raise_error(DiscourseHub::UsernameUnavailable)
end
end
describe '#unregister_username' do
it 'should return true when unregister succeeds' do
RestClient.stubs(:delete).returns( {success: 'OK'}.to_json )
DiscourseHub.unregister_username('byebye').should be_true
end
it 'should return false when unregister fails' do
RestClient.stubs(:delete).returns( {failed: -20}.to_json )
DiscourseHub.unregister_username('byebye').should be_false
end
end
describe '#discourse_version_check' do
it 'should return just return the json that the hub returns' do
hub_response = {'success' => 'OK', 'latest_version' => '0.8.1', 'critical_updates' => false}
@@ -74,33 +9,4 @@ describe DiscourseHub do
DiscourseHub.discourse_version_check.should == hub_response
end
end
describe '#change_username' do
it 'should return true when username is changed successfully' do
RestClient.stubs(:put).returns( {success: 'OK'}.to_json )
DiscourseHub.change_username('MacGyver', 'MacG').should be_true
end
it 'should return raise UsernameUnavailable when username is not available' do
RestClient.stubs(:put).returns( {failed: -200}.to_json )
expect {
DiscourseHub.change_username('MacGyver', 'MacG')
}.to raise_error(DiscourseHub::UsernameUnavailable)
end
# it 'should return raise UsernameUnavailable when username does not belong to this forum' do
# RestClient.stubs(:put).returns( {failed: -13}.to_json )
# expect {
# DiscourseHub.change_username('MacGyver', 'MacG')
# }.to raise_error(DiscourseHub::ActionForbidden)
# end
# it 'should return raise UsernameUnavailable when username does not belong to this forum' do
# RestClient.stubs(:put).returns( {failed: -13}.to_json )
# expect {
# DiscourseHub.change_username('MacGyver', 'MacG')
# }.to raise_error(DiscourseHub::ActionForbidden)
# end
end
end

View File

@@ -288,7 +288,6 @@ describe UsersController do
SiteSetting.stubs(:allow_new_registrations).returns(true)
@user = Fabricate.build(:user)
@user.password = "strongpassword"
DiscourseHub.stubs(:register_username).returns([true, nil])
end
def post_user
@@ -494,25 +493,9 @@ describe UsersController do
include_examples 'failed signup'
end
context 'when username is unavailable in DiscourseHub' do
before do
SiteSetting.stubs(:call_discourse_hub?).returns(true)
DiscourseHub.stubs(:register_username).raises(DiscourseHub::UsernameUnavailable.new(@user.name))
end
let(:create_params) {{
name: @user.name,
username: @user.username,
password: 'strongpassword',
email: @user.email
}}
include_examples 'failed signup'
end
context 'when an Exception is raised' do
[ ActiveRecord::StatementInvalid,
DiscourseHub::UsernameUnavailable,
RestClient::Forbidden ].each do |exception|
before { User.any_instance.stubs(:save).raises(exception) }
@@ -561,15 +544,11 @@ describe UsersController do
end
context '.check_username' do
before do
DiscourseHub.stubs(:username_available?).returns([true, nil])
end
it 'raises an error without any parameters' do
lambda { xhr :get, :check_username }.should raise_error(ActionController::ParameterMissing)
end
shared_examples 'when username is unavailable locally' do
shared_examples 'when username is unavailable' do
it 'should return success' do
response.should be_success
end
@@ -583,7 +562,7 @@ describe UsersController do
end
end
shared_examples 'when username is available everywhere' do
shared_examples 'when username is available' do
it 'should return success' do
response.should be_success
end
@@ -593,211 +572,59 @@ describe UsersController do
end
end
context 'when call_discourse_hub is disabled' do
before do
SiteSetting.stubs(:call_discourse_hub?).returns(false)
DiscourseHub.expects(:username_available?).never
DiscourseHub.expects(:username_match?).never
end
it 'returns nothing when given an email param but no username' do
xhr :get, :check_username, email: 'dood@example.com'
response.should be_success
end
it 'returns nothing when given an email param but no username' do
xhr :get, :check_username, email: 'dood@example.com'
context 'username is available' do
before do
xhr :get, :check_username, username: 'BruceWayne'
end
include_examples 'when username is available'
end
context 'username is unavailable' do
let!(:user) { Fabricate(:user) }
before do
xhr :get, :check_username, username: user.username
end
include_examples 'when username is unavailable'
end
shared_examples 'checking an invalid username' do
it 'should return success' do
response.should be_success
end
context 'username is available' do
before do
xhr :get, :check_username, username: 'BruceWayne'
end
include_examples 'when username is available everywhere'
end
context 'username is unavailable' do
let!(:user) { Fabricate(:user) }
before do
xhr :get, :check_username, username: user.username
end
include_examples 'when username is unavailable locally'
end
shared_examples 'checking an invalid username' do
it 'should return success' do
response.should be_success
end
it 'should not return an available key' do
::JSON.parse(response.body)['available'].should be_nil
end
it 'should return an error message' do
::JSON.parse(response.body)['errors'].should_not be_empty
end
end
context 'has invalid characters' do
before do
xhr :get, :check_username, username: 'bad username'
end
include_examples 'checking an invalid username'
it 'should return the invalid characters message' do
::JSON.parse(response.body)['errors'].should include(I18n.t(:'user.username.characters'))
end
end
context 'is too long' do
before do
xhr :get, :check_username, username: generate_username(User.username_length.last + 1)
end
include_examples 'checking an invalid username'
it 'should return the "too long" message' do
::JSON.parse(response.body)['errors'].should include(I18n.t(:'user.username.long', max: User.username_length.end))
end
end
end
context 'when call_discourse_hub is enabled' do
before do
SiteSetting.stubs(:call_discourse_hub?).returns(true)
end
context 'available locally and globally' do
before do
DiscourseHub.stubs(:username_available?).returns([true, nil])
DiscourseHub.stubs(:username_match?).returns([false, true, nil]) # match = false, available = true, suggestion = nil
end
shared_examples 'check_username when username is available everywhere' do
it 'should return success' do
response.should be_success
end
it 'should return available in the JSON' do
::JSON.parse(response.body)['available'].should be_true
end
it 'should return global_match false in the JSON' do
::JSON.parse(response.body)['global_match'].should be_false
end
end
context 'and email is not given' do
before do
xhr :get, :check_username, username: 'BruceWayne'
end
include_examples 'check_username when username is available everywhere'
end
context 'both username and email is given' do
before do
xhr :get, :check_username, username: 'BruceWayne', email: 'brucie@gmail.com'
end
include_examples 'check_username when username is available everywhere'
end
context 'only email is given' do
it "should check for a matching username" do
UsernameCheckerService.any_instance.expects(:check_username).with(nil, 'brucie@gmail.com').returns({json: 'blah'})
xhr :get, :check_username, email: 'brucie@gmail.com'
response.should be_success
end
end
end
shared_examples 'when email is needed to check username match' do
it 'should return success' do
response.should be_success
end
it 'should return available as false in the JSON' do
::JSON.parse(response.body)['available'].should be_false
end
it 'should not return a suggested username' do
::JSON.parse(response.body)['suggestion'].should_not be_present
end
end
context 'available locally but not globally' do
before do
DiscourseHub.stubs(:username_available?).returns([false, 'suggestion'])
end
context 'email param is not given' do
before do
xhr :get, :check_username, username: 'BruceWayne'
end
include_examples 'when email is needed to check username match'
end
context 'email param is an empty string' do
before do
xhr :get, :check_username, username: 'BruceWayne', email: ''
end
include_examples 'when email is needed to check username match'
end
context 'email matches global username' do
before do
DiscourseHub.stubs(:username_match?).returns([true, false, nil])
xhr :get, :check_username, username: 'BruceWayne', email: 'brucie@example.com'
end
include_examples 'when username is available everywhere'
it 'should indicate a global match' do
::JSON.parse(response.body)['global_match'].should be_true
end
end
context 'email does not match global username' do
before do
DiscourseHub.stubs(:username_match?).returns([false, false, 'suggestion'])
xhr :get, :check_username, username: 'BruceWayne', email: 'brucie@example.com'
end
include_examples 'when username is unavailable locally'
it 'should not indicate a global match' do
::JSON.parse(response.body)['global_match'].should be_false
end
end
end
context 'unavailable locally and globally' do
let!(:user) { Fabricate(:user) }
before do
DiscourseHub.stubs(:username_available?).returns([false, 'suggestion'])
xhr :get, :check_username, username: user.username
end
include_examples 'when username is unavailable locally'
end
context 'unavailable locally and available globally' do
let!(:user) { Fabricate(:user) }
before do
DiscourseHub.stubs(:username_available?).returns([true, nil])
xhr :get, :check_username, username: user.username
end
include_examples 'when username is unavailable locally'
end
end
context 'when discourse_org_access_key is wrong' do
before do
SiteSetting.stubs(:call_discourse_hub?).returns(true)
DiscourseHub.stubs(:username_available?).raises(RestClient::Forbidden)
DiscourseHub.stubs(:username_match?).raises(RestClient::Forbidden)
it 'should not return an available key' do
::JSON.parse(response.body)['available'].should be_nil
end
it 'should return an error message' do
xhr :get, :check_username, username: 'horsie'
json = JSON.parse(response.body)
json['errors'].should_not be_nil
json['errors'][0].should_not be_nil
::JSON.parse(response.body)['errors'].should_not be_empty
end
end
context 'has invalid characters' do
before do
xhr :get, :check_username, username: 'bad username'
end
include_examples 'checking an invalid username'
it 'should return the invalid characters message' do
::JSON.parse(response.body)['errors'].should include(I18n.t(:'user.username.characters'))
end
end
context 'is too long' do
before do
xhr :get, :check_username, username: generate_username(User.username_length.last + 1)
end
include_examples 'checking an invalid username'
it 'should return the "too long" message' do
::JSON.parse(response.body)['errors'].should include(I18n.t(:'user.username.long', max: User.username_length.end))
end
end
@@ -808,7 +635,7 @@ describe UsersController do
log_in_user(user)
xhr :get, :check_username, username: 'HanSolo'
end
include_examples 'when username is available everywhere'
include_examples 'when username is available'
end
context "it's someone else's username" do
@@ -817,7 +644,7 @@ describe UsersController do
log_in
xhr :get, :check_username, username: 'HanSolo'
end
include_examples 'when username is unavailable locally'
include_examples 'when username is unavailable'
end
context "an admin changing it for someone else" do
@@ -826,7 +653,7 @@ describe UsersController do
log_in_user(Fabricate(:admin))
xhr :get, :check_username, username: 'HanSolo', for_user_id: user.id
end
include_examples 'when username is available everywhere'
include_examples 'when username is available'
end
end
end

View File

@@ -244,26 +244,4 @@ describe AdminDashboardData do
end
end
describe "enforce_global_nicknames_check" do
subject { described_class.new.enforce_global_nicknames_check }
it 'returns nil when enforce_global_nicknames and discourse_org_access_key are set' do
SiteSetting.stubs(:enforce_global_nicknames).returns(true)
SiteSetting.stubs(:discourse_org_access_key).returns('123')
subject.should be_nil
end
it 'returns a string when enforce_global_nicknames is true but discourse_org_access_key is not' do
SiteSetting.stubs(:enforce_global_nicknames).returns(true)
SiteSetting.stubs(:discourse_org_access_key).returns('')
subject.should_not be_nil
end
it 'returns nil when enforce_global_nicknames is false and discourse_org_access_key is set' do
SiteSetting.stubs(:enforce_global_nicknames).returns(false)
SiteSetting.stubs(:discourse_org_access_key).returns('123')
subject.should be_nil
end
end
end

View File

@@ -4,32 +4,6 @@ require_dependency 'site_setting_extension'
describe SiteSetting do
describe 'call_discourse_hub?' do
it 'should be true when enforce_global_nicknames is true and discourse_org_access_key is set' do
SiteSetting.stubs(:enforce_global_nicknames).returns(true)
SiteSetting.stubs(:discourse_org_access_key).returns('asdfasfsafd')
SiteSetting.call_discourse_hub?.should == true
end
it 'should be false when enforce_global_nicknames is false and discourse_org_access_key is set' do
SiteSetting.stubs(:enforce_global_nicknames).returns(false)
SiteSetting.stubs(:discourse_org_access_key).returns('asdfasfsafd')
SiteSetting.call_discourse_hub?.should == false
end
it 'should be false when enforce_global_nicknames is true and discourse_org_access_key is not set' do
SiteSetting.stubs(:enforce_global_nicknames).returns(true)
SiteSetting.stubs(:discourse_org_access_key).returns('')
SiteSetting.call_discourse_hub?.should == false
end
it 'should be false when enforce_global_nicknames is false and discourse_org_access_key is not set' do
SiteSetting.stubs(:enforce_global_nicknames).returns(false)
SiteSetting.stubs(:discourse_org_access_key).returns('')
SiteSetting.call_discourse_hub?.should == false
end
end
describe "normalized_embeddable_host" do
it 'returns the `embeddable_host` value' do
SiteSetting.stubs(:embeddable_host).returns("eviltrout.com")

View File

@@ -143,7 +143,7 @@ describe User do
describe 'allow custom minimum username length from site settings' do
before do
@custom_min = User::GLOBAL_USERNAME_LENGTH_RANGE.begin - 1
@custom_min = 2
SiteSetting.min_username_length = @custom_min
end
@@ -161,11 +161,6 @@ describe User do
result = user.change_username('a' * (User.username_length.end + 1))
result.should be_false
end
it 'should use default length for validation if enforce_global_nicknames is true' do
SiteSetting.enforce_global_nicknames = true
User::username_length.should == User::GLOBAL_USERNAME_LENGTH_RANGE
end
end
end

View File

@@ -50,18 +50,6 @@ describe UserDestroyer do
StaffActionLogger.any_instance.expects(:log_user_deletion).with(@user, anything).once
destroy
end
it 'should unregister the nickname as the discourse hub if hub integration is enabled' do
SiteSetting.stubs(:call_discourse_hub?).returns(true)
DiscourseHub.expects(:unregister_username).with(@user.username)
destroy
end
it 'should not try to unregister the nickname as the discourse hub if hub integration is disabled' do
SiteSetting.stubs(:call_discourse_hub?).returns(false)
DiscourseHub.expects(:unregister_username).never
destroy
end
end
shared_examples "email block list" do
@@ -106,11 +94,6 @@ describe UserDestroyer do
StaffActionLogger.any_instance.expects(:log_user_deletion).never
destroy rescue nil
end
it 'should not unregister the user at the discourse hub' do
DiscourseHub.expects(:unregister_username).never
destroy rescue nil
end
end
context "delete_posts is true" do
@@ -190,11 +173,6 @@ describe UserDestroyer do
StaffActionLogger.any_instance.expects(:log_user_deletion).never
destroy
end
it 'should not unregister the user at the discourse hub' do
DiscourseHub.expects(:unregister_username).never
destroy rescue nil
end
end
end

View File

@@ -31,105 +31,18 @@ describe UsernameCheckerService do
end
end
context 'Using Discourse Hub' do
before do
SiteSetting.stubs(:call_discourse_hub?).returns(true)
end
context 'username and email is given' do
it 'username is available locally but not globally' do
DiscourseHub.expects(:username_available?).never
DiscourseHub.expects(:username_match?).returns([false, false, 'porkchop'])
result = @service.check_username('vincent', @email)
expected = { available: false, global_match: false, suggestion: 'porkchop' }
expect(result).to eq(expected)
end
it 'username is available both locally and globally' do
DiscourseHub.expects(:username_available?).never
DiscourseHub.stubs(:username_match?).returns([true, true, nil])
result = @service.check_username('vincent', @email)
expected = { available: true, global_match: true }
expect(result).to eq(expected)
end
it 'username is available locally but not globally' do
DiscourseHub.stubs(:username_match?).returns([false, false, 'suggestion'])
result = @service.check_username('vincent', @email)
expected = { available: false, global_match: false, suggestion: 'suggestion' }
expect(result).to eq(expected)
end
it 'username is available globally but not locally' do
DiscourseHub.stubs(:username_match?).returns([false, true, nil])
User.stubs(:username_available?).returns(false)
UserNameSuggester.stubs(:suggest).returns('einar-j')
expected = { available: false, suggestion: 'einar-j' }
result = @service.check_username('vincent', @email)
expect(result).to eq(expected)
end
it 'username not available anywhere' do
DiscourseHub.stubs(:username_match?).returns([false, false, 'suggestion'])
expected = { available: false, suggestion: 'suggestion', global_match: false }
@nil_email = nil
result = @service.check_username('vincent', @email)
expect(result).to eq(expected)
end
end
shared_examples "only email is given" do
it "should call the correct api" do
DiscourseHub.expects(:username_available?).never
DiscourseHub.expects(:username_match?).never
DiscourseHub.stubs(:username_for_email).returns(nil)
result
end
it 'no match on email' do
DiscourseHub.stubs(:username_for_email).returns(nil)
result.should == {suggestion: nil}
end
it 'match found for email' do
DiscourseHub.stubs(:username_for_email).returns('vincent')
result.should == {suggestion: 'vincent'}
end
it 'match found for email, but username is taken' do
# This case can happen when you've already signed up on the site,
# or enforce_global_nicknames used to be disabled.
DiscourseHub.stubs(:username_for_email).returns('taken')
User.stubs(:username_available?).with('taken').returns(false)
result.should == {suggestion: nil}
end
end
context 'username is nil' do
subject(:result) { @service.check_username(nil, @email) }
include_examples "only email is given"
end
context 'username is empty string' do
subject(:result) { @service.check_username('', @email) }
include_examples "only email is given"
end
it 'username not available locally' do
User.stubs(:username_available?).returns(false)
UserNameSuggester.stubs(:suggest).returns('einar-j')
result = @service.check_username('vincent', @nil_email)
result[:available].should be_false
result[:suggestion].should eq('einar-j')
end
context 'Discourse Hub disabled' do
it 'username not available locally' do
User.stubs(:username_available?).returns(false)
UserNameSuggester.stubs(:suggest).returns('einar-j')
result = @service.check_username('vincent', @nil_email)
result[:available].should be_false
result[:suggestion].should eq('einar-j')
end
it 'username available locally' do
User.stubs(:username_available?).returns(true)
result = @service.check_username('vincent', @nil_email)
result[:available].should be_true
end
it 'username available locally' do
User.stubs(:username_available?).returns(true)
result = @service.check_username('vincent', @nil_email)
result[:available].should be_true
end
end