2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
2013-02-14 12:57:26 -05:00
|
|
|
require_dependency 'discourse_hub'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-14 12:57:26 -05:00
|
|
|
describe DiscourseHub do
|
2014-03-12 12:39:27 -04:00
|
|
|
describe '#username_available?' do
|
|
|
|
it 'should return true when username is available and no suggestion' do
|
2013-02-05 14:16:51 -05:00
|
|
|
RestClient.stubs(:get).returns( {success: 'OK', available: true}.to_json )
|
2014-03-12 12:39:27 -04:00
|
|
|
DiscourseHub.username_available?('MacGyver').should == [true, nil]
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-03-12 12:39:27 -04:00
|
|
|
it 'should return false and a suggestion when username is not available' do
|
2013-02-05 14:16:51 -05:00
|
|
|
RestClient.stubs(:get).returns( {success: 'OK', available: false, suggestion: 'MacGyver1'}.to_json )
|
2014-03-12 12:39:27 -04:00
|
|
|
available, suggestion = DiscourseHub.username_available?('MacGyver')
|
2013-02-05 14:16:51 -05:00
|
|
|
available.should be_false
|
|
|
|
suggestion.should_not be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# How to handle connect errors? timeout? 401? 403? 429?
|
|
|
|
end
|
|
|
|
|
2014-03-12 12:39:27 -04:00
|
|
|
describe '#username_match?' do
|
2013-02-05 14:16:51 -05:00
|
|
|
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 )
|
2014-03-12 12:39:27 -04:00
|
|
|
DiscourseHub.username_match?('MacGyver', 'macg@example.com').should == [true, false, nil]
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-03-12 12:39:27 -04:00
|
|
|
it 'should return false and a suggestion when it is not a match and the username is not available' do
|
2013-02-05 14:16:51 -05:00
|
|
|
RestClient.stubs(:get).returns( {success: 'OK', match: false, available: false, suggestion: 'MacGyver1'}.to_json )
|
2014-03-12 12:39:27 -04:00
|
|
|
match, available, suggestion = DiscourseHub.username_match?('MacGyver', 'macg@example.com')
|
2013-02-05 14:16:51 -05:00
|
|
|
match.should be_false
|
|
|
|
available.should be_false
|
|
|
|
suggestion.should_not be_nil
|
|
|
|
end
|
|
|
|
|
2014-03-12 12:39:27 -04:00
|
|
|
it 'should return false and no suggestion when it is not a match and the username is available' do
|
2013-02-05 14:16:51 -05:00
|
|
|
RestClient.stubs(:get).returns( {success: 'OK', match: false, available: true}.to_json )
|
2014-03-12 12:39:27 -04:00
|
|
|
match, available, suggestion = DiscourseHub.username_match?('MacGyver', 'macg@example.com')
|
2013-02-05 14:16:51 -05:00
|
|
|
match.should be_false
|
|
|
|
available.should be_true
|
|
|
|
suggestion.should be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-12 12:39:27 -04:00
|
|
|
describe '#register_username' do
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'should return true when registration succeeds' do
|
|
|
|
RestClient.stubs(:post).returns( {success: 'OK'}.to_json )
|
2014-03-12 12:39:27 -04:00
|
|
|
DiscourseHub.register_username('MacGyver', 'macg@example.com').should be_true
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return raise an exception when registration fails' do
|
|
|
|
RestClient.stubs(:post).returns( {failed: -200}.to_json )
|
|
|
|
expect {
|
2014-03-12 12:39:27 -04:00
|
|
|
DiscourseHub.register_username('MacGyver', 'macg@example.com')
|
|
|
|
}.to raise_error(DiscourseHub::UsernameUnavailable)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-12 12:39:27 -04:00
|
|
|
describe '#unregister_username' do
|
2013-04-15 14:52:07 -04:00
|
|
|
it 'should return true when unregister succeeds' do
|
|
|
|
RestClient.stubs(:delete).returns( {success: 'OK'}.to_json )
|
2014-03-12 12:39:27 -04:00
|
|
|
DiscourseHub.unregister_username('byebye').should be_true
|
2013-04-15 14:52:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return false when unregister fails' do
|
|
|
|
RestClient.stubs(:delete).returns( {failed: -20}.to_json )
|
2014-03-12 12:39:27 -04:00
|
|
|
DiscourseHub.unregister_username('byebye').should be_false
|
2013-04-15 14:52:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-19 15:16:50 -05:00
|
|
|
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}
|
|
|
|
RestClient.stubs(:get).returns( hub_response.to_json )
|
|
|
|
DiscourseHub.discourse_version_check.should == hub_response
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-12 10:13:09 -05:00
|
|
|
|
2014-03-12 12:39:27 -04:00
|
|
|
describe '#change_username' do
|
|
|
|
it 'should return true when username is changed successfully' do
|
2013-02-12 10:13:09 -05:00
|
|
|
RestClient.stubs(:put).returns( {success: 'OK'}.to_json )
|
2014-03-12 12:39:27 -04:00
|
|
|
DiscourseHub.change_username('MacGyver', 'MacG').should be_true
|
2013-02-12 10:13:09 -05:00
|
|
|
end
|
|
|
|
|
2014-03-12 12:39:27 -04:00
|
|
|
it 'should return raise UsernameUnavailable when username is not available' do
|
2013-02-12 10:13:09 -05:00
|
|
|
RestClient.stubs(:put).returns( {failed: -200}.to_json )
|
|
|
|
expect {
|
2014-03-12 12:39:27 -04:00
|
|
|
DiscourseHub.change_username('MacGyver', 'MacG')
|
|
|
|
}.to raise_error(DiscourseHub::UsernameUnavailable)
|
2013-02-12 10:13:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-03-12 12:39:27 -04:00
|
|
|
# it 'should return raise UsernameUnavailable when username does not belong to this forum' do
|
2013-02-12 10:13:09 -05:00
|
|
|
# RestClient.stubs(:put).returns( {failed: -13}.to_json )
|
|
|
|
# expect {
|
2014-03-12 12:39:27 -04:00
|
|
|
# DiscourseHub.change_username('MacGyver', 'MacG')
|
2013-02-14 12:57:26 -05:00
|
|
|
# }.to raise_error(DiscourseHub::ActionForbidden)
|
2013-02-12 10:13:09 -05:00
|
|
|
# end
|
|
|
|
|
2014-03-12 12:39:27 -04:00
|
|
|
# it 'should return raise UsernameUnavailable when username does not belong to this forum' do
|
2013-02-12 10:13:09 -05:00
|
|
|
# RestClient.stubs(:put).returns( {failed: -13}.to_json )
|
|
|
|
# expect {
|
2014-03-12 12:39:27 -04:00
|
|
|
# DiscourseHub.change_username('MacGyver', 'MacG')
|
2013-02-14 12:57:26 -05:00
|
|
|
# }.to raise_error(DiscourseHub::ActionForbidden)
|
2013-02-12 10:13:09 -05:00
|
|
|
# end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|