mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Use the new mothership change_nickname API
This commit is contained in:
parent
238032051e
commit
4636b354b4
@ -131,11 +131,12 @@ class User < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def change_username(new_username)
|
def change_username(new_username)
|
||||||
|
current_username = self.username
|
||||||
self.username = new_username
|
self.username = new_username
|
||||||
|
|
||||||
if SiteSetting.call_mothership? and self.valid?
|
if SiteSetting.call_mothership? and self.valid?
|
||||||
begin
|
begin
|
||||||
Mothership.register_nickname( self.username, self.email )
|
Mothership.change_nickname( current_username, new_username )
|
||||||
rescue Mothership::NicknameUnavailable
|
rescue Mothership::NicknameUnavailable
|
||||||
return false
|
return false
|
||||||
rescue => e
|
rescue => e
|
||||||
|
@ -19,10 +19,20 @@ module Mothership
|
|||||||
if json.has_key?('success')
|
if json.has_key?('success')
|
||||||
true
|
true
|
||||||
else
|
else
|
||||||
raise NicknameUnavailable # json['failed'] == -200
|
raise NicknameUnavailable # TODO: report ALL the errors
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.change_nickname(current_nickname, new_nickname)
|
||||||
|
json = put("/users/#{current_nickname}/nickname", {nickname: new_nickname})
|
||||||
|
if json.has_key?('success')
|
||||||
|
true
|
||||||
|
else
|
||||||
|
raise NicknameUnavailable # TODO: report ALL the errors
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def self.current_discourse_version
|
def self.current_discourse_version
|
||||||
get('/current_version')['version']
|
get('/current_version')['version']
|
||||||
end
|
end
|
||||||
@ -40,6 +50,11 @@ module Mothership
|
|||||||
JSON.parse(response)
|
JSON.parse(response)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.put(rel_url, params={})
|
||||||
|
response = RestClient.put( "#{mothership_base_url}#{rel_url}", {access_token: access_token}.merge(params), content_type: :json, accept: accepts )
|
||||||
|
JSON.parse(response)
|
||||||
|
end
|
||||||
|
|
||||||
def self.mothership_base_url
|
def self.mothership_base_url
|
||||||
if Rails.env == 'production'
|
if Rails.env == 'production'
|
||||||
'http://api.discourse.org/api'
|
'http://api.discourse.org/api'
|
||||||
|
@ -61,4 +61,34 @@ describe Mothership do
|
|||||||
Mothership.current_discourse_version().should == 1.0
|
Mothership.current_discourse_version().should == 1.0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#change_nickname' do
|
||||||
|
it 'should return true when nickname is changed successfully' do
|
||||||
|
RestClient.stubs(:put).returns( {success: 'OK'}.to_json )
|
||||||
|
Mothership.change_nickname('MacGyver', 'MacG').should be_true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should return raise NicknameUnavailable when nickname is not available' do
|
||||||
|
RestClient.stubs(:put).returns( {failed: -200}.to_json )
|
||||||
|
expect {
|
||||||
|
Mothership.change_nickname('MacGyver', 'MacG')
|
||||||
|
}.to raise_error(Mothership::NicknameUnavailable)
|
||||||
|
end
|
||||||
|
|
||||||
|
# TODO: General error handling in mothership.rb
|
||||||
|
|
||||||
|
# it 'should return raise NicknameUnavailable when nickname does not belong to this forum' do
|
||||||
|
# RestClient.stubs(:put).returns( {failed: -13}.to_json )
|
||||||
|
# expect {
|
||||||
|
# Mothership.change_nickname('MacGyver', 'MacG')
|
||||||
|
# }.to raise_error(Mothership::ActionForbidden)
|
||||||
|
# end
|
||||||
|
|
||||||
|
# it 'should return raise NicknameUnavailable when nickname does not belong to this forum' do
|
||||||
|
# RestClient.stubs(:put).returns( {failed: -13}.to_json )
|
||||||
|
# expect {
|
||||||
|
# Mothership.change_nickname('MacGyver', 'MacG')
|
||||||
|
# }.to raise_error(Mothership::ActionForbidden)
|
||||||
|
# end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user