Signup form: prefill username if Discourse Hub has a match for the email address. Also, fix some bad specs in username_checker_service_spec that were passing...

This commit is contained in:
Neil Lalonde
2013-11-19 14:15:05 -05:00
parent 309904ef8f
commit 981d8f6aea
8 changed files with 148 additions and 64 deletions

View File

@@ -11,10 +11,6 @@ describe UsernameCheckerService do
end
context 'Username invalid' do
it 'rejects blank usernames' do
result = @service.check_username('', @nil_email)
expect(result).to have_key(:errors)
end
it 'rejects too short usernames' do
result = @service.check_username('a', @nil_email)
expect(result).to have_key(:errors)
@@ -40,47 +36,75 @@ describe UsernameCheckerService do
SiteSetting.stubs(:call_discourse_hub?).returns(true)
end
context 'and email is given' do
context 'username and email is given' do
it 'username is available locally but not globally' do
DiscourseHub.stubs(:nickname_available?).returns([false, 'suggestion'])
DiscourseHub.stubs(:nickname_match?).returns([true, false, nil])
DiscourseHub.expects(:nickname_available?).never
DiscourseHub.expects(:nickname_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(:nickname_available?).never
DiscourseHub.stubs(:nickname_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(:nickname_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(:nickname_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(:nickname_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
it 'username is available both locally and globally' do
DiscourseHub.stubs(:nickname_available?).returns([true, nil])
DiscourseHub.stubs(:nickname_match?).returns([false, true, nil])
result = @service.check_username('vincent', @email)
expected = { available: true, global_match: false }
expect(result).to eq(expected)
shared_examples "only email is given" do
it "should call the correct api" do
DiscourseHub.expects(:nickname_available?).never
DiscourseHub.expects(:nickname_match?).never
DiscourseHub.stubs(:nickname_for_email).returns(nil)
result
end
it 'no match on email' do
DiscourseHub.stubs(:nickname_for_email).returns(nil)
result.should == {suggestion: nil}
end
it 'match found for email' do
DiscourseHub.stubs(:nickname_for_email).returns('vincent')
result.should == {suggestion: 'vincent'}
end
end
it 'username is available locally but not globally' do
DiscourseHub.stubs(:nickname_match?).returns([false, true, nil])
result = @service.check_username('vincent', @email)
expected = { available: true, global_match: false }
expect(result).to eq(expected)
context 'username is nil' do
subject(:result) { @service.check_username(nil, @email) }
include_examples "only email is given"
end
it 'username is available globally but not locally' do
DiscourseHub.stubs(:nickname_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(:nickname_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)
context 'username is empty string' do
subject(:result) { @service.check_username('', @email) }
include_examples "only email is given"
end
end